Last active
August 17, 2019 12:32
-
-
Save cecton/538ce1a6e0b645bd6cbbecf6ccf51366 to your computer and use it in GitHub Desktop.
Script to comment on GitHub from a CircleCI build
This file contains hidden or 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/sh | |
{ # Prevent execution if this script was only partially downloaded | |
# NOTE: This script will use the GitHub Personal token stored in GITHUB_AUTH_TOKEN | |
# to post a comment from CircleCI to your pull request (or commit if there | |
# is no pull request). | |
# | |
# The message must be provided in /tmp/comment | |
# | |
# This is a less sophisticated replacement for: | |
# https://github.com/themadcreator/circle-github-bot | |
set -e | |
# NOTE: replace newlines by \n and " by \" | |
body=$(cat /tmp/comment | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g;s/"/\\"/g') | |
if [ -n "$CIRCLE_PULL_REQUEST" ]; then | |
location=$(echo $CIRCLE_PULL_REQUEST | grep -Po 'github.com/\K[^/]+/[^/]+') | |
curl -s -u ${GITHUB_AUTH_TOKEN:?}:x-oauth-basic --data '{"body":"'"$body"'"}' \ | |
https://api.github.com/repos/$location/issues/${CIRCLE_PULL_REQUEST##*/}/comments | |
else | |
curl -s -u ${GITHUB_AUTH_TOKEN:?}:x-oauth-basic --data '{"body":"'"$body"'"}' \ | |
https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME:?}/${CIRCLE_PROJECT_REPONAME:?}/commits/${CIRCLE_SHA1:?}/comments | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: