Skip to content

Commit f0b3207

Browse files
Copilotmmcky
andcommitted
Fix false positive change detection and duplicate PR comments issues
Co-authored-by: mmcky <8263752+mmcky@users.noreply.github.com>
1 parent 4a05647 commit f0b3207

File tree

1 file changed

+84
-42
lines changed

1 file changed

+84
-42
lines changed

.github/workflows/ci.yml

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,47 +101,65 @@ jobs:
101101
echo "Base SHA: ${{ github.event.pull_request.base.sha }}"
102102
echo "Head SHA: ${{ github.event.pull_request.head.sha }}"
103103
104-
# Get changed files in the lectures directory with better validation
105-
all_changed=$(git diff --name-only ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} || true)
106-
echo "All changed files:"
107-
echo "$all_changed"
104+
# Ensure we have both base and head commits available
105+
git fetch origin ${{ github.event.pull_request.base.sha }}:refs/remotes/origin/pr-base || true
106+
git fetch origin ${{ github.event.pull_request.head.sha }}:refs/remotes/origin/pr-head || true
107+
108+
# Get changed files using git diff with status to see the type of change
109+
echo "Getting diff between commits..."
110+
all_changed=$(git diff --name-status ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} 2>/dev/null || echo "")
111+
112+
if [ -z "$all_changed" ]; then
113+
echo "No changes detected or error in git diff"
114+
echo "changed_files=" >> $GITHUB_OUTPUT
115+
exit 0
116+
fi
108117
109-
changed_files=$(echo "$all_changed" | grep '^lectures/.*\.md$' | grep -v '^lectures/_' | grep -v '^lectures/intro\.md$' || true)
118+
echo "All changed files with status:"
119+
echo "$all_changed"
110120
111-
if [ ! -z "$changed_files" ]; then
112-
echo "Filtered lecture files:"
113-
echo "$changed_files"
121+
# Filter for lecture files that are Added or Modified (not Deleted)
122+
# Format: M lectures/file.md or A lectures/file.md
123+
changed_lecture_files=""
124+
while IFS=$'\t' read -r status file; do
125+
# Skip if empty line
126+
[ -z "$status" ] && continue
114127
115-
# Validate that these files actually exist and contain changes
116-
validated_files=""
117-
while IFS= read -r file; do
118-
if [ ! -z "$file" ] && [ -f "$file" ]; then
119-
# Check if the file actually has changes
120-
if git diff --quiet ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} -- "$file"; then
121-
echo "Warning: $file shows no actual changes, skipping"
122-
else
123-
echo "Confirmed changes in: $file"
124-
if [ -z "$validated_files" ]; then
125-
validated_files="$file"
128+
echo "Processing: status='$status' file='$file'"
129+
130+
# Only include Added (A) or Modified (M) files, skip Deleted (D)
131+
if [[ "$status" =~ ^[AM] ]] && [[ "$file" =~ ^lectures/.*\.md$ ]] && [[ ! "$file" =~ ^lectures/_ ]] && [[ "$file" != "lectures/intro.md" ]]; then
132+
# Double-check that the file exists and has real content changes
133+
if [ -f "$file" ]; then
134+
# Use git show to check if there are actual content changes (not just metadata)
135+
content_diff=$(git diff ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} -- "$file" | grep -E '^[+-]' | grep -v '^[+-]{3}' | wc -l)
136+
if [ "$content_diff" -gt 0 ]; then
137+
echo "✓ Confirmed content changes in: $file"
138+
if [ -z "$changed_lecture_files" ]; then
139+
changed_lecture_files="$file"
126140
else
127-
validated_files="$validated_files"$'\n'"$file"
141+
changed_lecture_files="$changed_lecture_files"$'\n'"$file"
128142
fi
143+
else
144+
echo "⚠ No content changes found in: $file (possibly metadata only)"
129145
fi
146+
else
147+
echo "⚠ File not found in working directory: $file"
130148
fi
131-
done <<< "$changed_files"
132-
133-
if [ ! -z "$validated_files" ]; then
134-
echo "Final validated changed files:"
135-
echo "$validated_files"
136-
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
137-
echo "$validated_files" >> $GITHUB_OUTPUT
138-
echo "EOF" >> $GITHUB_OUTPUT
139149
else
140-
echo "No lecture files with actual changes found"
141-
echo "changed_files=" >> $GITHUB_OUTPUT
150+
echo "⚠ Skipping: $file (status: $status, doesn't match lecture file pattern or is excluded)"
142151
fi
152+
done <<< "$all_changed"
153+
154+
if [ ! -z "$changed_lecture_files" ]; then
155+
echo ""
156+
echo "Final validated changed lecture files:"
157+
echo "$changed_lecture_files"
158+
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
159+
echo "$changed_lecture_files" >> $GITHUB_OUTPUT
160+
echo "EOF" >> $GITHUB_OUTPUT
143161
else
144-
echo "No lecture files changed"
162+
echo "No lecture files with actual content changes found"
145163
echo "changed_files=" >> $GITHUB_OUTPUT
146164
fi
147165
else
@@ -233,25 +251,39 @@ jobs:
233251
const deployUrl = `${{ steps.netlify-deploy.outputs.deploy_url }}`;
234252
const prNumber = ${{ github.event.pull_request.number }};
235253
const commitSha = `${{ github.sha }}`;
254+
const shortSha = commitSha.substring(0, 7);
255+
256+
console.log(`Checking for existing comments for commit: ${commitSha}`);
257+
console.log(`Deploy URL: ${deployUrl}`);
258+
console.log(`Changed files: ${changedFiles}`);
236259
237-
// Check if we already posted a comment for this commit
260+
// Get all comments on this PR to check for duplicates
238261
const comments = await github.rest.issues.listComments({
239262
issue_number: prNumber,
240263
owner: context.repo.owner,
241264
repo: context.repo.repo,
242265
});
243266
244-
const existingCommentForCommit = comments.data.find(c =>
245-
c.body.includes('**📖 Netlify Preview Ready!**') &&
246-
c.body.includes(`([${commitSha.substring(0, 7)}]`)
247-
);
267+
console.log(`Found ${comments.data.length} comments on PR`);
248268
249-
if (existingCommentForCommit) {
250-
console.log(`Comment already exists for commit ${commitSha}, skipping...`);
269+
// Look for existing comment with this exact commit SHA and deploy URL
270+
const duplicateComment = comments.data.find(comment => {
271+
const hasMarker = comment.body.includes('**📖 Netlify Preview Ready!**');
272+
const hasCommitSha = comment.body.includes(`([${shortSha}]`);
273+
const hasDeployUrl = comment.body.includes(deployUrl);
274+
275+
console.log(`Comment ${comment.id}: hasMarker=${hasMarker}, hasCommitSha=${hasCommitSha}, hasDeployUrl=${hasDeployUrl}`);
276+
277+
return hasMarker && hasCommitSha && hasDeployUrl;
278+
});
279+
280+
if (duplicateComment) {
281+
console.log(`Duplicate comment found (${duplicateComment.id}) for commit ${shortSha} and deploy URL ${deployUrl}, skipping...`);
251282
return;
252283
}
253284
254-
const shortSha = commitSha.substring(0, 7);
285+
console.log(`No duplicate found, creating new comment for commit ${shortSha}`);
286+
255287
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${commitSha}`;
256288
257289
let comment = `**📖 Netlify Preview Ready!**\n\n`;
@@ -264,9 +296,9 @@ jobs:
264296
265297
// Add direct links to changed lecture pages
266298
if (changedFiles && changedFiles.trim()) {
267-
console.log('Raw changedFiles:', JSON.stringify(changedFiles));
299+
console.log('Processing changed files for preview links...');
268300
const files = changedFiles.split('\n').filter(f => f.trim() && f.includes('lectures/') && f.endsWith('.md'));
269-
console.log('Filtered files:', files);
301+
console.log('Filtered lecture files:', files);
270302
271303
if (files.length > 0) {
272304
comment += `📚 **Changed Lecture Pages:** `;
@@ -276,22 +308,32 @@ jobs:
276308
const cleanFile = file.trim();
277309
if (cleanFile && cleanFile.startsWith('lectures/') && cleanFile.endsWith('.md')) {
278310
const fileName = cleanFile.replace('lectures/', '').replace('.md', '');
279-
console.log(`Processing file: ${cleanFile} -> ${fileName}`);
311+
console.log(`Creating preview link: ${cleanFile} -> ${fileName}.html`);
280312
const pageUrl = `${deployUrl}/${fileName}.html`;
281313
pageLinks.push(`[${fileName}](${pageUrl})`);
282314
}
283315
}
284316
285317
if (pageLinks.length > 0) {
286318
comment += pageLinks.join(', ') + '\n\n';
319+
} else {
320+
console.log('No valid page links created');
287321
}
322+
} else {
323+
console.log('No lecture files in changed files list');
288324
}
325+
} else {
326+
console.log('No changed files detected');
289327
}
290328
329+
console.log('Final comment:', comment);
330+
291331
// Post the comment
292332
await github.rest.issues.createComment({
293333
issue_number: prNumber,
294334
owner: context.repo.owner,
295335
repo: context.repo.repo,
296336
body: comment
297337
});
338+
339+
console.log('Comment posted successfully');

0 commit comments

Comments
 (0)