@@ -4,62 +4,39 @@ set -xe |
4 | 4 |
|
5 | 5 | OWNER=$1 |
6 | 6 | REPOSITORY=$2 |
7 | | -GITHUB_TOKEN=$3 |
8 | | -shift 3 |
| 7 | +shift 2 |
9 | 8 |
|
10 | 9 | UPSTREAM=origin |
11 | 10 | DEFAULT_BRANCH=master |
12 | 11 |
|
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" |
20 | 14 |
|
21 | 15 | 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" |
42 | 17 | } |
43 | 18 |
|
44 | 19 | 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}" |
46 | 23 |
|
47 | 24 | # shellcheck disable=SC2154 |
48 | 25 | 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" |
51 | 28 |
|
52 | | - gitHubCurl "$(commentsUrl "${1}")" POST --data @output.json |
| 29 | + gh pr comment "$pr" --body "$body" |
53 | 30 |
|
54 | | - rm output output.json |
| 31 | + rm output |
55 | 32 | } |
56 | 33 |
|
57 | 34 | # TODO(mmarchini): should this be set with whoever added the label for each PR? |
58 | 35 | |
59 | 36 | git config --local user.name "Node.js GitHub Bot" |
60 | 37 |
|
61 | 38 | for pr in "$@"; do |
62 | | - gitHubCurl "$(labelsUrl "$pr")" GET > labels.json |
| 39 | + gh pr view "$pr" --json labels --jq ".labels" > labels.json |
63 | 40 | # Skip PR if CI was requested |
64 | 41 | if jq -e 'map(.name) | index("request-ci")' < labels.json; then |
65 | 42 | echo "pr ${pr} skipped, waiting for CI to start" |
@@ -73,7 +50,7 @@ for pr in "$@"; do |
73 | 50 | fi |
74 | 51 |
|
75 | 52 | # Delete the commit queue label |
76 | | - gitHubCurl "$(labelsUrl "$pr")"/"$COMMIT_QUEUE_LABEL" DELETE |
| 53 | + gh pr edit "$pr" --remove-label "$COMMIT_QUEUE_LABEL" |
77 | 54 |
|
78 | 55 | if jq -e 'map(.name) | index("commit-queue-squash")' < labels.json; then |
79 | 56 | MULTIPLE_COMMIT_POLICY="--fixupAll" |
@@ -106,14 +83,18 @@ for pr in "$@"; do |
106 | 83 | continue |
107 | 84 | fi |
108 | 85 | 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). |
110 | 88 | jq -n \ |
111 | 89 | --arg title "$(git log -1 --pretty='format:%s')" \ |
112 | 90 | --arg body "$(git log -1 --pretty='format:%b')" \ |
113 | 91 | --arg head "$(grep 'Fetched commits as' output | cut -d. -f3 | xargs git rev-parse)" \ |
114 | 92 | '{merge_method:"squash",commit_title:$title,commit_message:$body,sha:$head}' > output.json |
115 | 93 | 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 |
117 | 98 | cat output |
118 | 99 | if ! commits="$(jq -r 'if .merged then .sha else error("not merged") end' < output)"; then |
119 | 100 | commit_queue_failed "$pr" |
@@ -124,9 +105,9 @@ for pr in "$@"; do |
124 | 105 |
|
125 | 106 | rm output |
126 | 107 |
|
127 | | - gitHubCurl "$(commentsUrl "$pr")" POST --data '{"body": "Landed in '"$commits"'"}' |
| 108 | + gh pr comment "$pr" --body "Landed in $commits" |
128 | 109 |
|
129 | | - [ -z "$MULTIPLE_COMMIT_POLICY" ] && gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}' |
| 110 | + [ -z "$MULTIPLE_COMMIT_POLICY" ] && gh pr close "$pr" |
130 | 111 | done |
131 | 112 |
|
132 | 113 | rm -f labels.json |