feat(mm): git suffix for webhooks (#52)

This commit is contained in:
mQzLjP 2025-02-28 04:15:07 +08:00 committed by GitHub
parent 34539f435e
commit 31374ce816
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 8 deletions

View File

@ -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. 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` ### `get-no-collaborator-repos`

View File

@ -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, " 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", "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 = [ repo_names = [
group_name group_name
for group_name in tea.pot.gitea.get_all_teams() 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}") 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( @app.command(

View File

@ -166,17 +166,20 @@ class Mattermost:
logger.info(f"Added member {member} to channel {channel_name}") 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 # one group corresponds to one repo so these concepts can be used interchangeably
for repo in repos: for repo in repos:
logger.info(f"Creating webhooks for repo {gitea.org_name}/{repo}") logger.info(f"Creating webhooks for repo {gitea.org_name}/{repo}")
channel_name = f"{repo}-git" if git_suffix else repo
try: try:
mm_channel = self.endpoint.channels.get_channel_by_name( mm_channel = self.endpoint.channels.get_channel_by_name(
self.team["id"], repo self.team["id"], channel_name
) )
except Exception as e: except Exception as e:
logger.warning( 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 continue
try: try:
@ -209,7 +212,7 @@ class Mattermost:
"username": "FOCS Gitea", "username": "FOCS Gitea",
"icon_url": f"https://{self.url}{self.url_suffix}/api/v4/brand/image", "icon_url": f"https://{self.url}{self.url_suffix}/api/v4/brand/image",
"content_type": "json", "content_type": "json",
"channel": repo, "channel": channel_name,
}, },
), ),
) )