Compare commits

...

2 Commits

3 changed files with 49 additions and 64 deletions

View File

@ -6,7 +6,7 @@ from flask import abort
import requests import requests
from .webgit import Gitea, Github from .webgit import Gitea, Github
from .utils import issue_sentinel, generate_sentinel from .utils import issue_sentinel, generate_sentinel, create_signature
app = Flask(__name__) app = Flask(__name__)
app.config.from_envvar('GIT_BRIDGE_SETTINGS') app.config.from_envvar('GIT_BRIDGE_SETTINGS')
@ -79,10 +79,11 @@ def gitea_handle_repo_action():
["issues", "issue_comment"] ["issues", "issue_comment"]
) )
return ''
elif repo_action == "deleted": elif repo_action == "deleted":
github.delete_repo(repo_owner, repo_name) github.delete_repo(repo_owner, repo_name)
return ''
@app.route("/bridge/endpoints/gitea/issue", methods=["POST"]) @app.route("/bridge/endpoints/gitea/issue", methods=["POST"])
def gitea_handle_issue_action(): def gitea_handle_issue_action():
@ -142,22 +143,14 @@ def gitea_handle_issue_action():
return '' return ''
if event_type == "opened": if event_type == "opened":
issue_header = "*This issue has automatically been created by [`gitea-github-sync`](https://{}/bridge/about) on behalf of [{}]({}).*".format( issue_footer = create_signature(
app.config["GITEA_INSTANCE_DOMAIN"],
issue_user, issue_user,
issue_user_url, issue_user_url,
app.config["GITEA_INSTANCE_DOMAIN"],
event_url,
) )
issue_footer = """
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".format(generate_sentinel(event_url))
issue_body = "\n\n".join([ issue_body = "\n\n".join([
issue_header,
event_body, event_body,
issue_footer issue_footer
]) ])
@ -168,25 +161,20 @@ def gitea_handle_issue_action():
event_title, event_title,
issue_body, issue_body,
) )
returned_data = new_issue.json() returned_data = new_issue.json()
issue_comment_body = """ issue_comment_body = create_signature(
*This issue is being mirrored on Github [here]({}).* "mirrored",
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".format(
returned_data["html_url"], returned_data["html_url"],
generate_sentinel(returned_data["url"]) app.config["GITEA_INSTANCE_DOMAIN"],
returned_data["url"],
) )
gitea.leave_comment_on_issue_by_number( gitea.leave_comment_on_issue_by_number(
repo_owner, repo_owner,
repo_name, repo_name,
issue_number, issue_number,
body, issue_comment_body,
) )
elif event_type == "created": elif event_type == "created":
@ -195,22 +183,13 @@ def gitea_handle_issue_action():
app.config["GITEA_INSTANCE_DOMAIN"], app.config["GITEA_INSTANCE_DOMAIN"],
comment_user, comment_user,
) )
comment_header = "*This comment has automatically been created by [`gitea-github-sync`](https://{}/bridge/about) on behalf of [{}]({}).*".format( comment_footer = create_signature(
app.config["GITEA_INSTANCE_DOMAIN"],
comment_user, comment_user,
comment_user_url, comment_user_url,
app.config["GITEA_INSTANCE_DOMAIN"],
event_url,
) )
comment_footer = """
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".format(generate_sentinel(event_url))
comment_body = "\n\n".join([ comment_body = "\n\n".join([
comment_header,
event_body, event_body,
comment_footer, comment_footer,
]) ])
@ -219,11 +198,11 @@ def gitea_handle_issue_action():
repo_owner, repo_owner,
repo_name, repo_name,
issue_number, issue_number,
body, comment_body,
) )
elif event_type == "closed": elif event_type == "closed":
github_close_issue_result = github.close_issue_by_number( github.close_issue_by_number(
repo_owner, repo_owner,
repo_name, repo_name,
issue_number, issue_number,
@ -273,22 +252,13 @@ def github_handle_issue_action():
return '' return ''
if event_type == "opened": if event_type == "opened":
issue_header = "*This issue has automatically been created by [`gitea-github-sync`](https://{}/bridge/about) on behalf of [{}]({}).*".format( issue_footer = create_signature(
app.config["GITEA_INSTANCE_DOMAIN"],
issue_user, issue_user,
issue_user_url, issue_user_url,
app.config["GITEA_INSTANCE_DOMAIN"],
event_url,
) )
issue_footer = """
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".format(generate_sentinel(event_url))
issue_body = "\n\n".join([ issue_body = "\n\n".join([
issue_header,
event_body, event_body,
issue_footer issue_footer
]) ])
@ -305,27 +275,18 @@ def github_handle_issue_action():
comment_user_url = "https://github.com/{}".format( comment_user_url = "https://github.com/{}".format(
comment_user, comment_user,
) )
comment_header = "*This comment has automatically been created by [`gitea-github-sync`](https://{}/bridge/about) on behalf of [{}]({}).*".format( comment_footer = create_signature(
app.config["GITEA_INSTANCE_DOMAIN"],
comment_user, comment_user,
comment_user_url, comment_user_url,
app.config["GITEA_INSTANCE_DOMAIN"],
event_url,
) )
comment_footer = """
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".format(generate_sentinel(event_url))
comment_body = "\n\n".join([ comment_body = "\n\n".join([
comment_header,
event_body, event_body,
comment_footer, comment_footer,
]) ])
gitea.leave_comment_on_issue_by_number( gitea.leave_comment_on_issue_by_number(
repo_owner, repo_owner,
repo_name, repo_name,

View File

@ -13,4 +13,28 @@ 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([issue_sentinel, to_base64(url)]) return ' '.join([issue_sentinel, to_base64(url)])
def create_signature(
username: str,
username_url: str,
domain_base: str,
url_to_encode: str,
):
return """
---
[{}]({}) via [gitea-github-sync](https://{}/bridge/about)
<details>
<summary>Internal information</summary>
{}
</details>
""".format(
username,
username_url,
domain_base,
generate_sentinel(url_to_encode),
)

View File

@ -183,11 +183,11 @@ class Gitea(WebgitClient):
repo_name, repo_name,
), ),
json={ json={
"active": True,
"config": { "config": {
"url": http_endpoint, "url": http_endpoint,
"content_type": "json", "content_type": "json",
}, },
"active": True,
"events": events, "events": events,
"type": "gitea", "type": "gitea",
}, },