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 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')
@ -47,26 +47,22 @@ def gitea_handle_repo_action():
except KeyError: except KeyError:
abort(400) # the data isn't formatted correctly abort(400) # the data isn't formatted correctly
if not repo_action == "created": if repo_action == "created":
return '' new_repo = github.create_repo(
github_created_repo_result = github.create_repo(
repo_name, repo_description repo_name, repo_description
) )
new_repo_url = new_repo.json()["html_url"]
new_github_repo_url = github_created_repo_result.json()["html_url"] gitea.add_push_target(
repo_owner, repo_name, new_repo_url, repo_owner,
gitea_push_target_result = gitea.add_push_target(
repo_owner, repo_name, new_github_repo_url, repo_owner,
app.config["GITHUB_ACCESS_TOKEN"] app.config["GITHUB_ACCESS_TOKEN"]
) )
gitea.force_push_target(
gitea_force_target_push = gitea.force_push_target(
repo_owner, repo_owner,
repo_name repo_name
) )
github_create_webhook_result = github.create_webhook( github.create_webhook(
repo_owner, repo_owner,
repo_name, repo_name,
"https://{}/bridge/endpoints/github/issue".format( "https://{}/bridge/endpoints/github/issue".format(
@ -74,8 +70,7 @@ def gitea_handle_repo_action():
), ),
["issues", "issue_comment"] ["issues", "issue_comment"]
) )
gitea.create_webhook(
gitea_create_webhook_result = gitea.create_webhook(
repo_owner, repo_owner,
repo_name, repo_name,
"https://{}/bridge/endpoints/gitea/issue".format( "https://{}/bridge/endpoints/gitea/issue".format(
@ -84,8 +79,12 @@ def gitea_handle_repo_action():
["issues", "issue_comment"] ["issues", "issue_comment"]
) )
elif repo_action == "deleted":
github.delete_repo(repo_owner, repo_name)
return '' 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():
""" """
@ -144,52 +143,38 @@ 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
]) ])
github_create_issue_result = github.create_issue( new_issue = github.create_issue(
repo_owner, repo_owner,
repo_name, repo_name,
event_title, event_title,
issue_body, issue_body,
) )
returned_data = github_create_issue_result.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_issue_comment_result = 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":
@ -198,35 +183,26 @@ 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,
]) ])
github_comment_post_result = github.leave_comment_on_issue_by_number( github.leave_comment_on_issue_by_number(
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,
@ -276,27 +252,18 @@ 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
]) ])
gitea_create_issue_result = gitea.create_issue( gitea.create_issue(
repo_owner, repo_owner,
repo_name, repo_name,
event_title, event_title,
@ -308,28 +275,19 @@ 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_comment_post_result = gitea.leave_comment_on_issue_by_number(
repo_owner, repo_owner,
repo_name, repo_name,
issue_number, issue_number,
@ -337,7 +295,7 @@ def github_handle_issue_action():
) )
elif event_type == "closed": elif event_type == "closed":
gitea_close_issue_result = gitea.close_issue_by_number( gitea.close_issue_by_number(
repo_owner, repo_owner,
repo_name, repo_name,
issue_number, issue_number,

View File

@ -14,3 +14,27 @@ 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

@ -43,6 +43,9 @@ class WebgitClient:
def patch(self, *args, **kwargs): def patch(self, *args, **kwargs):
return self._request_wrapper("PATCH", *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): def create_repo(self, name, description):
return self.post( return self.post(
self.api_prefix + "/user/repos", 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 @dataclass
class Github(WebgitClient): class Github(WebgitClient):
""" """
@ -175,6 +183,7 @@ 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",