|
| 1 | +--- |
| 2 | +agent: agent |
| 3 | +--- |
| 4 | + |
| 5 | +# Save Branch to PR Workflow |
| 6 | + |
| 7 | +This prompt guides through the complete workflow of saving a feature branch by creating a PR, waiting for checks, merging, and cleaning up. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | +- Git repository with GitHub remote |
| 11 | +- GitHub CLI (`gh`) installed and authenticated |
| 12 | +- Current branch should be a feature branch (not `main`) |
| 13 | + |
| 14 | +## Workflow Steps |
| 15 | + |
| 16 | +### Step 1: Verify Current Branch |
| 17 | +Check that we are NOT on the `main` branch. If on `main`, stop and inform the user. |
| 18 | + |
| 19 | +```powershell |
| 20 | +$currentBranch = git branch --show-current |
| 21 | +if ($currentBranch -eq "main") { |
| 22 | + Write-Error "Cannot run this workflow from the main branch. Please checkout a feature branch first." |
| 23 | + return |
| 24 | +} |
| 25 | +Write-Host "Current branch: $currentBranch" |
| 26 | +``` |
| 27 | + |
| 28 | +### Step 2: Push Branch and Create PR |
| 29 | +Push the current branch to remote and create a Pull Request. |
| 30 | + |
| 31 | +```powershell |
| 32 | +# Push branch to remote |
| 33 | +git push -u origin $currentBranch |
| 34 | +
|
| 35 | +# Create PR with auto-fill from commit messages |
| 36 | +gh pr create --fill |
| 37 | +``` |
| 38 | + |
| 39 | +If a PR already exists for this branch, continue with the existing PR. |
| 40 | + |
| 41 | +### Step 3: Wait for Workflow Checks |
| 42 | +Wait for all GitHub Actions workflow checks to complete and verify they pass. |
| 43 | + |
| 44 | +```powershell |
| 45 | +# Wait for checks to complete (will block until done) |
| 46 | +gh pr checks --watch --required --interval 1 --fail-fast |
| 47 | +
|
| 48 | +# Verify all checks passed |
| 49 | +$checksResult = gh pr checks |
| 50 | +if ($LASTEXITCODE -ne 0) { |
| 51 | + Write-Error "Some checks failed. Please review and fix before merging." |
| 52 | + return |
| 53 | +} |
| 54 | +Write-Host "All checks passed!" |
| 55 | +``` |
| 56 | + |
| 57 | +### Step 4: Merge the PR |
| 58 | +Merge the PR using a regular merge to preserve the branch history, and delete the remote branch. |
| 59 | + |
| 60 | +```powershell |
| 61 | +# Merge PR and delete remote branch (regular merge to keep history) |
| 62 | +gh pr merge --merge --delete-branch |
| 63 | +``` |
| 64 | + |
| 65 | +### Step 5: Clean Up Local Branch |
| 66 | +Switch to main and delete the local feature branch. |
| 67 | + |
| 68 | +```powershell |
| 69 | +# Switch to main branch |
| 70 | +git checkout main |
| 71 | +
|
| 72 | +# Pull latest changes |
| 73 | +git pull |
| 74 | +
|
| 75 | +# Delete local feature branch |
| 76 | +git branch -d $currentBranch |
| 77 | +``` |
| 78 | + |
| 79 | +### Step 6: Check and Remove Codespaces |
| 80 | +Check if there are any codespaces associated with this branch and remove them. |
| 81 | + |
| 82 | +```powershell |
| 83 | +# List codespaces for this repository |
| 84 | +$codespaces = gh codespace list --json name,gitStatus,repository --jq ".[] | select(.gitStatus.ref == `"$currentBranch`")" |
| 85 | +
|
| 86 | +if ($codespaces) { |
| 87 | + Write-Host "Found codespaces on branch $currentBranch. Removing..." |
| 88 | + # Delete codespaces on this branch |
| 89 | + gh codespace list --json name,gitStatus --jq ".[] | select(.gitStatus.ref == `"$currentBranch`") | .name" | ForEach-Object { |
| 90 | + gh codespace delete --codespace $_ --force |
| 91 | + Write-Host "Deleted codespace: $_" |
| 92 | + } |
| 93 | +} else { |
| 94 | + Write-Host "No codespaces found on branch $currentBranch" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Success Criteria |
| 99 | +- [ ] Verified not on main branch |
| 100 | +- [ ] PR created and pushed to remote |
| 101 | +- [ ] All workflow checks passed |
| 102 | +- [ ] PR merged successfully |
| 103 | +- [ ] Local and remote feature branches deleted |
| 104 | +- [ ] Any codespaces on the branch removed |
| 105 | + |
| 106 | +## Error Handling |
| 107 | +- If on `main` branch: Stop immediately with clear message |
| 108 | +- If checks fail: Stop and inform user to fix issues |
| 109 | +- If merge conflicts: Stop and inform user to resolve conflicts |
| 110 | +- If codespace deletion fails: Log warning but continue |
0 commit comments