| name | github-cli-patterns |
| description | Use when working with gh CLI. Provides patterns for PRs, issues, reviews, and repository operations. |
| version | 1.0.0 |
| author | JacobPEvans |
GitHub CLI Patterns
Common GitHub CLI (gh) command patterns. Single source of truth to reduce duplication across commands and agents.
PR Operations
List and View
gh pr list --state open --json number,title,headRefName
gh pr list --author @me --state open --json number,title,mergeable,statusCheckRollup
gh pr view <PR_NUMBER> --json title,body,state,mergeable
gh pr view <PR_NUMBER> --json number,title,state,mergeable,statusCheckRollup,reviews,reviewDecision,headRefName
gh pr view <PR_NUMBER> --json headRefName --jq '.headRefName'
Create
gh pr create --fill
gh pr create --draft --title "WIP: Feature" --body "Not ready for review"
gh pr create --base develop --title "Hotfix" --body "Emergency fix"
Check Status
gh pr checks <PR_NUMBER>
gh pr checks <PR_NUMBER> --watch
gh pr checks <PR_NUMBER> --watch --fail-fast
Other Operations
gh pr checkout <PR_NUMBER>
gh pr diff <PR_NUMBER>
gh pr view <PR_NUMBER> --json files --jq '.files[].path'
Issue Operations
gh issue list --state open --json number,title,labels,state,createdAt
gh issue list --search "-label:ai:created" --limit 20
gh issue view <ISSUE_NUMBER> --json title,body,labels,comments
gh issue create --title "Bug" --body "Description" --label "bug,priority:high"
gh issue create --title "..." --body "..." --label "ai:created"
gh issue close <ISSUE_NUMBER> --comment "Fixed in PR #123"
gh issue comment <ISSUE_NUMBER> --body "Comment text"
Review Operations
gh pr review <PR_NUMBER> --approve [--body TEXT]
gh pr review <PR_NUMBER> --request-changes --body "Please address..."
gh pr review <PR_NUMBER> --comment --body "Looks good overall"
gh pr view <PR_NUMBER> --json reviews,reviewDecision
gh pr comment <PR_NUMBER> --body "Comment text"
gh api repos/<OWNER>/<REPO>/pulls/<PR_NUMBER>/comments \
-f body="Comment" -F commit_id="<SHA>" -F path="file.js" -F line=42
Repository Operations
gh repo view --json nameWithOwner,description,defaultBranchRef
gh repo view --json nameWithOwner --jq '.nameWithOwner'
gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name'
gh repo list <USERNAME> --limit 100 --json name,description,updatedAt
Workflow and Check Operations
gh run list --limit 20
gh run view <RUN_ID>
gh run view <RUN_ID> --log
gh run watch <RUN_ID>
gh run rerun <RUN_ID> --failed
gh run rerun <RUN_ID>
API Operations
For GraphQL operations, use the github-graphql skill.
IMPORTANT: gh pr view --json does NOT support reviewThreads. Use GraphQL instead:
gh api graphql --raw-field 'query=query { repository(owner: "{OWNER}", name: "{REPO}") { pullRequest(number: {NUMBER}) { reviewThreads(last: 100) { ... } } } }'
gh api repos/<OWNER>/<REPO>/pulls/<PR_NUMBER>
gh api repos/<OWNER>/<REPO>/issues/<ISSUE_NUMBER>
JSON Processing
gh pr view <PR_NUMBER> --json state --jq '.state'
gh pr list --json number,title --jq '.[] | "\(.number): \(.title)"'
gh pr list --json number,state --jq '.[] | select(.state == "OPEN") | .number'
gh pr list --json number --jq '. | length'
Common Patterns
Check PR Merge-Readiness
gh pr view <PR_NUMBER> --json state,mergeable,statusCheckRollup,reviewDecision
STATE=$(gh pr view <PR_NUMBER> --json mergeable --jq '.mergeable')
if [ "$STATE" = "MERGEABLE" ]; then echo "Ready to merge"; fi
Get PR Author
gh pr view <PR_NUMBER> --json author --jq '.author.login'
Watch CI with Exit Code
gh pr checks <PR_NUMBER> --watch
if [ $? -eq 0 ]; then echo "Passed"; else echo "Failed"; fi
Authentication
gh auth status
gh auth login
Error Handling
| Error | Meaning | Solution |
|---|
HTTP 404: Not Found | PR/issue doesn't exist | Verify number |
HTTP 401: Unauthorized | Not authenticated | Run gh auth login |
HTTP 403: Forbidden | Insufficient permissions | Check repo access |
Resource not found | Invalid owner/repo | Verify repository name |
GraphQL error | Invalid query | Check syntax |
Best Practices
- Use
--json for programmatic parsing
- Use
jq for extraction, not text parsing
- Check auth before operations:
gh auth status
- Use specific field queries to reduce data transfer
- Use
--watch for long-running ops
- Handle errors with exit code checking
- Use
--limit when listing to control size
- Filter at query time when possible
Related Skills
- github-graphql - GraphQL query patterns
- pr-health-check - PR status validation
- pr-thread-resolution-enforcement - Review thread resolution