- Notifications
You must be signed in to change notification settings - Fork 720
/
Copy pathdeploy_docs_status.py
103 lines (86 loc) · 2.97 KB
/
deploy_docs_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
importlogging
importre
fromgithubimportGithub
frompydanticimportBaseModel, SecretStr
frompydantic_settingsimportBaseSettings
site_domain="sqlmodel.tiangolo.com"
classSettings(BaseSettings):
github_repository: str
github_token: SecretStr
deploy_url: str|None=None
commit_sha: str
run_id: int
is_done: bool=False
classLinkData(BaseModel):
previous_link: str
preview_link: str
defmain() ->None:
logging.basicConfig(level=logging.INFO)
settings=Settings()
logging.info(f"Using config: {settings.model_dump_json()}")
g=Github(settings.github_token.get_secret_value())
repo=g.get_repo(settings.github_repository)
use_pr=next(
(prforprinrepo.get_pulls() ifpr.head.sha==settings.commit_sha), None
)
ifnotuse_pr:
logging.error(f"No PR found for hash: {settings.commit_sha}")
return
commits=list(use_pr.get_commits())
current_commit= [cforcincommitsifc.sha==settings.commit_sha][0]
run_url=f"https://github.com/{settings.github_repository}/actions/runs/{settings.run_id}"
ifsettings.is_doneandnotsettings.deploy_url:
current_commit.create_status(
state="success",
description="No Docs Changes",
context="deploy-docs",
target_url=run_url,
)
logging.info("No docs changes found")
return
ifnotsettings.deploy_url:
current_commit.create_status(
state="pending",
description="Deploying Docs",
context="deploy-docs",
target_url=run_url,
)
logging.info("No deploy URL available yet")
return
current_commit.create_status(
state="success",
description="Docs Deployed",
context="deploy-docs",
target_url=run_url,
)
files=list(use_pr.get_files())
docs_files= [fforfinfilesiff.filename.startswith("docs/")]
deploy_url=settings.deploy_url.rstrip("/")
links: list[LinkData] = []
forfindocs_files:
match=re.match(r"docs/(.*)", f.filename)
ifnotmatch:
continue
path=match.group(1)
ifpath.endswith("index.md"):
use_path=path.replace("index.md", "")
else:
use_path=path.replace(".md", "/")
link=LinkData(
previous_link=f"https://{site_domain}/{use_path}",
preview_link=f"{deploy_url}/{use_path}",
)
links.append(link)
links.sort(key=lambdax: x.preview_link)
message=f"📝 Docs preview for commit {settings.commit_sha} at: {deploy_url}"
iflinks:
message+="\n\n### Modified Pages\n\n"
forlinkinlinks:
message+=f"* {link.preview_link}"
message+=f" - ([before]({link.previous_link}))"
message+="\n"
print(message)
use_pr.as_issue().create_comment(message)
logging.info("Finished")
if__name__=="__main__":
main()