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

29
scripts/contributors.py Normal file
View File

@@ -0,0 +1,29 @@
import requests
import sys
import os
# Generates copy-pasteable HTML with contributor avatars
# Used in our Team page and the PostHog/posthog README
image_size = sys.argv[1] if len(sys.argv) > 1 else 50
contributors_processed = set()
contributor_faces_html = ''
github_token = os.environ.get('GITHUB_PERSONAL_TOKEN', '') # Needed for rate limiting
auth_header = { 'Authorization': f'token {github_token}' }
repos = requests.get('https://api.github.com/orgs/PostHog/repos?type=all', headers=auth_header).json()
for repo in repos:
contributors = requests.get(repo['contributors_url'], headers=auth_header).json()
for contributor in contributors:
username = contributor['login']
if username not in contributors_processed:
contributors_processed.add(username)
avatar_url = contributor['avatar_url']
contributor_faces_html += f'<a href="https://github.com/{username}"><img src="{avatar_url}" title="{username}" width="{image_size}" height="{image_size}"></a>\n'
print(contributor_faces_html)

42
scripts/mdxImportGen.js Normal file
View File

@@ -0,0 +1,42 @@
const fs = require('fs')
const baseDir = './src/components'
const componentsToIgnore = new Set(['Layout', 'SidebarContents', 'Header', 'Menu', 'UserLogosCarousel'])
const getComponentsInDir = (dir, components = []) => {
const dirContents = fs.readdirSync(dir)
let subdirectories = []
let indexFileInDir = false
for (let f of dirContents) {
if (fs.lstatSync(`${dir}/${f}`).isDirectory() && !componentsToIgnore.has(f)) {
subdirectories.push(f)
continue
}
if (!indexFileInDir && f.includes('index')) {
indexFileInDir = true
}
}
if (!subdirectories || indexFileInDir) {
return [...components, dir]
}
for (let subdir of subdirectories) {
components = getComponentsInDir(`${dir}/${subdir}`, components)
}
return components
}
const generateFile = () => {
let imports = '// AUTO GENERATED FILE \n\n'
let componentNames = []
for (let component of getComponentsInDir(baseDir)) {
const destructuredPath = component.split('/')
const relativePath = './' + destructuredPath.slice(2).join('/')
const componentName = destructuredPath[destructuredPath.length - 1]
imports += `import { ${componentName} } from '${relativePath}'\n`
componentNames.push(componentName)
}
imports += '\nexport const shortcodes = {\n\t' + componentNames.join(',\n\t') + '\n}'
fs.writeFileSync('./src/mdxGlobalComponents.js', imports)
}
generateFile()

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)