Skip to content

Commit 613b140

Browse files
loft-botPiotr1215
andauthored
ci: add conflict marker detection to prevent accidental merges (#3466) (#3519)
backport PRs with commitConflicts enabled can have unresolved conflict markers committed to the branch. while visible in the diff, nothing prevents accidentally merging these PRs. adds a ci check that: - scans for conflict markers (<<<<<<, ======, >>>>>>) - posts a pr comment listing files and line numbers - fails the check to block merging refs: OPS-461 (cherry picked from commit 20a2710) Co-authored-by: Piotr <[email protected]>
1 parent bc6b593 commit 613b140

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Check for unresolved conflicts
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- 'v**'
7+
8+
jobs:
9+
conflict-check:
10+
runs-on: ubuntu-latest
11+
name: Check for conflict markers
12+
permissions:
13+
pull-requests: write
14+
contents: read
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Find conflict markers
19+
id: conflicts
20+
run: |
21+
# Look for git conflict markers: <<<<<<< followed by branch name
22+
# Exclude vendor, node_modules, and common non-source directories
23+
CONFLICTS=$(grep -rn "^<<<<<<< " \
24+
--include="*.go" --include="*.yaml" --include="*.yml" --include="*.json" \
25+
--include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx" \
26+
--include="*.py" --include="*.rb" --include="*.rs" --include="*.sh" \
27+
--include="*.md" --include="*.txt" \
28+
--exclude-dir=vendor --exclude-dir=node_modules --exclude-dir=.git \
29+
. 2>/dev/null || true)
30+
31+
if [ -n "$CONFLICTS" ]; then
32+
echo "found=true" >> "$GITHUB_OUTPUT"
33+
echo "### Conflict markers found:"
34+
echo "$CONFLICTS"
35+
36+
# Save to file for PR comment
37+
{
38+
echo "## Unresolved Merge Conflicts Detected"
39+
echo ""
40+
echo "This PR contains unresolved merge conflict markers. Please resolve them before merging."
41+
echo ""
42+
echo "### Conflicted Files"
43+
echo ""
44+
echo '```'
45+
echo "$CONFLICTS"
46+
echo '```'
47+
} > /tmp/conflict-report.md
48+
else
49+
echo "found=false" >> "$GITHUB_OUTPUT"
50+
echo "No conflict markers found"
51+
fi
52+
53+
- name: Comment on PR
54+
if: steps.conflicts.outputs.found == 'true' && github.event_name == 'pull_request'
55+
env:
56+
GH_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
57+
PR_NUMBER: ${{ github.event.pull_request.number }}
58+
run: |
59+
# Update existing comment or create new one
60+
gh pr comment "$PR_NUMBER" --body-file /tmp/conflict-report.md --edit-last --create-if-none 2>/dev/null || \
61+
gh pr comment "$PR_NUMBER" --body-file /tmp/conflict-report.md
62+
63+
- name: Fail if conflicts found
64+
if: steps.conflicts.outputs.found == 'true'
65+
run: |
66+
echo "::error::Unresolved merge conflict markers found"
67+
exit 1

0 commit comments

Comments
 (0)