|
| 1 | +name: Forward Port to Next |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: forward-port |
| 10 | + cancel-in-progress: false |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + forward-port: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + env: |
| 20 | + DISCORD_WEBHOOK: ${{ secrets.DISCORD_METRICS_WEBHOOK }} |
| 21 | + steps: |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Check for existing PR |
| 28 | + id: check-pr |
| 29 | + env: |
| 30 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 31 | + run: | |
| 32 | + EXISTING_PR=$(gh pr list --base next --head main --state open --json number --jq '.[0].number // empty') |
| 33 | + if [ -n "$EXISTING_PR" ]; then |
| 34 | + echo "existing_pr=$EXISTING_PR" >> $GITHUB_OUTPUT |
| 35 | + echo "PR #$EXISTING_PR already exists for main → next" |
| 36 | + else |
| 37 | + echo "existing_pr=" >> $GITHUB_OUTPUT |
| 38 | + echo "No existing PR found" |
| 39 | + fi |
| 40 | +
|
| 41 | + - name: Check if main has changes not in next |
| 42 | + id: check-diff |
| 43 | + if: steps.check-pr.outputs.existing_pr == '' |
| 44 | + run: | |
| 45 | + git fetch origin next |
| 46 | + DIFF_COUNT=$(git rev-list --count origin/next..origin/main) |
| 47 | + echo "diff_count=$DIFF_COUNT" >> $GITHUB_OUTPUT |
| 48 | + if [ "$DIFF_COUNT" -gt 0 ]; then |
| 49 | + echo "Found $DIFF_COUNT commit(s) in main not in next" |
| 50 | + else |
| 51 | + echo "No new commits to forward port" |
| 52 | + fi |
| 53 | +
|
| 54 | + - name: Check for merge conflicts |
| 55 | + id: check-conflicts |
| 56 | + if: steps.check-pr.outputs.existing_pr == '' && steps.check-diff.outputs.diff_count != '0' |
| 57 | + run: | |
| 58 | + git config user.name "github-actions[bot]" |
| 59 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 60 | +
|
| 61 | + # Try a test merge to detect conflicts |
| 62 | + git checkout origin/next |
| 63 | + if git merge --no-commit --no-ff origin/main 2>/dev/null; then |
| 64 | + echo "has_conflicts=false" >> $GITHUB_OUTPUT |
| 65 | + echo "No merge conflicts detected" |
| 66 | + else |
| 67 | + echo "has_conflicts=true" >> $GITHUB_OUTPUT |
| 68 | + # Get list of conflicting files |
| 69 | + CONFLICTING_FILES=$(git diff --name-only --diff-filter=U | head -10) |
| 70 | + echo "conflicting_files<<EOF" >> $GITHUB_OUTPUT |
| 71 | + echo "$CONFLICTING_FILES" >> $GITHUB_OUTPUT |
| 72 | + echo "EOF" >> $GITHUB_OUTPUT |
| 73 | + echo "Merge conflicts detected in: $CONFLICTING_FILES" |
| 74 | + fi |
| 75 | + git merge --abort 2>/dev/null || true |
| 76 | +
|
| 77 | + - name: Create forward-port PR |
| 78 | + id: create-pr |
| 79 | + if: steps.check-pr.outputs.existing_pr == '' && steps.check-diff.outputs.diff_count != '0' |
| 80 | + env: |
| 81 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 82 | + run: | |
| 83 | + # Get the commits being forward-ported for the PR body |
| 84 | + COMMITS=$(git log origin/next..origin/main --oneline --no-decorate | head -20) |
| 85 | + COMMIT_COUNT=$(git rev-list --count origin/next..origin/main) |
| 86 | +
|
| 87 | + # Build conflict warning if needed |
| 88 | + CONFLICT_WARNING="" |
| 89 | + if [ "${{ steps.check-conflicts.outputs.has_conflicts }}" = "true" ]; then |
| 90 | + CONFLICT_WARNING=" |
| 91 | + > [!WARNING] |
| 92 | + > **Merge conflicts detected.** Manual resolution required. |
| 93 | + > |
| 94 | + > Conflicting files: |
| 95 | + > \`\`\` |
| 96 | + > ${{ steps.check-conflicts.outputs.conflicting_files }} |
| 97 | + > \`\`\` |
| 98 | +
|
| 99 | + ### How to resolve |
| 100 | +
|
| 101 | + \`\`\`bash |
| 102 | + # Option 1: Resolve in a temporary branch (recommended) |
| 103 | + git fetch origin |
| 104 | + git checkout -b resolve-forward-port origin/next |
| 105 | + git merge origin/main |
| 106 | + # Fix conflicts in your editor, then: |
| 107 | + git add . |
| 108 | + git commit |
| 109 | + git push origin resolve-forward-port |
| 110 | + # Create PR from resolve-forward-port → next, then close this PR |
| 111 | +
|
| 112 | + # Option 2: Resolve directly on next |
| 113 | + git checkout next |
| 114 | + git pull origin next |
| 115 | + git merge origin/main |
| 116 | + # Fix conflicts, commit, and push |
| 117 | + \`\`\` |
| 118 | + " |
| 119 | + fi |
| 120 | +
|
| 121 | + # Create PR body |
| 122 | + BODY="## Forward Port: main → next |
| 123 | +
|
| 124 | + This PR forward-ports changes from \`main\` to \`next\` to ensure hotfixes and releases are included in the next development branch. |
| 125 | + $CONFLICT_WARNING |
| 126 | + ### Commits ($COMMIT_COUNT total) |
| 127 | + \`\`\` |
| 128 | + $COMMITS |
| 129 | + \`\`\` |
| 130 | + $([ "$COMMIT_COUNT" -gt 20 ] && echo "... and $((COMMIT_COUNT - 20)) more") |
| 131 | +
|
| 132 | + --- |
| 133 | + *Auto-generated by forward-port workflow*" |
| 134 | +
|
| 135 | + # Create the PR |
| 136 | + PR_URL=$(gh pr create \ |
| 137 | + --base next \ |
| 138 | + --head main \ |
| 139 | + --title "chore: forward port main to next" \ |
| 140 | + --label "forward-port" \ |
| 141 | + --label "automated" \ |
| 142 | + --body "$BODY") |
| 143 | +
|
| 144 | + PR_NUMBER=$(echo "$PR_URL" | grep -oE '[0-9]+$') |
| 145 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 146 | + echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT |
| 147 | +
|
| 148 | + # Add conflict label if needed |
| 149 | + if [ "${{ steps.check-conflicts.outputs.has_conflicts }}" = "true" ]; then |
| 150 | + gh pr edit "$PR_NUMBER" --add-label "has-conflicts" |
| 151 | + fi |
| 152 | +
|
| 153 | + - name: Send Discord notification |
| 154 | + if: steps.create-pr.outputs.pr_url != '' && env.DISCORD_WEBHOOK != '' |
| 155 | + uses: sarisia/actions-status-discord@v1 |
| 156 | + with: |
| 157 | + webhook: ${{ env.DISCORD_WEBHOOK }} |
| 158 | + status: ${{ steps.check-conflicts.outputs.has_conflicts == 'true' && 'Warning' || 'Success' }} |
| 159 | + title: "🔄 Forward Port PR Created" |
| 160 | + description: | |
| 161 | + **main → next** |
| 162 | +
|
| 163 | + ${{ steps.check-conflicts.outputs.has_conflicts == 'true' && '⚠️ **Merge conflicts detected** - manual resolution required' || '✅ No conflicts - ready for review' }} |
| 164 | +
|
| 165 | + **Commits:** ${{ steps.check-diff.outputs.diff_count }} |
| 166 | + **PR:** ${{ steps.create-pr.outputs.pr_url }} |
| 167 | + color: ${{ steps.check-conflicts.outputs.has_conflicts == 'true' && '0xFFA500' || '0x58AFFF' }} |
| 168 | + username: Task Master Bot |
| 169 | + avatar_url: https://raw.githubusercontent.com/eyaltoledano/claude-task-master/main/images/logo.png |
0 commit comments