|
| 1 | +// Finds platform docs pages whose content hasn't been edited in STALE_AFTER_MONTHS |
| 2 | +// and prints a Markdown report. |
| 3 | +// |
| 4 | +// The script ignores commits that: |
| 5 | +// - change more than BULK_FILE_LIMIT .md/.mdx files at once |
| 6 | +// - are listed in IGNORE_FILE |
| 7 | +// - only change a page's frontmatter, or move a page without changing it |
| 8 | +// |
| 9 | +// If none of a page's commits count, its newest commit is used instead. |
| 10 | +// |
| 11 | +// Pages with a `last_review: YYYY-MM-DD` frontmatter field with a date on or |
| 12 | +// after the cutoff are skipped. |
| 13 | +// |
| 14 | +// Requires the full git history (fetch-depth: 0 in GitHub Actions). |
| 15 | + |
| 16 | +import { execFileSync } from 'node:child_process'; |
| 17 | +import fs from 'node:fs'; |
| 18 | + |
| 19 | +const DOCS_DIR = 'sources/platform'; |
| 20 | +const STALE_AFTER_MONTHS = 6; |
| 21 | +const BULK_FILE_LIMIT = 10; |
| 22 | +const IGNORE_FILE = '.staleness-check-ignore'; |
| 23 | +const REPO_URL = 'https://github.com/apify/apify-docs'; |
| 24 | + |
| 25 | +const FRONTMATTER = /^---\r?\n[\s\S]*?\r?\n---(\r?\n|$)/; |
| 26 | +const LAST_REVIEW = /^last_review:\s*['"]?(\d{4}-\d{2}-\d{2})['"]?\s*$/m; |
| 27 | + |
| 28 | +const isDocFile = (file) => /\.mdx?$/.test(file); |
| 29 | + |
| 30 | +// 'pipe' keeps git's error messages out of the log. Thrown errors still include them. |
| 31 | +const git = (...args) => execFileSync('git', args, { encoding: 'utf8', stdio: 'pipe', maxBuffer: 16 * 1024 * 1024 }); |
| 32 | + |
| 33 | +function readIgnoredRevs() { |
| 34 | + return fs |
| 35 | + .readFileSync(IGNORE_FILE, 'utf8') |
| 36 | + .split('\n') |
| 37 | + .map((line) => line.replace(/#.*/, '').trim()) |
| 38 | + .filter(Boolean); |
| 39 | +} |
| 40 | + |
| 41 | +function findBulkCommits() { |
| 42 | + const bulk = new Set(); |
| 43 | + // git prints each commit as "\0<sha>", followed by the files it changed, one per line |
| 44 | + for (const entry of git('log', '--format=%x00%H', '--name-only').split('\0').slice(1)) { |
| 45 | + const [sha, ...files] = entry.split('\n').filter(Boolean); |
| 46 | + if (files.filter(isDocFile).length > BULK_FILE_LIMIT) bulk.add(sha); |
| 47 | + } |
| 48 | + return bulk; |
| 49 | +} |
| 50 | + |
| 51 | +function pageBodyAt(sha, path) { |
| 52 | + try { |
| 53 | + return git('show', `${sha}:${path}`).replace(FRONTMATTER, ''); |
| 54 | + } catch (error) { |
| 55 | + // The page was moved away in this commit and later recreated at the same path |
| 56 | + if (error.stderr?.includes('does not exist in')) return null; |
| 57 | + throw error; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// A missing or malformed date returns undefined, so the page is checked as usual |
| 62 | +function lastReview(file) { |
| 63 | + const frontmatter = fs.readFileSync(file, 'utf8').match(FRONTMATTER)?.[0] ?? ''; |
| 64 | + return frontmatter.match(LAST_REVIEW)?.[1]; |
| 65 | +} |
| 66 | + |
| 67 | +function lastContentEdit(file) { |
| 68 | + // git prints each commit as "\0<sha> <date>", followed by the page's path in that commit. |
| 69 | + // The path differs from `file` if the page was renamed later. Any other git error stops the script. |
| 70 | + const commits = git('log', '--follow', '--format=%x00%H %cs', '--name-only', '--', file) |
| 71 | + .split('\0') |
| 72 | + .slice(1) |
| 73 | + .map((entry) => { |
| 74 | + const [header, path] = entry.split('\n').filter(Boolean); |
| 75 | + const [sha, date] = header.split(' '); |
| 76 | + return { sha, date, path }; |
| 77 | + }); |
| 78 | + // A commit edits content if the page body differs from the page's previous commit |
| 79 | + const editsContent = ({ sha, path }, i) => { |
| 80 | + const previous = commits[i + 1]; |
| 81 | + if (!previous) return true; |
| 82 | + const body = pageBodyAt(sha, path); |
| 83 | + return body === null || body !== pageBodyAt(previous.sha, previous.path); |
| 84 | + }; |
| 85 | + return commits.find((commit, i) => !isSkipped(commit.sha) && editsContent(commit, i)) ?? commits[0]; |
| 86 | +} |
| 87 | + |
| 88 | +const ignoredRevs = readIgnoredRevs(); |
| 89 | +const bulkCommits = findBulkCommits(); |
| 90 | +const isSkipped = (sha) => bulkCommits.has(sha) || ignoredRevs.some((rev) => sha.startsWith(rev)); |
| 91 | + |
| 92 | +const cutoff = new Date(); |
| 93 | +cutoff.setMonth(cutoff.getMonth() - STALE_AFTER_MONTHS); |
| 94 | +const cutoffDate = cutoff.toISOString().slice(0, 10); |
| 95 | + |
| 96 | +const stalePages = git('ls-files', DOCS_DIR) |
| 97 | + .trim() |
| 98 | + .split('\n') |
| 99 | + .filter(isDocFile) |
| 100 | + .filter((file) => !(lastReview(file) >= cutoffDate)) |
| 101 | + .map((file) => ({ file, ...lastContentEdit(file) })) |
| 102 | + .filter(({ date }) => date < cutoffDate) |
| 103 | + .sort((a, b) => a.date.localeCompare(b.date)); |
| 104 | + |
| 105 | +const bySection = new Map(); |
| 106 | +for (const page of stalePages) { |
| 107 | + const section = page.file.split('/')[2]; |
| 108 | + if (!bySection.has(section)) bySection.set(section, []); |
| 109 | + bySection.get(section).push(page); |
| 110 | +} |
| 111 | + |
| 112 | +const lines = [ |
| 113 | + `${stalePages.length} pages in \`${DOCS_DIR}\` haven't been edited since ${cutoffDate}.`, |
| 114 | + '', |
| 115 | + `Frontmatter-only changes, and commits that change more than ${BULK_FILE_LIMIT} \`.md\`/\`.mdx\` files or are listed in \`${IGNORE_FILE}\`, don't count as edits.`, |
| 116 | + '', |
| 117 | + `If a page is still correct, add \`last_review: YYYY-MM-DD\` to its frontmatter. The check will skip it for ${STALE_AFTER_MONTHS} months from the review date.`, |
| 118 | +]; |
| 119 | +for (const section of [...bySection.keys()].sort()) { |
| 120 | + const pages = bySection.get(section); |
| 121 | + lines.push('', `## ${section} (${pages.length})`, ''); |
| 122 | + for (const { file, sha, date } of pages) { |
| 123 | + lines.push( |
| 124 | + `- [${file.slice(DOCS_DIR.length + 1)}](${REPO_URL}/blob/master/${file}) - [last edited ${date}](${REPO_URL}/commit/${sha})`, |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +console.log(lines.join('\n')); |
0 commit comments