From 85beb59157a2468dde9e75fff5e81aa6cd7bfa42 Mon Sep 17 00:00:00 2001 From: yingying Date: Fri, 6 Dec 2024 17:11:15 +0800 Subject: [PATCH] feat: handle the uninstall action --- server/core/dao/repositoryConfigDAO.py | 11 ++++++++- server/event_handler/intsall.py | 31 ++++++++++++++++++++++++++ server/github_app/handlers.py | 7 ++++-- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 server/event_handler/intsall.py diff --git a/server/core/dao/repositoryConfigDAO.py b/server/core/dao/repositoryConfigDAO.py index 7cf9f747..26151aa0 100644 --- a/server/core/dao/repositoryConfigDAO.py +++ b/server/core/dao/repositoryConfigDAO.py @@ -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 \ No newline at end of file + 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 diff --git a/server/event_handler/intsall.py b/server/event_handler/intsall.py new file mode 100644 index 00000000..7e843602 --- /dev/null +++ b/server/event_handler/intsall.py @@ -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)} diff --git a/server/github_app/handlers.py b/server/github_app/handlers.py index 25c03140..3caea4ad 100644 --- a/server/github_app/handlers.py +++ b/server/github_app/handlers.py @@ -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 @@ -25,6 +26,7 @@ def get_handler( DiscussionEventHandler, DiscussionCommentEventHandler, PullRequestReviewCommentEventHandler, + InstallEventHandler, None, ]: handlers = { @@ -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)