diff --git a/.github/workflows/close-issues-on-development.yml b/.github/workflows/close-issues-on-development.yml index b4044ce9..e2c21b9b 100644 --- a/.github/workflows/close-issues-on-development.yml +++ b/.github/workflows/close-issues-on-development.yml @@ -8,50 +8,105 @@ name: Close issues on development # this repository that the pull request's title, body, or commit messages name # with a closing keyword (close, fix, resolve and their -s/-d forms). An issue # that is already closed is left alone. +# +# It runs on the push a merge makes to `development`, then asks the API which +# pull request each merge commit belongs to. Two other triggers look closer but +# fail here: +# - Under `pull_request`, a run for a fork's pull request gets a read-only +# token, whatever `permissions:` asks for, so it cannot close anything. +# - GitHub blocks `pull_request_target` by default in public repositories +# from 2 December 2026, and reads its workflow from `main`, not +# `development`. +# A push runs with the token `permissions:` grants, from this repository's own +# copy of the workflow, for a fork's pull request and a branch's alike. +# +# The token can write issues, so the job never checks out or runs code from a +# pull request, and never expands pull-request text (title, body, commits) +# with `${{ }}`. It reads that text through the API, as plain data. on: - pull_request: - types: [closed] + push: branches: [development] permissions: + contents: read issues: write pull-requests: read jobs: close: name: close - if: ${{ github.event.pull_request.merged }} runs-on: ubuntu-latest steps: - - name: Close the issues this pull request resolves + - name: Close the issues the merged pull requests resolve env: GH_TOKEN: ${{ github.token }} REPO: ${{ github.repository }} - PR: ${{ github.event.pull_request.number }} + BEFORE: ${{ github.event.before }} + AFTER: ${{ github.event.after }} run: | - # The text is fetched here rather than interpolated into the script, - # so nothing a contributor writes in a title or body can run. - text="$(gh pr view "$PR" --repo "$REPO" --json title,body,commits \ - --jq '[.title, .body, (.commits[] | .messageHeadline, .messageBody)] | join("\n")')" - numbers="$(printf '%s\n' "$text" \ - | grep -oiE '\b(close[sd]?|fix(e[sd])?|resolve[sd]?):?[[:space:]]+#[0-9]+' \ - | grep -oE '[0-9]+$' | sort -un || true)" - if [ -z "$numbers" ]; then - echo "No closing references in #$PR." + # Called after `||`, where `set -e` is off, so every failure is + # handled by hand: one bad pull request or issue does not stop the + # others, and the run still ends failed. + close_named_issues() { + pr="$1" + status=0 + # The text is fetched here rather than interpolated into the + # script, so nothing a contributor writes in it can run. + text="$(gh pr view "$pr" --repo "$REPO" --json title,body,commits \ + --jq '[.title, .body, (.commits[] | .messageHeadline, .messageBody)] | join("\n")')" \ + || { echo "::error::Could not read #$pr."; return 1; } + numbers="$(printf '%s\n' "$text" \ + | grep -oiE '\b(close[sd]?|fix(e[sd])?|resolve[sd]?):?[[:space:]]+#[0-9]+' \ + | grep -oE '[0-9]+$' | sort -un || true)" + if [ -z "$numbers" ]; then + echo "No closing references in #$pr." + return 0 + fi + for number in $numbers; do + # Issues and pull requests share one number space; a pull + # request named here is never closed. + state="$(gh api "repos/$REPO/issues/$number" \ + --jq 'if .pull_request then "pull-request" else .state end' 2>/dev/null || echo missing)" + case "$state" in + open) + gh issue close "$number" --repo "$REPO" --reason completed \ + --comment "Closed by #$pr, merged into \`development\`." \ + || { echo "::error::Could not close #$number for #$pr."; status=1; } + ;; + closed) echo "#$number is already closed." ;; + *) echo "#$number is a $state, not an open issue, skipping." ;; + esac + done + return "$status" + } + + # A merge makes one merge commit, and one push can carry several. + # Only a merge commit (two parents) can be a pull request's merge. + merges="$(gh api --paginate "repos/$REPO/compare/$BEFORE...$AFTER?per_page=100" \ + --jq '.commits[] | select(.parents | length > 1) | .sha')" + if [ -z "$merges" ]; then + echo "No merge commits in this push." exit 0 fi - for number in $numbers; do - # Issues and pull requests share one number space; a pull request - # named here is never closed. - state="$(gh api "repos/$REPO/issues/$number" \ - --jq 'if .pull_request then "pull-request" else .state end' 2>/dev/null || echo missing)" - case "$state" in - open) - gh issue close "$number" --repo "$REPO" --reason completed \ - --comment "Closed by #$PR, merged into \`development\`." - ;; - closed) echo "#$number is already closed." ;; - *) echo "#$number is a $state, not an open issue, skipping." ;; - esac + find_pull_requests() { + gh api "repos/$REPO/commits/$1/pulls" \ + --jq ".[] | select(.merged_at != null and .merge_commit_sha == \"$1\") | .number" + } + failed=0 + for sha in $merges; do + prs="$(find_pull_requests "$sha")" + if [ -z "$prs" ] && [ "$sha" = "$AFTER" ]; then + # GitHub can take a moment to link the merge this push made. + sleep 15 + prs="$(find_pull_requests "$sha")" + fi + if [ -z "$prs" ]; then + echo "$sha is not a pull request's merge, skipping." + continue + fi + for pr in $prs; do + close_named_issues "$pr" || failed=1 + done done + exit "$failed"