1 #!/usr/bin/env bash
2 #
3 # Closes the issue / PR identified by GITHUB_EVENT_NUMBER.
4 #
5 # Number target is read only from GITHUB_EVENT_NUMBER, so a prompt-injection
6 # cannot redirect the close to a different issue/PR.
7 #
8 # `--workflow` selects which gh subcommand is used: `gh issue close` for
9 # triage (issues), `gh pr close` for review (PRs). `gh issue close` does
10 # not work on PRs, so this distinction matters.
11 #
12 # Issues are closed with reason "not planned".
13 #
14 # Usage:
15 # GITHUB_EVENT_NUMBER=N ./scripts/close_issue.sh --workflow <triage|review>
16 #
17
18 set -euo pipefail
19
20 WORKFLOW=""
21
22 require_value() {
23 # Treat a missing arg, an empty string, or a next token that looks
24 # like another flag (`--foo`) as a missing value so the failure points
25 # at the right argument.
26 if [[ $# -lt 2 || -z "${2:-}" || "$2" == --* ]]; then
27 echo "Error: $1 requires a value" >&2
28 exit 1
29 fi
30 }
31
32 while [[ $# -gt 0 ]]; do
33 case "$1" in
34 --workflow) require_value "$@"; WORKFLOW="$2"; shift 2 ;;
35 *)
36 echo "Error: unknown argument '$1'" >&2
37 exit 1
38 ;;
39 esac
40 done
41
42 if [[ -z "$WORKFLOW" ]]; then
43 echo "Usage: $0 --workflow <triage|review>" >&2
44 exit 1
45 fi
46
47 ISSUE="${GITHUB_EVENT_NUMBER:-}"
48 if ! [[ "$ISSUE" =~ ^[0-9]+$ ]]; then
49 echo "Error: GITHUB_EVENT_NUMBER must be a numeric issue/PR number" >&2
50 exit 1
51 fi
52
53 case "$WORKFLOW" in
54 triage)
55 gh issue close "$ISSUE" --reason "not planned"
56 echo "Closed issue #$ISSUE"
57 ;;
58 review)
59 gh pr close "$ISSUE"
60 echo "Closed PR #$ISSUE"
61 ;;
62 *)
63 echo "Error: --workflow must be 'triage' or 'review'" >&2
64 exit 1
65 ;;
66 esac