From 0c6b3f96bea7e262ea0bfceedb7f279479add68a Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Tue, 30 Jan 2024 14:23:26 +0100 Subject: [PATCH] feat: reload otterdog config upon changes --- otterdog/webapp/tasks/__init__.py | 12 ++++++--- otterdog/webapp/webhook/__init__.py | 33 +++++++++++++++++++++--- otterdog/webapp/webhook/github_models.py | 11 ++++---- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/otterdog/webapp/tasks/__init__.py b/otterdog/webapp/tasks/__init__.py index 259c7d04..81b55a27 100644 --- a/otterdog/webapp/tasks/__init__.py +++ b/otterdog/webapp/tasks/__init__.py @@ -101,20 +101,26 @@ def get_otterdog_config() -> OtterdogConfig: global _OTTERDOG_CONFIG if _OTTERDOG_CONFIG is None: - _OTTERDOG_CONFIG = load_otterdog_config() + _OTTERDOG_CONFIG = _load_otterdog_config() return _OTTERDOG_CONFIG -def load_otterdog_config() -> OtterdogConfig: +def refresh_otterdog_config(): + global _OTTERDOG_CONFIG + _OTTERDOG_CONFIG = _load_otterdog_config() + + +def _load_otterdog_config() -> OtterdogConfig: app_root = current_app.config["APP_ROOT"] config_file_url = current_app.config["OTTERDOG_CONFIG_URL"] + logger.info(f"loading otterdog config from url '{config_file_url}'") + import requests with requests.get(config_file_url) as response: config_file = os.path.join(app_root, "otterdog.json") - logger.info(f"writing otterdog configuration to '{config_file}'") with open(config_file, "w") as file: file.write(response.text) diff --git a/otterdog/webapp/webhook/__init__.py b/otterdog/webapp/webhook/__init__.py index fed9ec90..117c450e 100644 --- a/otterdog/webapp/webhook/__init__.py +++ b/otterdog/webapp/webhook/__init__.py @@ -13,14 +13,14 @@ from quart import Response, current_app from otterdog.utils import LogLevel -from otterdog.webapp.tasks import get_otterdog_config +from otterdog.webapp.tasks import get_otterdog_config, refresh_otterdog_config from otterdog.webapp.tasks.apply_changes import ApplyChangesTask from otterdog.webapp.tasks.check_sync import CheckConfigurationInSyncTask from otterdog.webapp.tasks.help_comment import HelpCommentTask from otterdog.webapp.tasks.retrieve_team_membership import RetrieveTeamMembershipTask from otterdog.webapp.tasks.validate_pull_request import ValidatePullRequestTask -from .github_models import IssueCommentEvent, PullRequestEvent +from .github_models import IssueCommentEvent, PullRequestEvent, PushEvent from .github_webhook import GitHubWebhook webhook = GitHubWebhook() @@ -36,6 +36,9 @@ async def on_pull_request_received(data): logger.error("failed to load pull request event data", exc_info=True) return success() + if event.installation is None or event.organization is None: + return success() + otterdog_config = get_otterdog_config() if event.repository.name != otterdog_config.default_config_repo: @@ -47,7 +50,7 @@ async def on_pull_request_received(data): event.installation.id, event.organization.login, event.repository, - event.issue.number, + event.pull_request, ).execute ) @@ -82,6 +85,9 @@ async def on_issue_comment_received(data): logger.error("failed to load issue comment event data", exc_info=True) return success() + if event.installation is None or event.organization is None: + return success() + otterdog_config = get_otterdog_config() if event.repository.name != otterdog_config.default_config_repo: @@ -145,5 +151,26 @@ async def on_issue_comment_received(data): return success() +@webhook.hook("push") +async def on_push_received(data): + try: + event = PushEvent.model_validate(data) + except ValidationError: + logger.error("failed to load push event data", exc_info=True) + return success() + + if ( + event.repository.name != current_app.config["OTTERDOG_CONFIG_REPO"] + or event.repository.owner.login != current_app.config["OTTERDOG_CONFIG_OWNER"] + ): + return success() + + if event.ref != f"refs/heads/{event.repository.default_branch}": + return success() + + current_app.add_background_task(refresh_otterdog_config) + return success() + + def success() -> Response: return Response({}, mimetype="application/json", status=200) diff --git a/otterdog/webapp/webhook/github_models.py b/otterdog/webapp/webhook/github_models.py index eb6a2428..e00cea74 100644 --- a/otterdog/webapp/webhook/github_models.py +++ b/otterdog/webapp/webhook/github_models.py @@ -72,10 +72,10 @@ class PullRequest(BaseModel): state: str locked: bool title: str - body: Optional[str] + body: Optional[str] = None draft: bool merged: bool - merge_commit_sha: Optional[str] + merge_commit_sha: Optional[str] = None user: Actor head: Ref @@ -108,15 +108,15 @@ class Issue(BaseModel): class Event(ABC, BaseModel): """Base class of events""" - action: str - installation: Installation - organization: Organization + installation: Optional[Installation] = None + organization: Optional[Organization] = None sender: Actor class PullRequestEvent(Event): """A payload sent for pull request specific events.""" + action: str number: int pull_request: PullRequest repository: Repository @@ -139,6 +139,7 @@ class PushEvent(Event): class IssueCommentEvent(Event): """A payload sent for issue comment events.""" + action: str issue: Issue comment: Comment repository: Repository