Setting the file. One moment. Query Prs · GitHub PR Query · github/gh-aw · Skills DocsRaw file
query-prs.sh
Shell·154 lines·5 KB
# --jq EXPRESSION jq filter expression to apply to output (REQUIRED for data)
16#
17# Note: When --jq is not provided, returns schema and data size instead of full data.
18# This prevents overwhelming responses. Use --jq '.' to get all data.
19
20set -e
21
22# Default values
23REPO=""
24STATE="open"
25LIMIT=30
26AUTHOR=""
27APP=""
28SEARCH=""
29JQ_FILTER=""
30
31# Parse arguments
32while [[ $# -gt 0 ]]; do
33 case $1 in
34 --repo)
35 REPO="$2"
36 shift 2
37 ;;
38 --state)
39 STATE="$2"
40 shift 2
41 ;;
42 --limit)
43 LIMIT="$2"
44 shift 2
45 ;;
46 --author)
47 AUTHOR="$2"
48 shift 2
49 ;;
50 --app)
51 APP="$2"
52 shift 2
53 ;;
54 --search)
55 SEARCH="$2"
56 shift 2
57 ;;
58 --jq)
59 JQ_FILTER="$2"
60 shift 2
61 ;;
62 *)
63 echo "Unknown option: $1" >&2
64 exit 1
65 ;;
66 esac
67done
68
69# JSON fields to fetch
70JSON_FIELDS="number,title,state,author,createdAt,updatedAt,mergedAt,closedAt,headRefName,baseRefName,isDraft,reviewDecision,mergeable,mergeStateStatus,statusCheckRollup,additions,deletions,changedFiles,labels,assignees,reviewRequests,latestReviews,reviews,comments,url"
71
72# Build and execute gh command with proper quoting
73GH_ARGS=(--state "$STATE" --limit "$LIMIT" --json "$JSON_FIELDS")
74
75if [[ -n "$AUTHOR" ]]; then
76 GH_ARGS+=(--author "$AUTHOR")
77fi
78if [[ -n "$APP" ]]; then
79 GH_ARGS+=(--app "$APP")
80fi
81if [[ -n "$SEARCH" ]]; then
82 GH_ARGS+=(--search "$SEARCH")
83fi
84if [[ -n "$REPO" ]]; then
85 GH_ARGS+=(--repo "$REPO")
86fi
87OUTPUT=$(gh pr list "${GH_ARGS[@]}")
88
89# Apply jq filter if specified
90if [[ -n "$JQ_FILTER" ]]; then
91 echo "$OUTPUT" | jq "$JQ_FILTER"
92else
93 # Return schema and size instead of full data
94 ITEM_COUNT=$(jq 'length' <<< "$OUTPUT")
95 DATA_SIZE=${#OUTPUT}
96
97 # Validate values are numeric
98 if ! [[ "$ITEM_COUNT" =~ ^[0-9]+$ ]]; then
99 ITEM_COUNT=0
100 fi
101 if ! [[ "$DATA_SIZE" =~ ^[0-9]+$ ]]; then
102 DATA_SIZE=0
103 fi
104
105 cat << EOF
106{
107 "message": "No --jq filter provided. Use --jq to filter and retrieve data.",
108 "item_count": $ITEM_COUNT,
109 "data_size_bytes": $DATA_SIZE,
110 "schema": {
111 "type": "array",
112 "description": "Array of pull request objects",
113 "item_fields": {
114 "number": "integer - PR number",
115 "title": "string - PR title",
116 "state": "string - PR state (OPEN, CLOSED, MERGED)",
117 "author": "object - Author info with login field",
118 "createdAt": "string - ISO timestamp of creation",
119 "updatedAt": "string - ISO timestamp of last update",
120 "mergedAt": "string|null - ISO timestamp of merge",
121 "closedAt": "string|null - ISO timestamp of close",
122 "headRefName": "string - Source branch name",
123 "baseRefName": "string - Target branch name",
124 "isDraft": "boolean - Whether PR is a draft",
125 "reviewDecision": "string|null - Review decision (APPROVED, CHANGES_REQUESTED, REVIEW_REQUIRED)",
126 "additions": "integer - Lines added",
127 "deletions": "integer - Lines deleted",
128 "changedFiles": "integer - Number of files changed",
129 "labels": "array - Array of label objects with name field",
130 "assignees": "array - Array of assignee objects with login field",
131 "reviewRequests": "array - Array of review request objects",
132 "latestReviews": "array - Latest reviews for each PR",
133 "reviews": "array - Review objects for each PR",
134 "comments": "object - PR conversation comments summary",
135 "mergeable": "string - Mergeability status",
136 "mergeStateStatus": "string - Merge state status",
137 "statusCheckRollup": "array|null - Check run rollup data",
138 "url": "string - PR URL"
139 }
140 },
141 "suggested_queries": [
142 {"description": "Get all data", "query": "."},
143 {"description": "Get PR numbers and titles", "query": ".[] | {number, title}"},
144 {"description": "Get open PRs only", "query": ".[] | select(.state == \"OPEN\")"},
145 {"description": "Get merged PRs", "query": ".[] | select(.mergedAt != null)"},
146 {"description": "Get PRs by author", "query": ".[] | select(.author.login == \"USERNAME\")"},
147 {"description": "Get large PRs", "query": ".[] | select(.changedFiles > 10) | {number, title, changedFiles}"},
148 {"description": "Get PRs with labels", "query": ".[] | {number, title, labels: [.labels[].name]}"},
149 {"description": "Count by state", "query": "group_by(.state) | map({state: .[0].state, count: length})"},
150 {"description": "Get PRs with actions-bot reviews", "query": ".[] | {number, title, botReviewCount: ([.reviews[]? | select(.author.login == \"github-actions[bot]\")] | length)}"}
151 ]
152}
153EOF
154fi