🔨 Update docs previews script (#1236)

This commit is contained in:
Sebastián Ramírez 2024-12-04 18:03:13 +01:00 committed by GitHub
parent 811fe9ffc9
commit 74496c25f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,9 +2,11 @@ import logging
import re import re
from github import Github from github import Github
from pydantic import SecretStr from pydantic import BaseModel, SecretStr
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
site_domain = "sqlmodel.tiangolo.com"
class Settings(BaseSettings): class Settings(BaseSettings):
github_repository: str github_repository: str
@ -15,7 +17,12 @@ class Settings(BaseSettings):
is_done: bool = False is_done: bool = False
def main(): class LinkData(BaseModel):
previous_link: str
preview_link: str
def main() -> None:
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
settings = Settings() settings = Settings()
@ -60,24 +67,31 @@ def main():
docs_files = [f for f in files if f.filename.startswith("docs/")] docs_files = [f for f in files if f.filename.startswith("docs/")]
deploy_url = settings.deploy_url.rstrip("/") deploy_url = settings.deploy_url.rstrip("/")
links: list[str] = [] links: list[LinkData] = []
for f in docs_files: for f in docs_files:
match = re.match(r"docs/(.*)", f.filename) match = re.match(r"docs/(.*)", f.filename)
assert match if not match:
continue
path = match.group(1) path = match.group(1)
if path.endswith("index.md"): if path.endswith("index.md"):
path = path.replace("index.md", "") use_path = path.replace("index.md", "")
else: else:
path = path.replace(".md", "/") use_path = path.replace(".md", "/")
link = f"{deploy_url}/{path}" link = LinkData(
previous_link=f"https://{site_domain}/{use_path}",
preview_link=f"{deploy_url}/{use_path}",
)
links.append(link) links.append(link)
links.sort() links.sort(key=lambda x: x.preview_link)
message = f"📝 Docs preview for commit {settings.commit_sha} at: {deploy_url}" message = f"📝 Docs preview for commit {settings.commit_sha} at: {deploy_url}"
if links: if links:
message += "\n\n### Modified Pages\n\n" message += "\n\n### Modified Pages\n\n"
message += "\n".join([f"* {link}" for link in links]) for link in links:
message += f"* {link.preview_link}"
message += f" - ([before]({link.previous_link}))"
message += "\n"
print(message) print(message)
use_pr.as_issue().create_comment(message) use_pr.as_issue().create_comment(message)