Skip to content

Commit b6d2955

Browse files
feat(gh-cli): add pull request involvement report (#171)
* feat(gh-cli): add pull request involvement report Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(gh-cli): address pull request report review Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(gh-cli): enforce pull request group precedence Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(gh-cli): clarify argument count errors Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(gh-cli): handle search API limits safely Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(gh-cli): reject incomplete search results Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(gh-cli): verify retrieval result counts Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(gh-cli): validate active GitHub authentication Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3c0b217 commit b6d2955

2 files changed

Lines changed: 166 additions & 0 deletions

File tree

‎gh-cli/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,28 @@ Returns the most recent migration ID for a given organization.
990990

991991
Returns the most recent migration ID for a given organization repository.
992992

993+
### get-my-pull-requests.sh
994+
995+
Gets open pull requests grouped by your involvement: created by you, assigned to you, awaiting your review, and other involvement. Groups are mutually exclusive using the precedence shown below. Optionally excludes one or more organizations.
996+
997+
Usage:
998+
999+
```shell
1000+
./get-my-pull-requests.sh
1001+
./get-my-pull-requests.sh joshjohanning-org
1002+
./get-my-pull-requests.sh joshjohanning-org,another-org
1003+
```
1004+
1005+
Output groups include:
1006+
1007+
- Created by me - PRs you authored
1008+
- Assigned to me - PRs assigned to you
1009+
- Awaiting my review - PRs requesting your review
1010+
- Other involvement - remaining PRs you were mentioned in or commented on
1011+
1012+
> [!NOTE]
1013+
> Requires GitHub CLI authentication with access to the repositories being searched. Uses the search API and exits with an error if results are incomplete or any group exceeds the API's 1,000-result limit.
1014+
9931015
### get-organization-active-repositories.sh
9941016

9951017
Gets a list of repositories in an organization that have had code pushed to it in the last X days.

‎gh-cli/get-my-pull-requests.sh‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/bin/bash
2+
# Get open pull requests grouped by involvement:
3+
# - Created by me
4+
# - Assigned to me
5+
# - Awaiting my review (requested reviewer)
6+
# - Other involvement (mentioned or commented)
7+
#
8+
# Usage: ./get-my-pull-requests.sh [<exclude-organizations>]
9+
# Example: ./get-my-pull-requests.sh
10+
# Example: ./get-my-pull-requests.sh joshjohanning-org
11+
# Example: ./get-my-pull-requests.sh joshjohanning-org,another-org
12+
# Requires GitHub CLI authentication with access to the repositories being searched.
13+
14+
set -euo pipefail
15+
16+
usage() {
17+
echo "Usage: $0 [<exclude-organizations>]" >&2
18+
}
19+
20+
fail() {
21+
echo "Error: $*" >&2
22+
exit 1
23+
}
24+
25+
search_pull_requests() {
26+
local incomplete_results key number owner repository results retrieval_count search_metadata search_query title total_count
27+
search_query="$1"
28+
29+
if ! search_metadata=$(gh api --method GET /search/issues \
30+
-f q="$search_query" \
31+
-F per_page=1 \
32+
--jq '[.total_count, .incomplete_results] | @tsv'); then
33+
fail "Pull request search failed. Check repository access and API rate limits with: gh api rate_limit"
34+
fi
35+
IFS=$'\t' read -r total_count incomplete_results <<< "$search_metadata"
36+
37+
if [[ "$incomplete_results" == "true" ]]; then
38+
fail "GitHub Search timed out while counting pull requests. Run the report again."
39+
fi
40+
41+
if [[ "$total_count" -gt 1000 ]]; then
42+
fail "Search matched $total_count pull requests, but GitHub Search only exposes 1,000 results. The report stopped before completion."
43+
fi
44+
45+
if ! results=$(gh api --method GET --paginate /search/issues \
46+
-f q="$search_query" \
47+
-F per_page=100 \
48+
-f sort=created \
49+
-f order=desc \
50+
--jq '(["__SEARCH_STATUS__", (.incomplete_results | tostring), (.total_count | tostring)] | @tsv),
51+
(.items[] | [(.repository_url | sub("^.*/repos/"; "")), .number, .title] | @tsv)'); then
52+
fail "Pull request search failed. Check repository access and API rate limits with: gh api rate_limit"
53+
fi
54+
55+
[[ -n "$results" ]] || return
56+
57+
while IFS=$'\t' read -r repository incomplete_results retrieval_count; do
58+
if [[ "$repository" == "__SEARCH_STATUS__" ]]; then
59+
if [[ "$incomplete_results" == "true" ]]; then
60+
fail "GitHub Search timed out while retrieving pull requests. Run the report again."
61+
fi
62+
if [[ "$retrieval_count" -gt 1000 ]]; then
63+
fail "Search grew to $retrieval_count pull requests during retrieval, but GitHub Search only exposes 1,000 results. The report stopped before completion."
64+
fi
65+
fi
66+
done <<< "$results"
67+
68+
while IFS=$'\t' read -r repository number title; do
69+
[[ "$repository" != "__SEARCH_STATUS__" ]] || continue
70+
71+
key="$repository#$number"
72+
if [[ "$seen_pull_requests" == *$'\n'"$key"$'\n'* ]]; then
73+
continue
74+
fi
75+
seen_pull_requests+="$key"$'\n'
76+
77+
owner="${repository%%/*}"
78+
if ! is_excluded_organization "$owner"; then
79+
printf '%s\t%s\t%s\n' "$number" "$title" "$repository"
80+
fi
81+
done <<< "$results"
82+
}
83+
84+
is_excluded_organization() {
85+
local organization owner="$1"
86+
87+
[[ -n "$exclude_orgs" ]] || return 1
88+
89+
for organization in "${organizations[@]}"; do
90+
if [[ "$owner" == "$organization" ]]; then
91+
return 0
92+
fi
93+
done
94+
95+
return 1
96+
}
97+
98+
if [[ $# -gt 1 ]]; then
99+
echo "Error: Expected at most one argument, but received $#." >&2
100+
usage
101+
exit 1
102+
fi
103+
command -v gh >/dev/null 2>&1 ||
104+
fail "Required command not found: gh"
105+
106+
exclude_orgs="${1:-}"
107+
if ! user=$(gh api user --jq '.login'); then
108+
fail "Unable to authenticate with GitHub. Run: gh auth login"
109+
fi
110+
111+
organizations=()
112+
if [ -n "$exclude_orgs" ]; then
113+
[[ "$exclude_orgs" != ,* && "$exclude_orgs" != *, && "$exclude_orgs" != *,,* ]] ||
114+
fail "Excluded organizations must be a comma-separated list without empty values."
115+
IFS=',' read -ra organizations <<< "$exclude_orgs"
116+
for organization in "${organizations[@]}"; do
117+
[[ "$organization" =~ ^[A-Za-z0-9]([A-Za-z0-9-]{0,37}[A-Za-z0-9])?$ ]] ||
118+
fail "Invalid organization name: $organization"
119+
done
120+
fi
121+
122+
shopt -s nocasematch
123+
seen_pull_requests=$'\n'
124+
125+
echo "Fetching open pull requests for @$user..." >&2
126+
echo "" >&2
127+
128+
echo "## Created by me"
129+
search_pull_requests "author:$user is:pr is:open"
130+
131+
echo "" >&2
132+
echo "## Assigned to me"
133+
search_pull_requests "assignee:$user -author:$user is:pr is:open"
134+
135+
echo "" >&2
136+
echo "## Awaiting my review (requested reviewer)"
137+
search_pull_requests "review-requested:$user -author:$user -assignee:$user is:pr is:open"
138+
139+
echo "" >&2
140+
echo "## Other involvement"
141+
search_pull_requests "involves:$user -author:$user -assignee:$user -review-requested:$user is:pr is:open"
142+
143+
echo "" >&2
144+
echo "Done!" >&2

0 commit comments

Comments
 (0)