Skip to content

Commit 64c6575

Browse files
targosBethGriggs
authored andcommitted
tools: simplify and fix commit queue
Use `gh` CLI for CI and commit queue jobs, and use the correct token to merge PRs. PR-URL: #40742 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Voltrex <[email protected]>
1 parent cba8eaf commit 64c6575

File tree

4 files changed

+35
-78
lines changed

4 files changed

+35
-78
lines changed

.github/workflows/auto-start-ci.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ jobs:
6060
ncu-config set repo ${{ env.REPOSITORY }}
6161
6262
- name: Start CI
63-
run: ./tools/actions/start-ci.sh ${{ secrets.GITHUB_TOKEN }} ${{ env.OWNER }} ${{ env.REPOSITORY }} $(echo '${{ steps.get_prs_for_ci.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
63+
run: ./tools/actions/start-ci.sh $(echo '${{ steps.get_prs_for_ci.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
64+
env:
65+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/commit-queue.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ jobs:
7878
ncu-config set owner "${OWNER}"
7979
8080
- name: Start the commit queue
81-
run: ./tools/actions/commit-queue.sh ${OWNER} ${REPOSITORY} ${{ secrets.GITHUB_TOKEN }} $(echo '${{ steps.get_mergable_pull_requests.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
81+
run: ./tools/actions/commit-queue.sh ${OWNER} ${REPOSITORY} $(echo '${{ steps.get_mergable_pull_requests.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
82+
env:
83+
GITHUB_TOKEN: ${{ secrets.GH_USER_TOKEN }}

tools/actions/commit-queue.sh

+21-40
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,39 @@ set -xe
44

55
OWNER=$1
66
REPOSITORY=$2
7-
GITHUB_TOKEN=$3
8-
shift 3
7+
shift 2
98

109
UPSTREAM=origin
1110
DEFAULT_BRANCH=master
1211

13-
API_URL=https://api.github.com
14-
COMMIT_QUEUE_LABEL='commit-queue'
15-
COMMIT_QUEUE_FAILED_LABEL='commit-queue-failed'
16-
17-
issueUrl() {
18-
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
19-
}
12+
COMMIT_QUEUE_LABEL="commit-queue"
13+
COMMIT_QUEUE_FAILED_LABEL="commit-queue-failed"
2014

2115
mergeUrl() {
22-
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/pulls/${1}/merge"
23-
}
24-
25-
labelsUrl() {
26-
echo "$(issueUrl "${1}")/labels"
27-
}
28-
29-
commentsUrl() {
30-
echo "$(issueUrl "${1}")/comments"
31-
}
32-
33-
gitHubCurl() {
34-
url=$1
35-
method=$2
36-
shift 2
37-
38-
curl -fsL --request "$method" \
39-
--url "$url" \
40-
--header "authorization: Bearer ${GITHUB_TOKEN}" \
41-
--header 'content-type: application/json' "$@"
16+
echo "repos/${OWNER}/${REPOSITORY}/pulls/${1}/merge"
4217
}
4318

4419
commit_queue_failed() {
45-
gitHubCurl "$(labelsUrl "${1}")" POST --data '{"labels": ["'"${COMMIT_QUEUE_FAILED_LABEL}"'"]}'
20+
pr=$1
21+
22+
gh pr edit "$pr" --add-label "${COMMIT_QUEUE_FAILED_LABEL}"
4623

4724
# shellcheck disable=SC2154
4825
cqurl="${GITHUB_SERVER_URL}/${OWNER}/${REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
49-
jq -n --arg content "<details><summary>Commit Queue failed</summary><pre>$(cat output)</pre><a href='$cqurl'>$cqurl</a></details>" '{body: $content}' > output.json
50-
cat output.json
26+
body="<details><summary>Commit Queue failed</summary><pre>$(cat output)</pre><a href='$cqurl'>$cqurl</a></details>"
27+
echo "$body"
5128

52-
gitHubCurl "$(commentsUrl "${1}")" POST --data @output.json
29+
gh pr comment "$pr" --body "$body"
5330

54-
rm output output.json
31+
rm output
5532
}
5633

5734
# TODO(mmarchini): should this be set with whoever added the label for each PR?
5835
git config --local user.email "[email protected]"
5936
git config --local user.name "Node.js GitHub Bot"
6037

6138
for pr in "$@"; do
62-
gitHubCurl "$(labelsUrl "$pr")" GET > labels.json
39+
gh pr view "$pr" --json labels --jq ".labels" > labels.json
6340
# Skip PR if CI was requested
6441
if jq -e 'map(.name) | index("request-ci")' < labels.json; then
6542
echo "pr ${pr} skipped, waiting for CI to start"
@@ -73,7 +50,7 @@ for pr in "$@"; do
7350
fi
7451

7552
# Delete the commit queue label
76-
gitHubCurl "$(labelsUrl "$pr")"/"$COMMIT_QUEUE_LABEL" DELETE
53+
gh pr edit "$pr" --remove-label "$COMMIT_QUEUE_LABEL"
7754

7855
if jq -e 'map(.name) | index("commit-queue-squash")' < labels.json; then
7956
MULTIPLE_COMMIT_POLICY="--fixupAll"
@@ -106,14 +83,18 @@ for pr in "$@"; do
10683
continue
10784
fi
10885
else
109-
# If there's only one commit, we can use the Squash and Merge feature from GitHub
86+
# If there's only one commit, we can use the Squash and Merge feature from GitHub.
87+
# TODO: use `gh pr merge` when the GitHub CLI allows to customize the commit title (https://github.com/cli/cli/issues/1023).
11088
jq -n \
11189
--arg title "$(git log -1 --pretty='format:%s')" \
11290
--arg body "$(git log -1 --pretty='format:%b')" \
11391
--arg head "$(grep 'Fetched commits as' output | cut -d. -f3 | xargs git rev-parse)" \
11492
'{merge_method:"squash",commit_title:$title,commit_message:$body,sha:$head}' > output.json
11593
cat output.json
116-
gitHubCurl "$(mergeUrl "$pr")" PUT --data @output.json > output
94+
if ! gh api -X PUT "$(mergeUrl "$pr")" --input output.json > output; then
95+
commit_queue_failed "$pr"
96+
continue
97+
fi
11798
cat output
11899
if ! commits="$(jq -r 'if .merged then .sha else error("not merged") end' < output)"; then
119100
commit_queue_failed "$pr"
@@ -124,9 +105,9 @@ for pr in "$@"; do
124105

125106
rm output
126107

127-
gitHubCurl "$(commentsUrl "$pr")" POST --data '{"body": "Landed in '"$commits"'"}'
108+
gh pr comment "$pr" --body "Landed in $commits"
128109

129-
[ -z "$MULTIPLE_COMMIT_POLICY" ] && gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}'
110+
[ -z "$MULTIPLE_COMMIT_POLICY" ] && gh pr close "$pr"
130111
done
131112

132113
rm -f labels.json

tools/actions/start-ci.sh

+8-36
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,11 @@
22

33
set -xe
44

5-
GITHUB_TOKEN=$1
6-
OWNER=$2
7-
REPOSITORY=$3
8-
API_URL=https://api.github.com
9-
REQUEST_CI_LABEL='request-ci'
10-
REQUEST_CI_FAILED_LABEL='request-ci-failed'
11-
shift 3
12-
13-
issueUrl() {
14-
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
15-
}
16-
17-
labelsUrl() {
18-
echo "$(issueUrl "${1}")/labels"
19-
}
20-
21-
commentsUrl() {
22-
echo "$(issueUrl "${1}")/comments"
23-
}
5+
REQUEST_CI_LABEL="request-ci"
6+
REQUEST_CI_FAILED_LABEL="request-ci-failed"
247

258
for pr in "$@"; do
26-
curl -sL --request DELETE \
27-
--url "$(labelsUrl "$pr")"/"$REQUEST_CI_LABEL" \
28-
--header "authorization: Bearer ${GITHUB_TOKEN}" \
29-
--header 'content-type: application/json'
9+
gh pr edit "$pr" --remove-label "$REQUEST_CI_LABEL"
3010

3111
ci_started=yes
3212
rm -f output;
@@ -35,19 +15,11 @@ for pr in "$@"; do
3515

3616
if [ "$ci_started" = "no" ]; then
3717
# Do we need to reset?
38-
curl -sL --request PUT \
39-
--url "$(labelsUrl "$pr")" \
40-
--header "authorization: Bearer ${GITHUB_TOKEN}" \
41-
--header 'content-type: application/json' \
42-
--data '{"labels": ["'"${REQUEST_CI_FAILED_LABEL}"'"]}'
43-
44-
jq -n --arg content "<details><summary>Couldn't start CI</summary><pre>$(cat output)</pre></details>" '{body: $content}' > output.json
45-
46-
curl -sL --request POST \
47-
--url "$(commentsUrl "$pr")" \
48-
--header "authorization: Bearer ${GITHUB_TOKEN}" \
49-
--header 'content-type: application/json' \
50-
--data @output.json
18+
gh pr edit "$pr" --add-label "$REQUEST_CI_FAILED_LABEL"
19+
20+
jq -n --arg content "<details><summary>Couldn't start CI</summary><pre>$(cat output)</pre></details>" > output.json
21+
22+
gh pr comment "$pr" --body-file output.json
5123

5224
rm output.json;
5325
fi

0 commit comments

Comments
 (0)