Skip to content

Instantly share code, notes, and snippets.

@mbaldessari
Last active November 29, 2022 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbaldessari/4b047d448c0c3716edc3a3f9a74fa95a to your computer and use it in GitHub Desktop.
Save mbaldessari/4b047d448c0c3716edc3a3f9a74fa95a to your computer and use it in GitHub Desktop.
renders every pr
#!/usr/bin/python3
import os
import subprocess
import yaml
from github import Github
baseurl = "https://docs-pr.acksyn.org/pr"
repo = "hybrid-cloud-patterns/newdocs"
base_www = "/var/www/docs-pr.acksyn.org"
pr_path = f"{base_www}/pr"
# We use this to reduce the cloning work"
git_base = "/srv/git/newdocs"
def update_git():
subprocess.run(["git", "-C", git_base, "pull"], universal_newlines=True, check=True)
def remove_pr(pr, path):
subprocess.run(["rm", "-rf", path], universal_newlines=True, check=False)
def checkout_pr(pr, path):
subprocess.run(["cp", "-rdp", git_base, path], universal_newlines=True, check=False)
patch_url = pr.patch_url
subprocess.run(
[f"curl -L {patch_url} | git apply"],
cwd=path,
shell=True,
universal_newlines=True,
check=True,
)
def tweak_config_yaml(pr, path):
config = os.path.join(path, "config.yaml")
with open(config, "r") as stream:
config_yaml = yaml.safe_load(stream)
number = str(pr.number)
base = f"{baseurl}/{number}/public"
current_img = config_yaml["params"]["site_logo"]
config_yaml["baseURL"] = base
config_yaml["params"]["site_logo"] = f"{base}{current_img}"
with open(config, "w") as stream:
yaml.dump(config_yaml, stream)
regex = f"'s,url: (.*)$,url: {base}$1,g'"
subprocess.run(
[f"perl -pi -e {regex} config.yaml"],
cwd=path,
universal_newlines=True,
check=True,
shell=True,
)
return
def render_pr(pr, path):
subprocess.run(["alias asciidoctor=\"asciidoctor --attribute=source-highlighter=pygments --attribute=last-'update-label!' --attribute=experimental=true --attribute=icons=
font -o --\"; /usr/local/bin/hugo --minify"], shell=True, cwd=path, universal_newlines=True, check=False)
return
g = Github(per_page=100)
r = g.get_repo(repo)
update_git()
for pull in r.get_pulls(state="open", sort="created"):
path = os.path.join(pr_path, str(pull.number))
remove_pr(pull, path)
checkout_pr(pull, path)
tweak_config_yaml(pull, path)
render_pr(pull, path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment