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

View File

@ -15,6 +15,7 @@ class WebgitClient:
"""
api_token: str
headers: Any = field(init=False)
api_prefix: str = field(init=False)
def _post_request(self, request_obj):
try:
@ -42,6 +43,83 @@ class WebgitClient:
def patch(self, *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
class Github(WebgitClient):
"""
@ -53,13 +131,39 @@ class Github(WebgitClient):
"Authorization": "token " + self.api_token,
"X-GitHub-Api-Version": "2022-11-28",
}
self.api_prefix = "https://api.github.com"
@dataclass
class Gitea(WebgitClient):
"""
A quite thin wrapper around Gitea's REST API.
"""
instance_name: str
def __post_init__(self):
self.headers = {
"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
),
)