Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to open a new Pull Request in a pipeline?

Phillip Demro August 31, 2020

I would like to create a pipeline which, on commit, will open a PR with the forked repository

2 answers

2 accepted

1 vote
Answer accepted
Jay Seletz
Contributor
September 1, 2020

You can do it via API calls and a shell script.  Here are some bash functions I wrote as part of a script that should get you started, the functions assume $srcBranch, $tgtBranch, $repo, $userName and $password are set:

function getPullRequestPostData() {
cat <<EOF
{
"title": "$title",
"source": {
"branch": {
"name": "$srcBranch"
}
},
"destination": {
"branch": {
"name": "$tgtBranch"
}
}
}
EOF
}

function
openPR() {
echo "Opening PR for repo: $repo"

response=$(curl --write-out %{http_code} --silent --output /dev/null https://api.bitbucket.org/2.0/repositories/YourWorkspace/$repo/pullrequests \
-u $userName:$password \
--request POST \
--header "Content-Type: application/json" \
--data "$(getPullRequestPostData)")

result=0

if [ "$response" -ne "200" ] && [ "$response" -ne "201" ]; then
echo "ERROR: Status code is $response"
result=-1
fi
return $result
}
Phillip Demro September 1, 2020

Thank you @Jay Seletz !   I will give this a try.  Quick question  - is the shell script able to load your $variables directly from the BitBucket repository variables?

Jay Seletz
Contributor
September 1, 2020

I recommend you wrap these functions in your own shell script and pass bitbucket variables into the shell script as command line args.  Then you can also run/test it locally first easier.  

Like Phillip Demro likes this
Phillip Demro September 1, 2020

I will give it a try this week and report back, thanks again @Jay Seletz 

0 votes
Answer accepted
Phillip Demro September 1, 2020

My answer below.. I had to specify "repository" in the source object since the PR was coming from a fork apparently:

 


title=$1
srcBranch=$2
tgtBranch=$3
repo=$4
userName=$5
password=$6

# echo "Title: $title"
# echo "srcBranch: $srcBranch"
# echo "tgtBranch: $tgtBranch"
# echo "repo: $repo"
# echo "userName: $userName"
# echo "password: $password"

function getPullRequestPostData() {
cat <<EOF
{
"title": "Automated Pull Request",
"source": {
"branch": {
"name": "$srcBranch"
},
"repository": {
"full_name":"$userName/$repo"
}
},
"destination": {
"branch": {
"name": "$tgtBranch"
}
}
}
EOF
}


function openPR() {
echo "Opening PR for repo: $repo"

response=$(curl https://api.bitbucket.org/2.0/repositories/[target repo org]/$repo/pullrequests \
-u $userName:$password \
--request POST \
--header 'Content-Type: application/json' \
--data "$(getPullRequestPostData)" \
)

result=0

if [ "$response" -ne "200" ] && [ "$response" -ne "201" ]; then
echo "ERROR: Status code is $response"
result=-1
fi
return $result
}


openPR

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events