---
name: gitflow-pr
description: GitFlow pull request creation - fast PR with auto-description
color: purple
model: haiku
tools: Bash, Glob
---

# GitFlow Pull Request Agent

Create PR with auto-generated description, Azure DevOps and GitHub support.

> **OPTIMISED:** haiku model, reduced tools. Access to current context.

## EXECUTION SEQUENCE

### 1. Load GitFlow Config (MANDATORY)

```bash
# Find and verify config exists
CONFIG_FILE=""
if [ -f ".claude/gitflow/config.json" ]; then
  CONFIG_FILE=".claude/gitflow/config.json"
else
  # Repair cross-platform worktree paths before listing (WSL ↔ Windows)
  # Source: _shared.md DETECT_PLATFORM + REPAIR_WORKTREE_PATHS
  detect_platform
  repair_worktree_paths "$(git rev-parse --git-common-dir 2>/dev/null || echo '.bare')"

  # Try develop worktree
  DEVELOP_PATH=$(git worktree list --porcelain 2>/dev/null | grep -A1 "branch refs/heads/develop" | grep "^worktree " | sed 's/^worktree //')
  if [ -n "$DEVELOP_PATH" ] && [ -f "$DEVELOP_PATH/.claude/gitflow/config.json" ]; then
    CONFIG_FILE="$DEVELOP_PATH/.claude/gitflow/config.json"
  fi
fi

# CRITICAL: Block if no config
if [ -z "$CONFIG_FILE" ] || [ ! -f "$CONFIG_FILE" ]; then
  echo "❌ GitFlow config not found"
  echo "→ Run: /gitflow init"
  exit 1
fi

# Extract config values
GF_PROVIDER=$(grep -oP '"provider":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
GF_MAIN_BRANCH=$(grep -oP '"main":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
GF_DEVELOP_BRANCH=$(grep -oP '"develop":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
GF_REMOTE_URL=$(grep -oP '"remoteUrl":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)

# Validate required values
if [ -z "$GF_MAIN_BRANCH" ] || [ -z "$GF_DEVELOP_BRANCH" ]; then
  echo "❌ Invalid GitFlow config (missing branches)"
  exit 1
fi
```

### 2. Detect Provider and Branch Context

```bash
# Detect provider from config or remote URL
if [ -z "$GF_PROVIDER" ]; then
  REMOTE_URL=$(git remote get-url origin 2>/dev/null)
  if [[ "$REMOTE_URL" == *"dev.azure.com"* ]] || [[ "$REMOTE_URL" == *"visualstudio.com"* ]]; then
    GIT_PROVIDER="azuredevops"
  else
    GIT_PROVIDER="github"
  fi
else
  GIT_PROVIDER="$GF_PROVIDER"
fi

# Extract Azure DevOps details if needed
if [ "$GIT_PROVIDER" = "azuredevops" ]; then
  REMOTE_URL="${GF_REMOTE_URL:-$(git remote get-url origin 2>/dev/null)}"
  [[ "$REMOTE_URL" =~ dev\.azure\.com/([^/]+)/([^/]+)/_git/([^/]+) ]] && {
    AZURE_ORG="${BASH_REMATCH[1]}"
    AZURE_PROJECT="${BASH_REMATCH[2]}"
    AZURE_REPO="${BASH_REMATCH[3]}"
  }
fi

# Get current branch (worktree-compatible)
CURRENT=$(git branch --show-current 2>/dev/null || git rev-parse --abbrev-ref HEAD)
BRANCH_TYPE=$(echo $CURRENT | cut -d'/' -f1)
BRANCH_NAME=$(echo $CURRENT | sed 's/.*\///')
```

### 3. Determine Target Branch

```bash
# Map branch type to target branch (using config values)
case "$BRANCH_TYPE" in
  feature) TARGET="$GF_DEVELOP_BRANCH" ;;
  release) TARGET="$GF_MAIN_BRANCH" ;;
  hotfix)  TARGET="$GF_MAIN_BRANCH" ;;
  *)       TARGET="$GF_DEVELOP_BRANCH" ;;
esac

echo "✓ Config loaded: $CONFIG_FILE"
echo "✓ Branch: $CURRENT (type: $BRANCH_TYPE)"
echo "✓ Target: $TARGET"
```

| Type | Target | Reason |
|------|--------|--------|
| `feature/*` | develop | Continuous integration |
| `release/*` | main | Production deployment |
| `hotfix/*` | main | Urgent production fix |

### 3. Check Prerequisites

```bash
# Ensure branch is pushed
git fetch origin
REMOTE_EXISTS=$(git branch -r --list "origin/$CURRENT")
if [ -z "$REMOTE_EXISTS" ]; then
  git push -u origin $CURRENT
  git fetch origin $CURRENT:refs/remotes/origin/$CURRENT --force --quiet
fi

# Check for uncommitted changes → warn user to /gitflow commit first
# Check if PR already exists → show existing PR and STOP
```

### 4. Build Check

```bash
# .NET project
if ls *.sln 2>/dev/null || ls src/*/*.csproj 2>/dev/null; then
  dotnet build --no-restore || STOP "Build failed"

# Node.js project
elif [ -f "package.json" ]; then
  npm run build || STOP "Build failed"
fi
```

### 5. Generate PR Content

**Title:**
- feature → `feat: {branch-name}`
- hotfix → `fix: {branch-name}`
- release → `release: v{version}`

**Body:** Auto-generate from `git log origin/{TARGET}..HEAD --oneline`

### 6. Create Pull Request

**GitHub:**
```bash
gh pr create --base $TARGET --title "$TITLE" --body "$(cat <<'EOF'
{PR_BODY}
EOF
)"
PR_URL=$(gh pr view --json url --jq '.url')
```

**Azure DevOps:**
```bash
az repos pr create \
  --title "$TITLE" \
  --description "{PR_BODY}" \
  --source-branch "$CURRENT" \
  --target-branch "$TARGET" \
  --repository "$AZURE_REPO" \
  --project "$AZURE_PROJECT" \
  --org "https://dev.azure.com/$AZURE_ORG"
```

### 7. Auto-complete with squash (Azure DevOps release/hotfix)

For release/hotfix PRs on Azure DevOps, set auto-complete with squash merge:
```bash
az repos pr update --id $PR_ID --auto-complete true --squash true --delete-source-branch true
```

### 8. Summary

```
PR CREATED
  PR:     #{number}
  Title:  {title}
  URL:    {url}
  Source: {branch} -> {target}
  Provider: {github|azuredevops}
  NEXT: Wait for review, then /gitflow merge
```

## Priority

Speed > Detail. PR created quickly with proper target.
