This is an old revision of the document!
Table of Contents
Bitbucket Server Pull Request Hook Plugin
TODOs
- Internationalization
- Support for HTTPs
- Test button in the config
Introduction
The plugin provides the following functionality:
- Notifying 3rd party services via REST interface about the changes in the project Pull Requests
- Notification on:
- PR creation/edit
- PR decline
- PR merge
- New/Edit/Delete comment in the PR
- Per-repository configuration via 'repository hooks' tab
- Accessible by the repository administrators
- The following HTTP operations are supported:
- POST
- GET
- PUT
- DELETE
Usage
Templating
Several variables can be passed by the message URL or body. There is a special variable containing
comma separated list of available variables:
${all_keys}
[IMAGE]
Authorization
There is no dedicated field for authorization. One can add authorization header into 'headers'. In such case the field will look like:
[IMAGE]
Please visit wikipedia to learn how to construct basic auth header.
For Jenkins it is recommended to use Build Token Root Plugin. It allows using anonymous request + build token (think of API key).
Example 1
- Create Jenkins job “Bitbucket Capture”
- Add String Parameter
ALL
- Add Token
BBBUILD
inBuild Triggers
Trigger builds remotely (e.g., from scripts)
- Check if the job is working
- Trigger job manually using your browser's private mode
- Enter the (appropriate to your setup) URL
http://localhost:8080/buildByToken/buildWithParameters?token=BBBUILD&job=Bitbucket%20Capture&ALL=Hello
Entering URL will take you to an empty page. Back in Jenkins you should see that the job was triggered and parameter is successfully passed:
- Add more String Parameters to the Jenkins job:
ACTION
PR_ID
REPO
PROJECT
- Configure Bitbucket Server
- Ensure that the plugin is installed.
- Enter any repository Settings, then Hooks
- Enable
Pull Request Hook
- Enter URL
http://localhost:8080/buildByToken/buildWithParameters?token=BBBUILD&job=Bitbucket%20Capture&ALL= ${all_keys}&EXTRA=${action}
- Enable all triggers
- Save
- Test connection
- Create pull request
- Add a comment
After that you should notice that a new builds were run. Inspect those builds parameters.
Extras
Jenkins job config.xml
:
<project> <actions/> <description/> <keepDependencies>false</keepDependencies> <properties> <hudson.model.ParametersDefinitionProperty> <parameterDefinitions> <hudson.model.StringParameterDefinition> <name>ALL</name> <description/> <defaultValue/> </hudson.model.StringParameterDefinition> </parameterDefinitions> </hudson.model.ParametersDefinitionProperty> </properties> <scm class="hudson.scm.NullSCM"/> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <authToken>BBBUILD</authToken> <triggers/> <concurrentBuild>false</concurrentBuild> <builders/> <publishers/> <buildWrappers/> </project>
Example 2
We want to capture the following behaviours:
- Creation of the new Pull Request
- Changes to the Pull Request
- New comment
Captured event will be propagated to Jenkins instance.
There is only a single URL per repository therefore we will need a relay job to extract necessary variables and call appropriate jobs.
1. Configure Jenkins
- Add Token
BBBUILD
- Add Parameters
2. Configure Bitbucket
- Set URL
localhost
Notes
- Connection timeout is set to 5 seconds