Skip to content

Commit 655a8cd

Browse files
panvaaduh95
authored andcommitted
tools: label PRs lacking second approval
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #65538 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Tierney Cyren <hello@bnb.im>
1 parent c3d0976 commit 655a8cd

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

.github/workflows/commit-queue.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
if: github.repository == 'nodejs/node'
2929
runs-on: ubuntu-slim
3030
outputs:
31+
aged_prs: ${{ steps.get_candidate_prs.outputs.aged_prs }}
3132
candidates: ${{ steps.get_candidate_prs.outputs.candidates }}
3233
steps:
3334
- name: Get Pull Request Candidates
@@ -50,6 +51,7 @@ jobs:
5051
--search "-label:blocked")
5152
candidates=$(printf '%s %s\n' "$fast_track_prs" "$aged_prs" |
5253
jq -r -s 'reduce .[] as $pr ([]; if index($pr) then . else . + [$pr] end) | join(" ")')
54+
echo "aged_prs=$aged_prs" >> "$GITHUB_OUTPUT"
5355
echo "candidates=$candidates" >> "$GITHUB_OUTPUT"
5456
env:
5557
GH_TOKEN: ${{ github.token }}
@@ -93,6 +95,7 @@ jobs:
9395
curl -fsSLo "$readme" "https://github.com/${GITHUB_REPOSITORY}/raw/${GITHUB_SHA}/README.md"
9496
9597
numbers=
98+
lacks_second_approval_prs=
9699
# shellcheck disable=SC2086
97100
for pr in $CANDIDATES; do
98101
metadata="${RUNNER_TEMP}/metadata-${pr}.json"
@@ -139,6 +142,14 @@ jobs:
139142
if [ "$metadata_status" -ge 20 ] && [ "$metadata_status" -le 29 ]; then
140143
echo "pr ${pr} skipped, not ready to land"
141144
echo "reason codes: ${metadata_reason_codes}"
145+
if jq -e '
146+
(.reasonCodes | index("wait-time")) and
147+
(.pullRequest.labels | index("lacks-second-approval") | not)
148+
' "$metadata" > /dev/null; then
149+
case " $AGED_PRS " in
150+
*" $pr "*) lacks_second_approval_prs="$lacks_second_approval_prs $pr" ;;
151+
esac
152+
fi
142153
continue
143154
fi
144155
@@ -148,11 +159,27 @@ jobs:
148159
done
149160
150161
numbers=$(echo "$numbers" | xargs)
162+
lacks_second_approval_prs=$(echo "$lacks_second_approval_prs" | xargs)
151163
echo "numbers=$numbers" >> "$GITHUB_OUTPUT"
164+
echo "lacks_second_approval_prs=$lacks_second_approval_prs" >> "$GITHUB_OUTPUT"
152165
env:
166+
AGED_PRS: ${{ needs.get_candidate_prs.outputs.aged_prs }}
153167
CANDIDATES: ${{ needs.get_candidate_prs.outputs.candidates }}
154168
GH_TOKEN: ${{ github.token }}
155169

170+
- name: Label Pull Requests Lacking a Second Approval
171+
if: steps.get_mergeable_prs.outputs.lacks_second_approval_prs != ''
172+
run: |
173+
# shellcheck disable=SC2086
174+
for pr in $PULL_REQUESTS; do
175+
if ! gh -R "$GITHUB_REPOSITORY" pr edit "$pr" --add-label 'lacks-second-approval'; then
176+
echo "::warning::Failed to add lacks-second-approval to PR ${pr}"
177+
fi
178+
done
179+
env:
180+
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
181+
PULL_REQUESTS: ${{ steps.get_mergeable_prs.outputs.lacks_second_approval_prs }}
182+
156183
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
157184
if: steps.get_mergeable_prs.outputs.numbers != ''
158185
with:

doc/contributing/commit-queue.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ landing process by automating it via GitHub Actions. With it, collaborators can
88
queue pull requests for landing by adding the `commit-queue` label to a PR. The
99
selector checks readiness with `@node-core/utils`. If the pull request is only
1010
blocked on a deferrable condition, currently wait time, the queue leaves the
11-
label in place and retries later. Other failures continue to the existing
12-
landing and failure-reporting path.
11+
label in place and retries later. For pull requests that are at least two days
12+
old and still waiting for a second approval, the queue adds the
13+
`lacks-second-approval` label. The queue removes that label when it removes the
14+
`commit-queue` label. Other failures continue to the existing landing and
15+
failure-reporting path.
1316

1417
To make the Commit Queue squash all the commits of a pull request into the
1518
first one, add the `commit-queue-squash` label.

tools/actions/commit-queue.sh

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DEFAULT_BRANCH=main
77

88
COMMIT_QUEUE_LABEL="commit-queue"
99
COMMIT_QUEUE_FAILED_LABEL="commit-queue-failed"
10+
LACKS_SECOND_APPROVAL_LABEL="lacks-second-approval"
1011

1112
cqurl="${GITHUB_SERVER_URL:?}/${GITHUB_REPOSITORY:?}/actions/runs/${GITHUB_RUN_ID:?}"
1213

@@ -24,11 +25,43 @@ escape_code_block_or_line() {
2425
printf '%s%s%s%s%s\n' "$fence" "$sep" "$1" "$sep" "$fence"
2526
}
2627

28+
edit_pr_labels() {
29+
pr=$1
30+
failure_mode=$2
31+
shift 2
32+
if gh -R "$GITHUB_REPOSITORY" pr edit "$pr" "$@"; then
33+
return
34+
fi
35+
if [ "$failure_mode" = warn ]; then
36+
echo "::warning::Failed to update labels for PR $pr"
37+
return
38+
fi
39+
return 1
40+
}
41+
42+
remove_labels_if_present() {
43+
pr=$1
44+
shift
45+
labels=
46+
for label in "$@"; do
47+
if jq -e --arg label "$label" \
48+
'map(.name) | index($label)' < labels.json > /dev/null; then
49+
labels="${labels}${labels:+,}${label}"
50+
fi
51+
done
52+
53+
if [ -n "$labels" ]; then
54+
edit_pr_labels "$pr" warn --remove-label "$labels"
55+
fi
56+
}
57+
2758
commit_queue_failed() {
2859
pr=$1
2960
reported_failure=${2:-}
3061

31-
gh -R "$GITHUB_REPOSITORY" pr edit "$pr" --add-label "${COMMIT_QUEUE_FAILED_LABEL}" --remove-label "${COMMIT_QUEUE_LABEL}"
62+
edit_pr_labels "$pr" required --add-label "$COMMIT_QUEUE_FAILED_LABEL" \
63+
--remove-label "$COMMIT_QUEUE_LABEL"
64+
remove_labels_if_present "$pr" "$LACKS_SECOND_APPROVAL_LABEL"
3265

3366
last_output_line=$(awk 'NF { line = $0 } END { sub(/^[[:space:]]*/, "", line); print line }' output)
3467
# shellcheck disable=SC2016
@@ -145,8 +178,9 @@ for pr in "$@"; do
145178

146179
[ -z "$MULTIPLE_COMMIT_POLICY" ] && gh -R "$GITHUB_REPOSITORY" pr close "$pr"
147180

148-
# Delete the commit queue label (but ignore errors, it's no big deal if a closed PR still has the label)
149-
gh -R "$GITHUB_REPOSITORY" pr edit "$pr" --remove-label "$COMMIT_QUEUE_LABEL" || true
181+
# Delete the commit queue labels (but ignore errors, it's no big deal if a closed PR still has them)
182+
remove_labels_if_present "$pr" "$COMMIT_QUEUE_LABEL" \
183+
"$LACKS_SECOND_APPROVAL_LABEL"
150184
done
151185

152186
rm -f labels.json

0 commit comments

Comments
 (0)