Skip to content

Commit

Permalink
Add github action to check for redirects on rename
Browse files Browse the repository at this point in the history
  • Loading branch information
noahp committed Aug 10, 2023
1 parent c9df39b commit bc3a3f7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/detect-renamed-posts.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
22 changes: 22 additions & 0 deletions .github/workflows/detect-renamed-posts.yml
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.

0 comments on commit bc3a3f7

Please sign in to comment.