Pr
Create PR and monitor CI. Usage: /pr [commit] to auto-commit changes
---
description: Create PR and monitor CI. Usage: /pr [commit] to auto-commit changes
---
You are a GitHub PR automation assistant.
## Parse optional instructions
If the user provided additional text after the command (e.g., `/pr "focus on auth changes"`), extract these instructions and use them as guidance throughout the process.
## Step 1: Handle uncommitted changes
If the user invoked this command with "commit" argument (e.g., `/pr commit`), then:
**First, check if we're on main/master and create a branch if needed:**
1. Get current branch:
```bash
git branch --show-current
- If on "main" or "master":
- Stage all changes first to analyze them:
git add -A- Get staged changes:
git diff --cached --stat- Generate branch name as
update-{largest-filename}where largest-filename is the file with the most changes (without extension, e.g., "update-auth" if src/auth.ts has most changes). - Create and switch to the new branch:
git checkout -b <generated-branch-name>- Display: "✓ Created and switched to branch:
"
Then proceed with commit:
- If not already staged (because we didn't create a branch above), stage all changes:
git add -A
- Get the staged changes to analyze:
git diff --cached --stat
- Analyze the staged changes and generate a meaningful commit message describing what was changed (follow normal Claude Code commit style). The message should end with:
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- Try to commit with the generated message:
git commit -m "<generated-commit-message>"
- If commit fails and the error contains "gitleaks":
- Display the full error output
- Determine if it's a false positive or a legitimate issue. If legitimate, exit immediately and instruct the user to fix the issue manually. When in doubt, ask the user for clarification.
- If it's a false positive, display: "⚠️ Gitleaks blocked commit - retrying with --no-verify" and retry with:
git commit --no-verify -m "<generated-commit-message>"
- If commit fails for other reasons:
- Display: "❌ Commit failed:"
- Display the full error output
- Exit with error
If the user did not provide "commit" argument, skip Step 1 entirely.
Step 2: Get branch info and push
Get the current branch name:
git branch --show-current
Get the main branch name from GitHub:
gh repo view --json defaultBranchRef --jq .defaultBranchRef.name 2>/dev/null || echo "main"
If the current branch is "main" or "master", exit with error message "❌ Cannot create PR from main/master. Run '/pr commit' to auto-create a branch."
Check if the branch exists on remote:
git ls-remote --heads origin <branch-name>
If the branch doesn't exist on remote, push it:
git push -u origin <branch-name>
If push fails:
- Display: "❌ Push failed (likely GitHub push protection):"
- Display the full error output
- Exit with error
Step 3: Generate PR title and description
SECURITY: Never include API keys, credentials, PII, or other sensitive information in PR titles or descriptions.
IMPORTANT: Generate intelligent 2-section PR content:
For the Changes section: Exclude testing/linting changes (e.g., "added tests", "updated eslint config", "fixed type errors") from bullets UNLESS the entire PR is about testing/linting improvements. Focus on actual feature/bug fix/refactor changes.
When in Claude Code chat:
- Analyze conversation history to understand what was implemented
- Generate concise, clear PR with:
- Summary: Non-technical overview (what/why) - 1-2 sentences
- Changes: Technical bullet list - 3-5 concise bullets
When standalone (no chat context):
- Analyze git diff and commits
- Generate based on code changes
- Keep it concise: Summary 1-2 sentences, Changes 3-5 bullets
Get the diff stats between main branch and current branch:
git diff <main-branch>...HEAD --stat
Get the list of changed files:
git diff <main-branch>...HEAD --name-only
Get the commit messages:
git log <main-branch>..HEAD --pretty=format:"%s"
Analyze the changes and generate:
- PR title: Use the first commit message (shortened to 50 chars max). If no commits, use "Update
". - PR body: Two sections only (Summary and Changes) following the length guidance above.
Step 4: Create or get PR
Try to create the PR with the generated title and body:
gh pr create --title "<title>" --body "<body>" 2>&1
If PR creation fails with "already exists" error:
- Get the existing PR URL:
gh pr view <branch-name> --json url --jq .url
If PR creation fails for other reasons:
- Display: "❌ PR creation failed:"
- Display the full error output
- Exit with error
If PR was created successfully, the URL is returned in the output.
Store the PR URL to display at the end.
Step 4b: Update existing PR description
To update an existing PR's description, use GitHub API:
gh api repos/<owner>/<repo>/pulls/<pr-number> -X PATCH -f body="<new-body>"
Step 5: Monitor CI
NOTE: You have access to all previous command outputs in conversation history - reference them directly instead of using bash variables.
IMPORTANT: Use gh run watch to monitor in-progress runs. Don't poll repeatedly with sleep - gh run watch blocks until completion.
First, sleep 10 seconds to allow CI checks to start:
sleep 10
Get workflow runs for the branch:
gh run list --branch <branch-name> --limit 10 --json name,status,conclusion,databaseId
If no runs found, wait 10 seconds and try again once.
If no runs to monitor, display the PR URL and exit successfully.
If there are in-progress runs to monitor, watch the first one (blocks until complete):
gh run
Maintain Pr?
Let people know it's listed here — add the badge (live metrics, light/dark aware) or a plain link to your README or docs.
[Pr on getagentictools](https://getagentictools.com/loops/thisisadityapatel-pr?ref=badge)