From d6f353d7bcfcc0f7dd5972f556dba310ecc1af22 Mon Sep 17 00:00:00 2001 From: Yakko Majuri <38760734+yakkomajuri@users.noreply.github.com> Date: Tue, 16 Mar 2021 18:08:32 +0000 Subject: [PATCH] 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 --- .../workflows/{spellcheck.yml => checks.yml} | 12 +++++-- package.json | 2 +- contributors.py => scripts/contributors.py | 0 mdxImportGen.js => scripts/mdxImportGen.js | 0 scripts/safe_rename.py | 31 +++++++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) rename .github/workflows/{spellcheck.yml => checks.yml} (72%) rename contributors.py => scripts/contributors.py (100%) rename mdxImportGen.js => scripts/mdxImportGen.js (100%) create mode 100644 scripts/safe_rename.py diff --git a/.github/workflows/spellcheck.yml b/.github/workflows/checks.yml similarity index 72% rename from .github/workflows/spellcheck.yml rename to .github/workflows/checks.yml index 6f16e465a..c26ac179c 100644 --- a/.github/workflows/spellcheck.yml +++ b/.github/workflows/checks.yml @@ -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 diff --git a/package.json b/package.json index 7cf42365b..d54407832 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/contributors.py b/scripts/contributors.py similarity index 100% rename from contributors.py rename to scripts/contributors.py diff --git a/mdxImportGen.js b/scripts/mdxImportGen.js similarity index 100% rename from mdxImportGen.js rename to scripts/mdxImportGen.js diff --git a/scripts/safe_rename.py b/scripts/safe_rename.py new file mode 100644 index 000000000..db87d7391 --- /dev/null +++ b/scripts/safe_rename.py @@ -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) \ No newline at end of file