This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
github_comment.sh
executable file
·100 lines (83 loc) · 2.44 KB
/
github_comment.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
set -e
# The directory this script is in.
REAL_PATH=`readlink -f "${BASH_SOURCE[0]}"`
SCRIPT_DIR=`dirname "$REAL_PATH"`
usage() {
cat << EOF
usage: github_comment.sh -a <[github_account]/[github_project]> -i <issue_number> -b <body> <<< "<github_token>"
OPTIONS
<[github_account]/[github_project]>
-a
This is the project owner account and project, as you might see in the github
URL. For instance, if you were going to post a comment on
https://github.com/q0rban/foo, you would set -a to q0rban/foo.
-i
The github issue number.
-b
The body of the comment.
ARGUMENTS
<github_token>
The Github authentication token. Pass this to stdin so that the value cannot
be seen in ps. If using this within Jenkins, it is highly recommended to use
the 'Inject passwords to the build environment as environment variables'
option that comes with the Environment Injector Plugin.
EOF
}
# Parse options.
WEBROOT=$WORKSPACE
DRUSH="drush"
VERBOSE=""
while getopts "ha:i:b:" OPTION; do
case $OPTION in
h)
usage
exit 1
;;
a)
ACCOUNT_PROJECT=$OPTARG
;;
i)
ISSUE_NUMBER=$OPTARG
;;
b)
BODY=$OPTARG
;;
?)
usage
exit
;;
esac
done
# Remove the switches we parsed above from the arguments.
shift `expr $OPTIND - 1`
# Grab the token from STDIN.
read TOKEN
# If we're missing some of these variables, show the usage and throw an error.
if [[ -z $TOKEN ]] || [[ -z $ACCOUNT_PROJECT ]] || [[ -z $ISSUE_NUMBER ]] || [[ -z $BODY ]]; then
usage
exit 1
fi
# Ensure curl exists.
command -v curl >/dev/null 2>&1 || {
echo >&2 "You must have cURL installed for this command to work properly.";
exit 1;
}
URL="https://api.github.com/repos/$ACCOUNT_PROJECT/issues/$ISSUE_NUMBER/comments"
PUBLIC_URL="http://github.com/$ACCOUNT_PROJECT/issues/$ISSUE_NUMBER"
# Escape all single quotes.
BODY=${BODY//\'/\\\'}
# Encode to json. PHP makes this pretty easy. Maybe there's something better?
DATA=`php -r "print json_encode(array('body' => '$BODY'));"`
# Now make the actual call to github.
OUTPUT=`curl -H "Authorization: token $TOKEN" -d "$DATA" $URL`
# Escape all single quotes again.
OUTPUT=${OUTPUT//\'/\\\'}
# Check for errors
ERROR=`php -r "\\$json = json_decode('$OUTPUT'); isset(\\$json->message) ? print \\$json->message : NULL;"`
if [[ -z $ERROR ]]; then
echo "Comment posted successfully to $PUBLIC_URL."
exit 0
fi
echo "Failed to post comment. Reason: $ERROR."
exit 1