Skip to content
Myr

👷🏻 Show docs deployment status and preview URLs in comment (#910)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent 142422a · Browse files · 3 files+101 added−44 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+101 added−44 removed

  • other:+101 added−44 removed

3 files+101 added−44 removed

modified:.github/workflows/deploy-docs.yml+22 added-13 removed
deleted:scripts/comment_docs_deploy_url_in_pr.py-31 removed
1import logging2import sys34from github import Github5from pydantic import SecretStr6from pydantic_settings import BaseSettings789class Settings(BaseSettings):10 github_repository: str11 github_token: SecretStr12 deploy_url: str13 commit_sha: str141516if __name__ == "__main__":17 logging.basicConfig(level=logging.INFO)18 settings = Settings()19 logging.info(f"Using config: {settings.model_dump_json()}")20 g = Github(settings.github_token.get_secret_value())21 repo = g.get_repo(settings.github_repository)22 use_pr = next(23 (pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None24 )25 if not use_pr:26 logging.error(f"No PR found for hash: {settings.commit_sha}")27 sys.exit(0)28 use_pr.as_issue().create_comment(29 f"📝 Docs preview for commit {settings.commit_sha} at: {settings.deploy_url}"30 )31 logging.info("Finished")
added:scripts/deploy_docs_status.py+79 added
1import logging2import re34from github import Github5from pydantic import SecretStr6from pydantic_settings import BaseSettings789class Settings(BaseSettings):10 github_repository: str11 github_token: SecretStr12 deploy_url: str | None = None13 commit_sha: str14 run_id: int151617def main():18 logging.basicConfig(level=logging.INFO)19 settings = Settings()2021 logging.info(f"Using config: {settings.model_dump_json()}")22 g = Github(settings.github_token.get_secret_value())23 repo = g.get_repo(settings.github_repository)24 use_pr = next(25 (pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None26 )27 if not use_pr:28 logging.error(f"No PR found for hash: {settings.commit_sha}")29 return30 commits = list(use_pr.get_commits())31 current_commit = [c for c in commits if c.sha == settings.commit_sha][0]32 run_url = f"https://github.com/{settings.github_repository}/actions/runs/{settings.run_id}"33 if not settings.deploy_url:34 current_commit.create_status(35 state="pending",36 description="Deploy Docs",37 context="deploy-docs",38 target_url=run_url,39 )40 logging.info("No deploy URL available yet")41 return42 current_commit.create_status(43 state="success",44 description="Deploy Docs",45 context="deploy-docs",46 target_url=run_url,47 )4849 files = list(use_pr.get_files())50 docs_files = [f for f in files if f.filename.startswith("docs/")]5152 deploy_url = settings.deploy_url.rstrip("/")53 links: list[str] = []54 for f in docs_files:55 match = re.match(r"docs/(.*)", f.filename)56 assert match57 path = match.group(1)58 if path.endswith("index.md"):59 path = path.replace("index.md", "")60 else:61 path = path.replace(".md", "/")62 link = f"{deploy_url}/{path}"63 links.append(link)64 links.sort()6566 message = f"📝 Docs preview for commit {settings.commit_sha} at: {deploy_url}"6768 if links:69 message += "\n\n### Modified Pages\n\n"70 message += "\n".join([f"* {link}" for link in links])7172 print(message)73 use_pr.as_issue().create_comment(message)7475 logging.info("Finished")767778if __name__ == "__main__":79 main()