Skip to content

Instantly share code, notes, and snippets.

@maheshgawali
Created August 10, 2018 09:01
Show Gist options
  • Save maheshgawali/30058cd0ad2effadce247a8a1e2a634d to your computer and use it in GitHub Desktop.
Save maheshgawali/30058cd0ad2effadce247a8a1e2a634d to your computer and use it in GitHub Desktop.
simple python script to check if a PR exists for the current branch in bitbucket pipeline, execute bitbucket pipelines only if a PR is created for a branch
#!/usr/bin/env python3
import requests
import json
import argparse
PAGE_LENGTH = '50'
PR_STATE = 'OPEN'
def get_open_pr_branches(current_branch=None,
team=None,
repo=None,
username=None,
app_password=None):
# bitbucket api-endpoint
url = "https://api.bitbucket.org/2.0/repositories/{TEAM}/{REPO}/pullrequests".format(
TEAM=team, REPO=repo)
headers = {'Content-Type': 'application/json'}
branch_names = list()
params = {'pagelen': PAGE_LENGTH, 'state': PR_STATE}
while url:
r = requests.get(
url=url,
params=params,
auth=(username, app_password),
headers=headers)
response_dict = r.json()
url = response_dict.get('next', None)
all_vals = response_dict.get('values', [])
for val in all_vals:
branch_name = val['source']['branch']['name']
# print("PR on source branch: %s" % branch_name)
branch_names.append(branch_name)
if current_branch == branch_name:
break
return branch_names
def main(
current_branch_name,
team,
repo,
username,
app_password,
):
branch_names = get_open_pr_branches(
current_branch_name,
team,
repo,
username,
app_password,
)
if current_branch_name in branch_names:
print("True")
else:
print("False")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='bitbucket pull request checker script')
parser.add_argument(
'current_branch',
help=
'name of your current branch, in pipeline you can get it by using $BITBUCKET_BRANCH'
)
parser.add_argument('team', help='bitbucket team name, normally the owner of the repo')
parser.add_argument('repo', help='bitbucket repo slug')
parser.add_argument('username', help='bitbucket username')
parser.add_argument('app_password', help='bitbucket app password')
args = parser.parse_args()
main(args.current_branch, args.team, args.repo, args.username,
args.app_password)
# in bitbucket pipeline *.yml use as below
# $PR_CHECKER_USERNAME $PR_CHECKER_APP_PASSWORD export these using bitbucket pipeline environment variables
# - IS_PR=$(python bb-pr-checker.py $BITBUCKET_BRANCH $BITBUCKET_REPO_OWNER $BITBUCKET_REPO_SLUG $PR_CHECKER_USERNAME $PR_CHECKER_APP_PASSWORD)
# - if [ $IS_PR != 'True' ]; then echo Skipping tests as branch not a pull request; fi
# - if [ $IS_PR == 'True' ]; then
# - <do steps>
# - <do steps>
# - <do steps>
# - <do steps>
# - fi
@icolwell
Copy link

This is great! Only issue is that opening a PR on a branch doesn't trigger a rebuild. So a freshly opened PR looks like it already passed the tests. Users need to manually rebuild or push to the branch in order to trigger it. Nothing we can do about that though, this is as good as it gets until Bitbucket implements the feature properly.

FYI, to make it slightly simpler, you can do:

 - IS_PR=$(python bb-pr-checker.py $BITBUCKET_BRANCH $BITBUCKET_REPO_OWNER $BITBUCKET_REPO_SLUG $PR_CHECKER_USERNAME $PR_CHECKER_APP_PASSWORD)
 - if [ $IS_PR != 'True' ]; then echo "Skipping tests as branch not a pull request" && exit 0; fi
 - <do steps>

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