GitHub API and CLI
Overview
Managing GitHub at scale requires moving beyond the web UI to the gh CLI and the REST API. This skill focuses on high-efficiency operations like bulk querying with pagination and managing API quotas.
When to Use
- Bulk Operations: Fetching all issues or PRs across a large repository.
- CI/CD Automation: Creating issues or comments programmatically.
- Compliance/Auditing: Checking rate limits or repository permissions.
Decision Tree
- Is it a standard task (e.g., creating a PR, checking issue status)?
- YES: Use
ghCLI commands.
- YES: Use
- Do you need custom data fields or specific API endpoints?
- YES: Use
gh apiwith--jq.
- YES: Use
- Are there more than 100 results?
- YES: Use
--paginateand--slurp.
- YES: Use
Workflows
1. Bulk Querying with Pagination
- Construct a
gh apicall to a list endpoint (e.g.,repos/{owner}/{repo}/issues). - Add the
--paginateflag to ensure all pages are fetched. - Use
--slurpto group results and--jqto filter for specific fields like.[] | {title, user: .user.login}.
2. Automated Issue Creation
- Run
gh auth loginto establish credentials. - Define the issue payload using
-f title="Bug Report"and-f body=@issue_template.md. - Execute the POST request to
repos/{owner}/{repo}/issuesusing theghCLI.
3. Handling API Rate Limits
- Query the rate limit status using
gh api rate_limit. - Inspect the
X-RateLimit-Resetheader in response objects to determine when the quota refreshes. - Implement exponential backoff in scripts when a 403 Forbidden (rate limited) status is encountered.
Non-Obvious Insights
- Magic Type Conversion: In the
ghCLI, the-F/--fieldflag automatically converts literals liketrue,false,null, and integers to their JSON types. - User-Agent Requirement: All direct REST API requests MUST include a valid
User-Agentheader, or GitHub will reject them. - Path Placeholders: Using
{owner},{repo}, and{branch}ingh apiarguments automatically pulls values from the local repository context.
Evidence
- "In --paginate mode, all pages of results will sequentially be requested until there are no more pages..." - gh CLI Docs
- "All API requests must include a valid User-Agent header... Requests with no User-Agent header will be rejected." - GitHub Docs
- "Placeholder values {owner}, {repo}, and {branch} in the endpoint argument will get replaced with values from the repository..." - gh CLI Docs
Scripts
scripts/github_tool.py: Python wrapper for rate limit checking and pagination.scripts/github_tool.js: Node.js script using theghCLI for bulk querying.
Dependencies
gh(GitHub CLI)jq(Recommended for processing JSON outputs)