Skip to content

Instantly share code, notes, and snippets.

@vjayajv
Created January 19, 2022 06:43
Show Gist options
  • Save vjayajv/080b02a17a59c0ae0a6ce0ee17db280f to your computer and use it in GitHub Desktop.
Save vjayajv/080b02a17a59c0ae0a6ce0ee17db280f to your computer and use it in GitHub Desktop.
A shell-script to create and merge pull-requests in bit-bucket.
#!/bin/bash
function usage
{
echo "This script is used to create a PR for a specified branch"
echo "usage: ./pull-requests.sh [-h --help] | [-u --username] USERNAME | [-p --password] PASSWORD | [-w --workspace] WORKSPACE | [-r --repo] | [-s --source] SOURCE_BRANCH [-d --destination] DESTINATION_BRANCH [-a --action] ACTION_TYPE | [--desc] DESCRIPTION"
echo "-a --action Allowed values : create (create), merge(create and merge) , default is create"
echo "Example: ./pull-request.sh -r myrepo -s develop -d master"
}
desc=""
while [ "$1" != "" ]; do
case $1 in
-u | --username )
shift
username=$1
;;
-p | --password )
shift
password=$1
;;
-w | --workspace )
shift
workspace=$1
;;
-s | --source )
shift
src_branch=$1
;;
-r | --repo )
shift
repo=$1
;;
-d | --destination )
shift
dst_branch=$1
;;
-a | --action )
shift
action=$1
;;
--desc )
shift
desc="${desc}\n * $1"
;;
-h | --help )
usage
exit
;;
* )
usage
exit 1
esac
shift
done
if [[ -n "$src_branch" ]] && [[ -n "$dst_branch" ]] && [[ -n "$repo" ]] && [[ -n "$username" ]] && [[ -n "$password" ]] && [[ -n "$workspace" ]]; then
if [[ -z "$action" ]]; then
action="create"
elif [ "$action" = "create" ] || [ "$action" = "merge" ]; then
echo ""
else
echo "Invalid action type $action"
usage
exit 1
fi
title="$repo : Merging $src_branch into $dst_branch (for $(git config --get user.name))"
echo "$title"
echo
commits=$(curl -s -u $username:$password https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/commits/\?include="$src_branch"\&exclude="$dst_branch" | jq -r '.values')
if [ "$commits" == "[]" ]; then
echo "No commits in source for ${repo}! Skipping creation of PR!"
else
echo "Found commits for ${repo}, creating PR!"
default_reviewers=$(curl -s -u $username:$password https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/default-reviewers | jq -rc '.values' | jq '. | map({"uuid": .uuid})')
pr_id=$(curl -s https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/pullrequests?fields=id -u $username:$password --request POST --header 'Content-Type: application/json' --data '{ "title": "'"$title"'", "description": "'"$desc"'", "destination": { "branch": { "name": "'"$dst_branch"'" } }, "source": { "branch": { "name": "'"$src_branch"'" } }, "reviewers": '"$default_reviewers"' }' | jq -r '.id')
pr_url="https://bitbucket.org/${workspace}/${repo}/pull-requests/${pr_id}"
if [ "$action" = "merge" ]; then
echo "Merging PR: ${pr_id} ..."
merge_status=$(curl -s https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/pullrequests/${pr_id}/merge -u $username:$password --request POST | jq -r '.state')
if [ "$merge_status" = "null" ]; then
merge_status="Merge failed, check for conflicts"
echo "${merge_status}" 1>&2
fi
echo "Status: ${merge_status}"
fi
echo "${repo} : $title"
printf "$desc"
echo "pull-request: $pr_url"
fi
echo
else
echo "Missing args!"
usage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment