|
| 1 | +name: Conflict Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + types: [opened, synchronize, reopened] |
| 7 | + |
| 8 | +jobs: |
| 9 | + check-conflicts: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + pull-requests: write |
| 13 | + steps: |
| 14 | + - name: Check for merge conflicts |
| 15 | + uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + let mergeable = null; |
| 19 | + let attempts = 0; |
| 20 | +
|
| 21 | + // Poll until GitHub computes mergeability (can take a few seconds) |
| 22 | + while (mergeable === null && attempts < 10) { |
| 23 | + await new Promise(r => setTimeout(r, 3000)); |
| 24 | + const { data: pr } = await github.rest.pulls.get({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + pull_number: context.issue.number, |
| 28 | + }); |
| 29 | + mergeable = pr.mergeable; |
| 30 | + attempts++; |
| 31 | + } |
| 32 | +
|
| 33 | + if (mergeable === false) { |
| 34 | + // Avoid posting duplicate comments |
| 35 | + const { data: comments } = await github.rest.issues.listComments({ |
| 36 | + owner: context.repo.owner, |
| 37 | + repo: context.repo.repo, |
| 38 | + issue_number: context.issue.number, |
| 39 | + }); |
| 40 | +
|
| 41 | + const alreadyCommented = comments.some(c => |
| 42 | + c.body.includes('Merge Conflict Detected') && |
| 43 | + c.user.login === 'github-actions[bot]' |
| 44 | + ); |
| 45 | +
|
| 46 | + if (!alreadyCommented) { |
| 47 | + await github.rest.issues.createComment({ |
| 48 | + owner: context.repo.owner, |
| 49 | + repo: context.repo.repo, |
| 50 | + issue_number: context.issue.number, |
| 51 | + body: `⚠️ **Merge Conflict Detected**\n\nThis PR has conflicts with \`main\` that must be resolved before it can be merged.\n\n**Steps to fix:**\n\`\`\`bash\ngit checkout main\ngit pull origin main\ngit checkout your-branch\ngit merge main\n# resolve conflicts in your editor\ngit add .\ngit commit\ngit push\n\`\`\`\n\nIf you need help resolving the conflict, leave a comment and the maintainer will assist.\n\n---\n*🤖 Automated conflict check*` |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
0 commit comments