Skip to content

Commit

Permalink
feat: reload otterdog config upon changes
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Jan 30, 2024
1 parent 1a9d36e commit 0c6b3f9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
12 changes: 9 additions & 3 deletions otterdog/webapp/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
33 changes: 30 additions & 3 deletions otterdog/webapp/webhook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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
)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
11 changes: 6 additions & 5 deletions otterdog/webapp/webhook/github_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 0c6b3f9

Please sign in to comment.