Bugbot/Scripts/server_gitea_api.gd
2024-05-19 19:57:08 -04:00

141 lines
5.6 KiB
GDScript

class_name BugbotServerGiteaAPI
extends "res://addons/Bugbot/Scripts/server_api.gd"
#region Consts
const DEFAULT_SERVER : StringName = &"https://gitea.example.com"
const DEFAULT_BASE_URL : StringName = &"api/v1/"
const DEFAULT_OWNER_NAME : StringName = &"ProjectOwner"
const DEFAULT_REPO_NAME : StringName = &"ProjectName"
const DEFAULT_API_KEY : StringName = &"0123456789abcdef0123456789abcdef01234567"
const DEFAULT_LABEL : StringName = &"Kind/Bug"
const DEFAULT_STATUS : StringName = &"Status/Need More Info"
const UNRESOLVED_TAG : String = &"Status/Unresolved"
const IN_PROGRESS_TAG : String = &"Status/In Progress"
const RESOLVED_TAG : String = &"Status/Resolved"
#endregion
static func _return_list_of_bugs(callback:Callable) -> int:
if not __bugbot_server_thread:
printerr("Server thread is null.")
return BugbotServerError.THREAD_NULL
if __bugbot_server_thread.is_started():
printerr("Server thread is currently busy.")
return BugbotServerError.THREAD_BUSY;
__bugbot_server_thread = Thread.new()
__bugbot_server_thread.start(__return_list_of_bugs_thread.bind(callback))
return BugbotServerError.OK
#static func _prepare_form() -> void:
#print("Prepare ", _current_server_api(), " form here.")
#
#static func _send_form_data() -> void:
#print("Send ", _current_server_api(), " form data here.")
func __return_list_of_bugs_thread(callback:Callable) -> void:
var http_client : HTTPClient = HTTPClient.new()
var server : String = ProjectSettings.get_setting("bugbot/reporting/gitea/server", DEFAULT_SERVER)
var error : int = http_client.connect_to_host(server, 443)
assert(error == Error.OK)
while http_client.get_status() == HTTPClient.STATUS_CONNECTING or http_client.get_status() == HTTPClient.STATUS_RESOLVING:
http_client.poll()
assert(http_client.get_status() == HTTPClient.STATUS_CONNECTED)
var api_url : String = _build_url_string()
api_url += "?state=all&labels=" + ProjectSettings.get_setting("bugbot/reporting/gitea/default_label", DEFAULT_LABEL).uri_encode()
var header_data : Array = _create_header_data("application/json", ProjectSettings.get_setting("bugbot/reporting/gitea/API_key", DEFAULT_API_KEY))
error = http_client.request(HTTPClient.METHOD_GET, api_url, header_data)
assert(error == Error.OK)
while http_client.get_status() == HTTPClient.STATUS_REQUESTING:
http_client.poll()
assert(http_client.get_status() == HTTPClient.STATUS_BODY or http_client.get_status() == HTTPClient.STATUS_CONNECTED)
var response_string : String = ""
if http_client.has_response():
var response_body : PackedByteArray = PackedByteArray()
while http_client.get_status() == HTTPClient.STATUS_BODY:
http_client.poll()
var chunk : PackedByteArray = http_client.read_response_body_chunk()
if chunk.size() > 0:
response_body = response_body + chunk
response_string = response_body.get_string_from_utf8()
var response_array : Array
var response_data := JSON.parse_string(response_string)
if response_data is Array:
# If the response is an array, we can make the assumption that this is
# a list of returned bugs.
var resolved_labels : Array = ProjectSettings.get_setting("bugbot/markers/resolved/resolved_statuses")
var in_progress_labels : Array = ProjectSettings.get_setting("bugbot/markers/in_progress/in_progress_statuses")
var unresolved_labels : Array = ProjectSettings.get_setting("bugbot/markers/unresolved/unresolved_statuses")
for bug_in:Dictionary in response_data:
var bug : BugbotBugData = BugbotBugData.new()
bug.id = bug_in["id"]
bug.title = bug_in["title"]
bug.body = bug_in["body"]
bug.component = &"Unused"
bug.platform = &"Unused"
bug.operating_system = &"Unused"
bug.is_open = (bug_in["state"] == "open")
bug.severity = "Severity will go here."
bug.status = bug_in["state"]
bug.duplicate_of = -1
bug.map_name = "Map name will go here."
bug.marker_location = "Marker location will go here."
if not bug.is_open:
bug.resolution = RESOLVED_TAG
else:
for resolution_label:Dictionary in bug_in["labels"]:
var label_name : String = resolution_label["name"]
if resolved_labels.has(label_name):
bug.resolution = RESOLVED_TAG
break
elif in_progress_labels.has(label_name):
bug.resolution = IN_PROGRESS_TAG
break
elif unresolved_labels.has(label_name):
bug.resolution = UNRESOLVED_TAG
break
response_array.append(bug)
elif response_data is Dictionary:
# If the response is a dictionary and not an array, make the assumption
# that this is because the response was an error code.
var error_data : BugbotErrorData = BugbotErrorData.new()
error_data.code = 1
error_data.message = response_data["message"]
error_data.url = response_data["url"]
response_array.append(error_data)
printerr(error_data.message)
callback.call_deferred(response_array)
__bugbot_server_thread.call_deferred("wait_to_finish")
static func _current_server_api() -> String:
return "Gitea"
static func _build_url_string() -> String:
return "/" + \
ProjectSettings.get_setting("bugbot/reporting/gitea/base_URL", DEFAULT_BASE_URL) + \
"/repos/" + \
ProjectSettings.get_setting("bugbot/reporting/gitea/owner_name", DEFAULT_OWNER_NAME) + \
"/" + \
ProjectSettings.get_setting("bugbot/reporting/gitea/repo_name", DEFAULT_REPO_NAME) + \
"/issues"
static func _create_header_data(mime_type:String, api_key:String) -> Array:
var header : Array = [
"User-Agent: Pirulo/1.0 (Godot)",
"Accept: " + mime_type,
"Content-Type: " + mime_type,
]
if api_key: header.append("Authorization: token " + api_key)
return header