Skip to content

Commit 59ec264

Browse files
authored
Add last-updated-at ci check (#599)
* Add script to check last_updated metadata in FAQ files * Made some change to a 1005-and1006.. file to test the last updated check * Test commit without updating last_updated * Test commit without updating last_updated * Test commit without updating last_updated * Test commit without updating last_updated * Testing on one of the articles * Testing on one of the articles * Testing CI check for last_updated * Testing CI check for last_updated * Testing CI check for last_updated * Testing CI check for last_updated * Updated the last updated check * Test: missing last_updated update * Test: missing last_updated update * Test: missing last_updated update * Updated yml for check lastest updated date * Updated yml for check lastest updated date * Updated yml to have the latest paths
1 parent 85b5ee6 commit 59ec264

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Check Last Updated Fields
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'content/resources/faq/**'
7+
- 'content/resources/articles/**'
8+
push:
9+
branches:
10+
- '**'
11+
paths:
12+
- 'content/resources/faq/**'
13+
- 'content/resources/articles/**'
14+
15+
jobs:
16+
check-last-updated:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v3
21+
with:
22+
fetch-depth: 2
23+
24+
- name: Setup Node.js
25+
uses: actions/setup-node@v3
26+
with:
27+
node-version: '18'
28+
29+
- name: Run Last Updated Check
30+
run: node scripts/check-last-updated.js

scripts/check-last-updated.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { execSync } = require("child_process");
2+
const fs = require("fs");
3+
4+
const GIT_RANGE = "HEAD~1..HEAD";
5+
const TARGET_DIRS = [
6+
"content/resources/faqs/**",
7+
"content/resources/articles/**"
8+
];
9+
10+
function getChangedFiles() {
11+
const output = execSync(`git diff --name-only ${GIT_RANGE}`).toString();
12+
return output
13+
.split("\n")
14+
.filter(f => f.endsWith(".md") && TARGET_DIRS.some(dir => f.startsWith(dir)));
15+
}
16+
17+
function didUpdateLastUpdated(filePath) {
18+
const diffOutput = execSync(`git diff ${GIT_RANGE} -- ${filePath}`).toString();
19+
20+
const lastUpdatedLines = diffOutput
21+
.split('\n')
22+
.filter(line => line.includes("last_updated:"));
23+
24+
// Check if there's both a removed (-) and added (+) line
25+
const hasRemoved = lastUpdatedLines.some(line => line.startsWith("-"));
26+
const hasAdded = lastUpdatedLines.some(line => line.startsWith("+"));
27+
28+
return hasRemoved && hasAdded;
29+
}
30+
31+
32+
const changedFiles = getChangedFiles();
33+
let failed = false;
34+
35+
if (changedFiles.length > 0) {
36+
console.log("🔍 Detected changed files:", changedFiles);
37+
}
38+
39+
for (const file of changedFiles) {
40+
const diff = execSync(`git diff ${GIT_RANGE} -- ${file}`).toString();
41+
console.log(`🔎 Diff for ${file}:\n${diff}`);
42+
if (!didUpdateLastUpdated(file)) {
43+
console.error(`❌ Error: ${file} was modified but 'last_updated' was not updated.`);
44+
failed = true;
45+
}
46+
}
47+
48+
if (failed) {
49+
console.error("\n❌ CI FAILED: The following files were modified without updating 'last_updated':");
50+
for (const file of changedFiles) {
51+
if (!didUpdateLastUpdated(file)) {
52+
console.error(` • ${file}`);
53+
}
54+
}
55+
56+
throw new Error("🚫 Commit rejected: 'last_updated' must be updated for each modified FAQ/article.");
57+
} else {
58+
console.log("✅ All modified FAQ/article files have updated 'last_updated' fields.");
59+
}

0 commit comments

Comments
 (0)