How do I create a pull request in bitbucket from my jenkins pipeline

Kylie Collins
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 13, 2023

Hello,

I am new to this community and this is my first post.

I am trying to create a Pull request using Jenkins in bitbucket server repo, even though I have installed the pull request plugin I'm still getting errors.

Can anyone help me with this?

1 answer

1 accepted

1 vote
Answer accepted
Kylie Collins
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 16, 2023

I was finally able to figure this out and was able to create a bitbucket rest api pull request using the python option of the code and run it in your jenkins pipeline as a script. I hope this helps someone

Find more options to pass into your PR and token documentation here

https://developer.atlassian.com/cloud/bitbucket/rest/intro/#pullrequest

https://confluence.atlassian.com/bitbucketserver/http-access-tokens-939515499.html

This is the code I used:

 

import requests
import json

url = "https://{base-url}/projects/my-project/repos/my-repo/pull-requests"

headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer {bitbucket-token}"
}


payload = json.dumps({
"title": "Jenkins Pull Request",
"description": "Updated deployment specification",
"fromRef": {
"id": "refs/heads/source-branch-name",
"repository": "my-repo",
"project": {
"key": "my-project"
}
},
"toRef": {
"id": "refs/heads/base-branch-name",
"repository": "my-repo",
"project": {
"key": "my-project"
}
},
"reviewers": [
{
"user": {
"name": "reviewer-name"
}
}
]
})



response = requests.request(
"POST",
url,
data=payload,
headers=headers
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events