-
Notifications
You must be signed in to change notification settings - Fork 6
/
github.sh
140 lines (125 loc) · 4.08 KB
/
github.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Determine GitHub-related capabilities
#OAUTH_FILE from local.sh
OAUTH_TOKEN=
POST_COMMENT=true
EDIT_COMMENT=true
# repository with the working branch and pull requests
REPOSITORY=cms-sw
# OAuth token to post comments on GitHub
if [ "$OAUTH_FILE" ] && [ -f "$OAUTH_FILE" ]; then
OAUTH_TOKEN=$(< "$OAUTH_FILE")
else
echo "OAUTH token not found." 1>&2
POST_COMMENT=false
fi
# Tools to post comments on GitHub
if ! which "curl" > /dev/null; then
echo "Command \"curl\" not found." 1>&2
POST_COMMENT=false
fi
if ! which "jq" > /dev/null; then
echo "Command \"jq\" not found." 1>&2
EDIT_COMMENT=false
fi
[ $POST_COMMENT ] || echo "The report will not be posted to GitHub." 1>&2
[ $POST_COMMENT ] && [ $EDIT_COMMENT ] || "Only the final report will be posted to GitHub." 1>&2
function can_post_comment() {
local ISSUE_NUMBER=$1
$POST_COMMENT && [ "$ISSUE_NUMBER" ]
}
function can_update_comment() {
local ISSUE_NUMBER="$1"
local COMMENT_ID="$2"
can_post_comment $ISSUE_NUMBER && $EDIT_COMMENT && [ "$COMMENT_ID" ]
}
# Post a comment to a GitHub issue
#
# Usage:
# post_comment ISSUE BODY
#
# Any format accepted by curl is valid for BODY. For example,
# the format "@FILE" will use the content of FILE.
#
# Returns the comment id for further editing
#
function post_comment() {
local ISSUE_NUMBER=$1
can_post_comment $ISSUE_NUMBER || return
local BODY="$2" # body of the message, wrapped in JSON format (see https://developer.github.com/v3/issues/comments/ )
local RESPONSE
if ! RESPONSE=$(curl -s -S -H "Authorization: token $OAUTH_TOKEN" -X "POST" "https://api.github.com/repos/cms-patatrack/cmssw/issues/$ISSUE_NUMBER/comments" -d "$BODY"); then
# failed; disable further posting
POST_COMMENT=false
EDIT_COMMENT=false
elif $EDIT_COMMENT; then
# success; return the comment id for further editing
echo "$RESPONSE" | jq ".id"
fi
}
# Update a comment on a GitHub issue
#
# Usage:
# update_comment ISSUE COMMENT BODY
#
# Any format accepted by curl is valid for BODY. For example,
# the format "@FILE" will use the content of FILE.
#
function update_comment() {
local ISSUE_NUMBER=$1
local COMMENT_ID="$2"
can_update_comment $ISSUE_NUMBER $COMMENT_ID || return
local BODY="$3" # body of the message, wrapped in JSON format (see https://developer.github.com/v3/issues/comments/ )
local RESPONSE
if ! RESPONSE=$(curl -s -S -H "Authorization: token $OAUTH_TOKEN" -X "PATCH" "https://api.github.com/repos/cms-patatrack/cmssw/issues/comments/$COMMENT_ID" -d "$BODY"); then
# failed; disable further posting
POST_COMMENT=false
EDIT_COMMENT=false
fi
}
function upload_final_report() {
local ISSUE_NUMBER=$1
local COMMENT_ID="$2"
local JSON=$(mktemp -p $BASE report.XXXXXXXXXX.json)
{
echo -e -n '{\n "body": "'
cat $BASE/report.md | tr '\n' '\r' | sed -e's/\r/\\r\\n/g' -e's/"/\\"/g'
echo -e '"\n}'
} > $JSON
if can_update_comment $ISSUE_NUMBER $COMMENT_ID; then
update_comment $ISSUE_NUMBER $COMMENT_ID "@$JSON"
elif can_post_comment $ISSUE_NUMBER; then
post_comment $ISSUE_NUMBER "@$JSON"
fi
rm -f $JSON
}
function upload_report() {
local ISSUE_NUMBER=$1
local COMMENT_ID="$2"
local JSON=$(mktemp -p $BASE report.XXXXXXXXXX.json)
{
echo -e -n '{\n "body": "'
cat $BASE/report.md | tr '\n' '\r' | sed -e's/\r/\\r\\n/g' -e's/"/\\"/g'
echo -e "\\\r\\\n\\\r\\\n:construction: Validation running at $(hostname):$BASE ...\"\n}"
} > $JSON
if can_update_comment $ISSUE_NUMBER $COMMENT_ID; then
update_comment $ISSUE_NUMBER $COMMENT_ID "@$JSON"
elif can_post_comment $ISSUE_NUMBER; then
post_comment $ISSUE_NUMBER "@$JSON"
fi
rm -f $JSON
}
function is_PR() {
local PR="$1"
# check for a numerical (decimal) value
[ "$(echo "$PR" | sed -e's/^[1-9][0-9]*//')" ] && return 1
# check for a valid issue which is also a pull request
curl -s -S -H "Authorization: token $OAUTH_TOKEN" -X "GET" "https://api.github.com/repos/cms-patatrack/cmssw/issues/$PR" | grep -q "pull_request"
}
function filter_PRs() {
local PR=
for PR in "$@"; do
if is_PR $PR; then
echo $PR
fi
done
}