experimental 1

This commit is contained in:
stupidcomputer 2024-10-09 05:53:57 -05:00
parent 54d0759759
commit 13d6747f5e
2 changed files with 162 additions and 144 deletions

View File

@ -27,7 +27,10 @@ def gitea_handle_repo_action():
Gitea and Github ends Gitea and Github ends
""" """
gitea = Gitea(app.config["GITEA_ACCESS_TOKEN"]) gitea = Gitea(
api_token=app.config["GITEA_ACCESS_TOKEN"],
instance_name=app.config["GITEA_INSTANCE_DOMAIN"]
)
github = Github(app.config["GITHUB_ACCESS_TOKEN"]) github = Github(app.config["GITHUB_ACCESS_TOKEN"])
data = request.json data = request.json
@ -47,94 +50,38 @@ def gitea_handle_repo_action():
if not repo_action == "created": if not repo_action == "created":
return '' return ''
github_created_repo_result = github.post( github_created_repo_result = github.create_repo(
"https://api.github.com/user/repos", repo_name, repo_description
json={
"name": repo_name,
"description": repo_description,
"homepage": "https://{}/{}/{}".format(
app.config["GITEA_INSTANCE_DOMAIN"],
repo_owner,
repo_name,
),
},
) )
new_github_repo_url = github_created_repo_result.json()["html_url"] new_github_repo_url = github_created_repo_result.json()["html_url"]
gitea_add_github_repo_url_result = gitea.patch( gitea_push_target_result = gitea.add_push_target(
"https://{}/api/v1/repos/{}/{}".format( repo_owner, repo_name, new_github_repo_url, repo_owner,
app.config["GITEA_INSTANCE_DOMAIN"], app.config["GITHUB_ACCESS_TOKEN"]
repo_owner,
repo_name,
),
json={
"website": new_github_repo_url,
},
) )
gitea_push_target_result = gitea.post( gitea_force_target_push = gitea.force_push_target(
"https://{}/api/v1/repos/{}/{}/push_mirrors".format( repo_owner,
app.config["GITEA_INSTANCE_DOMAIN"], repo_name
repo_owner,
repo_name
),
json={
"interval": "8h0m0s",
"remote_address": new_github_repo_url,
"remote_password": app.config["GITHUB_ACCESS_TOKEN"],
"remote_username": repo_owner,
"sync_on_commit": True,
},
) )
gitea_force_target_push = gitea.post( github_create_webhook_result = github.create_webhook(
"https://{}/api/v1/repos/{}/{}/push_mirrors-sync".format( repo_owner,
app.config["GITEA_INSTANCE_DOMAIN"], repo_name,
repo_owner, "https://{}/bridge/endpoints/github/issue".format(
repo_name app.config["GITEA_INSTANCE_DOMAIN"]
), ),
["issues", "issue_comment"]
) )
github_create_webhook_result = github.post( gitea_create_webhook_result = gitea.create_webhook(
"https://api.github.com/repos/{}/{}/hooks".format( repo_owner,
repo_owner, repo_name,
repo_name, "https://{}/bridge/endpoints/github/issue".format(
app.config["GITEA_INSTANCE_DOMAIN"]
), ),
json={ ["issues", "issue_comment"]
"name": "web",
"config": {
"url": "https://{}/bridge/endpoints/github/issue".format(
app.config["GITEA_INSTANCE_DOMAIN"]
),
"content_type": "json",
},
"events": [
"issues", "issue_comment",
],
},
)
gitea_create_webhook_result = gitea.post(
"https://{}/api/v1/repos/{}/{}/hooks".format(
app.config["GITEA_INSTANCE_DOMAIN"],
repo_owner,
repo_name,
),
json={
"active": True,
"type": "gitea",
"config": {
"content_type": "json",
"url": "https://{}/bridge/endpoints/gitea/issue".format(
app.config["GITEA_INSTANCE_DOMAIN"],
),
"http_method": "post",
},
"events": [
"issues", "issue_comment",
],
},
) )
return '' return ''
@ -214,15 +161,11 @@ def gitea_handle_issue_action():
issue_footer issue_footer
]) ])
github_create_issue_result = github.post( github_create_issue_result = github.create_issue(
"https://api.github.com/repos/{}/{}/issues".format( repo_owner,
repo_owner, repo_name,
repo_name, event_title,
), issue_body,
json={
"title": event_title,
"body": issue_body,
},
) )
returned_data = github_create_issue_result.json() returned_data = github_create_issue_result.json()
@ -239,16 +182,11 @@ def gitea_handle_issue_action():
generate_sentinel(returned_data["url"]) generate_sentinel(returned_data["url"])
) )
gitea_issue_comment_result = gitea.post( gitea_issue_comment_result = gitea.leave_comment_on_issue_by_number(
"https://{}/api/v1/repos/{}/{}/issues/{}/comments".format( repo_owner,
app.config["GITEA_INSTANCE_DOMAIN"], repo_name,
repo_owner, issue_number,
repo_name, body,
issue_number,
),
json={
"body": issue_comment_body,
},
) )
elif event_type == "created": elif event_type == "created":
@ -277,27 +215,18 @@ def gitea_handle_issue_action():
comment_footer, comment_footer,
]) ])
github_comment_post_result = github.post( github_comment_post_result = github.leave_comment_on_issue_by_number(
"https://api.github.com/repos/{}/{}/issues/{}/comments".format( repo_owner,
repo_owner, repo_name,
repo_name, issue_number,
issue_number, body,
),
json={
"body": comment_body,
},
) )
elif event_type == "closed": elif event_type == "closed":
github_close_issue_result = github.patch( github_close_issue_result = github.close_issue_by_number(
"https://api.github.com/repos/{}/{}/issues/{}".format( repo_owner,
repo_owner, repo_name,
repo_name, issue_number,
issue_number,
),
json={
"state": "closed",
},
) )
return '' return ''
@ -361,16 +290,11 @@ def github_handle_issue_action():
issue_footer issue_footer
]) ])
gitea_create_issue_result = gitea.post( gitea_create_issue_result = gitea.create_issue(
"https://{}/api/v1/repos/{}/{}/issues".format( repo_owner,
app.config["GITEA_INSTANCE_DOMAIN"], repo_name,
repo_owner, event_title,
repo_name, issue_body
),
json={
"title": event_title,
"body": issue_body,
}
) )
elif event_type == "created": elif event_type == "created":
@ -398,29 +322,19 @@ def github_handle_issue_action():
comment_footer, comment_footer,
]) ])
gitea_comment_post_result = gitea.post(
"https://{}/api/v1/repos/{}/{}/issues/{}/comments".format( gitea_comment_post_result = gitea.leave_comment_on_issue_by_number(
app.config["GITEA_INSTANCE_DOMAIN"], repo_owner,
repo_owner, repo_name,
repo_name, issue_number,
issue_number, comment_body
),
json={
"body": comment_body,
},
) )
elif event_type == "closed": elif event_type == "closed":
gitea_close_issue_result = gitea.patch( gitea_close_issue_result = gitea.close_issue_by_number(
"https://{}/api/v1/repos/{}/{}/issues/{}".format( repo_owner,
app.config["GITEA_INSTANCE_DOMAIN"], repo_name,
repo_owner, issue_number,
repo_name,
issue_number,
),
json={
"state": "closed",
},
) )
return '' return ''

View File

@ -15,6 +15,7 @@ class WebgitClient:
""" """
api_token: str api_token: str
headers: Any = field(init=False) headers: Any = field(init=False)
api_prefix: str = field(init=False)
def _post_request(self, request_obj): def _post_request(self, request_obj):
try: try:
@ -42,6 +43,83 @@ 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 create_repo(self, name, description):
return self.post(
self.api_prefix + "/user/repos",
json={
"name": name,
"description": description,
},
)
def create_webhook(self, owner, repo_name, http_endpoint, events: List[str]):
return self.post(
self.api_prefix + "/repos/{}/{}/hooks".format(
owner,
repo_name,
),
json={
"name": "web",
"config": {
"url": http_endpoint,
"content_type": "json",
},
"events": events,
},
)
def create_issue(self, owner, repo_name, title, body):
return self.post(
self.api_prefix + "/repos/{}/{}/issues".format(
owner,
repo_name,
),
json={
"title": title,
"body": body,
},
)
def leave_comment_on_issue_by_number(self, owner, repo_name, issue_number, body):
return self.post(
self.api_prefix + "/repos/{}/{}/issues/{}/comments".format(
owner,
repo_name,
issue_number,
),
json={
"body": body,
},
)
def leave_comment_on_issue_by_url(self, url, body):
return self.post(
url + "/comments",
json={
"body": body,
},
)
def close_issue_by_number(self, owner, repo_name, issue_number):
return self.patch(
self.api_prefix + "/repos/{}/{}/issues/{}".format(
owner,
repo_name,
issue_number,
),
json={
"state": "closed",
},
)
def close_issue_by_url(self, url):
return self.patch(
url,
json={
"state": "closed",
},
)
@dataclass @dataclass
class Github(WebgitClient): class Github(WebgitClient):
""" """
@ -53,13 +131,39 @@ class Github(WebgitClient):
"Authorization": "token " + self.api_token, "Authorization": "token " + self.api_token,
"X-GitHub-Api-Version": "2022-11-28", "X-GitHub-Api-Version": "2022-11-28",
} }
self.api_prefix = "https://api.github.com"
@dataclass @dataclass
class Gitea(WebgitClient): class Gitea(WebgitClient):
""" """
A quite thin wrapper around Gitea's REST API. A quite thin wrapper around Gitea's REST API.
""" """
instance_name: str
def __post_init__(self): def __post_init__(self):
self.headers = { self.headers = {
"Authorization": "token " + self.api_token, "Authorization": "token " + self.api_token,
} }
self.api_prefix = "https://{}/api/v1".format(self.instance_name)
def add_push_target(self, owner, repo_name, address, username, password):
return self.post(
self.api_prefix + "/repos/{}/{}/push_mirrors".format(
owner, repo_name
),
json={
"interval": "8h0m0s",
"remote_address": address,
"remote_password": password,
"remote_username": username,
"sync_on_commit": True,
},
)
def force_push_target(self, owner, repo_name):
return self.post(
self.api_prefix + "/repos/{}/{}/push_mirrors-sync".format(
owner,
repo_name
),
)