Skip to content

Instantly share code, notes, and snippets.

@mynktl
Last active May 13, 2020 10:52
Show Gist options
  • Save mynktl/4fc6ac6d9aa7f1e6b321f9219986a510 to your computer and use it in GitHub Desktop.
Save mynktl/4fc6ac6d9aa7f1e6b321f9219986a510 to your computer and use it in GitHub Desktop.
Script to create a pull request in github
#!/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}
@mynktl
Copy link
Author

mynktl commented May 13, 2020

Before creating a PR, you need to setup few environment variables.
This script expects following environment variable for credentials

To create a PR:

  • Set environment variable
  • Create a branch(ley say, branch-1) on your local workspace
  • Create new commits in branch branch-1.
  • Execute following command to create PR:
./create_pr.sh github-org/repo-name target_branch_for_pr branch-1

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