diff --git a/server/event_handler/intsall.py b/server/event_handler/intsall.py index 4baedb9b..757fd739 100644 --- a/server/event_handler/intsall.py +++ b/server/event_handler/intsall.py @@ -6,7 +6,7 @@ import time -class InstallEventHandler: +class InstallationEventHandler: event: Any auth: Auth.AppAuth g: Github @@ -50,3 +50,49 @@ async def execute(self): except GithubException as e: print(f"处理 GitHub 请求时出错:{e}") return {"success": False, "error": str(e)} + + +class InstallationEditEventHandler: + 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_removed"] + repo_ids = [str(repo["id"]) for repo in repositories] + repository_config = RepositoryConfigDAO() + repository_config.delete_by_repo_ids(repo_ids) + + def add_config(self): + repositories = self.event["repositories_added"] + owner_id = self.event["installation"]["account"]["id"] + repository_config_dao = RepositoryConfigDAO() + repository_configs: List[RepositoryConfig] = [] + for repo in repositories: + repository_config = RepositoryConfig( + owner_id=str(owner_id), + repo_name=repo["full_name"], + repo_id=str(repo["id"]), + robot_id="", + created_at=int(time.time()), + ) + repository_configs.append(repository_config) + repository_config_dao.create_batch(repository_configs) + + async def execute(self): + try: + action = self.event["action"] + if action == "removed": + self.delete_config() + return {"success": True} + if action == "added": + self.add_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 3caea4ad..0581221f 100644 --- a/server/github_app/handlers.py +++ b/server/github_app/handlers.py @@ -1,6 +1,6 @@ from typing import Union -from event_handler.intsall import InstallEventHandler +from event_handler.intsall import InstallationEventHandler, InstallationEditEventHandler from petercat_utils import get_env_variable from github import Auth @@ -26,7 +26,8 @@ def get_handler( DiscussionEventHandler, DiscussionCommentEventHandler, PullRequestReviewCommentEventHandler, - InstallEventHandler, + InstallationEventHandler, + InstallationEditEventHandler, None, ]: handlers = { @@ -37,7 +38,8 @@ def get_handler( "discussion_comment": DiscussionCommentEventHandler, "pull_request_review_comment": PullRequestReviewCommentEventHandler, "pull_request_review": PullRequestReviewCommentEventHandler, - "installation": InstallEventHandler, + "installation": InstallationEventHandler, + "installation_repositories": InstallationEditEventHandler, } return ( handlers.get(event)(payload=payload, auth=auth, installation_id=installation_id)