---
description: Create comprehensive pull requests with all necessary context
argument-hint: <target branch>
---

# /eg:pr

You are tasked with creating comprehensive pull requests with detailed descriptions including problem context, solution approach, testing performed, and review checklist.

Input: `$ARGUMENTS`

## Step 1: Parse Target Branch

If argument provided, it's the target branch. Otherwise default to main/master.

## Step 2: Pre-flight Checks

Verify PR can be created:

```
!git branch --show-current
!git status --porcelain
```

Check for uncommitted changes:
```
!if [ -n "$(git status --porcelain)" ]; then
  echo "Warning: Uncommitted changes detected. Commit or stash them first."
fi
```

Check if branch exists on remote:
```
!git ls-remote origin $(git branch --show-current) >/dev/null 2>&1 || echo "Branch not pushed yet"
```

## Step 3: Gather PR Context

Analyze changes for the PR:

```
# Get commits not in target branch
!git log origin/{target-branch}..HEAD --oneline

# Get file changes
!git diff origin/{target-branch}...HEAD --stat

# Get detailed diff (limited)
!git diff origin/{target-branch}...HEAD | head -1000

# Extract issue references from commits
!git log origin/{target-branch}..HEAD --grep="#[0-9]" --oneline
```

## Step 4: Generate PR Content

Use the Task tool to create PR description:

```
Task(
  description="Generate PR description",
  prompt=`
    Create a comprehensive pull request description.
    
    Branch: {current-branch} → {target-branch}
    Commits: {commit-list}
    Files changed: {file-stats}
    
    Create PR description including:
    
    ## Problem
    What issue or need this PR addresses
    
    ## Solution  
    Overview of the implementation approach
    
    ## Changes
    - Key changes grouped by component/area
    - Technical decisions made
    - Any trade-offs considered
    
    ## Testing
    - Unit tests added/updated
    - Integration tests performed
    - Manual testing steps
    - Test coverage metrics
    
    ## Screenshots
    (If UI changes) Before/after screenshots
    
    ## Checklist
    - [ ] Tests pass locally
    - [ ] Documentation updated
    - [ ] No console.logs or debug code
    - [ ] Follows project conventions
    - [ ] Self-reviewed the changes
    
    ## Breaking Changes
    Any backwards compatibility issues
    
    ## Migration Guide
    If breaking changes, how to migrate
    
    Include issue references like "Fixes #123" or "Relates to #456"
  `,
  subagent_type="summarizer"
)
```

## Step 5: Push Branch if Needed

```
!if ! git ls-remote origin $(git branch --show-current) >/dev/null 2>&1; then
  echo "Pushing branch to origin..."
  git push -u origin $(git branch --show-current)
fi
```

## Step 6: Create Pull Request

Create the PR using GitHub CLI:

```
!gh pr create \
  --title "{pr-title}" \
  --body "{pr-description}" \
  --base {target-branch} \
  --head $(git branch --show-current)
```

## Step 7: Post-Creation

After PR is created:

```
# Get PR URL
!gh pr view --json url -q .url

# Suggest reviewers based on CODEOWNERS
!if [ -f ".github/CODEOWNERS" ]; then
  echo "Suggested reviewers from CODEOWNERS:"
  cat .github/CODEOWNERS | grep -v "^#" | grep -v "^$"
fi
```

## Next Steps

Provide:
- PR URL for easy access
- Reminder to wait for CI checks
- Suggestion to notify relevant reviewers
- Any manual testing instructions

The PR should have all context needed for an effective review.