-
-
Notifications
You must be signed in to change notification settings - Fork 673
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👷🏻 Show docs deployment status and preview URLs in comment (#1054)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a20a3a8
commit ad375dc
Showing
3 changed files
with
101 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import logging | ||
import re | ||
|
||
from github import Github | ||
from pydantic import SecretStr | ||
from pydantic_settings import BaseSettings | ||
|
||
|
||
class Settings(BaseSettings): | ||
github_repository: str | ||
github_token: SecretStr | ||
deploy_url: str | None = None | ||
commit_sha: str | ||
run_id: int | ||
|
||
|
||
def main(): | ||
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( | ||
(pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None | ||
) | ||
if not use_pr: | ||
logging.error(f"No PR found for hash: {settings.commit_sha}") | ||
return | ||
commits = list(use_pr.get_commits()) | ||
current_commit = [c for c in commits if c.sha == settings.commit_sha][0] | ||
run_url = f"https://github.com/{settings.github_repository}/actions/runs/{settings.run_id}" | ||
if not settings.deploy_url: | ||
current_commit.create_status( | ||
state="pending", | ||
description="Deploy Docs", | ||
context="deploy-docs", | ||
target_url=run_url, | ||
) | ||
logging.info("No deploy URL available yet") | ||
return | ||
current_commit.create_status( | ||
state="success", | ||
description="Deploy Docs", | ||
context="deploy-docs", | ||
target_url=run_url, | ||
) | ||
|
||
files = list(use_pr.get_files()) | ||
docs_files = [f for f in files if f.filename.startswith("docs/")] | ||
|
||
deploy_url = settings.deploy_url.rstrip("/") | ||
links: list[str] = [] | ||
for f in docs_files: | ||
match = re.match(r"docs/(.*)", f.filename) | ||
assert match | ||
path = match.group(1) | ||
if path.endswith("index.md"): | ||
path = path.replace("index.md", "") | ||
else: | ||
path = path.replace(".md", "/") | ||
link = f"{deploy_url}/{path}" | ||
links.append(link) | ||
links.sort() | ||
|
||
message = f"📝 Docs preview for commit {settings.commit_sha} at: {deploy_url}" | ||
|
||
if links: | ||
message += "\n\n### Modified Pages\n\n" | ||
message += "\n".join([f"* {link}" for link in links]) | ||
|
||
print(message) | ||
use_pr.as_issue().create_comment(message) | ||
|
||
logging.info("Finished") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |