Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Trigger HTTP requests on pull-request events from Bitbucket

Ginés Sanchez April 30, 2024

Hello, 

we are using Bitbucket Server and as part of our build pipeline we have been using the 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

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, 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.

1 answer

2 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 1, 2024

Hi Ginés and welcome to the community!

I did a search in Atlassian Marketplace with search terms 'pull request' and '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 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:

Kind regards,
Theodora

Ginés Sanchez May 28, 2024

 

Hi @Theodora Boudale, 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

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_...}

 

Thanks in advance,

 

Ginés

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 28, 2024

Hi Ginés,

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

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

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:

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):

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

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events