mirror of
https://github.com/BillyOutlast/posthog.com.git
synced 2026-02-04 03:11:21 +01:00
Fix date serialization in JS redirect generation and remove old Python version (#2097)
* Make sure proper ISO date is used in redirect comments * Purge Python version of `safe_rename` * Add test
This commit is contained in:
14
.github/workflows/checks.yml
vendored
14
.github/workflows/checks.yml
vendored
@@ -35,16 +35,6 @@ jobs:
|
||||
git config user.name PostHog Bot
|
||||
git config user.email hey@posthog.com
|
||||
|
||||
# Replaced by JS checks (remove when sure JS checks are working)
|
||||
# - name: Add safe redirects
|
||||
# if: ${{ github.base_ref == 'master' }}
|
||||
# run: |
|
||||
# git diff ${{ github.base_ref }} ${{ github.head_ref }} > pr_diff
|
||||
# python3 ./scripts/safe_rename.py
|
||||
# rm pr_diff
|
||||
# git add .
|
||||
# [ "$(git status -s)" = "" ] && exit 0 || git commit -m "Add safe redirects"
|
||||
|
||||
- name: Fix typos
|
||||
run: |
|
||||
codespell ./contents -w -S "./contents/images/*,./contents/docs/_media/*" -q 8 -I ./.codespellignore
|
||||
@@ -68,7 +58,7 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: yarn
|
||||
- name: Run tests
|
||||
run: yarn test-generate-redirects
|
||||
run: yarn test-redirects
|
||||
- name: Configure Git
|
||||
run: |
|
||||
git fetch origin master:master
|
||||
@@ -76,7 +66,7 @@ jobs:
|
||||
git config user.email hey@posthog.com
|
||||
- name: Add safe redirects
|
||||
if: ${{ github.base_ref == 'master' }}
|
||||
run: yarn generate-redirects-js
|
||||
run: yarn generate-redirects
|
||||
- name: Cleanup and commit
|
||||
if: ${{ github.base_ref == 'master' }}
|
||||
run: |
|
||||
|
||||
@@ -770,7 +770,7 @@
|
||||
from = "/handbook/product/scale-features-prioritization"
|
||||
to = "/handbook/product/enterprise-features-prioritization"
|
||||
|
||||
# Added: 2021-9-27
|
||||
# Added: 2021-09-27
|
||||
[[redirects]]
|
||||
from = "/handbook/engineering/debugging"
|
||||
to = "/handbook/engineering/production-access"
|
||||
|
||||
@@ -18,9 +18,8 @@
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"typegen": "kea-typegen write .",
|
||||
"update-sprite": "svg-sprite -s --symbol-dest src/components/productFeature/images/icons --symbol-sprite sprited-icons.svg src/components/productFeature/images/icons/*.svg",
|
||||
"generate-redirects": "rm pr_diff && git fetch && git diff origin/master $(git branch --show-current) > pr_diff && python3 ./scripts/safe_rename.py",
|
||||
"generate-redirects-js": "git diff origin/master $(git branch --show-current) > pr_diff && DEBUG=true RUN_AS_SCRIPT=true node ./scripts/safe_rename.js",
|
||||
"test-generate-redirects": "jest"
|
||||
"generate-redirects": "git diff origin/master $(git branch --show-current) > pr_diff && DEBUG=true RUN_AS_SCRIPT=true node ./scripts/safe_rename.js",
|
||||
"test-redirects": "jest scripts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ant-design/icons": "^4.1.0",
|
||||
|
||||
@@ -10,8 +10,9 @@ const log = (...args) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** Return date (without time) in ISO format. */
|
||||
const formatDate = (date) => {
|
||||
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
|
||||
return date.toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
const redirectText = (from, to) => {
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
import re
|
||||
import requests
|
||||
from datetime import date
|
||||
|
||||
redirect_text = '''
|
||||
[[redirects]]
|
||||
from = "{}"
|
||||
to = "{}"
|
||||
'''
|
||||
|
||||
redirect_with_date_comment_text = '''
|
||||
# Added: {}{}'''
|
||||
|
||||
netlify_config_text = None
|
||||
local_netlify_config_text = None
|
||||
|
||||
# Rules
|
||||
def same_path(from_path, to_path, new_redirect):
|
||||
return from_path == to_path
|
||||
|
||||
def md_to_mdx(from_path, to_path, new_redirect):
|
||||
# old rule
|
||||
# return '.mdx' not in from_paths[i] and '.mdx' in to_paths[i]
|
||||
return from_path.endswith('.md') and to_path.endswith('.mdx')
|
||||
|
||||
def redirect_exists(from_path, to_path, new_redirect):
|
||||
return new_redirect.strip() in local_netlify_config_text
|
||||
|
||||
def from_dot_star(from_path, to_path, new_redirect):
|
||||
return from_path == '(.*)'
|
||||
|
||||
def is_snippets_rename(from_path, to_path, new_redirect):
|
||||
return '/snippets/' in from_path or '/snippets' in to_path
|
||||
|
||||
skip_rules = [
|
||||
same_path,
|
||||
md_to_mdx,
|
||||
redirect_exists,
|
||||
from_dot_star,
|
||||
is_snippets_rename
|
||||
]
|
||||
|
||||
try:
|
||||
with open("./pr_diff", "r") as git_diff_file:
|
||||
git_diff = git_diff_file.read()
|
||||
|
||||
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)
|
||||
|
||||
if len(from_paths) > 0 and len(from_paths) == len(to_paths):
|
||||
new_redirects = ''
|
||||
|
||||
for i in range(len(from_paths)):
|
||||
# Load existing remote redirect config
|
||||
netlify_config_text = requests.get('https://raw.githubusercontent.com/PostHog/posthog.com/master/netlify.toml').text
|
||||
|
||||
# Load existing redirect file to be used to avoid duplicates
|
||||
local_netlify_config_file = open("./netlify.toml", "r")
|
||||
local_netlify_config_text = local_netlify_config_file.read()
|
||||
local_netlify_config_file.close()
|
||||
|
||||
# handle index default directory files. /path/index will become /path
|
||||
from_paths[i] = re.sub("\/index$", "", from_paths[i])
|
||||
to_paths[i] = re.sub("\/index$", "", to_paths[i])
|
||||
|
||||
new_redirect = redirect_text.format(from_paths[i], to_paths[i])
|
||||
|
||||
print(f'Testing if redirects are required for: "{from_paths[i]}" to "{to_paths[i]}"')
|
||||
skip_redirect = False
|
||||
for skip_rule in skip_rules:
|
||||
if(not skip_redirect):
|
||||
skip_redirect = skip_rule(from_paths[i], to_paths[i], new_redirect)
|
||||
print(f'Rule: "{skip_rule.__name__}"', 'skip_redirect?', skip_redirect)
|
||||
|
||||
if not skip_redirect:
|
||||
print("Creating redirect: ", new_redirect)
|
||||
new_redirects += redirect_with_date_comment_text.format(date.today(), new_redirect)
|
||||
else:
|
||||
print('Not including redirect:', new_redirect)
|
||||
|
||||
with open("./netlify.toml", "a") as netlify_config:
|
||||
netlify_config.write(new_redirects)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
@@ -1,6 +1,8 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
const getRedirects = require('./safe_rename').getRedirects
|
||||
|
||||
jest.useFakeTimers().setSystemTime(new Date('2021-08-09').getTime())
|
||||
|
||||
const debug = false
|
||||
|
||||
const singleRenameGitDiff = `
|
||||
@@ -33,6 +35,7 @@ test('redirect will be added for single file rename', async () => {
|
||||
const redirects = await getRedirects({ gitDiff, localConfig, remoteConfig, debug })
|
||||
|
||||
expect(redirects.length).toBe(1)
|
||||
expect(redirects[0]).toContain('# Added: 2021-08-09')
|
||||
expect(redirects[0]).toContain('from = "/docs/one"')
|
||||
expect(redirects[0]).toContain('to = "/docs/two"')
|
||||
})
|
||||
@@ -46,8 +49,12 @@ test('redirect will be added for multi file rename', async () => {
|
||||
const redirects = await getRedirects({ gitDiff, localConfig, remoteConfig, debug })
|
||||
|
||||
expect(redirects.length).toBe(3)
|
||||
|
||||
expect(redirects[0]).toContain('# Added: 2021-08-09')
|
||||
expect(redirects[0]).toContain('from = "/docs/one"')
|
||||
expect(redirects[0]).toContain('to = "/docs/two"')
|
||||
|
||||
expect(redirects[1]).toContain('# Added: 2021-08-09')
|
||||
expect(redirects[1]).toContain('from = "/docs/cheese"')
|
||||
expect(redirects[1]).toContain('to = "/docs/mushroom"')
|
||||
expect(redirects[2]).toContain('from = "/docs/fish"')
|
||||
|
||||
Reference in New Issue
Block a user