|
| 1 | +# Commit as Pull Request |
| 2 | + |
| 3 | +You are an automated assistant that takes the current uncommitted changes in the workspace, creates a branch, commits, pushes, opens a pull request, merges it, and syncs the local `main` branch. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- The workspace must be a git repository with a configured remote named `origin`. |
| 8 | +- There must be uncommitted changes (staged or unstaged) in the working tree. |
| 9 | +- The GitHub MCP tools must be available for creating and merging pull requests. |
| 10 | + |
| 11 | +## Workflow |
| 12 | + |
| 13 | +Execute the following steps **in order**. Stop immediately if any step fails. |
| 14 | + |
| 15 | +### Step 1: Verify there are changes to commit |
| 16 | + |
| 17 | +Run `git status --porcelain` to confirm there are uncommitted changes. If the output is empty, inform the user there is nothing to commit and stop. |
| 18 | + |
| 19 | +### Step 2: Determine the repository owner and name |
| 20 | + |
| 21 | +Read the repository remote URL to extract the GitHub `owner` and `repo`: |
| 22 | + |
| 23 | +```bash |
| 24 | +git remote get-url origin |
| 25 | +``` |
| 26 | + |
| 27 | +Parse the owner and repo from the URL (handles both HTTPS and SSH formats). |
| 28 | + |
| 29 | +### Step 3: Auto-detect branch name and commit message |
| 30 | + |
| 31 | +Analyze the changed files using `git diff` (and `git diff --cached` for staged changes) to understand what was modified. Generate: |
| 32 | + |
| 33 | +- **Branch name**: A short, kebab-case branch name prefixed with an appropriate category (`fix/`, `feat/`, `docs/`, `refactor/`, `chore/`). Example: `fix/cliurl-auto-correct-usestdio`. |
| 34 | +- **Commit message**: A clear, descriptive commit message following the project conventions: |
| 35 | + - First line: imperative verb, under 72 characters (e.g., "Fix cliUrl to auto-correct useStdio") |
| 36 | + - Body (if needed): explain *why* the change was made |
| 37 | + |
| 38 | +If the user has provided an explicit branch name or commit message, use those instead. |
| 39 | + |
| 40 | +### Step 4: Run code formatter |
| 41 | + |
| 42 | +If the project has a formatter configured, run it before committing: |
| 43 | + |
| 44 | +```bash |
| 45 | +mvn spotless:apply |
| 46 | +``` |
| 47 | + |
| 48 | +Only run this if a `pom.xml` exists with Spotless configured. Skip for non-Maven projects. |
| 49 | + |
| 50 | +### Step 5: Create branch, stage, and commit |
| 51 | + |
| 52 | +```bash |
| 53 | +git checkout -b <branch-name> |
| 54 | +git add -A |
| 55 | +git commit -m "<commit-message>" |
| 56 | +``` |
| 57 | + |
| 58 | +### Step 6: Push the branch |
| 59 | + |
| 60 | +```bash |
| 61 | +git push -u origin <branch-name> |
| 62 | +``` |
| 63 | + |
| 64 | +If the push reports "Everything up-to-date", verify with `git log --oneline -1` that the commit exists, then retry with `git push -u origin <branch-name> 2>&1`. |
| 65 | + |
| 66 | +### Step 7: Create a pull request |
| 67 | + |
| 68 | +Use the GitHub MCP `create_pull_request` tool with: |
| 69 | + |
| 70 | +- **owner** and **repo**: from Step 2 |
| 71 | +- **title**: the first line of the commit message |
| 72 | +- **head**: the branch name |
| 73 | +- **base**: `main` (or the repository's default branch) |
| 74 | +- **body**: A well-structured PR description including: |
| 75 | + - **Summary**: What the change does and why |
| 76 | + - **Changes**: Bullet list of files/areas modified |
| 77 | + - **Testing**: How the changes were verified |
| 78 | + |
| 79 | +### Step 8: Merge the pull request |
| 80 | + |
| 81 | +Use the GitHub MCP `merge_pull_request` tool with: |
| 82 | + |
| 83 | +- **merge_method**: `squash` |
| 84 | +- **commit_title**: `<PR title> (#<PR number>)` |
| 85 | + |
| 86 | +### Step 9: Sync local main |
| 87 | + |
| 88 | +```bash |
| 89 | +git checkout main |
| 90 | +git pull |
| 91 | +``` |
| 92 | + |
| 93 | +### Step 10: Clean up the local branch (optional) |
| 94 | + |
| 95 | +```bash |
| 96 | +git branch -d <branch-name> |
| 97 | +``` |
| 98 | + |
| 99 | +## Error Handling |
| 100 | + |
| 101 | +- If the branch name already exists, append a numeric suffix (e.g., `fix/my-change-2`). |
| 102 | +- If the push fails due to authentication, inform the user and stop. |
| 103 | +- If the PR creation fails, provide the error and stop. |
| 104 | +- If the merge fails (e.g., merge conflicts, required checks), inform the user and leave the PR open. |
| 105 | + |
| 106 | +## Output |
| 107 | + |
| 108 | +After completion, provide a brief summary: |
| 109 | + |
| 110 | +1. Branch name |
| 111 | +2. PR URL and number |
| 112 | +3. Merge commit SHA |
| 113 | +4. Confirmation that local `main` is up to date |
0 commit comments