---
name: github-pr
description: Create GitHub PR using GitHub App authentication. Use when user asks to create/submit PR.
---

# GitHub PR Creation Skill

This skill automates PR creation using GitHub App authentication when gh CLI is not logged in.

## When to Use

- User asks to "create PR", "submit PR", "提 PR"
- gh CLI returns "You are not logged into any GitHub hosts"

## Prerequisites

Ensure these env vars are set:
- `GITHUB_APP_PRIVATE_KEY_PATH`: Path to PEM file (e.g., `~/.github/data-service-code-assist.pem`)
- `GITHUB_APP_ID`: GitHub App ID
- `GITHUB_APP_INSTALLATION_ID`: Installation ID

## Process

### Step 1: Generate GitHub App Token

```python
import jwt, time, requests, os

private_key_path = os.environ.get("GITHUB_APP_PRIVATE_KEY_PATH", "~/.github/data-service-code-assist.pem")
app_id = os.environ.get("GITHUB_APP_ID")
installation_id = os.environ.get("GITHUB_APP_INSTALLATION_ID")

with open(os.path.expanduser(private_key_path)) as f:
    private_key = f.read()

payload = {
    "iss": app_id,
    "iat": int(time.time()),
    "exp": int(time.time()) + 600
}
jwt_token = jwt.encode(payload, private_key, algorithm="RS256")

resp = requests.post(
    f"https://api.github.com/app/installations/{installation_id}/access_tokens",
    headers={
        "Authorization": f"Bearer {jwt_token}",
        "Accept": "application/vnd.github+json"
    }
)
token = resp.json().get("token")
print(token)
```

### Step 2: Create PR

```bash
export GH_TOKEN="<token>"
cd ~/github/data-service-infra-ai-configurations

# Ensure branch is pushed
git push origin <branch>

# Create PR
gh pr create --base main --head <branch> --title "..." --body "..."
```

## Error Handling

- If "Head sha can't be blank": Push branch first with `git push origin <branch>`
- If branch already has PR: Use `gh pr view` to check status
- If no changes to commit: Check `git status` first
