-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpr-comment.sh
More file actions
executable file
·332 lines (267 loc) · 9.54 KB
/
Copy pathpr-comment.sh
File metadata and controls
executable file
·332 lines (267 loc) · 9.54 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env bash
# Maintains a single "sticky" axe DevHub comment on the pull request for this run.
#
# MODE=upsert create the comment, or update it in place if it already exists
# MODE=hide collapse the existing comment as OUTDATED
#
# Everything derivable from the workflow context is passed in by action.yml
# rather than looked up here -- PR_NUMBER is github.event.pull_request.number.
#
# Commenting is best effort: every failure warns and exits 0. This also runs on
# the success path, where a non-zero exit would turn a passing run red.
set -uo pipefail
# A data format, not a label. Comments already sitting in consumers' open pull
# requests carry these exact bytes, and this is how we find them again. The
# missing space after "Comment" is deliberate. Changing any of it orphans them all.
readonly MARKER='<!-- Sticky Pull Request Commentaxe-devhub -->'
readonly CLASSIFIER='OUTDATED'
# Workflow commands are terminated by a newline and use % as an escape
# character, so API-supplied text has to be encoded or it truncates the
# annotation -- or forges another command.
escape_data() {
local data="$*"
data="${data//%/%25}"
data="${data//$'\r'/%0D}"
data="${data//$'\n'/%0A}"
printf '%s' "$data"
}
warn() {
printf '::warning::%s\n' "$(escape_data "$*")"
}
notice() {
printf '::notice::%s\n' "$(escape_data "$*")"
}
TmpDir=""
# shellcheck disable=SC2329 # invoked by the EXIT trap below
cleanup() {
[ -n "$TmpDir" ] && rm -rf "$TmpDir"
}
trap cleanup EXIT
TmpDir=$(mktemp -d) || {
warn "Could not create a temporary directory; skipping the pull request comment."
exit 0
}
readonly ResponseBody="$TmpDir/response.json"
readonly RequestBody="$TmpDir/request.json"
# Writes the response body to $ResponseBody and prints the status code.
http() {
local method="$1" url="$2" data_file="${3:-}" code
local -a args=(
--silent
--show-error
--request "$method"
--header "Authorization: Bearer $GITHUB_TOKEN"
--header "Accept: application/vnd.github+json"
--header "X-GitHub-Api-Version: 2022-11-28"
--output "$ResponseBody"
--write-out '%{http_code}'
)
if [ -n "$data_file" ]; then
args+=(--header "Content-Type: application/json" --data "@$data_file")
fi
# curl prints 000 itself when it cannot reach the host
code=$(curl "${args[@]}" --url "$url" 2>/dev/null)
echo "${code:-000}"
}
report_http_failure() {
local what="$1" status="$2" message
message=$(jq -r '.message // empty' <"$ResponseBody" 2>/dev/null)
if [ "$status" = "403" ] || [ "$status" = "404" ]; then
warn "$what failed (HTTP $status${message:+: $message}). The GITHUB_TOKEN is read-only on pull requests from forks, and needs 'pull-requests: write' otherwise."
else
warn "$what failed (HTTP $status${message:+: $message})."
fi
}
# GraphQL reports errors with HTTP 200 and an `errors` array, so the status
# code alone cannot tell success from failure.
graphql() {
local payload="$1" what="$2" status
status=$(http POST "$GITHUB_GRAPHQL_URL" "$payload")
if [ "$status" != "200" ]; then
report_http_failure "$what" "$status"
return 1
fi
if jq -e 'has("errors")' <"$ResponseBody" >/dev/null 2>&1; then
warn "$what failed: $(jq -c '.errors' <"$ResponseBody")"
return 1
fi
return 0
}
PrNumber="${PR_NUMBER:-}"
is_count() {
case "${1:-}" in
"" | *[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
is_pr_number() {
is_count "${1:-}" && [ "$1" -gt 0 ]
}
PreviousCommentId=""
# Takes the first comment we wrote that is not collapsed and carries the marker.
# Skipping collapsed ones is what makes a failure after a clean run post a fresh,
# visible comment. REST has no dependable minimized flag, hence GraphQL.
find_previous_comment() {
local after="null" match has_next
# shellcheck disable=SC2016 # $owner and friends are GraphQL variables, not shell ones
local query='query($owner: String!, $repo: String!, $number: Int!, $after: String) {
viewer { login }
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
comments(first: 100, after: $after) {
nodes {
id
isMinimized
body
author { login }
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}'
while :; do
jq -n \
--arg query "$query" \
--arg owner "${GITHUB_REPOSITORY%%/*}" \
--arg repo "${GITHUB_REPOSITORY#*/}" \
--argjson number "$PrNumber" \
--argjson after "$after" \
'{query: $query, variables: {owner: $owner, repo: $repo, number: $number, after: $after}}' \
>"$RequestBody" || return 1
graphql "$RequestBody" "Reading the comments on pull request #$PrNumber" || return 1
# A bot's GraphQL author login omits the "[bot]" suffix that viewer carries
match=$(jq -r --arg marker "$MARKER" '
(.data.viewer.login // "" | sub("\\[bot\\]"; "")) as $me
| (.data.repository.pullRequest.comments.nodes // [])
| [ .[] | select(
.author.login == $me
and (.isMinimized | not)
and (.body | contains($marker))
) ]
| .[0].id // empty
' <"$ResponseBody" 2>/dev/null)
if [ -n "$match" ]; then
PreviousCommentId="$match"
return 0
fi
has_next=$(jq -r '.data.repository.pullRequest.comments.pageInfo.hasNextPage // false' <"$ResponseBody" 2>/dev/null)
[ "$has_next" = "true" ] || return 0
after=$(jq -c '.data.repository.pullRequest.comments.pageInfo.endCursor' <"$ResponseBody" 2>/dev/null)
[ -n "$after" ] && [ "$after" != "null" ] || return 0
done
}
build_message() {
local message
message="axe DevHub found **${ISSUE_COUNT:-}** accessibility violations in this PR."
if [ "${ENABLE_A11Y_THRESHOLD:-}" = "true" ]; then
message="$message
axe DevHub found **${ISSUES_OVER_A11Y_THRESHOLD:-}** accessibility violations over your a11y threshold in this PR."
fi
message="$message
See the full report on [axe DevHub](${AXE_URL:-})."
printf '%s' "$message"
}
# Trailing whitespace is trimmed so bodies stay byte-identical to the comments
# already in consumers' pull requests.
build_body() {
local message="$1"
message="${message%"${message##*[![:space:]]}"}"
printf '%s\n%s' "$message" "$MARKER"
}
create_comment() {
local body="$1" status
jq -n --arg body "$body" '{body: $body}' >"$RequestBody" || return 1
status=$(http POST "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PrNumber/comments" "$RequestBody")
if [ "$status" != "201" ]; then
report_http_failure "Commenting on pull request #$PrNumber" "$status"
return 1
fi
notice "Added the axe DevHub comment to pull request #$PrNumber."
return 0
}
update_comment() {
local body="$1"
# shellcheck disable=SC2016 # $id and $body are GraphQL variables, not shell ones
local mutation='mutation($id: ID!, $body: String!) {
updateIssueComment(input: {id: $id, body: $body}) {
issueComment { id }
}
}'
jq -n --arg query "$mutation" --arg id "$PreviousCommentId" --arg body "$body" \
'{query: $query, variables: {id: $id, body: $body}}' >"$RequestBody" || return 1
graphql "$RequestBody" "Updating the axe DevHub comment on pull request #$PrNumber" || return 1
notice "Updated the axe DevHub comment on pull request #$PrNumber."
return 0
}
minimize_comment() {
# shellcheck disable=SC2016 # $id and $classifier are GraphQL variables, not shell ones
local mutation='mutation($id: ID!, $classifier: ReportedContentClassifiers!) {
minimizeComment(input: {subjectId: $id, classifier: $classifier}) {
clientMutationId
}
}'
jq -n --arg query "$mutation" --arg id "$PreviousCommentId" --arg classifier "$CLASSIFIER" \
'{query: $query, variables: {id: $id, classifier: $classifier}}' >"$RequestBody" || return 1
graphql "$RequestBody" "Hiding the axe DevHub comment on pull request #$PrNumber" || return 1
notice "Hid the outdated axe DevHub comment on pull request #$PrNumber."
return 0
}
do_upsert() {
local body
# main.sh writes no outputs when the axe request itself fails, which would
# otherwise produce a comment reading "found **** violations"
if ! is_count "${ISSUE_COUNT:-}"; then
warn "No violation count is available, so no comment was added to pull request #$PrNumber."
return 0
fi
body=$(build_body "$(build_message)")
find_previous_comment || return 0
if [ -n "$PreviousCommentId" ]; then
update_comment "$body" || return 0
else
create_comment "$body" || return 0
fi
}
do_hide() {
find_previous_comment || return 0
if [ -z "$PreviousCommentId" ]; then
notice "No axe DevHub comment to hide on pull request #$PrNumber."
return 0
fi
minimize_comment || return 0
}
main() {
local mode="${MODE:-}" var
if [ "$mode" != "upsert" ] && [ "$mode" != "hide" ]; then
warn "MODE must be 'upsert' or 'hide', got '${mode}'; skipping the pull request comment."
return 0
fi
for var in GITHUB_TOKEN GITHUB_API_URL GITHUB_GRAPHQL_URL GITHUB_REPOSITORY; do
if [ -z "${!var:-}" ]; then
warn "$var is not set; skipping the pull request comment."
return 0
fi
done
if ! command -v jq >/dev/null 2>&1; then
warn "jq is not installed on this runner; skipping the pull request comment."
return 0
fi
# PR_NUMBER comes from github.event.pull_request.number, so it is empty on
# any event that is not a pull request.
if ! is_pr_number "$PrNumber"; then
notice "This run is not associated with a pull request; skipping the comment."
return 0
fi
echo "Using pull request #$PrNumber."
if [ "$mode" = "upsert" ]; then
do_upsert
else
do_hide
fi
}
main
exit 0