Skip to content

Commit bccf87a

Browse files
Fix PR preview workflow issues (#1544)
Fix PR preview workflow issues - Limit preview tables to max 20 rows to prevent GitHub comment size limit - Sort files by change delta (lines changed) when truncating - Show truncation indicator when tables are limited - Add comment size check warning --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent d2ec44f commit bccf87a

1 file changed

Lines changed: 93 additions & 9 deletions

File tree

.github/workflows/pr-preview-links.yml

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,19 @@ jobs:
6161
- name: Build Hugo (generate pageurls)
6262
if: steps.changed.outputs.any_changed == 'true'
6363
run: |
64+
# Debug: Check what ref we're building
65+
echo "Current branch: $(git branch --show-current)"
66+
echo "HEAD commit: $(git rev-parse HEAD)"
67+
echo "PR head SHA: ${{ github.event.pull_request.head.sha }}"
68+
6469
hugo --minify
6570
test -f public/pageurls.json && echo "Found pageurls.json" || (echo "Missing pageurls.json" && ls -la public || true)
71+
72+
# Debug: Check a specific file's title in the generated JSON
73+
if [ -f public/pageurls.json ]; then
74+
echo "Checking for public-api entries in pageurls.json:"
75+
jq '.[] | select(.path | contains("public-api"))' public/pageurls.json || true
76+
fi
6677
6778
- name: Create or find preview comment
6879
if: steps.changed.outputs.any_changed == 'true'
@@ -287,13 +298,20 @@ jobs:
287298
const rel = (p.path || '').replace(/^\/+/, '').replace(/\\/g, '/');
288299
const withLang = lang && !rel.startsWith(lang + '/') ? `${lang}/${rel}` : rel;
289300
const withoutLang = lang && rel.startsWith(lang + '/') ? rel.slice(lang.length + 1) : rel;
301+
302+
// Create all possible key variations
290303
const keys = new Set([
291304
rel,
292305
withLang,
293306
withoutLang,
294307
path.posix.join('content', withLang),
295308
path.posix.join('content', withoutLang),
309+
path.posix.join('content', rel),
310+
// Add explicit handling for language in path
311+
path.posix.join('content', lang, rel),
312+
path.posix.join('content', lang, withoutLang),
296313
].filter(Boolean).map(k => k.replace(/\\/g, '/')));
314+
297315
for (const k of keys) byPath.set(k, p);
298316
}
299317
@@ -457,25 +475,91 @@ jobs:
457475
return rows;
458476
}
459477
460-
const addedRows = buildRows(added);
461-
const modifiedRows = buildRows(modified);
462-
const deletedRows = buildRows(deleted);
478+
// Calculate file sizes/changes for sorting by delta
479+
function getFileStats(files) {
480+
const { execSync } = require('child_process');
481+
return files.map(fp => {
482+
let size = 0;
483+
try {
484+
// Use git to get the number of changed lines
485+
// For added files, count all lines; for modified, count insertions + deletions
486+
const diffStat = execSync(`git diff --numstat origin/${{ github.base_ref }}...HEAD -- "${fp}" 2>/dev/null || echo "0 0 ${fp}"`, { encoding: 'utf8' }).trim();
487+
const parts = diffStat.split(/\s+/);
488+
if (parts.length >= 2) {
489+
const insertions = parseInt(parts[0]) || 0;
490+
const deletions = parseInt(parts[1]) || 0;
491+
size = insertions + deletions;
492+
}
493+
} catch (e) {
494+
// If git diff fails, try file size as fallback
495+
try {
496+
if (fs.existsSync(fp)) {
497+
const stats = fs.statSync(fp);
498+
size = stats.size;
499+
}
500+
} catch (e2) {
501+
// Ignore errors
502+
}
503+
}
504+
return { path: fp, size };
505+
});
506+
}
507+
508+
// Sort files by size (as a proxy for change delta) and limit to maxRows
509+
function limitAndSortFiles(files, maxRows = 20) {
510+
if (files.length <= maxRows) {
511+
return { files: files.sort(), truncated: false };
512+
}
513+
514+
// Get file stats and sort by size descending
515+
const stats = getFileStats(files);
516+
stats.sort((a, b) => b.size - a.size);
517+
518+
// Take top maxRows files
519+
const topFiles = stats.slice(0, maxRows).map(s => s.path);
520+
// Sort alphabetically for consistent display
521+
topFiles.sort();
522+
523+
return { files: topFiles, truncated: true, total: files.length };
524+
}
525+
526+
const MAX_ROWS_PER_TABLE = 20;
527+
const addedInfo = limitAndSortFiles(added, MAX_ROWS_PER_TABLE);
528+
const modifiedInfo = limitAndSortFiles(modified, MAX_ROWS_PER_TABLE);
529+
const deletedInfo = limitAndSortFiles(deleted, MAX_ROWS_PER_TABLE);
530+
531+
const addedRows = buildRows(addedInfo.files);
532+
const modifiedRows = buildRows(modifiedInfo.files);
533+
const deletedRows = buildRows(deletedInfo.files);
463534
464535
const header = '<!-- docs-preview-links -->\n<!-- preview-base: ' + (previewBase || '') + ' -->\n**PR Preview: Changed content**' + (previewBase ? `\n\nBase preview: ${previewBase}` : '\n\n⏳ *Links will be added automatically when Cloudflare Pages finishes deploying (typically 2-5 minutes)*');
465536
if (!previewBase) {
466537
core.info('Preview base URL not available yet. Comment will be automatically updated when Cloudflare posts the Branch Preview URL.');
467538
}
468-
function section(title, rows) {
469-
if (rows.length === 0) return '';
470-
return `\n\n### ${title}\n\n| Title | Path |\n| --- | --- |\n${rows.join('\n')}`;
539+
540+
function section(title, rows, info) {
541+
if (rows.length === 0 && !info.truncated) return '';
542+
543+
let sectionTitle = title;
544+
if (info.truncated) {
545+
sectionTitle += ` (showing ${rows.length} of ${info.total} files with largest changes)`;
546+
}
547+
548+
return `\n\n### ${sectionTitle}\n\n| Title | Path |\n| --- | --- |\n${rows.join('\n')}`;
471549
}
472550
473551
const body = [
474552
header,
475-
section('Added', addedRows),
476-
section('Modified', modifiedRows),
477-
section('Deleted', deletedRows),
553+
section('Added', addedRows, addedInfo),
554+
section('Modified', modifiedRows, modifiedInfo),
555+
section('Deleted', deletedRows, deletedInfo),
478556
].join('');
557+
558+
// Check if body exceeds GitHub's limit
559+
const MAX_COMMENT_SIZE = 65536;
560+
if (body.length > MAX_COMMENT_SIZE) {
561+
core.warning(`Preview comment size (${body.length}) exceeds GitHub's limit (${MAX_COMMENT_SIZE}). This should not happen with row limits in place.`);
562+
}
479563
480564
// Update the existing comment
481565
const commentId = process.env.COMMENT_ID;

0 commit comments

Comments
 (0)