From fe4e4f62b6e13e62d09f7ed7f109f1042dd61ec8 Mon Sep 17 00:00:00 2001 From: Atsushi Watanabe Date: Mon, 7 Aug 2017 13:23:13 +0900 Subject: [PATCH] Initial drop. --- README.md | 16 ++++++++++++++++ gh-pr-comment.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 README.md create mode 100755 gh-pr-comment.sh diff --git a/README.md b/README.md new file mode 100644 index 00000000..b20e1654 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# gh-pr-comment + +## Usage + +``` +$ gh-pr-comment title comment +``` + +The script escapes special charactors for json encoding. + +## Required environment variables + +- ***TRAVIS\_REPO\_SLUG*** = user/repos +- ***TRAVIS\_PULL\_REQUEST*** = pull request number +- ***TRAVIS\_BOT\_GITHUB\_TOKEN*** = token with comment write permission + diff --git a/gh-pr-comment.sh b/gh-pr-comment.sh new file mode 100755 index 00000000..42eae521 --- /dev/null +++ b/gh-pr-comment.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +function gh-pr-comment() +{ + if [ $# -lt 2 ]; + then + echo "usage: gh-pr-comment title comment" >&2 + echo " env:" >&2 + echo " - TRAVIS_REPO_SLUG : user/repos" >&2 + echo " - TRAVIS_PULL_REQUEST : pull request number" >&2 + echo " - TRAVIS_BOT_GITHUB_TOKEN : token with comment write permission" >&2 + echo ' note: the script escapes special charactors for json encoding.' >&2 + + return + fi + + if [ ${TRAVIS_PULL_REQUEST} != "false" ]; + then + text=`echo "$2" | sed 's/\\\\/\\\\\\\\/g' | sed -n '1h;1!H;${x;s/\n/\\\\n/g;p;}' | sed 's/\"/\\\\"/g' | sed 's/\t/\\\\t/g' | sed 's/\//\\\\\//g'` + curl -vs -X POST -H 'Content-Type:application/json' -d "{\"body\":\"## $1\n\n$text\"}" \ + https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments?access_token=${TRAVIS_BOT_GITHUB_TOKEN} 2> /dev/null + else + echo 'gh-pr-comment: TRAVIS_PULL_REQUEST is false.' >&2 + fi +} +