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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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
}

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?

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

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

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
Community showcase
Published in Bitbucket Pipelines

What We Learned When We Researched Open Source Vulnerabilities in 7 Popular Coding Languages

...hey are a part of us, shaping how we interact with the world around us. The same holds true for programming languages when we think about how different kinds of vulnerabilities raise their heads in t...

1,328 views 0 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you