|
| 1 | +#!/usr/bin/env bash |
| 2 | +# close-issues-on-dev.sh — close the issues named by pull requests that merged into a branch other than |
| 3 | +# dev, once their merge commit is part of dev (see .github/workflows/close-issues-on-dev.yml). |
| 4 | +# |
| 5 | +# DRY_RUN=1 .github/scripts/close-issues-on-dev.sh # list, close nothing |
| 6 | +# |
| 7 | +# Needs: gh (authenticated; GH_TOKEN in CI), a checkout with full history, and origin/dev or HEAD on dev. |
| 8 | +# LOOKBACK: how many recently merged pull requests to consider (default 300). |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +DRY_RUN="${DRY_RUN:-0}" |
| 12 | +LOOKBACK="${LOOKBACK:-300}" |
| 13 | +REPO="${GITHUB_REPOSITORY:-$(gh repo view --json nameWithOwner -q .nameWithOwner)}" |
| 14 | +DEV_REF="${DEV_REF:-HEAD}" |
| 15 | +[ "$DEV_REF" = HEAD ] && [ "$(git rev-parse --abbrev-ref HEAD)" != dev ] && DEV_REF=origin/dev |
| 16 | + |
| 17 | +closed=0; pending=0 |
| 18 | +while IFS=$'\t' read -r pr base merge body64; do |
| 19 | + [ -n "$merge" ] || continue |
| 20 | + [ "$base" = dev ] && continue # GitHub closes these itself |
| 21 | + if ! git merge-base --is-ancestor "$merge" "$DEV_REF" 2>/dev/null; then |
| 22 | + pending=$((pending + 1)); continue # merged, but not on dev yet |
| 23 | + fi |
| 24 | + body=$(printf '%s' "$body64" | base64 --decode) |
| 25 | + for n in $(printf '%s\n' "$body" | grep -oiE '\b(close[sd]?|fix(e[sd])?|resolve[sd]?)[[:space:]]*:?[[:space:]]+#[0-9]+' \ |
| 26 | + | grep -oE '[0-9]+$' | sort -un); do |
| 27 | + state=$(gh issue view "$n" --repo "$REPO" --json state -q .state 2>/dev/null || echo MISSING) |
| 28 | + [ "$state" = OPEN ] || continue |
| 29 | + if [ "$DRY_RUN" = 1 ]; then |
| 30 | + echo "would close #$n (pull request #$pr into $base, ${merge:0:9} is on dev)" |
| 31 | + else |
| 32 | + gh issue close "$n" --repo "$REPO" \ |
| 33 | + --comment "Closed by #$pr, which merged into \`$base\` and reached \`dev\` in ${merge:0:9}." |
| 34 | + echo "closed #$n (pull request #$pr into $base)" |
| 35 | + fi |
| 36 | + closed=$((closed + 1)) |
| 37 | + done |
| 38 | +done < <(gh pr list --repo "$REPO" --state merged --limit "$LOOKBACK" \ |
| 39 | + --json number,baseRefName,mergeCommit,body \ |
| 40 | + -q '.[] | [.number, .baseRefName, (.mergeCommit.oid // ""), (.body // "" | @base64)] | @tsv') |
| 41 | + |
| 42 | +verb=closed; [ "$DRY_RUN" = 1 ] && verb="to close" |
| 43 | +echo "issues $verb: $closed; merged pull requests not on dev yet: $pending" |
0 commit comments