Skip to content

Instantly share code, notes, and snippets.

@wirths
Created December 5, 2019 16:56
Show Gist options
  • Save wirths/2a3952ec681bee95e551c4e40025793b to your computer and use it in GitHub Desktop.
Save wirths/2a3952ec681bee95e551c4e40025793b to your computer and use it in GitHub Desktop.
Bitbucket Pipelines - Create Pull Request (PR) with default reviewers
image: node:12
definitions:
steps:
- step: &create-pr
name: Create Pull Request
caches:
- node
script:
- apt-get update
- apt-get -y install curl jq
- '[[ "$BITBUCKET_BRANCH" == feature/* || $BITBUCKET_BRANCH == bugfix/* ]] && export DESTINATION_BRANCH="develop" || export DESTINATION_BRANCH="master"'
- '[[ "$BITBUCKET_BRANCH" == "develop" ]] && export CLOSE_ME=false || export CLOSE_ME=true'
- >
export BB_TOKEN=$(curl -s -S -f -X POST -u "${BB_AUTH_STRING}" \
https://bitbucket.org/site/oauth2/access_token \
-d grant_type=client_credentials -d scopes="repository" | jq --raw-output '.access_token')
- >
export DEFAULT_REVIEWERS=$(curl https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/default-reviewers \
-s -S -f -X GET \
-H "Authorization: Bearer ${BB_TOKEN}" | jq '.values' | jq 'map({uuid})' )
- >
curl https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/pullrequests \
-s -S -f -X POST \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${BB_TOKEN}" \
-d '{
"title": "Merge in '"${BITBUCKET_BRANCH}"'",
"description": "Automated PR creation process :-)",
"source": {
"branch": {
"name": "'"${BITBUCKET_BRANCH}"'"
}
},
"destination": {
"branch": {
"name": "'"${DESTINATION_BRANCH}"'"
}
},
"close_source_branch": '"${CLOSE_ME}"',
"reviewers": '"${DEFAULT_REVIEWERS}"'
}'
pipelines:
branches:
'{bugfix/*,feature/*}':
- step: *create-pr
'{develop,release/*,hotfix/*}':
- step: *create-pr
@USSliberty
Copy link

hello @wirths, just a question: What it should be BB_AUTH_STRING? I tried to add an "App Password", also with all permission, but it gives always "curl: (22) The requested URL returned error: 400" on every request.

@wirths
Copy link
Author

wirths commented Nov 13, 2020

@USS

hello @wirths, just a question: What it should be BB_AUTH_STRING? I tried to add an "App Password", also with all permission, but it gives always "curl: (22) The requested URL returned error: 400" on every request.

This article describes it quite well (step 2). It just a combination of username and password:
https://support.atlassian.com/bitbucket-cloud/docs/publish-and-link-your-build-artifacts/

@artitambe26
Copy link

What is bit bucket repo slug exactly?

@nfb-transact
Copy link

@artitambe26

What is bit bucket repo slug exactly?

Just passing through, but the official documentation for automatic environment variables like BITBUCKET_REPO_SLUG is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment