Compare commits

...

2 Commits

2 changed files with 126 additions and 3 deletions

View File

@ -278,7 +278,7 @@ def gitea_handle_issue_action():
]) ])
github_comment_post_result = github.post( github_comment_post_result = github.post(
"https://api.github.com/repos/{}/{}/issues/{}/comment".format( "https://api.github.com/repos/{}/{}/issues/{}/comments".format(
repo_owner, repo_owner,
repo_name, repo_name,
issue_number, issue_number,
@ -300,4 +300,127 @@ def gitea_handle_issue_action():
}, },
) )
return '' 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 = """
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".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 = """
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".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 ''

View File

@ -13,4 +13,4 @@ def from_base64(s: str) -> str:
issue_sentinel = "GITEA_GITHUB_ISSUE_SYNC_SENTINEL" issue_sentinel = "GITEA_GITHUB_ISSUE_SYNC_SENTINEL"
def generate_sentinel(url: str) -> str: def generate_sentinel(url: str) -> str:
return ' '.join([sentinel, to_base64(url)]) return ' '.join([issue_sentinel, to_base64(url)])