Get the FREE Ultimate OpenClaw Setup Guide →

github-cli-patterns

npx machina-cli add skill JacobPEvans/ai-assistant-instructions/github-cli-patterns --openclaw
Files (1)
SKILL.md
6.0 KB

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

# List: gh pr list [--state STATE] [--author USER] [--label LABEL] [--limit N] [--json FIELDS]
gh pr list --state open --json number,title,headRefName
gh pr list --author @me --state open --json number,title,mergeable,statusCheckRollup

# View: gh pr view <PR_NUMBER> [--json FIELDS]
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'  # Get branch name

Create

# Template: gh pr create [--title TEXT] [--body TEXT] [--draft] [--base BRANCH]
gh pr create --fill  # Auto-fill from commits
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>  # View status
gh pr checks <PR_NUMBER> --watch  # Wait for completion
gh pr checks <PR_NUMBER> --watch --fail-fast  # Exit on first failure

Other Operations

gh pr checkout <PR_NUMBER>  # Checkout locally
gh pr diff <PR_NUMBER>  # Show changes
gh pr view <PR_NUMBER> --json files --jq '.files[].path'  # Get changed files

Issue Operations

# List: gh issue list [--state STATE] [--label LABEL] [--assignee USER] [--limit N] [--json FIELDS]
gh issue list --state open --json number,title,labels,state,createdAt
gh issue list --search "-label:ai:created" --limit 20  # Exclude AI-created

# View: gh issue view <ISSUE_NUMBER> [--json FIELDS]
gh issue view <ISSUE_NUMBER> --json title,body,labels,comments

# Create: gh issue create [--title TEXT] [--body TEXT] [--label LABELS]
gh issue create --title "Bug" --body "Description" --label "bug,priority:high"
gh issue create --title "..." --body "..." --label "ai:created"  # AI-created (requires human review)

# Close: gh issue close <ISSUE_NUMBER> [--comment TEXT]
gh issue close <ISSUE_NUMBER> --comment "Fixed in PR #123"

# Comment
gh issue comment <ISSUE_NUMBER> --body "Comment text"

Review Operations

# Submit review
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"

# View reviews
gh pr view <PR_NUMBER> --json reviews,reviewDecision

# Add comment
gh pr comment <PR_NUMBER> --body "Comment text"

# Line-level comments (via API)
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

# View current repo
gh repo view --json nameWithOwner,description,defaultBranchRef
gh repo view --json nameWithOwner --jq '.nameWithOwner'  # Get owner/repo
gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name'  # Get default branch

# List repos
gh repo list <USERNAME> --limit 100 --json name,description,updatedAt

Workflow and Check Operations

# List runs
gh run list --limit 20

# View run
gh run view <RUN_ID>  # Details
gh run view <RUN_ID> --log  # Logs
gh run watch <RUN_ID>  # Wait for completion

# Rerun
gh run rerun <RUN_ID> --failed  # Failed jobs only
gh run rerun <RUN_ID>  # Entire workflow

API Operations

For GraphQL operations, use the github-graphql skill.

IMPORTANT: gh pr view --json does NOT support reviewThreads. Use GraphQL instead:

# GraphQL (required for reviewThreads) - see github-graphql skill for full patterns
gh api graphql --raw-field 'query=query { repository(owner: "{OWNER}", name: "{REPO}") { pullRequest(number: {NUMBER}) { reviewThreads(last: 100) { ... } } } }'

# REST API
gh api repos/<OWNER>/<REPO>/pulls/<PR_NUMBER>
gh api repos/<OWNER>/<REPO>/issues/<ISSUE_NUMBER>

JSON Processing

# Extract field: --jq '.field'
gh pr view <PR_NUMBER> --json state --jq '.state'

# Extract array: --jq '.[] | "\(.field1): \(.field2)"'
gh pr list --json number,title --jq '.[] | "\(.number): \(.title)"'

# Filter: --jq '.[] | select(.field == "VALUE") | .field'
gh pr list --json number,state --jq '.[] | select(.state == "OPEN") | .number'

# Count: --jq '. | length'
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  # Check auth (safe for automation)
gh auth login  # Login interactively

Error Handling

ErrorMeaningSolution
HTTP 404: Not FoundPR/issue doesn't existVerify number
HTTP 401: UnauthorizedNot authenticatedRun gh auth login
HTTP 403: ForbiddenInsufficient permissionsCheck repo access
Resource not foundInvalid owner/repoVerify repository name
GraphQL errorInvalid queryCheck syntax

Best Practices

  1. Use --json for programmatic parsing
  2. Use jq for extraction, not text parsing
  3. Check auth before operations: gh auth status
  4. Use specific field queries to reduce data transfer
  5. Use --watch for long-running ops
  6. Handle errors with exit code checking
  7. Use --limit when listing to control size
  8. 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

Source

git clone https://github.com/JacobPEvans/ai-assistant-instructions/blob/main/agentsmd/skills/github-cli-patterns/SKILL.mdView on GitHub

Overview

GitHub CLI Patterns provides standardized gh command templates for PRs, issues, reviews, and repository operations. It acts as a single source of truth to reduce duplication across agents and workflows.

How This Skill Works

The skill offers concrete gh command templates for common tasks (list, view, create, review, and repo operations) and includes notes when GraphQL is needed via gh api. Patterns cover PRs, issues, reviews, runs, and repository queries, enabling agents to reuse proven commands.

When to Use It

  • Automating PR discovery and status checks with gh pr list and gh pr view
  • Creating or filling PRs quickly with gh pr create --fill and templates
  • Reviewing PRs and fetching status with gh pr review and gh pr view --json
  • Managing issues: list, view, create, close, and comment
  • Inspecting repositories and workflows with gh repo and gh run commands

Quick Start

  1. Step 1: Install gh CLI and confirm it is on a recent version
  2. Step 2: Save or copy the patterns into a central reference document
  3. Step 3: Run example commands from the patterns to validate behavior

Best Practices

  • Use --json with --jq to extract structured fields
  • Use gh pr create --fill to auto-fill templates
  • Keep all gh command patterns in a single authoritative file or doc
  • Be precise with --body and --title to improve clarity in reviews
  • For advanced fields like reviewThreads, use GraphQL via gh api

Example Use Cases

  • gh pr list --state open --json number,title,headRefName
  • gh pr create --fill --title "WIP: Feature" --body "Not ready for review"
  • gh issue create --title "Bug" --body "Description" --label "bug,priority:high"
  • gh pr review 42 --approve --body "Looks good"
  • gh api graphql --raw-field 'query { repository(owner: "OWNER", name: "REPO") { pullRequest(number: 1) { id } } }'

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers