From bc3a3f73162c282b18bbf7075ad6b95d12766d93 Mon Sep 17 00:00:00 2001 From: Noah Pendleton <2538614+noahp@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:12:23 -0400 Subject: [PATCH] Add github action to check for redirects on rename --- .github/workflows/detect-renamed-posts.js | 32 +++++++++++++++++++ .github/workflows/detect-renamed-posts.yml | 22 +++++++++++++ ...v.md => 2023-08-09-a-modern-c-devl-env.md} | 0 3 files changed, 54 insertions(+) create mode 100644 .github/workflows/detect-renamed-posts.js create mode 100644 .github/workflows/detect-renamed-posts.yml rename _posts/{2023-08-09-a-modern-c-dev-env.md => 2023-08-09-a-modern-c-devl-env.md} (100%) diff --git a/.github/workflows/detect-renamed-posts.js b/.github/workflows/detect-renamed-posts.js new file mode 100644 index 000000000..89871d3d4 --- /dev/null +++ b/.github/workflows/detect-renamed-posts.js @@ -0,0 +1,32 @@ +const fs = require('fs'); +const path = require('path'); + +const redirectsFilePath = path.join(__dirname, '_redirects'); +const postsDirectory = path.join(__dirname, '_posts'); + +function updateRedirects(originalName, newName) { + const redirects = fs.readFileSync(redirectsFilePath, 'utf-8'); + const lines = redirects.split('\n'); + + const newRedirect = `/blog/${originalName} /blog/${newName}`; + const updatedRedirects = lines.map(line => { + if (line.trim() === newRedirect.trim()) { + return newRedirect; + } + return line; + }).join('\n'); + + fs.writeFileSync(redirectsFilePath, updatedRedirects, 'utf-8'); +} + +const changedFiles = JSON.parse(process.env.GITHUB_EVENT_PATH).commits[0].added.concat( + JSON.parse(process.env.GITHUB_EVENT_PATH).commits[0].modified +); + +changedFiles.forEach(file => { + if (file.startsWith('_posts/')) { + const originalName = path.basename(file, path.extname(file)); + const newName = path.basename(file, path.extname(file)).replace(/^\d{4}-\d{2}-\d{2}-/, ''); + updateRedirects(originalName, newName); + } +}); diff --git a/.github/workflows/detect-renamed-posts.yml b/.github/workflows/detect-renamed-posts.yml new file mode 100644 index 000000000..c57f5d9f4 --- /dev/null +++ b/.github/workflows/detect-renamed-posts.yml @@ -0,0 +1,22 @@ +name: Detect Renamed Posts + +on: + + pull_request: + branches: [master] + paths: + - '_posts/**' + +jobs: + detect_renamed_post: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Set Up Node.js + uses: actions/setup-node@v2 + + - name: Run Renaming Script + run: node .github/workflows/detect-renamed-posts.js diff --git a/_posts/2023-08-09-a-modern-c-dev-env.md b/_posts/2023-08-09-a-modern-c-devl-env.md similarity index 100% rename from _posts/2023-08-09-a-modern-c-dev-env.md rename to _posts/2023-08-09-a-modern-c-devl-env.md