class_name BugbotServerGiteaAPI extends "res://addons/Bugbot/Scripts/server_api.gd" #region Consts const DEFAULT_SERVER : String = "https://gitea.example.com" const DEFAULT_BASE_URL : String = "api/v1/" const DEFAULT_OWNER_NAME : String = "ProjectOwner" const DEFAULT_REPO_NAME : String = "ProjectName" const DEFAULT_API_KEY : String = "0123456789abcdef0123456789abcdef01234567" const DEFAULT_LABEL : String = "Kind/Bug" const DEFAULT_STATUS : String = "Status/Need More Info" #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() 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. 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 = "Status will go here." bug.resolution = "Resolution will go here." bug.duplicate_of = -1 bug.map_name = "Map name will go here." bug.marker_location = "Marker location will go here." 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