Debian Codegraph @

scripts/deploy_docs_status.py

raw
1 import logging
2 import re
3 from typing import Literal
4
5 from github import Auth, Github
6 from pydantic import BaseModel, SecretStr
7 from pydantic_settings import BaseSettings
8
9
10 class Settings(BaseSettings):
11 github_repository: str
12 github_token: SecretStr
13 deploy_url: str | None = None
14 commit_sha: str
15 run_id: int
16 state: Literal["pending", "success", "error"] = "pending"
17
18
19 class LinkData(BaseModel):
20 previous_link: str
21 preview_link: str
22 en_link: str | None = None
23
24
25 def main() -> None:
26 logging.basicConfig(level=logging.INFO)
27 settings = Settings() # ty: ignore[missing-argument]
28
29 logging.info(f"Using config: {settings.model_dump_json()}")
30 g = Github(auth=Auth.Token(settings.github_token.get_secret_value()))
31 repo = g.get_repo(settings.github_repository)
32 use_pr = next(
33 (pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None
34 )
35 if not use_pr:
36 logging.error(f"No PR found for hash: {settings.commit_sha}")
37 return
38 commits = list(use_pr.get_commits())
39 current_commit = [c for c in commits if c.sha == settings.commit_sha][0]
40 run_url = f"https://github.com/{settings.github_repository}/actions/runs/{settings.run_id}"
41 if settings.state == "pending":
42 current_commit.create_status(
43 state="pending",
44 description="Deploying Docs",
45 context="deploy-docs",
46 target_url=run_url,
47 )
48 logging.info("No deploy URL available yet")
49 return
50 if settings.state == "error":
51 current_commit.create_status(
52 state="error",
53 description="Error Deploying Docs",
54 context="deploy-docs",
55 target_url=run_url,
56 )
57 logging.info("Error deploying docs")
58 return
59 assert settings.state == "success"
60 if not settings.deploy_url:
61 current_commit.create_status(
62 state="success",
63 description="No Docs Changes",
64 context="deploy-docs",
65 target_url=run_url,
66 )
67 logging.info("No docs changes found")
68 return
69 assert settings.deploy_url
70 current_commit.create_status(
71 state="success",
72 description="Docs Deployed",
73 context="deploy-docs",
74 target_url=run_url,
75 )
76
77 files = list(use_pr.get_files())
78 docs_files = [f for f in files if f.filename.startswith("docs/")]
79
80 deploy_url = settings.deploy_url.rstrip("/")
81 lang_links: dict[str, list[LinkData]] = {}
82 for f in docs_files:
83 match = re.match(r"docs/([^/]+)/docs/(.*)", f.filename)
84 if not match:
85 continue
86 lang = match.group(1)
87 path = match.group(2)
88 if path.endswith("index.md"):
89 path = path.replace("index.md", "")
90 else:
91 path = path.replace(".md", "/")
92 en_path = path
93 if lang == "en":
94 use_path = en_path
95 else:
96 use_path = f"{lang}/{path}"
97 link = LinkData(
98 previous_link=f"https://fastapi.tiangolo.com/{use_path}",
99 preview_link=f"{deploy_url}/{use_path}",
100 )
101 if lang != "en":
102 link.en_link = f"https://fastapi.tiangolo.com/{en_path}"
103 lang_links.setdefault(lang, []).append(link)
104
105 links: list[LinkData] = []
106 en_links = lang_links.get("en", [])
107 en_links.sort(key=lambda x: x.preview_link)
108 links.extend(en_links)
109
110 langs = list(lang_links.keys())
111 langs.sort()
112 for lang in langs:
113 if lang == "en":
114 continue
115 current_lang_links = lang_links[lang]
116 current_lang_links.sort(key=lambda x: x.preview_link)
117 links.extend(current_lang_links)
118
119 header = "## 📝 Docs preview"
120 message = header
121 message += f"\n\nLast commit {settings.commit_sha} at: {deploy_url}"
122
123 if links:
124 message += "\n\n### Modified Pages\n\n"
125 for link in links:
126 message += f"* {link.preview_link}"
127 message += f" - ([before]({link.previous_link}))"
128 if link.en_link:
129 message += f" - ([English]({link.en_link}))"
130 message += "\n"
131
132 print(message)
133 issue = use_pr.as_issue()
134 comments = list(issue.get_comments())
135 for comment in comments:
136 if (
137 comment.body.startswith(header)
138 and comment.user.login == "github-actions[bot]"
139 ):
140 comment.edit(message)
141 break
142 else:
143 issue.create_comment(message)
144
145 logging.info("Finished")
146
147
148 if __name__ == "__main__":
149 main()