|
| 1 | +name: Sync feature branch with main |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + target_branch: |
| 9 | + description: "Feature branch to sync with main" |
| 10 | + required: true |
| 11 | + default: "new-resolver-config" |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + sync-branch: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + if: ${{ github.repository_owner == 'python-wheel-build' }} |
| 20 | + concurrency: |
| 21 | + group: sync-${{ inputs.target_branch || 'new-resolver-config' }} |
| 22 | + cancel-in-progress: false |
| 23 | + permissions: |
| 24 | + contents: write |
| 25 | + issues: write |
| 26 | + env: |
| 27 | + TARGET_BRANCH: ${{ inputs.target_branch || 'new-resolver-config' }} |
| 28 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + steps: |
| 30 | + - name: Checkout target branch |
| 31 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 32 | + with: |
| 33 | + ref: ${{ env.TARGET_BRANCH }} |
| 34 | + fetch-depth: 0 |
| 35 | + persist-credentials: false |
| 36 | + |
| 37 | + - name: Configure git |
| 38 | + run: | |
| 39 | + git config user.name "github-actions[bot]" |
| 40 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 41 | + gh auth setup-git |
| 42 | +
|
| 43 | + - name: Merge main into feature branch |
| 44 | + id: merge |
| 45 | + run: | |
| 46 | + git fetch origin main |
| 47 | + BEFORE=$(git rev-parse HEAD) |
| 48 | + if git merge origin/main --no-edit; then |
| 49 | + AFTER=$(git rev-parse HEAD) |
| 50 | + if [ "$BEFORE" = "$AFTER" ]; then |
| 51 | + echo "result=up-to-date" >> "$GITHUB_OUTPUT" |
| 52 | + else |
| 53 | + echo "result=merged" >> "$GITHUB_OUTPUT" |
| 54 | + fi |
| 55 | + else |
| 56 | + CONFLICTS=$(git diff --name-only --diff-filter=U) |
| 57 | + git merge --abort |
| 58 | + echo "result=conflict" >> "$GITHUB_OUTPUT" |
| 59 | + echo "conflicts<<DELIM" >> "$GITHUB_OUTPUT" |
| 60 | + echo "$CONFLICTS" >> "$GITHUB_OUTPUT" |
| 61 | + echo "DELIM" >> "$GITHUB_OUTPUT" |
| 62 | + fi |
| 63 | +
|
| 64 | + - name: Push merged changes |
| 65 | + if: steps.merge.outputs.result == 'merged' |
| 66 | + run: git push origin "HEAD:${TARGET_BRANCH}" |
| 67 | + |
| 68 | + - name: Close resolved sync-conflict issues |
| 69 | + if: steps.merge.outputs.result == 'merged' || steps.merge.outputs.result == 'up-to-date' |
| 70 | + run: | |
| 71 | + EXPECTED_TITLE="Sync conflict: ${TARGET_BRANCH} cannot merge main" |
| 72 | + gh issue list --label "sync-conflict" --state open --json number,title \ |
| 73 | + | jq -r --arg t "$EXPECTED_TITLE" '.[] | select(.title == $t) | .number' \ |
| 74 | + | xargs -rI{} gh issue close {} --comment "Resolved by automated sync." |
| 75 | +
|
| 76 | + - name: Report up-to-date |
| 77 | + if: steps.merge.outputs.result == 'up-to-date' |
| 78 | + run: echo "Branch ${TARGET_BRANCH} is already up to date with main." |
| 79 | + |
| 80 | + - name: Open issue on conflict |
| 81 | + if: steps.merge.outputs.result == 'conflict' |
| 82 | + run: | |
| 83 | + EXPECTED_TITLE="Sync conflict: ${TARGET_BRANCH} cannot merge main" |
| 84 | +
|
| 85 | + EXISTING=$(gh issue list \ |
| 86 | + --label "sync-conflict" \ |
| 87 | + --state open \ |
| 88 | + --json title \ |
| 89 | + | jq --arg expected "$EXPECTED_TITLE" \ |
| 90 | + '[.[] | select(.title == $expected)] | length') |
| 91 | +
|
| 92 | + if [ "$EXISTING" -gt 0 ]; then |
| 93 | + echo "An open sync-conflict issue for ${TARGET_BRANCH} already exists, skipping." |
| 94 | + exit 0 |
| 95 | + fi |
| 96 | +
|
| 97 | + gh label create "sync-conflict" \ |
| 98 | + --description "Automated sync with main hit merge conflicts" \ |
| 99 | + --color "D93F0B" \ |
| 100 | + --force |
| 101 | +
|
| 102 | + gh issue create \ |
| 103 | + --title "$EXPECTED_TITLE" \ |
| 104 | + --label "sync-conflict" \ |
| 105 | + --body "$(cat <<EOF |
| 106 | + The automated sync workflow failed to merge \`main\` into \`${TARGET_BRANCH}\` due to merge conflicts. |
| 107 | +
|
| 108 | + **Conflicting files:** |
| 109 | + \`\`\` |
| 110 | + ${{ steps.merge.outputs.conflicts }} |
| 111 | + \`\`\` |
| 112 | +
|
| 113 | + **Manual resolution required.** To fix: |
| 114 | +
|
| 115 | + \`\`\`bash |
| 116 | + git checkout ${TARGET_BRANCH} |
| 117 | + git fetch origin |
| 118 | + git merge origin/main |
| 119 | + # resolve conflicts |
| 120 | + git commit |
| 121 | + git push |
| 122 | + \`\`\` |
| 123 | +
|
| 124 | + **Workflow run:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 125 | + EOF |
| 126 | + )" |
0 commit comments