diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml index 00c4d504be..c523ee1331 100644 --- a/.github/workflows/automerge.yml +++ b/.github/workflows/automerge.yml @@ -3,7 +3,9 @@ name: Auto-merge # A maintainer labels a pull request `automerge`: it's approved and joins the merge queue as soon # as the checks the `main` ruleset requires pass; the queue squash-merges it once its checks pass # on top of main. A later push drops the label and turns auto-merge off, -# so new commits need another look; removing the label turns it off too. +# so new commits need another look; removing the label turns it off and dismisses the approval +# automerge posted (manual approvals and approvals created before this workflow marks them are +# left untouched). # # `pull_request_target` so fork pull requests get the token; the pull request's code is never # checked out or run. The token is a maintainer's (`AUTOMERGE_TOKEN`), not GITHUB_TOKEN: Actions @@ -16,10 +18,12 @@ on: permissions: {} -# One group per action: a pending run is replaced by the next one in its group, so a push's revoke -# run mustn't share a group with runs for other labels. +# `automerge` label and unlabel runs share a queue, so a pending revoke run isn't replaced; GitHub +# doesn't guarantee their order, so each run checks the label as it is now rather than relying on +# it. `synchronize` and unrelated labels get their own groups. concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number }}-${{ github.event.action }} + group: ${{ github.workflow }}-${{ github.event.pull_request.number }}-${{ github.event.action == 'synchronize' && 'synchronize' || (github.event.label.name == 'automerge' && 'label' || 'other') }} + queue: max jobs: enable: @@ -35,16 +39,39 @@ jobs: SENDER: ${{ github.event.sender.login }} AUTHOR: ${{ github.event.pull_request.user.login }} run: | + set -euo pipefail + # A refused label revokes whatever an earlier, authorized label armed, here rather than + # through the unlabel run, so it holds even if taking the label off fails. The step fails + # anyway, so each call is attempted and its own failure ignored. + refuse() { + echo "::error::$1" + gh pr merge "$PR" --disable-auto || true + if me=$(gh api user --jq .login) && ids=$(gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$NUMBER/reviews?per_page=100" \ + | jq -r --arg owner "$me" '.[] | select((.user.login // "") == $owner and .state == "APPROVED" and (.body // "") == "Approved via automerge.") | .id'); then + for review_id in $ids; do + gh api -X PUT "repos/$GITHUB_REPOSITORY/pulls/$NUMBER/reviews/$review_id/dismissals" \ + -f message="automerge label refused" --silent || true + done + fi + gh pr edit "$PR" --remove-label automerge || true + exit 1 + } # Triage can label too: only someone who could merge it themselves may use the label. - perm=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$SENDER/permission" --jq .permission) + perm=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$SENDER/permission" --jq .permission) || perm=unknown case "$perm" in admin|maintain|write) ;; - *) echo "::error::$SENDER ($perm) can't use automerge."; exit 1 ;; + *) refuse "$SENDER ($perm) can't use automerge." ;; esac owner=$(gh api user --jq .login) # The approval stands in for someone else's review, so it isn't for the labeller's own work. if [ "$SENDER" = "$AUTHOR" ] && [ "$AUTHOR" != "$owner" ]; then - echo "::error::$SENDER can't use automerge on their own pull request."; exit 1 + refuse "$SENDER can't use automerge on their own pull request." + fi + # Re-verify the label is still present in case it was removed before this run started. + has_label=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$NUMBER" --jq 'any(.labels[]; .name == "automerge")') + if [ "$has_label" != true ]; then + echo "automerge label was removed before enabling; skipping." + exit 0 fi # Both pinned to the commit that was labelled: if the head has moved, enabling auto-merge # fails before anything is approved. @@ -52,7 +79,7 @@ jobs: # The token owner can't approve their own pull requests, and needn't: they bypass the review. if [ "$AUTHOR" != "$owner" ]; then gh api "repos/$GITHUB_REPOSITORY/pulls/$NUMBER/reviews" -f commit_id="$SHA" -f event=APPROVE \ - --silent || { gh pr merge "$PR" --disable-auto; exit 1; } + -f body="Approved via automerge." --silent || { gh pr merge "$PR" --disable-auto; exit 1; } fi revoke: @@ -65,7 +92,41 @@ jobs: - env: GH_TOKEN: ${{ secrets.AUTOMERGE_TOKEN }} PR: ${{ github.event.pull_request.html_url }} + NUMBER: ${{ github.event.pull_request.number }} ACTION: ${{ github.event.action }} run: | - gh pr merge "$PR" --disable-auto || echo "Auto-merge was off." - if [ "$ACTION" = synchronize ]; then gh pr edit "$PR" --remove-label automerge; fi + set -euo pipefail + # The label is back: a re-add's enable run may already have armed it, and a refused + # re-add's run revokes by itself. Each step below is + # attempted even if an earlier one fails, and the step fails at the end if any did. + if [ "$ACTION" = unlabeled ]; then + has_label=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$NUMBER" --jq 'any(.labels[]; .name == "automerge")') \ + || has_label=unknown + if [ "$has_label" = true ]; then + echo "automerge label is present again; skipping revoke." + exit 0 + fi + fi + failed=0 + # Disable only when armed (or when that can't be read), so a real failure is reported. + armed=$(gh pr view "$PR" --json autoMergeRequest --jq '.autoMergeRequest != null') || armed=unknown + if [ "$armed" != false ]; then + gh pr merge "$PR" --disable-auto || failed=1 + else + echo "Auto-merge was off." + fi + if [ "$ACTION" = synchronize ]; then + gh pr edit "$PR" --remove-label automerge || failed=1 + fi + # Dismiss on both actions, so a push doesn't depend on the label removal succeeding. + # Reads in an if condition, where set -e doesn't stop the step; a failed read counts. + if owner=$(gh api user --jq .login) && ids=$(gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$NUMBER/reviews?per_page=100" \ + | jq -r --arg owner "$owner" '.[] | select((.user.login // "") == $owner and .state == "APPROVED" and (.body // "") == "Approved via automerge.") | .id'); then + for review_id in $ids; do + gh api -X PUT "repos/$GITHUB_REPOSITORY/pulls/$NUMBER/reviews/$review_id/dismissals" \ + -f message="Automerge label removed" --silent || failed=1 + done + else + failed=1 + fi + exit "$failed" diff --git a/.github/workflows/test-rust.yml b/.github/workflows/test-rust.yml index 83b8bfb6fd..faa578d119 100644 --- a/.github/workflows/test-rust.yml +++ b/.github/workflows/test-rust.yml @@ -157,9 +157,19 @@ jobs: path: ${{ runner.temp }}/overlay-findings if-no-files-found: ignore # The command line's peak memory per workflow, as a multiple of loading alone, against - # xtask/memory-limits.txt (xtask/src/memory.rs). It runs the rustgis-cli the tests above - # built, so it costs only its own run; on Linux (GNU time), when the tests cover rustgis-cli, - # as they do whenever a package it uses changes. + # xtask/memory-limits.txt (xtask/src/memory.rs). On Linux (GNU time), when the tests cover + # rustgis-cli (as they do whenever a package it uses changes) or xtask itself (such as + # changes to memory-limits.txt or memory.rs). Reuses the rustgis-cli binary nextest built + # when the tests cover it. Otherwise (such as an xtask-only run) it builds rustgis-cli again, + # since a cached binary may be stale. - name: Peak memory - if: matrix.os == 'ubuntu-latest' && (inputs.packages == '--workspace' || contains(format('{0} ', inputs.packages), '-p rustgis-cli ')) - run: cargo xtask memory --cli target/debug/rustgis-cli + if: matrix.os == 'ubuntu-latest' && (inputs.packages == '--workspace' || contains(format('{0} ', inputs.packages), '-p rustgis-cli ') || contains(format('{0} ', inputs.packages), '-p xtask ')) + shell: bash + env: + PACKAGES: ${{ inputs.packages }} + run: | + set -euo pipefail + if [ ! -x target/debug/rustgis-cli ] || { [ "$PACKAGES" != --workspace ] && [[ " $PACKAGES " != *" -p rustgis-cli "* ]]; }; then + cargo build --locked -p rustgis-cli + fi + cargo xtask memory --cli target/debug/rustgis-cli