diff --git a/bridge/__init__.py b/bridge/__init__.py index 9a3f26f..d4f51ee 100644 --- a/bridge/__init__.py +++ b/bridge/__init__.py @@ -300,4 +300,127 @@ def gitea_handle_issue_action(): }, ) - return '' \ No newline at end of file + return '' + +@app.route("/bridge/endpoints/github/issue", methods=["POST"]) +def github_handle_issue_action(): + gitea = Gitea(app.config["GITEA_ACCESS_TOKEN"]) + github = Github(app.config["GITHUB_ACCESS_TOKEN"]) + + data = request.json + + try: + event_type = data["action"] + + repo_owner = data["repository"]["owner"]["login"] + repo_name = data["repository"]["name"] + issue_user = data["issue"]["user"]["login"] + issue_user_url = "https://github.com/{}".format( + issue_user, + ) + issue_number = data["issue"]["number"] + + if event_type == "opened": # new issue created + event_title = data["issue"]["title"] + event_body = data["issue"]["body"] + elif event_type == "created": # new comment on that issue + event_title = None + event_body = data["comment"]["body"] + elif event_type == "closed": # issue closed + event_title = None + event_body = data["issue"]["body"] + + if not event_body: event_body = "" + event_url = data["issue"]["url"] + + except KeyError as e: + print(e, type(e)) + abort(400) # the data isn't formatted correctly + + if issue_sentinel in event_body: + return '' + + if event_type == "opened": + issue_header = "*This issue has automatically been created by [`gitea-github-sync`](https://{}/bridge/about) on behalf of [{}]({}).*".format( + app.config["GITEA_INSTANCE_DOMAIN"], + issue_user, + issue_user_url, + ) + + issue_footer = """ +
+ Internal issue metadata + + {} +
+ """.format(generate_sentinel(event_url)) + + issue_body = "\n\n".join([ + issue_header, + event_body, + issue_footer + ]) + + gitea_create_issue_result = gitea.post( + "https://{}/api/v1/repos/{}/{}/issues".format( + app.config["GITEA_INSTANCE_DOMAIN"], + repo_owner, + repo_name, + ), + json={ + "title": event_title, + "body": issue_body, + } + ) + + elif event_type == "created": + comment_user = data["comment"]["user"]["login"] + comment_user_url = "https://github.com/{}".format( + comment_user, + ) + comment_header = "*This comment has automatically been created by [`gitea-github-sync`](https://{}/bridge/about) on behalf of [{}]({}).*".format( + app.config["GITEA_INSTANCE_DOMAIN"], + comment_user, + comment_user_url, + ) + + comment_footer = """ +
+ Internal issue metadata + + {} +
+ """.format(generate_sentinel(event_url)) + + comment_body = "\n\n".join([ + comment_header, + event_body, + comment_footer, + ]) + + gitea_comment_post_result = gitea.post( + "https://{}/api/v1/repos/{}/{}/issues/{}/comments".format( + app.config["GITEA_INSTANCE_DOMAIN"], + repo_owner, + repo_name, + issue_number, + ), + json={ + "body": comment_body, + }, + ) + + elif event_type == "closed": + gitea_close_issue_result = gitea.patch( + "https://{}/api/v1/repos/{}/{}/issues/{}".format( + app.config["GITEA_INSTANCE_DOMAIN"], + repo_owner, + repo_name, + issue_number, + ), + json={ + "state": "closed", + }, + ) + + return ''