Skip to content

Commit b7e7368

Browse files
aduh95targos
authored andcommitted
tools: use GitHub Squash and Merge feature when using CQ
PR-URL: #40666 Refs: https://docs.github.com/en/rest/reference/pulls#merge-a-pull-request Reviewed-By: Mary Marchini <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent ac3bc6e commit b7e7368

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

tools/actions/commit-queue.sh

+32-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ issueUrl() {
1818
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
1919
}
2020

21+
mergeUrl() {
22+
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/pulls/${1}/merge"
23+
}
24+
2125
labelsUrl() {
2226
echo "$(issueUrl "${1}")/labels"
2327
}
@@ -55,8 +59,9 @@ git config --local user.email "[email protected]"
5559
git config --local user.name "Node.js GitHub Bot"
5660

5761
for pr in "$@"; do
62+
gitHubCurl "$(labelsUrl "$pr")" GET > labels.json
5863
# Skip PR if CI was requested
59-
if gitHubCurl "$(labelsUrl "$pr")" GET | jq -e 'map(.name) | index("request-ci")'; then
64+
if jq -e 'map(.name) | index("request-ci")' < labels.json; then
6065
echo "pr ${pr} skipped, waiting for CI to start"
6166
continue
6267
fi
@@ -70,9 +75,9 @@ for pr in "$@"; do
7075
# Delete the commit queue label
7176
gitHubCurl "$(labelsUrl "$pr")"/"$COMMIT_QUEUE_LABEL" DELETE
7277

73-
if gitHubCurl "$(labelsUrl "$pr")" GET | jq -e 'map(.name) | index("commit-queue-squash")'; then
78+
if jq -e 'map(.name) | index("commit-queue-squash")' < labels.json; then
7479
MULTIPLE_COMMIT_POLICY="--fixupAll"
75-
elif gitHubCurl "$(labelsUrl "$pr")" GET | jq -e 'map(.name) | index("commit-queue-rebase")'; then
80+
elif jq -e 'map(.name) | index("commit-queue-rebase")' < labels.json; then
7681
MULTIPLE_COMMIT_POLICY=""
7782
else
7883
MULTIPLE_COMMIT_POLICY="--oneCommitMax"
@@ -92,17 +97,35 @@ for pr in "$@"; do
9297
git node land --abort --yes
9398
continue
9499
fi
95-
96-
commits="$(git rev-parse $UPSTREAM/$DEFAULT_BRANCH)...$(git rev-parse HEAD)"
97100

98-
if ! git push $UPSTREAM $DEFAULT_BRANCH >> output 2>&1; then
99-
commit_queue_failed "$pr"
100-
continue
101+
if [ -z "$MULTIPLE_COMMIT_POLICY" ]; then
102+
commits="$(git rev-parse $UPSTREAM/$DEFAULT_BRANCH)...$(git rev-parse HEAD)"
103+
104+
if ! git push $UPSTREAM $DEFAULT_BRANCH >> output 2>&1; then
105+
commit_queue_failed "$pr"
106+
continue
107+
fi
108+
else
109+
# If there's only one commit, we can use the Squash and Merge feature from GitHub
110+
jq -n \
111+
--arg title "$(git log -1 --pretty='format:%s')" \
112+
--arg body "$(git log -1 --pretty='format:%b')" \
113+
'{merge_method:"squash",commit_title:$title,commit_message:$body}' > output.json
114+
cat output.json
115+
gitHubCurl "$(mergeUrl "$pr")" PUT --data @output.json > output
116+
cat output
117+
if ! commits="$(jq -r 'if .merged then .sha else error("not merged") end' < output)"; then
118+
commit_queue_failed "$pr"
119+
continue
120+
fi
121+
rm output.json
101122
fi
102123

103124
rm output
104125

105126
gitHubCurl "$(commentsUrl "$pr")" POST --data '{"body": "Landed in '"$commits"'"}'
106127

107-
gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}'
128+
[ -z "$MULTIPLE_COMMIT_POLICY" ] && gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}'
108129
done
130+
131+
rm -f labels.json

0 commit comments

Comments
 (0)