Skip to content

Instantly share code, notes, and snippets.

@JoeGannon
Last active December 13, 2017 21:51
Show Gist options
  • Save JoeGannon/f115b290d5e92b1610a4b01edfeed7a9 to your computer and use it in GitHub Desktop.
Save JoeGannon/f115b290d5e92b1610a4b01edfeed7a9 to your computer and use it in GitHub Desktop.
BitBucket PR from CLI
#!/bin/bash
gitDir="$PWD/.git"
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
headCommit=$(git log develop..$branch --reverse --format='%s' | head -n 1)
title=$headCommit
tailCommits=$(git log develop..$branch --reverse --pretty=format:*%s\\n\\n)
desc=$tailCommits
prFile="$gitDir/pull_req_msg.txt"
createPR="1"
if [ "$1" = "n" ]
then
nano "$prFile"
if [ -f "$prFile" ]
then
title=$(head -1 "$prFile")
desc=""
line=1
while read msg
do
#third line
if [ $line -gt 2 ]
then
desc+="$msg""\n\n"
fi
((line++))
done < "$prFile"
else
createPR="0"
fi
fi
remote=$(git config --get remote.origin.url)
remote=$(echo "${remote##*bitbucket.org:}" | sed 's/.\{4\}$//')
payload="{
\"title\": \"$title\",
\"description\": \"$desc\",
\"source\": {
\"branch\": {
\"name\": \"$branch\"
},
\"repository\": {
\"full_name\": \"$remote\"
}
},
\"destination\": {
\"branch\": {
\"name\": \"develop\"
}
},
\"reviewers\": [
{ \"username\": \"UserName\" }
],
\"close_source_branch\": true
}"
echo $payload > payload.json
#cat payload.json
if [ $createPR -eq "1" ]
then
statusCode=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
https://bitbucket.org/api/2.0/repositories/$remote/pullrequests/ \
-H 'authorization: Basic base64(user:pass)' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d @payload.json)
rm payload.json
if [ -f "$prFile" ]
then
rm "$prFile"
fi
echo -e "\n Result: " $statusCode
else
echo -e "\n Canceled"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment