Repository navigation
355 lines (330 loc) · 20.3 KB
/
Copy pathpr-comment.yml
File metadata and controls
355 lines (330 loc) · 20.3 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Description: This workflow is triggered when the "Check" workflow completes.
# Since this pull request has write permissions on the target repo, we should **NOT** execute any untrusted code.
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
# Based on https://github.com/spring-projects/spring-security/pull/15477/files
name: Comment on PR
on:
workflow_run:
# Review activity (Qodo submitting a review, the PR author replying in a thread) is relevant
# too, but its pull_request_review* runs are unprivileged on fork PRs - "PR Review Activity"
# bridges them into this list.
# note when updating via a PR and testing - `workflow_run` executes from the `main` branch and not the PR branch
workflows: [ "Source Code Tests", "On PR opened/updated", "Check PR Format", "Link PR to Issue", "Check PR Modifications", "Check PR CHANGELOG.md", "PR Review Activity" ]
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-a-workflow-based-on-the-conclusion-of-another-workflow
types: [ completed ]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number'
required: true
workflow_run_id:
description: 'Workflow run id'
required: true
head_sha:
description: 'PR head SHA the dispatching run evaluated; the run does nothing if the PR moved on or conflicts again'
required: false
jobs:
comment:
if: ${{ github.repository == 'JabRef/jabref' }}
# The label decision reads live review-thread state and writes it back minutes later, so two
# runs for the same PR would race and the slower one would restore the state it started from.
# Same group as "Auto-remove ready-for-review", so a draft conversion cannot interleave with the
# label evaluation below (which re-reads isDraft right before writing the label).
# The PR number is only known after the artifact is read, too late for a group expression -
# head repository plus head branch identifies the same pull request just as well, for fork PRs too.
# Exception: "PR Review Activity" runs triggered by issue_comment run on the default branch, so
# head_branch is "main" for every PR - one group for all PRs, which cancelled the pending
# evaluations of other PRs. Their run title carries the PR number instead.
concurrency:
group: pr-status-${{ github.event.workflow_run.head_repository.full_name || github.repository }}-${{ (github.event.workflow_run.event == 'issue_comment' && github.event.workflow_run.display_title) || github.event.workflow_run.head_branch || github.event.inputs.pr_number }}
cancel-in-progress: false
runs-on: ubuntu-slim
permissions:
actions: read
checks: read
contents: read
issues: write
pull-requests: write
statuses: read
timeout-minutes: 10
steps:
- name: Show GitHub context
env:
PR_NUMBER: ${{ github.event.inputs.pr_number }}
WORKFLOW_RUN_ID: ${{ github.event.inputs.workflow_run_id }}
EVENT_NAME: ${{ github.event_name }}
WORKFLOW: ${{ github.event.workflow_run.name }}
RUN_ID: ${{ github.run_id }}
RUN_NUMBER: ${{ github.run_number }}
run: |
echo "PR_NUMBER=${PR_NUMBER}"
echo "WORKFLOW_RUN_ID=${WORKFLOW_RUN_ID}"
echo "EVENT_NAME=${EVENT_NAME}"
echo "WORKFLOW=${WORKFLOW}"
echo "RUN_ID=${RUN_ID}"
echo "RUN_NUMBER=${RUN_NUMBER}"
echo "Workflow $WORKFLOW" >> $GITHUB_STEP_SUMMARY
- name: Download PR number
if: ${{ github.event_name == 'workflow_run' }}
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8
continue-on-error: true
with:
name: pr_number
path: ${{ runner.temp }}/pr-artifacts
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Read pr_number.txt
if: ${{ github.event_name == 'workflow_run' }}
id: read-pr_number
run: |
ARTIFACT_FILE="${{ runner.temp }}/pr-artifacts/pr_number.txt"
if [ ! -f "$ARTIFACT_FILE" ]; then
echo "No pr_number.txt found in downloaded artifact"
echo "pr_number=" >> "$GITHUB_OUTPUT"
exit 0
fi
if ! IFS= read -r PR_NUMBER < "$ARTIFACT_FILE"; then
echo "Invalid PR number in artifact"
echo "pr_number=" >> "$GITHUB_OUTPUT"
exit 0
fi
PR_NUMBER="${PR_NUMBER%$'\r'}"
if sed -n '2,$p' "$ARTIFACT_FILE" | grep -q '^'; then
echo "Invalid PR number in artifact"
echo "pr_number=" >> "$GITHUB_OUTPUT"
exit 0
fi
if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
echo "Invalid PR number in artifact"
echo "pr_number=" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Read PR number $PR_NUMBER"
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
echo "PR #$PR_NUMBER" >> "$GITHUB_STEP_SUMMARY"
- name: Checkout
# required for gh tool and .github/ghprcomment.yml
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
show-progress: 'false'
token: ${{ secrets.GITHUB_TOKEN }}
- uses: awalsh128/cache-apt-pkgs-action@bb2fa420ce4bd7a598b0e3bbd1aad278a0af5544 # v2.0.2
with:
packages: jq
version: 1.0
- name: Is PR from forked?
if: ${{ github.event_name == 'workflow_run' && steps.read-pr_number.outputs.pr_number != '' }}
id: isCrossRepository
run: |
isCrossRepository=$(gh pr view $pr_number --json isCrossRepository --jq '.isCrossRepository')
echo "Got isCrossRepository $isCrossRepository"
echo isCrossRepository=$isCrossRepository >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ github.token }}
pr_number: ${{ steps.read-pr_number.outputs.pr_number }}
# Set a common variable for PR number and workflow_run_id from either event.
- name: Set variables
if: ${{ github.event_name == 'workflow_dispatch' || steps.read-pr_number.outputs.pr_number != '' }}
id: set-vars
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "pr_number=${{ github.event.inputs.pr_number }}" >> $GITHUB_OUTPUT
echo "workflow_run_id=${{ github.event.inputs.workflow_run_id }}" >> $GITHUB_OUTPUT
else
echo "pr_number=${{ steps.read-pr_number.outputs.pr_number }}" >> $GITHUB_OUTPUT
echo "workflow_run_id=${{ github.event.workflow_run.id }}" >> $GITHUB_OUTPUT
fi
- name: "Check if PR has 'dev: no-bot-comments' label"
if: ${{ github.event_name == 'workflow_dispatch' || (steps.read-pr_number.outputs.pr_number != '' && steps.isCrossRepository.outputs.isCrossRepository == 'true' && github.event.workflow_run.name != 'PR Review Activity') }}
id: check-label
run: |
has_label=$(gh pr view "${{ steps.set-vars.outputs.pr_number }}" --json labels -q \
'.labels[].name' | grep -Fxq "dev: no-bot-comments" && echo "true" || echo "false")
echo "has_label=$has_label" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# No setup-gradle here: this job never runs Gradle, it only needs a JDK and JBang.
- uses: actions/setup-java@d1374f8ac74474200d16ef8ecee22c7ae7a51880 # v6.3.0
if: ${{ github.event_name == 'workflow_dispatch' || (steps.check-label.outputs.has_label == 'false' && steps.read-pr_number.outputs.pr_number != '' && steps.isCrossRepository.outputs.isCrossRepository == 'true' && github.event.workflow_run.name != 'PR Review Activity') }}
with:
distribution: 'corretto'
java-version: '25'
- name: Generate JBang cache key
if: ${{ github.event_name == 'workflow_dispatch' || (steps.check-label.outputs.has_label == 'false' && steps.read-pr_number.outputs.pr_number != '' && steps.isCrossRepository.outputs.isCrossRepository == 'true' && github.event.workflow_run.name != 'PR Review Activity') }}
id: cache-key
run: echo "cache_key=jbang-ghprcomment-$(date +%Y-%m)" >> $GITHUB_OUTPUT
- name: Cache JBang
# Own key: ghprcomment pulls different dependencies than the other JBang jobs.
if: ${{ github.event_name == 'workflow_dispatch' || (steps.check-label.outputs.has_label == 'false' && steps.read-pr_number.outputs.pr_number != '' && steps.isCrossRepository.outputs.isCrossRepository == 'true' && github.event.workflow_run.name != 'PR Review Activity') }}
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6
with:
path: ~/.jbang
key: ${{ steps.cache-key.outputs.cache_key }}
restore-keys:
jbang-ghprcomment-
- name: Resolve //DEPS into the cached directory
# JBang resolves script dependencies into ~/.m2, which is not cached here.
if: ${{ github.event_name == 'workflow_dispatch' || (steps.check-label.outputs.has_label == 'false' && steps.read-pr_number.outputs.pr_number != '' && steps.isCrossRepository.outputs.isCrossRepository == 'true' && github.event.workflow_run.name != 'PR Review Activity') }}
run: echo "JBANG_REPO=$HOME/.jbang/repository" >> "$GITHUB_ENV"
- name: Setup JBang
if: ${{ github.event_name == 'workflow_dispatch' || (steps.check-label.outputs.has_label == 'false' && steps.read-pr_number.outputs.pr_number != '' && steps.isCrossRepository.outputs.isCrossRepository == 'true' && github.event.workflow_run.name != 'PR Review Activity') }}
uses: jbangdev/setup-jbang@2b1b465a7b75f4222b81426f23a01e013aa7b95c # v0.1.1
- name: ghprcomment@main
if: ${{ github.event_name == 'workflow_dispatch' || (steps.check-label.outputs.has_label == 'false' && steps.read-pr_number.outputs.pr_number != '' && steps.isCrossRepository.outputs.isCrossRepository == 'true' && github.event.workflow_run.name != 'PR Review Activity') }}
run: |
# A dispatched cleanup runs asynchronously; a newer push may have made the PR conflicting
# again in the meantime, and a stale run must not remove that conflict's comment or labels.
# Checked here, right before mutating, so the setup steps above leave no stale-check window.
# Fail closed: only a confirmed MERGEABLE state at the dispatched head SHA may proceed.
if [ -n "$HEAD_SHA" ]; then
# Retry a few times until mergeable != UNKNOWN
# See https://github.com/cli/cli/discussions/8020#discussioncomment-7057040 for details
for i in 1 2 3 4 5; do
PR=$(gh pr view "$PR_NUMBER" --json headRefOid,mergeable)
MERGEABLE=$(jq -r .mergeable <<< "$PR")
if [ "$MERGEABLE" != "UNKNOWN" ]; then break; fi
sleep 5
done
if [ "$(jq -r .headRefOid <<< "$PR")" != "$HEAD_SHA" ] || [ "$MERGEABLE" != "MERGEABLE" ]; then
echo "⊘ PR moved on since dispatch - skipping" >> $GITHUB_STEP_SUMMARY
exit 0
fi
fi
jbang trust add https://github.com/koppor/ghprcomment/blob/main/ghprcomment.java
jbang https://github.com/koppor/ghprcomment/blob/main/ghprcomment.java -r JabRef/jabref -p ${{ steps.set-vars.outputs.pr_number }} -w ${{ steps.set-vars.outputs.workflow_run_id }}
env:
GITHUB_OAUTH: ${{ secrets.GH_TOKEN_JABREF_MACHINE_PR_APPROVE }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ steps.set-vars.outputs.pr_number }}
HEAD_SHA: ${{ github.event.inputs.head_sha }}
- name: 'Adapt label "status: changes-required"'
# Every workflow in this workflow's workflow_run trigger list is a label-relevant producer:
# the pending guard below defers evaluation to whichever of them finishes last, so each of
# their completion events must be allowed to run this step - not only Source Code Tests.
# Deliberately not restricted to fork PRs (unlike ghprcomment, which only forks need):
# labels are computed nowhere else, and with the restriction a same-repo PR whose Qodo
# review arrived while a check was still running never got a second chance to be labelled.
if: ${{ github.event_name == 'workflow_dispatch' || steps.read-pr_number.outputs.pr_number != '' }}
run: |
# Same stale-dispatch guard as in ghprcomment@main above, re-checked immediately before
# this step's own mutations (ghprcomment may have run for a while in between).
if [ -n "$HEAD_SHA" ]; then
for i in 1 2 3 4 5; do
PR=$(gh pr view "$PR_NUMBER" --json headRefOid,mergeable)
MERGEABLE=$(jq -r .mergeable <<< "$PR")
if [ "$MERGEABLE" != "UNKNOWN" ]; then break; fi
sleep 5
done
if [ "$(jq -r .headRefOid <<< "$PR")" != "$HEAD_SHA" ] || [ "$MERGEABLE" != "MERGEABLE" ]; then
echo "⊘ PR moved on since dispatch - skipping" >> $GITHUB_STEP_SUMMARY
exit 0
fi
fi
echo "PR #$PR_NUMBER" >> $GITHUB_STEP_SUMMARY
REPO="${{ github.repository }}"
# The labels reflect the complete picture only once every producer of bot comments is done.
# Whatever is still pending re-triggers this workflow when it finishes (workflow_run for the
# workflows below, "PR Review Activity" for Qodo), so skipping here loses nothing.
CHECKS=$(gh pr checks --repo $REPO $PR_NUMBER --json state,workflow \
--jq '[.[] | select(.workflow == "Source Code Tests" or .workflow == "On PR opened/updated" or .workflow == "Check PR Format"
or .workflow == "Link PR to Issue" or .workflow == "Check PR Modifications" or .workflow == "Check PR CHANGELOG.md")]')
PENDING=$(jq -r '[.[] | select(.state == "IN_PROGRESS" or .state == "QUEUED" or .state == "PENDING") | .workflow] | unique | join(", ")' <<< "$CHECKS")
# Only Qodo's most recently touched comment counts: a "busy" comment Qodo never completed
# would otherwise block the labels forever, although later reviews finished.
if gh api repos/$REPO/issues/$PR_NUMBER/comments --paginate \
| jq -s '[.[][] | select(.user.login == "qodo-free-for-open-source-projects[bot]")] | max_by(.updated_at) | .body // ""' \
| grep -q "Qodo is busy working"; then
PENDING="${PENDING:+$PENDING, }Qodo"
fi
if [ -n "$PENDING" ]; then
echo "⏳ Still pending: $PENDING - labels are evaluated when the last one finishes"
echo "⏳ Still pending: $PENDING - labels are evaluated when the last one finishes" >> $GITHUB_STEP_SUMMARY
exit 0
fi
COMMENTS=$(gh api \
repos/$REPO/issues/$PR_NUMBER/comments \
--jq '.[] | select(.user.login=="jabref-machine")')
# Qodo comments in review threads (not issue comments); a thread counts as long as it is
# active (not resolved, not outdated) and the PR author has not replied to it
QODO_THREADS=$(gh api graphql \
-f owner="${REPO%/*}" -f repo="${REPO#*/}" -F pr="$PR_NUMBER" \
-f query='
query($owner: String!, $repo: String!, $pr: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
author { login }
reviewThreads(first: 100) {
nodes {
isResolved
isOutdated
comments(first: 100) { nodes { author { login } } }
}
}
}
}
}' \
--jq '.data.repository.pullRequest as $pr
| [ $pr.reviewThreads.nodes[]
| select((.isResolved or .isOutdated) | not)
| [.comments.nodes[].author.login // ""]
| select(.[0] == "qodo-free-for-open-source-projects")
| select([.[1:][] | select(. == $pr.author.login)] == [])
] | length')
# Same-repo PRs get no ghprcomment bot comment (see above), so a failed check would leave
# no trace here - count the failures themselves, for forks as well.
FAILED=$(jq -r '[.[] | select(.state == "FAILURE") | .workflow] | unique | join(", ")' <<< "$CHECKS")
# A human "Request changes" review counts like a bot finding, unless the PR is a draft or
# commits were pushed after the review: then the author presumably addressed it, and the
# PR goes back to the reviewer as ready-for-review instead of staying changes-required
# until the reviewer dismisses or redoes the review. Only the latest review of each
# reviewer counts, as GitHub's own review decision does.
CHANGES_REQUESTED=$(gh api graphql \
-f owner="${REPO%/*}" -f repo="${REPO#*/}" -F pr="$PR_NUMBER" \
-f query='
query($owner: String!, $repo: String!, $pr: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
isDraft
headRefOid
latestOpinionatedReviews(first: 100) {
nodes { state commit { oid } }
}
}
}
}' \
--jq '.data.repository.pullRequest as $pr
| (($pr.isDraft | not)
and ([$pr.latestOpinionatedReviews.nodes[]
| select(.state == "CHANGES_REQUESTED" and .commit.oid == $pr.headRefOid)] | length > 0))')
if [[ -n "$COMMENTS" || "$QODO_THREADS" -gt 0 || -n "$FAILED" || "$CHANGES_REQUESTED" == "true" ]]; then
echo "Bot comments, failed checks ($FAILED) or changes requested by a reviewer - adding label changes-required"
gh issue --repo $REPO edit $PR_NUMBER --remove-label "status: ready-for-review,status: stale" --add-label "status: changes-required"
echo "Bot comments - added label changes-required" >> $GITHUB_STEP_SUMMARY
else
# A PR without bot comments is ready for review unless it is a draft
# or it already progressed further in the review process. Drafts end up without any status label;
# progressed PRs keep their awaiting-second-review / to-be-merged label.
PR_STATE=$(gh pr view --repo $REPO $PR_NUMBER --json isDraft,labels \
--jq 'if .isDraft
or ([.labels[].name] | any(. == "status: awaiting-second-review" or . == "status: to-be-merged"))
then "not-ready" else "ready" end')
if [ "$PR_STATE" = "ready" ]; then
echo "No bot comments and PR is ready for review - adding label ready-for-review"
gh issue --repo $REPO edit $PR_NUMBER --remove-label "status: changes-required,status: stale" --add-label "status: ready-for-review"
echo "No bot comments - removed label changes-required and added ready-for-review" >> $GITHUB_STEP_SUMMARY
else
echo "No bot comments, but PR is draft or is already further in review - removing changes-required, stale and ready-for-review"
gh issue --repo $REPO edit $PR_NUMBER --remove-label "status: changes-required,status: stale,status: ready-for-review"
echo "No bot comments, but not ready for review - removed changes-required, stale and ready-for-review" >> $GITHUB_STEP_SUMMARY
fi
fi
env:
# The default token keeps the timeline consistent: every status label is moved by
# github-actions[bot], not by jabref-machine. This workflow only runs on workflow_run
# (and workflow_dispatch), where that token has pull-requests: write even for fork PRs.
# Its downside - labels it sets emit no "labeled" event - costs nothing here: the only
# listener acting on a status label ("Adapt PR status") marks drafts ready for review,
# and this step never puts "status: ready-for-review" on a draft.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ steps.set-vars.outputs.pr_number }}
HEAD_SHA: ${{ github.event.inputs.head_sha }}