Setting up a repository hook with JSON
To use the notification hook in Notifyr a JSON configuration must be added to the root of your source code. The advantage of having a JSON file that is part of your source code is that:
- You can include a default setting in your project architecture.
- Developers can include their own notification if required
- Much more flexibility on the type of notifications based on branches
Prepare your repository for the notification hook
For a repository to use the notification hook, a JSON file named notifications.json
needs to be added to the root of the repository. A bare minimum notifications.json
file would look like this:
{ "rules": [{ "notifications": [{ "address" : "[email protected]" }] }] }
The above configuration would send a notification to [email protected]
for every push made to the repository.
The JSON configuration has more options and flexibility in when to send what kind of notification and to who. Take the following configuration for example:
{ "rules": [ { "match": "/**/feature/*", "inverted" : false, "notifications" : [ { "address" : "[email protected]", "sendSeparate" : true, "includeDiff" : true }, { "address" : "[email protected]", "prefix" : "[HOOK]" } ] }, { "match": "/**/*", "events": ["Push", "BranchCreate"], "notifications": [ { "address": "[email protected]" } ] } ] }
There are a few things happening here, but the first thing you might notice is that you are able to specify multiple rules. Let's explain the options
match | An Ant-based pattern to match your branch against/**/* matches all branches (default)/**/feature/* matches all feature branches | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
filePattern | SINCE 4.3.0 An Ant-based pattern to match your file paths against | ||||||||||||||||||||||
inverted | true or false should the result from the match be inverted? Defaults to false | ||||||||||||||||||||||
events | SINCE 4.1.0 An array of events on which the rule should be triggered. Possible events are
| ||||||||||||||||||||||
notifications | an array of notifications:
|
Enable to Notification Hook
Repository administrators can set up the hooks that are available in Bitbucket by going to Settings > Hooks for a Bitbucket repository. Once installed, hooks are available across all repositories in a Bitbucket instance but are enabled separately on each repository in a project.
To set up the Notification Hook:
- Browse to the Hooks overview in your repository settings
- Browse to the relevant repository
- Choose Settings from the repository navigation
- Select Hooks from the left menu
- Toggle the Notifyr - Notification Hook setting to Enabled.