|  | 
|  | 1 | +# Update PR Branches Workflow | 
|  | 2 | + | 
|  | 3 | +## Overview | 
|  | 4 | + | 
|  | 5 | +The "Update PR Branches to Latest Main" workflow allows maintainers to automatically update open pull request branches with the latest changes from the `main` branch. This helps keep PRs up-to-date and reduces merge conflicts. | 
|  | 6 | + | 
|  | 7 | +## When to Use | 
|  | 8 | + | 
|  | 9 | +- When `main` branch has significant updates and you want all open PRs to be current | 
|  | 10 | +- Before merging long-running PRs that might have fallen behind | 
|  | 11 | +- As part of regular repository maintenance | 
|  | 12 | + | 
|  | 13 | +## How to Run | 
|  | 14 | + | 
|  | 15 | +### Via GitHub UI | 
|  | 16 | + | 
|  | 17 | +1. Go to the [Actions tab](../../actions/workflows/update-pr-branches.yml) in the repository | 
|  | 18 | +2. Click on "Update PR Branches to Latest Main" workflow | 
|  | 19 | +3. Click the "Run workflow" button | 
|  | 20 | +4. Configure options: | 
|  | 21 | +   - **PR numbers**: Leave empty to update all open PRs, or specify comma-separated PR numbers (e.g., `220,224,226`) | 
|  | 22 | +   - **Dry run**: Check this to see what would be updated without making changes | 
|  | 23 | +5. Click "Run workflow" | 
|  | 24 | + | 
|  | 25 | +### Via GitHub CLI | 
|  | 26 | + | 
|  | 27 | +Update all open PRs: | 
|  | 28 | +```bash | 
|  | 29 | +gh workflow run update-pr-branches.yml | 
|  | 30 | +``` | 
|  | 31 | + | 
|  | 32 | +Update specific PRs: | 
|  | 33 | +```bash | 
|  | 34 | +gh workflow run update-pr-branches.yml -f pr_numbers="220,224,226" | 
|  | 35 | +``` | 
|  | 36 | + | 
|  | 37 | +Dry run mode: | 
|  | 38 | +```bash | 
|  | 39 | +gh workflow run update-pr-branches.yml -f dry_run=true | 
|  | 40 | +``` | 
|  | 41 | + | 
|  | 42 | +## What the Workflow Does | 
|  | 43 | + | 
|  | 44 | +For each PR (or specified PRs): | 
|  | 45 | + | 
|  | 46 | +1. **Checks PR status**: Skips closed PRs | 
|  | 47 | +2. **Verifies branch exists**: Ensures the PR branch is accessible | 
|  | 48 | +3. **Checks if up-to-date**: Skips if already current with main | 
|  | 49 | +4. **Detects merge conflicts**: Tests merge and reports conflicts | 
|  | 50 | +5. **Merges main branch**: Updates PR branch with latest main | 
|  | 51 | +6. **Pushes changes**: Pushes updated branch back to GitHub | 
|  | 52 | +7. **Comments on PR**: Adds comment confirming the update | 
|  | 53 | + | 
|  | 54 | +## Handling Conflicts | 
|  | 55 | + | 
|  | 56 | +If the workflow detects merge conflicts: | 
|  | 57 | + | 
|  | 58 | +- The merge is aborted automatically | 
|  | 59 | +- A comment is added to the PR noting manual intervention is needed | 
|  | 60 | +- The PR author should: | 
|  | 61 | +  1. Pull latest changes from main | 
|  | 62 | +  2. Resolve conflicts locally | 
|  | 63 | +  3. Push the resolved changes | 
|  | 64 | + | 
|  | 65 | +## Limitations | 
|  | 66 | + | 
|  | 67 | +- **Protected branches**: Cannot push to branches with strict protection rules | 
|  | 68 | +- **Forked repositories**: Cannot update PRs from forks (different repository) | 
|  | 69 | +- **Draft PRs**: Updates all open PRs including drafts | 
|  | 70 | +- **Permissions**: Requires write access to repository and PRs | 
|  | 71 | + | 
|  | 72 | +## Best Practices | 
|  | 73 | + | 
|  | 74 | +1. **Use dry run first**: Test with dry run mode before actual updates | 
|  | 75 | +2. **Update specific PRs**: When possible, target specific PRs rather than all | 
|  | 76 | +3. **Communicate**: Let PR authors know you're updating their branches | 
|  | 77 | +4. **Monitor results**: Check workflow logs to see which PRs were updated | 
|  | 78 | +5. **Handle conflicts promptly**: Address any reported conflicts quickly | 
|  | 79 | + | 
|  | 80 | +## Troubleshooting | 
|  | 81 | + | 
|  | 82 | +### Workflow fails with permission error | 
|  | 83 | +- Check that `GITHUB_TOKEN` has write permissions | 
|  | 84 | +- Verify branch protection rules allow GitHub Actions to push | 
|  | 85 | + | 
|  | 86 | +### PR not updated | 
|  | 87 | +- Check workflow logs for specific error messages | 
|  | 88 | +- Verify PR is in OPEN state | 
|  | 89 | +- Ensure branch exists and is accessible | 
|  | 90 | +- Check for merge conflicts reported in comments | 
|  | 91 | + | 
|  | 92 | +### Push fails | 
|  | 93 | +- Branch may have protection rules preventing push | 
|  | 94 | +- PR might be from a forked repository | 
|  | 95 | +- Network issues or rate limiting | 
|  | 96 | + | 
|  | 97 | +## Security Considerations | 
|  | 98 | + | 
|  | 99 | +- Workflow uses built-in `GITHUB_TOKEN` - no additional secrets needed | 
|  | 100 | +- Only repository collaborators can trigger the workflow | 
|  | 101 | +- Branch protection rules are respected | 
|  | 102 | +- All actions are logged and auditable | 
|  | 103 | + | 
|  | 104 | +## Examples | 
|  | 105 | + | 
|  | 106 | +### Example 1: Update all open PRs | 
|  | 107 | +```bash | 
|  | 108 | +gh workflow run update-pr-branches.yml | 
|  | 109 | +``` | 
|  | 110 | + | 
|  | 111 | +### Example 2: Update specific PRs (220, 224, 226) | 
|  | 112 | +```bash | 
|  | 113 | +gh workflow run update-pr-branches.yml -f pr_numbers="220,224,226" | 
|  | 114 | +``` | 
|  | 115 | + | 
|  | 116 | +### Example 3: Dry run to see what would be updated | 
|  | 117 | +```bash | 
|  | 118 | +gh workflow run update-pr-branches.yml -f dry_run=true | 
|  | 119 | +``` | 
|  | 120 | + | 
|  | 121 | +## Related Workflows | 
|  | 122 | + | 
|  | 123 | +- **api-gen.yml**: Automatically updates API type definitions | 
|  | 124 | +- **react-ci.yml**: Runs CI checks on PRs | 
|  | 125 | +- **unit-test.yml**: Runs unit tests on PRs | 
|  | 126 | + | 
|  | 127 | +## Future Enhancements | 
|  | 128 | + | 
|  | 129 | +Potential improvements for this workflow: | 
|  | 130 | + | 
|  | 131 | +- [ ] Add option to auto-merge PRs that are ready and pass checks | 
|  | 132 | +- [ ] Support for updating PRs based on labels | 
|  | 133 | +- [ ] Slack/Discord notifications for update results | 
|  | 134 | +- [ ] Automatic conflict resolution for simple conflicts | 
|  | 135 | +- [ ] Schedule option to run weekly/monthly | 
0 commit comments