From 31374ce81699f15f219dd32066becde1df9c6c1f Mon Sep 17 00:00:00 2001 From: mQzLjP <91550006+mQzLjP@users.noreply.github.com> Date: Fri, 28 Feb 2025 04:15:07 +0800 Subject: [PATCH] feat(mm): git suffix for webhooks (#52) --- README.md | 2 +- joint_teapot/app.py | 8 +++++--- joint_teapot/workers/mattermost.py | 11 +++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e02fc28..3fdc9fe 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ create teams on gitea by canvas groups Create a pair of webhooks on gitea and mm for all student groups on gitea, and configure them so that updates on gitea will be pushed to the mm channel. Optionally specify a prefix to ignore all repos whose names do not start with it. -Example: `python3 -m joint_teapot create-webhooks-for-mm p1` will fetch all repos whose names start with `"p1"` and create two-way webhooks for these repos. All repos should already have same-name mm channels. If not, use `create-channels-on-mm` to create them. +Example: `python3 -m joint_teapot create-webhooks-for-mm p1 -git-suffix` will fetch all repos whose names start with `"p1"` and create two-way webhooks for these repos and channels of the same name but with a "-git" suffix. All repos should already have mm channels following the requirement. If not, use `create-channels-on-mm` to create them. ### `get-no-collaborator-repos` diff --git a/joint_teapot/app.py b/joint_teapot/app.py index aabb970..f02dbff 100644 --- a/joint_teapot/app.py +++ b/joint_teapot/app.py @@ -215,14 +215,16 @@ def create_personal_channels_on_mm( help="create a pair of webhooks on gitea and mm for all student groups on gitea, " "and configure them so that updates on gitea will be pushed to the mm channel", ) -def create_webhooks_for_mm(prefix: str = Argument("")) -> None: +def create_webhooks_for_mm( + regex: str = Argument(""), git_suffix: bool = Option(False) +) -> None: repo_names = [ group_name for group_name in tea.pot.gitea.get_all_teams() - if group_name.startswith(prefix) + if re.match(regex, group_name) ] logger.info(f"{len(repo_names)} pair(s) of webhooks to be created: {repo_names}") - tea.pot.mattermost.create_webhooks_for_repos(repo_names, tea.pot.gitea) + tea.pot.mattermost.create_webhooks_for_repos(repo_names, tea.pot.gitea, git_suffix) @app.command( diff --git a/joint_teapot/workers/mattermost.py b/joint_teapot/workers/mattermost.py index f47f6a3..f67c699 100644 --- a/joint_teapot/workers/mattermost.py +++ b/joint_teapot/workers/mattermost.py @@ -166,17 +166,20 @@ class Mattermost: logger.info(f"Added member {member} to channel {channel_name}") - def create_webhooks_for_repos(self, repos: List[str], gitea: Gitea) -> None: + def create_webhooks_for_repos( + self, repos: List[str], gitea: Gitea, git_suffix: bool + ) -> None: # one group corresponds to one repo so these concepts can be used interchangeably for repo in repos: logger.info(f"Creating webhooks for repo {gitea.org_name}/{repo}") + channel_name = f"{repo}-git" if git_suffix else repo try: mm_channel = self.endpoint.channels.get_channel_by_name( - self.team["id"], repo + self.team["id"], channel_name ) except Exception as e: logger.warning( - f"Error when getting channel {repo} from Mattermost team {self.team['name']}: {e}" + f"Error when getting channel {channel_name} from Mattermost team {self.team['name']}: {e}" ) continue try: @@ -209,7 +212,7 @@ class Mattermost: "username": "FOCS Gitea", "icon_url": f"https://{self.url}{self.url_suffix}/api/v4/brand/image", "content_type": "json", - "channel": repo, + "channel": channel_name, }, ), )