Skip to content

Commit

Permalink
Allow filtering VCS webhooks based on branch name
Browse files Browse the repository at this point in the history
Fixes #258
  • Loading branch information
rubenwardy committed Jun 22, 2024
1 parent 09e06a1 commit 7a94b93
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion app/blueprints/vcs/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
7 changes: 6 additions & 1 deletion app/blueprints/vcs/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
24 changes: 16 additions & 8 deletions app/flatpages/help/release_webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<p class="alert alert-warning">
"New commit" or "push" based webhooks will currently only work on branches named `master` or
`main`.
</p>

## Setting up

Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 7a94b93

Please sign in to comment.