-
Notifications
You must be signed in to change notification settings - Fork 61
feat(gh-cli): add pull request involvement report #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4989d4d
feat(gh-cli): add pull request involvement report
joshjohanning 57b791e
fix(gh-cli): address pull request report review
joshjohanning bbfa3ec
fix(gh-cli): enforce pull request group precedence
joshjohanning 433c90c
fix(gh-cli): clarify argument count errors
joshjohanning 18e0a87
fix(gh-cli): handle search API limits safely
joshjohanning a60a9d3
fix(gh-cli): reject incomplete search results
joshjohanning c6ce235
fix(gh-cli): verify retrieval result counts
joshjohanning de1c164
fix(gh-cli): validate active GitHub authentication
joshjohanning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #!/bin/bash | ||
| # Get open pull requests grouped by involvement: | ||
| # - Created by me | ||
| # - Assigned to me | ||
| # - Awaiting my review (requested reviewer) | ||
| # - Other involvement (mentioned or commented) | ||
| # | ||
| # Usage: ./get-my-pull-requests.sh [<exclude-organizations>] | ||
| # Example: ./get-my-pull-requests.sh | ||
| # Example: ./get-my-pull-requests.sh joshjohanning-org | ||
| # Example: ./get-my-pull-requests.sh joshjohanning-org,another-org | ||
| # Requires GitHub CLI authentication with access to the repositories being searched. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| echo "Usage: $0 [<exclude-organizations>]" >&2 | ||
| } | ||
|
|
||
| fail() { | ||
| echo "Error: $*" >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| search_pull_requests() { | ||
| local incomplete_results key number owner repository results retrieval_count search_metadata search_query title total_count | ||
| search_query="$1" | ||
|
|
||
| if ! search_metadata=$(gh api --method GET /search/issues \ | ||
| -f q="$search_query" \ | ||
| -F per_page=1 \ | ||
| --jq '[.total_count, .incomplete_results] | @tsv'); then | ||
| fail "Pull request search failed. Check repository access and API rate limits with: gh api rate_limit" | ||
| fi | ||
| IFS=$'\t' read -r total_count incomplete_results <<< "$search_metadata" | ||
|
|
||
| if [[ "$incomplete_results" == "true" ]]; then | ||
| fail "GitHub Search timed out while counting pull requests. Run the report again." | ||
| fi | ||
|
|
||
| if [[ "$total_count" -gt 1000 ]]; then | ||
| fail "Search matched $total_count pull requests, but GitHub Search only exposes 1,000 results. The report stopped before completion." | ||
| fi | ||
|
|
||
| if ! results=$(gh api --method GET --paginate /search/issues \ | ||
| -f q="$search_query" \ | ||
| -F per_page=100 \ | ||
| -f sort=created \ | ||
| -f order=desc \ | ||
| --jq '(["__SEARCH_STATUS__", (.incomplete_results | tostring), (.total_count | tostring)] | @tsv), | ||
| (.items[] | [(.repository_url | sub("^.*/repos/"; "")), .number, .title] | @tsv)'); then | ||
| fail "Pull request search failed. Check repository access and API rate limits with: gh api rate_limit" | ||
| fi | ||
|
|
||
| [[ -n "$results" ]] || return | ||
|
|
||
| while IFS=$'\t' read -r repository incomplete_results retrieval_count; do | ||
| if [[ "$repository" == "__SEARCH_STATUS__" ]]; then | ||
| if [[ "$incomplete_results" == "true" ]]; then | ||
| fail "GitHub Search timed out while retrieving pull requests. Run the report again." | ||
| fi | ||
| if [[ "$retrieval_count" -gt 1000 ]]; then | ||
| fail "Search grew to $retrieval_count pull requests during retrieval, but GitHub Search only exposes 1,000 results. The report stopped before completion." | ||
| fi | ||
| fi | ||
| done <<< "$results" | ||
|
|
||
| while IFS=$'\t' read -r repository number title; do | ||
| [[ "$repository" != "__SEARCH_STATUS__" ]] || continue | ||
|
|
||
| key="$repository#$number" | ||
| if [[ "$seen_pull_requests" == *$'\n'"$key"$'\n'* ]]; then | ||
| continue | ||
| fi | ||
| seen_pull_requests+="$key"$'\n' | ||
|
|
||
| owner="${repository%%/*}" | ||
| if ! is_excluded_organization "$owner"; then | ||
| printf '%s\t%s\t%s\n' "$number" "$title" "$repository" | ||
| fi | ||
| done <<< "$results" | ||
| } | ||
|
|
||
| is_excluded_organization() { | ||
| local organization owner="$1" | ||
|
|
||
| [[ -n "$exclude_orgs" ]] || return 1 | ||
|
|
||
| for organization in "${organizations[@]}"; do | ||
| if [[ "$owner" == "$organization" ]]; then | ||
| return 0 | ||
| fi | ||
| done | ||
|
|
||
| return 1 | ||
| } | ||
|
|
||
| if [[ $# -gt 1 ]]; then | ||
| echo "Error: Expected at most one argument, but received $#." >&2 | ||
| usage | ||
| exit 1 | ||
| fi | ||
| command -v gh >/dev/null 2>&1 || | ||
| fail "Required command not found: gh" | ||
|
|
||
| exclude_orgs="${1:-}" | ||
| if ! user=$(gh api user --jq '.login'); then | ||
| fail "Unable to authenticate with GitHub. Run: gh auth login" | ||
| fi | ||
|
|
||
| organizations=() | ||
| if [ -n "$exclude_orgs" ]; then | ||
| [[ "$exclude_orgs" != ,* && "$exclude_orgs" != *, && "$exclude_orgs" != *,,* ]] || | ||
| fail "Excluded organizations must be a comma-separated list without empty values." | ||
| IFS=',' read -ra organizations <<< "$exclude_orgs" | ||
| for organization in "${organizations[@]}"; do | ||
| [[ "$organization" =~ ^[A-Za-z0-9]([A-Za-z0-9-]{0,37}[A-Za-z0-9])?$ ]] || | ||
| fail "Invalid organization name: $organization" | ||
| done | ||
| fi | ||
|
|
||
| shopt -s nocasematch | ||
| seen_pull_requests=$'\n' | ||
|
|
||
| echo "Fetching open pull requests for @$user..." >&2 | ||
| echo "" >&2 | ||
|
|
||
| echo "## Created by me" | ||
| search_pull_requests "author:$user is:pr is:open" | ||
|
|
||
| echo "" >&2 | ||
| echo "## Assigned to me" | ||
| search_pull_requests "assignee:$user -author:$user is:pr is:open" | ||
|
|
||
| echo "" >&2 | ||
| echo "## Awaiting my review (requested reviewer)" | ||
| search_pull_requests "review-requested:$user -author:$user -assignee:$user is:pr is:open" | ||
|
|
||
| echo "" >&2 | ||
| echo "## Other involvement" | ||
| search_pull_requests "involves:$user -author:$user -assignee:$user -review-requested:$user is:pr is:open" | ||
|
|
||
| echo "" >&2 | ||
| echo "Done!" >&2 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.