Skip to content

Commit 9b90c34

Browse files
3w36zj6ultimatile
authored andcommitted
refactor: simplify conflict marker detection using git grep
1 parent 61de0b5 commit 9b90c34

File tree

1 file changed

+20
-40
lines changed

1 file changed

+20
-40
lines changed

.github/workflows/merge-upstream.yml

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,46 +75,26 @@ jobs:
7575
core.setOutput("pr_number", pr.data.number);
7676
7777
- name: Check for conflict markers
78+
id: check-conflicts
79+
run: |
80+
CONFLICT_FILES=$(git grep -l "^<<<<<<<\|^=======\|^>>>>>>>" HEAD || echo "")
81+
if [ -n "$CONFLICT_FILES" ]; then
82+
echo "has_conflicts=true" >> $GITHUB_OUTPUT
83+
echo "conflict_files<<EOF" >> $GITHUB_OUTPUT
84+
echo "$CONFLICT_FILES" >> $GITHUB_OUTPUT
85+
echo "EOF" >> $GITHUB_OUTPUT
86+
fi
87+
88+
- name: Comment on Pull Request if conflicts found
89+
if: steps.check-conflicts.outputs.has_conflicts == 'true'
7890
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
7991
with:
8092
script: |
81-
const fs = require('fs');
82-
const path = require('path');
83-
84-
function findConflictMarkers(dirPath, conflictFiles = []) {
85-
const files = fs.readdirSync(dirPath);
86-
87-
for (const file of files) {
88-
const filePath = path.join(dirPath, file);
89-
const stat = fs.statSync(filePath);
90-
91-
if (stat.isDirectory()) {
92-
if (!['.git', 'node_modules', '.github'].includes(file)) {
93-
findConflictMarkers(filePath, conflictFiles);
94-
}
95-
} else if (!['.lock', '.lockb'].some(ext => file.endsWith(ext))) {
96-
try {
97-
const content = fs.readFileSync(filePath, 'utf8');
98-
if (/^(<{7}|={7}|>{7})/m.test(content)) {
99-
conflictFiles.push(filePath);
100-
}
101-
} catch (e) {
102-
// Skip binary files
103-
}
104-
}
105-
}
106-
107-
return conflictFiles;
108-
}
109-
110-
const conflictFiles = findConflictMarkers('.');
111-
112-
if (conflictFiles.length > 0) {
113-
const fileList = conflictFiles.map(f => `- \`${f}\``).join('\n');
114-
await github.rest.issues.createComment({
115-
owner: context.repo.owner,
116-
repo: context.repo.repo,
117-
issue_number: ${{ steps.create-pr.outputs.pr_number }},
118-
body: `> [!WARNING]\n> コンフリクトマーカーが検出されました。以下のファイルを確認し、コンフリクトを解消してください。\n\n${fileList}`
119-
});
120-
}
93+
const files = `${{ steps.check-conflicts.outputs.conflict_files }}`.split('\n').filter(f => f);
94+
const fileList = files.map(f => `- \`${f}\``).join('\n');
95+
await github.rest.issues.createComment({
96+
owner: context.repo.owner,
97+
repo: context.repo.repo,
98+
issue_number: ${{ steps.create-pr.outputs.pr_number }},
99+
body: `> [!WARNING]\n> コンフリクトマーカーが検出されました。以下のファイルを確認し、コンフリクトを解消してください。\n\n${fileList}`
100+
});

0 commit comments

Comments
 (0)