Trigger HTTP requests on pull-request events from Bitbucket
Asked by Gin?s Sanchez:

Hello,

we are using Bitbucket Server and as part of our build pipeline we have been using the [pull-request-notifier-for-bitbucket](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket) to trigger http request from Bitbucket to Jenkins in order to invoke custom events.

Basically, that plugins allows us to have custom buttoms in the PR, for example for doing a "Delegate Merge" or "Run Jenkins Build"; and if user selects it, an http request is sent to Jenkins:

![Selection_045.png](/t5/image/serverpage/image-id/321277iE4A71E0B2F2BFC60/image-dimensions/262x145?v=v2 "Selection_045.png")

The http request params contains parameter like PULL_REQUEST_ID, PULL_REQUEST_FROM_REPO_PROJECT_KEY, PULL_REQUEST_FROM_REPO_NAME, PULL_REQUEST_USER_NAME, PULL_REQUEST_FROM_BRANCH, PULL_REQUEST_TO_BRANCH, PULL_REQUEST_TITLE, PULL_REQUEST_AUTHOR_NAME, PULL_REQUEST_AUTHOR_EMAIL and some others.

There is a fork of that plugin for [Bitbucket Datacenter](https://marketplace.atlassian.com/apps/1231124/pull-request-notifier-for-bitbucket), but there is nothing similar for Bitbucket Cloud. As Atlassian is going not going to provide support for Bitbucket Server anymore, I would like to know if is there any alternative to that plugin for Cloud (Datacenter is very expensive for us).

Or, if is there any other way (if possible from a PR in Bitbucket Cloud) to send an http request with such parameters to Jenkins.

Thanks in advance.


Answers:
author: Theodora Boudale
body: Hi Gin?s and welcome to the community!

I did a search in Atlassian Marketplace with search terms 'pull request' and 'Jenkins'

* [https://marketplace.atlassian.com/search?hosting=cloud\&product=bitbucket\&query=pull%20request](https://marketplace.atlassian.com/search?hosting=cloud&product=bitbucket&query=pull%20request)
* [https://marketplace.atlassian.com/search?hosting=cloud\&product=bitbucket\&query=jenkins](https://marketplace.atlassian.com/search?hosting=cloud&product=bitbucket&query=jenkins)

I don't have in-depth knowledge of every single app, but checking their details, none of them seems to offer the option to manually trigger a Jenkins job.

The [Webhook to Jenkins for Bitbucket](https://marketplace.atlassian.com/apps/1211284/webhook-to-jenkins-for-bitbucket?hosting=cloud&tab=overview) app, as well as the Jenkins plugins for Bitbucket Cloud, seem to be based on webhooks. This means that they trigger a build as soon as an event from the webhook triggers occurs (e.g. pull request created, pull request updated, etc).

Bitbucket's CI/CD tool, Pipelines, can also be used to trigger a job in Jenkins, and **pull-requests** pipelines have PR-related variables. **Pull-requests** pipelines also get triggered when a PR is created or updated. However, if you set this up, you could do the following:

* add the text **\[skip ci\]** in the commit messages of the source branch, so that the **pull-requests** pipeline doesn't get triggered automatically on PR creation or update
* push an empty commit without **\[skip ci\]** to the source branch, whenever you want to trigger the **pull-requests** pipeline, which in turn can trigger a Jenkins job

Another option would be to develop your own app with Forge, which would allow you to add a pull request action:

* <https://developer.atlassian.com/platform/forge/manifest-reference/modules/bitbucket-repository-pull-request-action/>

Kind regards,  
Theodora

comments: author: Gin?s Sanchez
body: Hi [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086), thank you so much fot the update.

We are working to build the plugin (I can see the option we included "Delegate Merge" by selecting the three dots":

![Selection_069.png](https://community.atlassian.com/t5/image/serverpage/image-id/326286i7B76664A50C847BB/image-size/large?v=v2&px=999 "Selection_069.png")

and would like to ask you how we can get the next parameters from the PR context in bitbucket cloud, to be included in the Http Request we send to Jenkins.

The parameters we need are:

PULL_REQUEST_FROM_REPO_PROJECT_KEY  
PULL_REQUEST_FROM_REPO_NAME  
PULL_REQUEST_ID  
BUTTON_FORM_DATA  
PULL_REQUEST_ID  
PULL_REQUEST_FROM_REPO_PROJECT_KEY  
PULL_REQUEST_FROM_REPO_NAME  
PULL_REQUEST_USER_NAME  
PULL_REQUEST_FROM_BRANCH  
PULL_REQUEST_TO_BRANCH  
PULL_REQUEST_TITLE  
PULL_REQUEST_AUTHOR_NAME  
PULL_REQUEST_AUTHOR_EMAIL  
PULL_REQUEST_DESCRIPTION

By getting them we should be able to trigger an HTTP request to Jenkins like:

[https://my-jenkins.com/job/${PULL_REQUEST_FROM_REPO_PROJECT_KEY}/job/${PULL_REQUEST_FROM_REPO_NAME}/job/PR-${PULL_REQUEST_ID}/buildWithParameters?token=\&BUTTON_FORM_DATA=${BUTTON_FORM_DATA}\&PULL_REQUEST_ID=${PULL_REQUEST_ID}\&PULL_REQUEST_FROM_REPO_PROJECT_KEY=${PULL_REQUEST_FROM_REPO_PROJECT_KEY}\&PULL_REQUEST_FROM_REPO_NAME=${PULL_REQUEST_FROM_REPO_NAME}\&PULL_REQUEST_USER_NAME=${PULL_REQUEST_USER_NAME}\&PULL_REQUEST_FROM_BRANCH=${PULL_REQUEST_FROM_BRANCH}\&PULL_REQUEST_TO_BRANCH=${PULL_REQUEST_TO_...](https://my-jenkins.com/job/${PULL_REQUEST_FROM_REPO_PROJECT_KEY}/job/${PULL_REQUEST_FROM_REPO_NAME}/job/PR-${PULL_REQUEST_ID}/buildWithParameters?token=&BUTTON_FORM_DATA=${BUTTON_FORM_DATA}&PULL_REQUEST_ID=${PULL_REQUEST_ID}&PULL_REQUEST_FROM_REPO_PROJECT_KEY=${PULL_REQUEST_FROM_REPO_PROJECT_KEY}&PULL_REQUEST_FROM_REPO_NAME=${PULL_REQUEST_FROM_REPO_NAME}&PULL_REQUEST_USER_NAME=${PULL_REQUEST_USER_NAME}&PULL_REQUEST_FROM_BRANCH=${PULL_REQUEST_FROM_BRANCH}&PULL_REQUEST_TO_BRANCH=${PULL_REQUEST_TO_BRANCH}&PULL_REQUEST_TITLE=${PULL_REQUEST_TITLE}&PULL_REQUEST_AUTHOR_NAME=${PULL_REQUEST_AUTHOR_NAME}&PULL_REQUEST_AUTHOR_EMAIL=${PULL_REQUEST_AUTHOR_EMAIL}&PULL_REQUEST_DESCRIPTION=${PULL_REQUEST_DESCRIPTION)}

Thanks in advance,

Gin?s


author: Theodora Boudale
body: Hi Gin?s,

The pull request ID and the repo UUID are available properties (pullRequest.id and repository.uuid):

* <https://developer.atlassian.com/platform/forge/manifest-reference/modules/bitbucket-repository-pull-request-action/>

I believe you can get most of the other parameters using our API. This API endpoint:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-pull-request-id-get>

can be used to get the PR's source branch, destination branch, author name, PR title, and PR description. The author email is not returned, we do not return users' email addresses in the API. I'm not sure what you are referring to by BUTTON_FORM_DATA and PULL_REQUEST_USER_NAME?

You can use the following API endpoint to get the repo name and project key:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-get>

If you have any other questions specific to app development with Forge, I would recommend posting in the following group (I haven't worked a lot with Forge app development, so the support I can provide is limited):

* <https://community.atlassian.com/t5/Forge-for-Bitbucket-Cloud/gh-p/bitbucket-cloud-forge>

On the following page, you can also find links to the source code of some example apps for Bitbucket:

* <https://developer.atlassian.com/platform/forge/example-apps-bitbucket/>

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud