Skip to content

Commit

Permalink
feat: add the InstallationEditEventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwanying committed Dec 10, 2024
1 parent 22e06a2 commit 52646b4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
48 changes: 47 additions & 1 deletion server/event_handler/intsall.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time


class InstallEventHandler:
class InstallationEventHandler:
event: Any
auth: Auth.AppAuth
g: Github
Expand Down Expand Up @@ -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)}
8 changes: 5 additions & 3 deletions server/github_app/handlers.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -26,7 +26,8 @@ def get_handler(
DiscussionEventHandler,
DiscussionCommentEventHandler,
PullRequestReviewCommentEventHandler,
InstallEventHandler,
InstallationEventHandler,
InstallationEditEventHandler,
None,
]:
handlers = {
Expand All @@ -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,

Check warning on line 42 in server/github_app/handlers.py

View check run for this annotation

Codecov / codecov/patch

server/github_app/handlers.py#L42

Added line #L42 was not covered by tests
}
return (
handlers.get(event)(payload=payload, auth=auth, installation_id=installation_id)
Expand Down

0 comments on commit 52646b4

Please sign in to comment.