GitHub CLI (gh) Skill
Process GitHub-related requests using the gh CLI tool.
Quick Start
# View issue
gh issue view 12345 --repo owner/repo
# View PR
gh pr view 67890 --repo owner/repo
# View PR with details
gh pr view 67890 --repo owner/repo --json title,body,author,state,additions,deletions
Instructions
1. Parse GitHub URLs
Extract components from GitHub URLs:
| URL Pattern | Components |
|---|---|
https://github.com/owner/repo |
owner, repo |
https://github.com/owner/repo/issues/123 |
owner, repo, issue=123 |
https://github.com/owner/repo/pull/456 |
owner, repo, pr=456 |
https://github.com/owner/repo/blob/main/path/file.py |
owner, repo, branch, file path |
https://github.com/owner/repo/tree/main/path |
owner, repo, branch, directory |
Parse with:
# Extract from URL
echo "https://github.com/pytorch/pytorch/issues/12345" | sed -E 's|https://github.com/([^/]+)/([^/]+)/(issues\|pull)/([0-9]+)|\1 \2 \3 \4|'
2. Fetch Issue Information
# Basic view
gh issue view <number> --repo <owner>/<repo>
# JSON output for parsing
gh issue view <number> --repo <owner>/<repo> --json title,body,author,state,labels,comments
# With comments
gh issue view <number> --repo <owner>/<repo> --comments
3. Fetch Pull Request Information
# Basic view
gh pr view <number> --repo <owner>/<repo>
# Detailed JSON
gh pr view <number> --repo <owner>/<repo> --json title,body,author,state,additions,deletions,files,commits,reviews
# View diff
gh pr diff <number> --repo <owner>/<repo>
# View checks
gh pr checks <number> --repo <owner>/<repo>
# View merge status
gh pr view <number> --repo <owner>/<repo> --json mergeable,mergeStateStatus
4. Fetch Repository Information
# Repo info
gh repo view <owner>/<repo>
# Repo topics/description
gh repo view <owner>/<repo> --json name,description,topics,stargazerCount
# List issues
gh issue list --repo <owner>/<repo> --state open --limit 20
# List PRs
gh pr list --repo <owner>/<repo> --state open --limit 20
5. Handle API Rate Limits
# Check rate limit
gh api rate_limit
# Use --paginate for large result sets
gh issue list --repo <owner>/<repo> --state all --paginate
6. Advanced API Access
# Direct API call
gh api repos/<owner>/<repo>/issues/<number>
# GraphQL query
gh api graphql -f query='
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
issue(number: $number) {
title
body
author { login }
}
}
}
' -f owner=<owner> -f repo=<repo> -F number=<number>
Common Workflows
Triage an Issue
- Parse URL to get owner/repo/number
- Fetch issue details:
gh issue view <number> --repo <owner>/<repo> --json title,body,author,state,labels,comments - Analyze and provide summary
Review a PR
- Parse URL to get owner/repo/number
- Fetch PR details:
gh pr view <number> --repo <owner>/<repo> --json title,body,author,state,files,additions,deletions - Optionally fetch diff:
gh pr diff <number> --repo <owner>/<repo> - Review and provide feedback
Check CI Status
gh pr checks <number> --repo <owner>/<repo> --watch
Best Practices
- Use
--jsonfor structured output when processing data programmatically - Use
--jqto filter JSON output:gh issue view 123 --json title,body | jq '.title' - Default to
--repo <owner>/<repo>for clarity in multi-repo contexts - Check
gh auth statusif commands fail with auth errors - Use
gh apifor endpoints not covered by convenience commands
Error Handling
# Check if gh is installed
if ! command -v gh &> /dev/null; then
echo "gh CLI not installed. Install from: https://cli.github.com/"
exit 1
fi
# Check authentication
gh auth status
# Handle missing resources
gh issue view 999999 --repo owner/repo 2>&1 | grep -q "not found" && echo "Issue not found"
Reference
For complete command reference:
gh help
gh issue --help
gh pr --help
gh api --help