Skip to content

Instantly share code, notes, and snippets.

@bradymholt
Last active August 29, 2015 14:27
Show Gist options
  • Save bradymholt/b40fa59b6c5a09fbf0d8 to your computer and use it in GitHub Desktop.
Save bradymholt/b40fa59b6c5a09fbf0d8 to your computer and use it in GitHub Desktop.
Create a GitHub Pull Request and include formatted release notes
#!/bin/bash
HEAD_BRANCH=$1
BASE_BRANCH=$2
GH_OAUTH_TOKEN=$3
REPO_NAME="bradyholt/foobar"
RELEASE_NOTES_FILE_NAME="release_notes.md"
echo "Will now attempt to create and close a PR from ${HEAD_BRANCH} to ${BASE_BRANCH} with comment showing merged PRs."
echo "Generating Release Notes..."
git fetch
echo "# Release Notes for ${BASE_BRANCH}" > $RELEASE_NOTES_FILE_NAME
echo "" >> $RELEASE_NOTES_FILE_NAME
# We're going to use git log --date=short --pretty=format:"* [%s;%cd] %b" to print out log in this format:
# * [Merge pull request #2296 from bug_fix;2015-08-11] Make grid hover color persist when hovering
# and then pipe it to sed to get it to look like this:
# * [PR #2296, Merged on 08/11](https://github.com/bradyholt/foobar/pull/2296) - Make grid hover color persist when hovering
git log --merges --grep='Merge pull request #' --date=short --pretty=format:"* [%s;%cd] %b" origin/${BASE_BRANCH}..origin/${HEAD_BRANCH} | sed -E "s/\[.*\#([0-9]*).*\;[0-9]+\-([0-9]+)\-([0-9]+)\]/\[PR \#\1; Merged on \2\/\3]\(https\:\/\/github\.com\/${REPO_NAME/\//\\/}\/pull\/\1\) -/g" >> $RELEASE_NOTES_FILE_NAME
#Escape characters for JSON formatting
RELEASE_NOTES_CONTENT=$(cat $RELEASE_NOTES_FILE_NAME | tr '\n' '^' | sed -E "s/\\\/\\\\\\\\/g" | sed -E "s/\\\"/\\\\\\\"/g" | sed -E "s/'/\\\\'/g" | sed -E "s/\^/\\\\n/g")
echo "Creating Pull Request on ${REPO_NAME}..."
JSON=$(cat <<EOF
{
"title": "Release ${BUILD_NAME} to ${BASE_BRANCH}",
"body": "${RELEASE_NOTES_CONTENT}",
"head": "${HEAD_BRANCH}",
"base": "${BASE_BRANCH}"
}
EOF
)
PR_RESULT=$(curl -i -s -H "Authorization: token $GH_OAUTH_TOKEN" -d "$JSON" https://api.github.com/repos/$REPO_NAME/pulls)
PR_NUMBER=$(echo "${PR_RESULT}" | grep "Location" | sed -E "s/.*\/([0-9]+).*/\1/")
if [ -z "$PR_NUMBER" ]; then
echo "Attempt to create pull request failed: ${PR_RESULT}"
exit 1
fi
echo "Success!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment