From bbc10f18497e00bcf9cf83c5b55f6d67c8eb2ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 7 Nov 2021 15:26:45 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/40742 Reviewed-By: Antoine du Hamel Reviewed-By: Voltrex --- .github/workflows/auto-start-ci.yml | 4 +- .github/workflows/commit-queue.yml | 4 +- tools/actions/commit-queue.sh | 61 ++++++++++------------------- tools/actions/start-ci.sh | 44 ++++----------------- 4 files changed, 35 insertions(+), 78 deletions(-) diff --git a/.github/workflows/auto-start-ci.yml b/.github/workflows/auto-start-ci.yml index ef7c97d9e502b6..21a8de921798d1 100644 --- a/.github/workflows/auto-start-ci.yml +++ b/.github/workflows/auto-start-ci.yml @@ -60,4 +60,6 @@ jobs: ncu-config set repo ${{ env.REPOSITORY }} - name: Start CI - 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) | .[]') + run: ./tools/actions/start-ci.sh $(echo '${{ steps.get_prs_for_ci.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]') + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/commit-queue.yml b/.github/workflows/commit-queue.yml index b2d37f80906896..54b114b7b7e813 100644 --- a/.github/workflows/commit-queue.yml +++ b/.github/workflows/commit-queue.yml @@ -78,4 +78,6 @@ jobs: ncu-config set owner "${OWNER}" - name: Start the commit queue - 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) | .[]') + run: ./tools/actions/commit-queue.sh ${OWNER} ${REPOSITORY} $(echo '${{ steps.get_mergable_pull_requests.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]') + env: + GITHUB_TOKEN: ${{ secrets.GH_USER_TOKEN }} diff --git a/tools/actions/commit-queue.sh b/tools/actions/commit-queue.sh index 37b8cdcfc0fb68..8ffbf9ea8d9da4 100755 --- a/tools/actions/commit-queue.sh +++ b/tools/actions/commit-queue.sh @@ -4,54 +4,31 @@ set -xe OWNER=$1 REPOSITORY=$2 -GITHUB_TOKEN=$3 -shift 3 +shift 2 UPSTREAM=origin DEFAULT_BRANCH=master -API_URL=https://api.github.com -COMMIT_QUEUE_LABEL='commit-queue' -COMMIT_QUEUE_FAILED_LABEL='commit-queue-failed' - -issueUrl() { - echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}" -} +COMMIT_QUEUE_LABEL="commit-queue" +COMMIT_QUEUE_FAILED_LABEL="commit-queue-failed" mergeUrl() { - echo "$API_URL/repos/${OWNER}/${REPOSITORY}/pulls/${1}/merge" -} - -labelsUrl() { - echo "$(issueUrl "${1}")/labels" -} - -commentsUrl() { - echo "$(issueUrl "${1}")/comments" -} - -gitHubCurl() { - url=$1 - method=$2 - shift 2 - - curl -fsL --request "$method" \ - --url "$url" \ - --header "authorization: Bearer ${GITHUB_TOKEN}" \ - --header 'content-type: application/json' "$@" + echo "repos/${OWNER}/${REPOSITORY}/pulls/${1}/merge" } commit_queue_failed() { - gitHubCurl "$(labelsUrl "${1}")" POST --data '{"labels": ["'"${COMMIT_QUEUE_FAILED_LABEL}"'"]}' + pr=$1 + + gh pr edit "$pr" --add-label "${COMMIT_QUEUE_FAILED_LABEL}" # shellcheck disable=SC2154 cqurl="${GITHUB_SERVER_URL}/${OWNER}/${REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" - jq -n --arg content "
Commit Queue failed
$(cat output)
$cqurl
" '{body: $content}' > output.json - cat output.json + body="
Commit Queue failed
$(cat output)
$cqurl
" + echo "$body" - gitHubCurl "$(commentsUrl "${1}")" POST --data @output.json + gh pr comment "$pr" --body "$body" - rm output output.json + rm output } # TODO(mmarchini): should this be set with whoever added the label for each PR? @@ -59,7 +36,7 @@ git config --local user.email "github-bot@iojs.org" git config --local user.name "Node.js GitHub Bot" for pr in "$@"; do - gitHubCurl "$(labelsUrl "$pr")" GET > labels.json + gh pr view "$pr" --json labels --jq ".labels" > labels.json # Skip PR if CI was requested if jq -e 'map(.name) | index("request-ci")' < labels.json; then echo "pr ${pr} skipped, waiting for CI to start" @@ -73,7 +50,7 @@ for pr in "$@"; do fi # Delete the commit queue label - gitHubCurl "$(labelsUrl "$pr")"/"$COMMIT_QUEUE_LABEL" DELETE + gh pr edit "$pr" --remove-label "$COMMIT_QUEUE_LABEL" if jq -e 'map(.name) | index("commit-queue-squash")' < labels.json; then MULTIPLE_COMMIT_POLICY="--fixupAll" @@ -106,14 +83,18 @@ for pr in "$@"; do continue fi else - # If there's only one commit, we can use the Squash and Merge feature from GitHub + # If there's only one commit, we can use the Squash and Merge feature from GitHub. + # TODO: use `gh pr merge` when the GitHub CLI allows to customize the commit title (https://github.com/cli/cli/issues/1023). jq -n \ --arg title "$(git log -1 --pretty='format:%s')" \ --arg body "$(git log -1 --pretty='format:%b')" \ --arg head "$(grep 'Fetched commits as' output | cut -d. -f3 | xargs git rev-parse)" \ '{merge_method:"squash",commit_title:$title,commit_message:$body,sha:$head}' > output.json cat output.json - gitHubCurl "$(mergeUrl "$pr")" PUT --data @output.json > output + if ! gh api -X PUT "$(mergeUrl "$pr")" --input output.json > output; then + commit_queue_failed "$pr" + continue + fi cat output if ! commits="$(jq -r 'if .merged then .sha else error("not merged") end' < output)"; then commit_queue_failed "$pr" @@ -124,9 +105,9 @@ for pr in "$@"; do rm output - gitHubCurl "$(commentsUrl "$pr")" POST --data '{"body": "Landed in '"$commits"'"}' + gh pr comment "$pr" --body "Landed in $commits" - [ -z "$MULTIPLE_COMMIT_POLICY" ] && gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}' + [ -z "$MULTIPLE_COMMIT_POLICY" ] && gh pr close "$pr" done rm -f labels.json diff --git a/tools/actions/start-ci.sh b/tools/actions/start-ci.sh index 72a04b6b321b1f..174939aa4c413b 100755 --- a/tools/actions/start-ci.sh +++ b/tools/actions/start-ci.sh @@ -2,31 +2,11 @@ set -xe -GITHUB_TOKEN=$1 -OWNER=$2 -REPOSITORY=$3 -API_URL=https://api.github.com -REQUEST_CI_LABEL='request-ci' -REQUEST_CI_FAILED_LABEL='request-ci-failed' -shift 3 - -issueUrl() { - echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}" -} - -labelsUrl() { - echo "$(issueUrl "${1}")/labels" -} - -commentsUrl() { - echo "$(issueUrl "${1}")/comments" -} +REQUEST_CI_LABEL="request-ci" +REQUEST_CI_FAILED_LABEL="request-ci-failed" for pr in "$@"; do - curl -sL --request DELETE \ - --url "$(labelsUrl "$pr")"/"$REQUEST_CI_LABEL" \ - --header "authorization: Bearer ${GITHUB_TOKEN}" \ - --header 'content-type: application/json' + gh pr edit "$pr" --remove-label "$REQUEST_CI_LABEL" ci_started=yes rm -f output; @@ -35,19 +15,11 @@ for pr in "$@"; do if [ "$ci_started" = "no" ]; then # Do we need to reset? - curl -sL --request PUT \ - --url "$(labelsUrl "$pr")" \ - --header "authorization: Bearer ${GITHUB_TOKEN}" \ - --header 'content-type: application/json' \ - --data '{"labels": ["'"${REQUEST_CI_FAILED_LABEL}"'"]}' - - jq -n --arg content "
Couldn't start CI
$(cat output)
" '{body: $content}' > output.json - - curl -sL --request POST \ - --url "$(commentsUrl "$pr")" \ - --header "authorization: Bearer ${GITHUB_TOKEN}" \ - --header 'content-type: application/json' \ - --data @output.json + gh pr edit "$pr" --add-label "$REQUEST_CI_FAILED_LABEL" + + jq -n --arg content "
Couldn't start CI
$(cat output)
" > output.json + + gh pr comment "$pr" --body-file output.json rm output.json; fi