I would like to create a pipeline which, on commit, will open a PR with the forked repository
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=-1fi return $result}
My answer below.. I had to specify "repository" in the source object since the PR was coming from a fork apparently:
title=$1srcBranch=$2tgtBranch=$3repo=$4userName=$5password=$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=0if [ "$response" -ne "200" ] && [ "$response" -ne "201" ]; thenecho "ERROR: Status code is $response"result=-1fireturn $result}openPR
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.
I will give it a try this week and report back, thanks again @Jay Seletz
It looks like you're new here. Sign in or register to get started.