1 #!/usr/bin/env bash
2 #
3 # Fetches issues linked in a PR body via closing keywords (closes/fixes/resolves #<number>).
4 # Usage: ./scripts/get_linked_issues.sh [pr_number]
5 #
6 # If pr_number is not provided, reads from the workflow event payload.
7 # Returns a JSON array of linked issue objects, or [] if none found.
8 #
9
10 set -euo pipefail
11
12 if [[ -n "${1:-}" ]]; then
13 PR="$1"
14 else
15 PR=$(jq -r '.pull_request.number // empty' "${GITHUB_EVENT_PATH:?GITHUB_EVENT_PATH not set}")
16 fi
17
18 if ! [[ "$PR" =~ ^[0-9]+$ ]]; then
19 echo "Error: no valid PR number provided or found in event payload" >&2
20 exit 1
21 fi
22
23 # Fetch PR body
24 BODY=$(gh pr view "$PR" --json body --jq '.body // ""')
25
26 # Extract issue numbers referenced by closing keywords (case-insensitive)
27 ISSUE_NUMBERS=$(echo "$BODY" | grep -oiE '(closes|fixes|resolves)[[:space:]]+#[0-9]+' | grep -oE '[0-9]+' || true)
28
29 if [[ -z "$ISSUE_NUMBERS" ]]; then
30 echo "[]"
31 exit 0
32 fi
33
34 # Fetch details for each linked issue
35 RESULTS="["
36 FIRST=true
37 while IFS= read -r NUM; do
38 ISSUE_DATA=$(gh issue view "$NUM" --json number,title,labels,state,url 2>/dev/null || echo "")
39 if [[ -n "$ISSUE_DATA" ]]; then
40 if [[ "$FIRST" == "false" ]]; then
41 RESULTS+=","
42 fi
43 RESULTS+="$ISSUE_DATA"
44 FIRST=false
45 fi
46 done <<< "$ISSUE_NUMBERS"
47 RESULTS+="]"
48
49 echo "$RESULTS"