Skip to content

Commit 60a3f97

Browse files
feat: add GitHub Actions workflow to check for PR merge conflicts (#1586)
This workflow automatically checks if a pull request has merge conflicts with the base branch. It uses the GitHub API to query the mergeable state and fails the check if conflicts are detected, making it easy to spot PRs that need to be updated before merging.
1 parent ac38e8e commit 60a3f97

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

.changeset/silly-lizards-jump.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Check for Merge Conflicts
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
# Also run when the base branch is updated
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
check-merge-conflict:
13+
runs-on: ubuntu-latest
14+
if: github.event_name == 'pull_request'
15+
16+
steps:
17+
- name: Check for merge conflicts
18+
uses: actions/github-script@v7
19+
with:
20+
script: |
21+
const pr = context.payload.pull_request;
22+
23+
// Get the PR details to check mergeable state
24+
const { data: pullRequest } = await github.rest.pulls.get({
25+
owner: context.repo.owner,
26+
repo: context.repo.repo,
27+
pull_number: pr.number
28+
});
29+
30+
console.log(`PR #${pr.number} mergeable state: ${pullRequest.mergeable_state}`);
31+
console.log(`PR #${pr.number} mergeable: ${pullRequest.mergeable}`);
32+
33+
// The mergeable field can be: true, false, or null (if GitHub is still calculating)
34+
if (pullRequest.mergeable === null) {
35+
console.log('GitHub is still calculating mergeable status. Waiting...');
36+
// Wait a bit and try again
37+
await new Promise(resolve => setTimeout(resolve, 5000));
38+
39+
const { data: updatedPR } = await github.rest.pulls.get({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
pull_number: pr.number
43+
});
44+
45+
console.log(`Updated mergeable state: ${updatedPR.mergeable_state}`);
46+
console.log(`Updated mergeable: ${updatedPR.mergeable}`);
47+
48+
if (updatedPR.mergeable === false) {
49+
core.setFailed(`❌ PR #${pr.number} has merge conflicts that must be resolved.`);
50+
} else if (updatedPR.mergeable === null) {
51+
core.warning('⚠️ Could not determine merge conflict status. GitHub may still be calculating.');
52+
} else {
53+
console.log(`✅ PR #${pr.number} has no merge conflicts.`);
54+
}
55+
} else if (pullRequest.mergeable === false) {
56+
core.setFailed(`❌ PR #${pr.number} has merge conflicts that must be resolved.`);
57+
} else {
58+
console.log(`✅ PR #${pr.number} has no merge conflicts.`);
59+
}

0 commit comments

Comments
 (0)