From 7a94b9361f77dd38c3ad866797181095b3eefcaf Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 22 Jun 2024 12:13:49 +0100 Subject: [PATCH] Allow filtering VCS webhooks based on branch name Fixes #258 --- app/blueprints/vcs/github.py | 5 ++++- app/blueprints/vcs/gitlab.py | 7 ++++++- app/flatpages/help/release_webhooks.md | 24 ++++++++++++++++-------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/blueprints/vcs/github.py b/app/blueprints/vcs/github.py index 059264e0..553fa66a 100644 --- a/app/blueprints/vcs/github.py +++ b/app/blueprints/vcs/github.py @@ -162,7 +162,10 @@ def github_webhook(): ref = json["after"] title = datetime.datetime.utcnow().strftime("%Y-%m-%d") + " " + ref[:5] branch = json["ref"].replace("refs/heads/", "") - if branch not in [ "master", "main" ]: + if package.update_config and package.update_config.ref: + if branch != package.update_config.ref: + continue + elif branch not in ["master", "main"]: continue elif event == "create": diff --git a/app/blueprints/vcs/gitlab.py b/app/blueprints/vcs/gitlab.py index 1b5c8009..f5e9aa8e 100644 --- a/app/blueprints/vcs/gitlab.py +++ b/app/blueprints/vcs/gitlab.py @@ -48,11 +48,16 @@ def webhook_impl(): ref = json["after"] title = datetime.datetime.utcnow().strftime("%Y-%m-%d") + " " + ref[:5] branch = json["ref"].replace("refs/heads/", "") - if branch not in ["master", "main"]: + if package.update_config and package.update_config.ref: + if branch != package.update_config.ref: + continue + elif branch not in ["master", "main"]: continue + elif event == "tag_push": ref = json["ref"] title = ref.replace("refs/tags/", "") + else: return error(400, "Unsupported event: '{}'. Only 'push', 'create:tag', and 'ping' are supported." .format(event or "null")) diff --git a/app/flatpages/help/release_webhooks.md b/app/flatpages/help/release_webhooks.md index 14500f32..cbc4b405 100644 --- a/app/flatpages/help/release_webhooks.md +++ b/app/flatpages/help/release_webhooks.md @@ -16,14 +16,21 @@ See [Git Update Detection](/help/update_config/). The process is as follows: 1. The user creates an API Token and a webhook to use it. -2. The user pushes a commit to the git host (Gitlab or Github). +2. The user pushes a commit to the git host (GitLab or GitHub). 3. The git host posts a webhook notification to ContentDB, using the API token assigned to it. 4. ContentDB checks the API token and issues a new release. + * If multiple packages match, then only the first will have a release created. + +### Branch filtering + +By default, "New commit" or "push" based webhooks will only work on "master"/"main" branches. +You can configure the branch used by changing "Branch name" in [Git update detection](update_config). + +For example, to support production and beta packages you can have multiple packages with the same VCS repo URL +but different [Git update detection](update_config) branch names. + +Tag-based webhooks are accepted on any branch. -

- "New commit" or "push" based webhooks will currently only work on branches named `master` or - `main`. -

## Setting up @@ -36,10 +43,10 @@ The process is as follows: 5. Set the content type to JSON. 6. Set the secret to the access token that you copied. 7. Set the events - * If you want a rolling release, choose "just the push event". - * Or if you want a stable release cycle based on tags, - choose "Let me select" > Branch or tag creation. + * If you want a rolling release, choose "just the push event". + * Or if you want a stable release cycle based on tags, choose "Let me select" > Branch or tag creation. 8. Create. +9. If desired, change [Git update detection](update_config) > Branch name to configure the [branch filtering](#branch-filtering). ### GitLab @@ -53,6 +60,7 @@ The process is as follows: * Or if you want a stable release cycle based on tags, choose "Tag push events". 8. Add webhook. +9. If desired, change [Git update detection](update_config) > Branch name to configure the [branch filtering](#branch-filtering). ## Configuring Release Creation