Add auto redirect for changed file names (#1094)

* rename

* rename 2

* Add auto redirect for changed file names

* convert into one action

* fixes

* attempted fix

* Add redirects

* minor fixes

* updates

* Revert "rename"

This reverts commit 147ea3eab1b20af48267e521fa52cc4ac91aa4ba.

* Revert "rename 2"

This reverts commit 69c6c4a4be2051cc6db37eca441f72a5abfb44eb.

* update netlify config

* update script

Co-authored-by: PostHog <hey@posthog.com>
This commit is contained in:
Yakko Majuri
2021-03-16 18:08:32 +00:00
committed by GitHub
parent 5b793833f3
commit d6f353d7bc
5 changed files with 42 additions and 3 deletions

View File

@@ -1,11 +1,11 @@
name: Spell Checker
name: Standard Checks
on:
- pull_request
jobs:
spell-check:
name: Spell Checker
name: Standard Checks
runs-on: ubuntu-latest
if: github.event.pull_request.head.repo.full_name == github.repository
@@ -31,3 +31,11 @@ jobs:
git config user.email hey@posthog.com
git add .
[ "$(git status -s)" = "" ] && exit 0 || git commit -m "Fix typos" && git push
- name: Add safe redirects
run: |
git fetch origin master:master
export PR_DIFF=$(git diff master)
python3 ./scripts/safe_rename.py
git add .
[ "$(git status -s)" = "" ] && exit 0 || git commit -m "Add redirects" && git push

View File

@@ -86,7 +86,7 @@
},
"husky": {
"hooks": {
"pre-commit": "node mdxImportGen && git add src/mdxGlobalComponents.js && lint-staged"
"pre-commit": "node ./scripts/mdxImportGen && git add src/mdxGlobalComponents.js && lint-staged"
}
},
"lint-staged": {

31
scripts/safe_rename.py Normal file
View File

@@ -0,0 +1,31 @@
import re
import os
git_diff = os.environ['PR_DIFF']
rename_from_regex = r'rename from contents(.*).md'
rename_to_regex = r'rename to contents(.*).md'
from_paths = re.findall(rename_from_regex, git_diff)
to_paths = re.findall(rename_to_regex, git_diff)
redirect_text = '''
[[redirects]]
from = "{}"
to = "{}"
'''
if len(from_paths) > 0 and len(from_paths) == len(to_paths):
with open("./netlify.toml", "r") as netlify_config:
netlify_config_text = netlify_config.read()
new_redirects = ''
for i in range(len(from_paths)-1):
if from_paths[i] not in netlify_config_text:
new_redirects += redirect_text.format(from_paths[i], to_paths[i])
with open("./netlify.toml", "a") as netlify_config:
netlify_config.write(new_redirects)