Compare commits

...

5 Commits

3 changed files with 93 additions and 102 deletions

View File

@ -6,7 +6,7 @@ from flask import abort
import requests
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.config.from_envvar('GIT_BRIDGE_SETTINGS')
@ -47,45 +47,44 @@ def gitea_handle_repo_action():
except KeyError:
abort(400) # the data isn't formatted correctly
if not repo_action == "created":
return ''
if repo_action == "created":
new_repo = github.create_repo(
repo_name, repo_description
)
new_repo_url = new_repo.json()["html_url"]
github_created_repo_result = github.create_repo(
repo_name, repo_description
)
gitea.add_push_target(
repo_owner, repo_name, new_repo_url, repo_owner,
app.config["GITHUB_ACCESS_TOKEN"]
)
gitea.force_push_target(
repo_owner,
repo_name
)
new_github_repo_url = github_created_repo_result.json()["html_url"]
github.create_webhook(
repo_owner,
repo_name,
"https://{}/bridge/endpoints/github/issue".format(
app.config["GITEA_INSTANCE_DOMAIN"]
),
["issues", "issue_comment"]
)
gitea.create_webhook(
repo_owner,
repo_name,
"https://{}/bridge/endpoints/gitea/issue".format(
app.config["GITEA_INSTANCE_DOMAIN"]
),
["issues", "issue_comment"]
)
gitea_push_target_result = gitea.add_push_target(
repo_owner, repo_name, new_github_repo_url, repo_owner,
app.config["GITHUB_ACCESS_TOKEN"]
)
gitea_force_target_push = gitea.force_push_target(
repo_owner,
repo_name
)
github_create_webhook_result = github.create_webhook(
repo_owner,
repo_name,
"https://{}/bridge/endpoints/github/issue".format(
app.config["GITEA_INSTANCE_DOMAIN"]
),
["issues", "issue_comment"]
)
gitea_create_webhook_result = gitea.create_webhook(
repo_owner,
repo_name,
"https://{}/bridge/endpoints/gitea/issue".format(
app.config["GITEA_INSTANCE_DOMAIN"]
),
["issues", "issue_comment"]
)
elif repo_action == "deleted":
github.delete_repo(repo_owner, repo_name)
return ''
@app.route("/bridge/endpoints/gitea/issue", methods=["POST"])
def gitea_handle_issue_action():
"""
@ -144,52 +143,38 @@ def gitea_handle_issue_action():
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_footer = create_signature(
issue_user,
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_header,
event_body,
issue_footer
])
github_create_issue_result = github.create_issue(
new_issue = github.create_issue(
repo_owner,
repo_name,
event_title,
issue_body,
)
returned_data = github_create_issue_result.json()
issue_comment_body = """
*This issue is being mirrored on Github [here]({}).*
<details>
<summary>Internal issue metadata</summary>
{}
</details>
""".format(
returned_data = new_issue.json()
issue_comment_body = create_signature(
"mirrored",
returned_data["html_url"],
generate_sentinel(returned_data["url"])
app.config["GITEA_INSTANCE_DOMAIN"],
returned_data["url"],
)
gitea_issue_comment_result = gitea.leave_comment_on_issue_by_number(
gitea.leave_comment_on_issue_by_number(
repo_owner,
repo_name,
issue_number,
body,
issue_comment_body,
)
elif event_type == "created":
@ -198,35 +183,26 @@ def gitea_handle_issue_action():
app.config["GITEA_INSTANCE_DOMAIN"],
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_footer = create_signature(
comment_user,
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_header,
event_body,
comment_footer,
])
github_comment_post_result = github.leave_comment_on_issue_by_number(
github.leave_comment_on_issue_by_number(
repo_owner,
repo_name,
issue_number,
body,
comment_body,
)
elif event_type == "closed":
github_close_issue_result = github.close_issue_by_number(
github.close_issue_by_number(
repo_owner,
repo_name,
issue_number,
@ -276,27 +252,18 @@ def github_handle_issue_action():
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_footer = create_signature(
issue_user,
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_header,
event_body,
issue_footer
])
gitea_create_issue_result = gitea.create_issue(
gitea.create_issue(
repo_owner,
repo_name,
event_title,
@ -308,28 +275,19 @@ def github_handle_issue_action():
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_footer = create_signature(
comment_user,
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_header,
event_body,
comment_footer,
])
gitea_comment_post_result = gitea.leave_comment_on_issue_by_number(
gitea.leave_comment_on_issue_by_number(
repo_owner,
repo_name,
issue_number,
@ -337,7 +295,7 @@ def github_handle_issue_action():
)
elif event_type == "closed":
gitea_close_issue_result = gitea.close_issue_by_number(
gitea.close_issue_by_number(
repo_owner,
repo_name,
issue_number,

View File

@ -14,3 +14,27 @@ issue_sentinel = "GITEA_GITHUB_ISSUE_SYNC_SENTINEL"
def generate_sentinel(url: str) -> str:
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

@ -43,6 +43,9 @@ class WebgitClient:
def patch(self, *args, **kwargs):
return self._request_wrapper("PATCH", *args, **kwargs)
def delete(self, *args, **kwargs):
return self._request_wrapper("DELETE", *args, **kwargs)
def create_repo(self, name, description):
return self.post(
self.api_prefix + "/user/repos",
@ -104,6 +107,11 @@ class WebgitClient:
},
)
def delete_repo(self, owner, repo_name):
return self.delete(
self.api_prefix + "/repos/{}/{}".format(owner, repo_name)
)
@dataclass
class Github(WebgitClient):
"""
@ -175,6 +183,7 @@ class Gitea(WebgitClient):
repo_name,
),
json={
"active": True,
"config": {
"url": http_endpoint,
"content_type": "json",