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)