Last active
May 13, 2020 10:52
-
-
Save mynktl/4fc6ac6d9aa7f1e6b321f9219986a510 to your computer and use it in GitHub Desktop.
Script to create a pull request in github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ $# -ne 3 ]; then | |
echo "Insufficient arguments" | |
echo "Usage: $0 github-org/repo-name production-branch your-branch" | |
echo "Example: $0 openebs/velero-plugin master fix-123" | |
exit 1 | |
fi | |
if [ -z $USERNAME ] || [ -z $TOKEN ] || [ -z $EMAIL ]; then | |
echo "No crendentials provided for giithub" | |
echo -e "\033[1mThis script expects following environment variable for credentials\033[0m" | |
echo -e "- \033[1mUSERNAME\033[0m" | |
echo -e " This is your github username, configured through \033[3mgit config --global user.name\033[0m" | |
echo " This script doesn't print your USERNAME on output" | |
echo "" | |
echo -e "- \033[1mTOKEN\033[0m" | |
echo " This is github token. This script doesn't print your TOKEN on output." | |
echo " To create a new token," | |
echo " refer https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line" | |
echo "" | |
echo -e "- \033[1mEMAIL\033[0m" | |
echo -e " This is your email id, configured through \033[3mgit config --global user.email\033[0m, to sign the commit" | |
echo " This script doesn't print your EMAIL on output" | |
echo "" | |
exit 1 | |
fi | |
EDITOR=vim | |
BRANCH=$3 | |
GIT_URL="https://api.github.com/repos/${1}/pulls" | |
GIT_REPO=$1 | |
GIT_PRODUCTION_BRANCH=$2 | |
get_input() { | |
local f=$1 | |
$EDITOR $f | |
} | |
get_title() { | |
local -n d=$1 | |
f=$(mktemp) | |
echo "#ADD PR TITLE BELOW" >> ${f} | |
get_input $f | |
sed -i '/#ADD PR TITLE BELOW/d' $f | |
d=$(cat $f) | |
rm $f | |
} | |
get_body() { | |
local -n d=$1 | |
f=$(mktemp) | |
o=$(mktemp) | |
echo "#ADD PR MESSAGE BELOW" >> ${f} | |
get_input $f | |
sed -i '/#ADD PR MESSAGE BELOW/d' $f | |
cat $f | while read line; do echo -n "$line\r\n" >> $o; done | |
d=$(cat $o) | |
rm $f $o | |
} | |
#PR title | |
PR_TITLE="" | |
#PR description | |
PR_MSG="" | |
get_title PR_TITLE | |
get_body PR_MSG | |
HEAD="$USERNAME:$BRANCH" | |
# update_remote adds new remote with username and repo_name | |
update_remote() { | |
echo "Updating remote" | |
git config --global user.name ${USERNAME} | |
git config --global user.email ${EMAIL} | |
local repo_name=$(echo $GIT_REPO | cut -d '/' -f2) | |
git remote add upstream https://${USERNAME}:${TOKEN}@github.com/$USERNAME/$repo_name.git > /dev/null 2>&1 | |
rc=$? | |
if [[ $rc -eq 0 ]] || [[ $rc -eq 128 ]]; then | |
return 0 | |
fi | |
return $rc | |
} | |
#push_branch push upstream to github | |
push_branch() { | |
echo "Pushing commit to github" | |
git push upstream $BRANCH | |
} | |
update_remote && push_branch | |
rc=0 | |
if [ $rc -ne 0 ]; then | |
echo "Branch related operation failed" | |
exit 1 | |
fi | |
# file to store curl output | |
RESP_FILE=$(mktemp) | |
EXIT_CODE=0 | |
PR_CREATE_DATA='{ | |
"title": "'${PR_TITLE}'", | |
"body": "'${PR_MSG}'", | |
"head": "'${HEAD}'", | |
"base": "'${GIT_PRODUCTION_BRANCH}'" | |
}' | |
res=$(curl -u ${USERNAME}:${TOKEN} --silent \ | |
-w "%{http_code}" \ | |
--output ${RESP_FILE} \ | |
--url ${GIT_URL} \ | |
--request POST \ | |
--header 'content-type: application/vnd.github.VERSION.raw+json'\ | |
--data "$PR_CREATE_DATA" | |
) | |
# if response value is 201 then PR is created successfully | |
# Refer https://developer.github.com/v3/pulls/#response-2 | |
if [ $res != "201" ]; then | |
echo "Error: Unable to create a PR for changelog.. REST API output is as below" | |
cat ${RESP_FILE} | |
EXIT_CODE=1 | |
else | |
echo "Successfully raised a PR for changelog" | |
fi | |
rm -rf ${RESP_FILE} | |
exit ${EXIT_CODE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before creating a PR, you need to setup few environment variables.
This script expects following environment variable for credentials
USERNAME
This is your github username, configured through git config --global user.name
This script doesn't print your USERNAME on output
TOKEN
This is github token. This script doesn't print your TOKEN on output.
To create a new token,
refer https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
EMAIL
This is your email id, configured through git config --global user.email, to sign the commit
This script doesn't print your EMAIL on output
To create a PR: