Skip to content

Commit

Permalink
feat: handle the uninstall action (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwanying authored Dec 6, 2024
1 parent a38c4dc commit 5401bef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
11 changes: 10 additions & 1 deletion server/core/dao/repositoryConfigDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ def get_by_bot_id(self, bot_id: str):
return None
repo_configs = [RepositoryConfig(**repo) for repo in response.data]

return repo_configs
return repo_configs

def delete_by_repo_ids(self, repo_ids: list):
response = (
self.client.table("github_repo_config")
.delete()
.in_("repo_id", repo_ids)
.execute()
)
return response
31 changes: 31 additions & 0 deletions server/event_handler/intsall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any
from github import Github, Auth
from github import GithubException
from core.dao.repositoryConfigDAO import RepositoryConfigDAO


class InstallEventHandler:
event: Any
auth: Auth.AppAuth
g: Github

def __init__(self, payload: Any, auth: Auth.AppAuth, installation_id: int) -> None:
self.event: Any = payload
self.auth: Auth.AppAuth = auth
self.g: Github = Github(auth=auth)

def delete_config(self):
repositories = self.event["repositories"]
repo_ids = [str(repo["id"]) for repo in repositories]
repository_config = RepositoryConfigDAO()
repository_config.delete_by_repo_ids(repo_ids)

async def execute(self):
try:
action = self.event["action"]
if action == "deleted":
self.delete_config()
return {"success": True}
except GithubException as e:
print(f"处理 GitHub 请求时出错:{e}")
return {"success": False, "error": str(e)}
7 changes: 5 additions & 2 deletions server/github_app/handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Union

from event_handler.intsall import InstallEventHandler
from petercat_utils import get_env_variable
from github import Auth

Expand All @@ -25,6 +26,7 @@ def get_handler(
DiscussionEventHandler,
DiscussionCommentEventHandler,
PullRequestReviewCommentEventHandler,
InstallEventHandler,
None,
]:
handlers = {
Expand All @@ -33,8 +35,9 @@ def get_handler(
"issue_comment": IssueCommentEventHandler,
"discussion": DiscussionEventHandler,
"discussion_comment": DiscussionCommentEventHandler,
"pull_request_review_comment":PullRequestReviewCommentEventHandler,
"pull_request_review":PullRequestReviewCommentEventHandler,
"pull_request_review_comment": PullRequestReviewCommentEventHandler,
"pull_request_review": PullRequestReviewCommentEventHandler,
"installation": InstallEventHandler,
}
return (
handlers.get(event)(payload=payload, auth=auth, installation_id=installation_id)
Expand Down

0 comments on commit 5401bef

Please sign in to comment.