1 #!/usr/bin/env bash
2 #
3 # Manages labels on a GitHub issue / PR with a hardcoded allowlist.
4 #
5 # Usage:
6 # GITHUB_EVENT_NUMBER=123 ./scripts/labels.sh \
7 # --workflow <triage|review> \
8 # --action <add|remove> \
9 # --label <label>
10 #
11 # The issue / PR number is pinned to GITHUB_EVENT_NUMBER so a prompt-injection
12 # cannot redirect label changes to a different issue.
13 #
14 # Only labels in the hardcoded allowlist for the given workflow are accepted.
15 # Anything else is rejected (not silently dropped) so misuse surfaces in logs.
16 #
17
18 set -euo pipefail
19
20 WORKFLOW=""
21 ACTION=""
22 LABEL=""
23
24 require_value() {
25 if [[ $# -lt 2 || -z "${2:-}" || "$2" == --* ]]; then
26 echo "Error: $1 requires a value" >&2
27 exit 1
28 fi
29 }
30
31 while [[ $# -gt 0 ]]; do
32 case "$1" in
33 --workflow) require_value "$@"; WORKFLOW="$2"; shift 2 ;;
34 --action) require_value "$@"; ACTION="$2"; shift 2 ;;
35 --label) require_value "$@"; LABEL="$2"; shift 2 ;;
36 *)
37 echo "Error: unknown argument '$1'" >&2
38 exit 1
39 ;;
40 esac
41 done
42
43 if [[ -z "$WORKFLOW" || -z "$ACTION" || -z "$LABEL" ]]; then
44 echo "Usage: $0 --workflow <triage|review> --action <add|remove> --label <label>" >&2
45 exit 1
46 fi
47
48 case "$ACTION" in
49 add|remove) ;;
50 *)
51 echo "Error: --action must be 'add' or 'remove'" >&2
52 exit 1
53 ;;
54 esac
55
56 ISSUE="${GITHUB_EVENT_NUMBER:-}"
57 if ! [[ "$ISSUE" =~ ^[0-9]+$ ]]; then
58 echo "Error: GITHUB_EVENT_NUMBER must be a numeric issue/PR number" >&2
59 exit 1
60 fi
61
62 # Hardcoded per-workflow allowlists.
63 TRIAGE_LABELS=("type: bug" "requires-more" "requires-team" "help-wanted" "good first issue" "feedback")
64 REVIEW_LABELS=("initial-approval" "requires-more" "requires-team")
65
66 case "$WORKFLOW" in
67 triage) ALLOWED=("${TRIAGE_LABELS[@]}") ;;
68 review) ALLOWED=("${REVIEW_LABELS[@]}") ;;
69 *)
70 echo "Error: --workflow must be 'triage' or 'review'" >&2
71 exit 1
72 ;;
73 esac
74
75 OK=0
76 for L in "${ALLOWED[@]}"; do
77 if [[ "$L" == "$LABEL" ]]; then OK=1; break; fi
78 done
79 if [[ "$OK" -ne 1 ]]; then
80 echo "Error: label '$LABEL' is not in the $WORKFLOW allowlist" >&2
81 exit 1
82 fi
83
84 # The hardcoded allowlist controls what the agent is permitted to ask
85 # for, but the repo may not have every allowlisted label defined yet.
86 # `gh issue edit --add-label` fails hard on unknown labels, which would
87 # break the whole apply step over one missing label. Skip with a warning
88 # instead so the rest of the decision still applies.
89 if ! gh label list --limit 500 --json name --jq '.[].name' | grep -qxF "$LABEL"; then
90 echo "Warning: label '$LABEL' is not defined in this repo; skipping $ACTION." >&2
91 exit 0
92 fi
93
94 if [[ "$ACTION" == "add" ]]; then
95 gh issue edit "$ISSUE" --add-label "$LABEL"
96 echo "Added label '$LABEL' to #$ISSUE"
97 else
98 gh issue edit "$ISSUE" --remove-label "$LABEL"
99 echo "Removed label '$LABEL' from #$ISSUE"
100 fi