Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bandit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bandit]
skips = B603,B607
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
name: ci

on:
push:
branches: [master]
pull_request:
branches: [master]

# Cancel old builds when pushing new commits.
concurrency:
group: ci-${{ github.event.pull_request.number || github.ref }}
group: ci-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

# Cancel old builds when pushing new commits.
concurrency:
group: pages-${{ github.event.pull_request.number || github.ref }}
group: pages-${{ github.ref }}
cancel-in-progress: true

jobs:
Expand Down
6 changes: 4 additions & 2 deletions .restyled.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---
exclude:
- "**/*.md"
restylers:
- pyment:
enabled: false
- "*"
112 changes: 112 additions & 0 deletions deploy/preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env python3
import re
import subprocess # nosec
from functools import cache
from typing import Any

import requests


@cache
def git_branch() -> str:
"""Get the current git branch."""
return (subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip())


@cache
def git_origin() -> str:
"""Get the URL of the origin remote.

E.g. "git@github.com:iphydf/website".
"""
return (subprocess.check_output(["git", "remote", "get-url",
"origin"]).decode().strip())


@cache
def git_upstream() -> str:
"""Get the URL of the upstream remote.

E.g. "git@github.com:toktok/website".
"""
return (subprocess.check_output(["git", "remote", "get-url",
"upstream"]).decode().strip())


@cache
def github_slug() -> str:
"""Extract the GitHub slug from the upstream remote."""
match = re.match(r".*github\.com[:/](.*?)(\.git)?$", git_upstream())
if not match or not match.group(1):
raise ValueError("Could not extract GitHub slug.")
return match.group(1)


@cache
def github_prs() -> Any:
"""Check GitHub API for PRs on the upstream slug."""
response = requests.get(
f"https://api.github.com/repos/{github_slug()}/pulls?state=open",
timeout=5,
)
response.raise_for_status()
return response.json()


def github_pr_for_branch(branch: str) -> int:
"""Find the PR for a branch."""
for pr in github_prs():
if pr["head"]["ref"] == branch:
return int(pr["number"])
raise ValueError(f"No PR found for branch {branch}.")


def main():
""" """
print(f"Branch: {git_branch()}")
print(f"Origin: {git_origin()}")
print(f"Upstream: {git_upstream()}")
print(f"Slug: {github_slug()}")
print(f"PRs: {len(github_prs())}")
print(f"PR: {github_pr_for_branch(git_branch())}")

print("Building preview...")
subprocess.check_call(
["docker", "build", "-t", "toxchat/toktok.github.io:latest", "."])

print("Extracting site from docker image...")
subprocess.check_call(["rm", "-rf", "toktok-site"])
tar = subprocess.Popen(["tar", "-x"], stdin=subprocess.PIPE)
if tar.stdin is None:
raise ValueError("Could not open tar stdin.")
subprocess.check_call(
[
"docker",
"run",
"--rm",
"--entrypoint",
"tar",
"toxchat/toktok.github.io:latest",
"-C",
"/home/builder/build",
"-c",
"toktok-site",
],
stdout=tar.stdin,
)
tar.stdin.close()
print("Waiting for tar to finish...")
tar.wait()

print("Deploying preview...")
subprocess.check_call([
"netlify",
"deploy",
"--alias",
f"deploy-preview-{github_pr_for_branch(git_branch())}",
])


if __name__ == "__main__":
main()
Loading