diff --git a/.github/workflows/check-last-updated.yml b/.github/workflows/check-last-updated.yml new file mode 100644 index 0000000000..71877fd87f --- /dev/null +++ b/.github/workflows/check-last-updated.yml @@ -0,0 +1,30 @@ +name: Check Last Updated Fields + +on: + pull_request: + paths: + - 'content/resources/faq/**' + - 'content/resources/articles/**' + push: + branches: + - '**' + paths: + - 'content/resources/faq/**' + - 'content/resources/articles/**' + +jobs: + check-last-updated: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 2 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '18' + + - name: Run Last Updated Check + run: node scripts/check-last-updated.js diff --git a/scripts/check-last-updated.js b/scripts/check-last-updated.js new file mode 100644 index 0000000000..7e6da3f422 --- /dev/null +++ b/scripts/check-last-updated.js @@ -0,0 +1,59 @@ +const { execSync } = require("child_process"); +const fs = require("fs"); + +const GIT_RANGE = "HEAD~1..HEAD"; +const TARGET_DIRS = [ + "content/resources/faqs/**", + "content/resources/articles/**" +]; + +function getChangedFiles() { + const output = execSync(`git diff --name-only ${GIT_RANGE}`).toString(); + return output + .split("\n") + .filter(f => f.endsWith(".md") && TARGET_DIRS.some(dir => f.startsWith(dir))); +} + +function didUpdateLastUpdated(filePath) { + const diffOutput = execSync(`git diff ${GIT_RANGE} -- ${filePath}`).toString(); + + const lastUpdatedLines = diffOutput + .split('\n') + .filter(line => line.includes("last_updated:")); + + // Check if there's both a removed (-) and added (+) line + const hasRemoved = lastUpdatedLines.some(line => line.startsWith("-")); + const hasAdded = lastUpdatedLines.some(line => line.startsWith("+")); + + return hasRemoved && hasAdded; +} + + +const changedFiles = getChangedFiles(); +let failed = false; + +if (changedFiles.length > 0) { + console.log("šŸ” Detected changed files:", changedFiles); +} + +for (const file of changedFiles) { + const diff = execSync(`git diff ${GIT_RANGE} -- ${file}`).toString(); + console.log(`šŸ”Ž Diff for ${file}:\n${diff}`); + if (!didUpdateLastUpdated(file)) { + console.error(`āŒ Error: ${file} was modified but 'last_updated' was not updated.`); + failed = true; + } +} + +if (failed) { + console.error("\nāŒ CI FAILED: The following files were modified without updating 'last_updated':"); + for (const file of changedFiles) { + if (!didUpdateLastUpdated(file)) { + console.error(` • ${file}`); + } + } + + throw new Error("🚫 Commit rejected: 'last_updated' must be updated for each modified FAQ/article."); +} else { + console.log("āœ… All modified FAQ/article files have updated 'last_updated' fields."); +} \ No newline at end of file