1 #!/usr/bin/env bash
2 #
3 # Finds OPEN pull requests that reference a given issue in their body,
4 # e.g. "closes #123", "related to #123", or a bare "#123" mention. This
5 # catches PRs that mention an issue without formally linking it via a
6 # closing keyword. Read-only.
7 #
8 # Usage: ./scripts/search_prs.sh <issue_number>
9 #
10 # GitHub's search index tokenizes numbers, so a text search for "#123"
11 # also matches PRs that merely contain the number 123 in an unrelated
12 # context (a port, a timeout, a byte count). To avoid those false
13 # positives we post-filter the search hits with a word-boundary regex,
14 # keeping only PRs whose body contains a real "#123" reference.
15 #
16 # Returns a JSON array of matching PRs (number, title, state, url,
17 # createdAt), or [] if none found.
18 #
19
20 set -euo pipefail
21
22 if [[ -z "${1:-}" ]] || ! [[ "$1" =~ ^[0-9]+$ ]]; then
23 echo "Usage: $0 <issue_number>" >&2
24 exit 1
25 fi
26
27 export ISSUE="$1"
28 REPO="${GITHUB_REPOSITORY:?GITHUB_REPOSITORY not set}"
29
30 # `#<issue>` followed by a non-digit or end-of-string, so searching #123
31 # does not match #1234. `.body` may be null on some PRs — coalesce first.
32 gh search prs "#${ISSUE}" \
33 --repo "$REPO" \
34 --state open \
35 --match body \
36 --limit 30 \
37 --json number,title,state,url,createdAt,body \
38 --jq '[ .[] | select((.body // "") | test("#" + env.ISSUE + "(\\D|$)")) | {number, title, state, url, createdAt} ]'