Skip to content

A research note on reading, crowding and finding, and the ten measures (closes #628) #388

A research note on reading, crowding and finding, and the ten measures (closes #628)

A research note on reading, crowding and finding, and the ten measures (closes #628) #388

name: Close issues on dev
# GitHub acts on `Closes #N` only when a pull request merges into the default
# branch, which is `main`. Work lands on `dev` first and reaches `main`
# only at release, so without this an issue stays open until the next release.
#
# When a pull request merges into `dev`, this closes every issue in
# 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.
# Two triggers, because each covers what the other cannot:
#
# - `pull_request` reads this file from the pull request itself, so it works
# on `dev` before this file has ever reached `main`. A pull request from a
# fork runs it with a read-only token, so the job skips those.
# - `pull_request_target` runs with a token that can close an issue whoever
# opened the pull request, but GitHub reads this file from the default
# branch, `main`, so it does nothing until a release carries it there. It is
# safe because no code is checked out: the job reads the pull request's text
# through the API and nothing else.
#
# Once the file is on `main`, a pull request from this repository runs both.
# The second finds its issues already closed and says so.
on:
pull_request:
types: [closed]
branches: [dev]
pull_request_target:
types: [closed]
branches: [dev]
permissions:
issues: write
pull-requests: read
jobs:
close:
name: close
if: >-
${{ github.event.pull_request.merged
&& (github.event_name == 'pull_request_target'
|| github.event.pull_request.head.repo.full_name == github.repository) }}
runs-on: ubuntu-latest
steps:
- name: Close the issues this pull request resolves
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
PR: ${{ github.event.pull_request.number }}
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."
exit 0
fi
for number in $numbers; do
# Issues and pull requests share one number space; a pull request
# named here is never closed.
# A number that names nothing is skipped. Any other failure, such
# as a rate limit, stops the run red, so it never passes for
# "nothing to close".
if ! state="$(gh api "repos/$REPO/issues/$number" \
--jq 'if .pull_request then "pull-request" else .state end' 2>&1)"; then
case "$state" in
*"HTTP 404"*) state=missing ;;
*) echo "::error::Could not read #$number: $state"; exit 1 ;;
esac
fi
case "$state" in
open)
gh issue close "$number" --repo "$REPO" --reason completed \
--comment "Closed by #$PR, merged into \`dev\`."
;;
closed) echo "#$number is already closed." ;;
*) echo "#$number is a $state, not an open issue, skipping." ;;
esac
done