Skip to content

Commit 62bcc9d

Browse files
authored
ci: add workflow to check for merge conflicts with main branch (#1278)
This workflow automatically checks if PRs would create merge conflicts with the main branch. It performs the following actions: 1. Skips checks if the PR is targeting the main branch 2. For PRs to other branches, attempts a test merge with main 3. Fails the workflow if conflicts are detected, prompting users to rebase 4. Provides clear error messages with instructions for resolving conflicts 5. Handles Git identity configuration and merge state properly The workflow helps maintain a clean branch structure by encouraging developers to resolve conflicts before creating PRs. 🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
1 parent 749fbe1 commit 62bcc9d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Check Merge Conflicts
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
check-merge-conflicts:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v3
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Check if PR is against main branch
17+
id: check-target-branch
18+
run: |
19+
TARGET_BRANCH="${{ github.base_ref }}"
20+
if [[ "$TARGET_BRANCH" == "main" ]]; then
21+
echo "PR is against main branch, no need to check for conflicts"
22+
echo "is_main=true" >> $GITHUB_OUTPUT
23+
else
24+
echo "PR is not against main branch, will check for conflicts"
25+
echo "is_main=false" >> $GITHUB_OUTPUT
26+
fi
27+
28+
- name: Check for merge conflicts with main
29+
if: steps.check-target-branch.outputs.is_main == 'false'
30+
id: check-conflicts
31+
run: |
32+
33+
# Set Git identity for the merge operation
34+
git config --global user.email "[email protected]"
35+
git config --global user.name "GitHub Actions"
36+
37+
# Fetch main branch
38+
git fetch origin main:main
39+
40+
# Try to merge main into the current branch
41+
MERGE_EXIT_CODE=0
42+
git merge main --no-commit --no-ff || MERGE_EXIT_CODE=$?
43+
44+
if [ $MERGE_EXIT_CODE -eq 0 ]; then
45+
echo "No merge conflicts detected"
46+
# Abort the merge since we're just checking
47+
git merge --abort || true # Don't fail if there's nothing to abort
48+
else
49+
echo "Merge conflicts detected!"
50+
# Abort the merge
51+
git merge --abort || true # Don't fail if there's nothing to abort
52+
echo "::error::This branch has merge conflicts with main. Please pull from main and rebase your branch before creating a PR."
53+
exit 1
54+
fi
55+
56+
- name: Success message
57+
if: success()
58+
run: echo "No merge conflicts detected or PR is against main branch"

0 commit comments

Comments
 (0)