👷 Update docs previews comment, single comment, add failure status (#1359)
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 dc07284 · Browse files · 2 files·+46 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+46 added−14 removed
- other:+46 added−14 removed
Provenance
- Commit
dc51ff4- Tree
ffca931- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
dc07284
2 files changed+46 added−14 removed
modified:.github/workflows/deploy-docs.yml+10 added-2 removed
42 unmodified lines4344454647474849505115 unmodified lines67686970717273747576777879808182838485788642 unmodified lines env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} RUN_ID: ${{ github.run_id }}
STATE: "pending" - name: Clean site run: | rm -rf ./site mkdir ./site15 unmodified lines with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} command: pages deploy ./site --project-name=${{ env.PROJECT_NAME }} --branch=${{ env.BRANCH }} - name: Deploy Docs Status Error if: failure() run: python ./scripts/deploy_docs_status.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} RUN_ID: ${{ github.run_id }} STATE: "error" - name: Comment Deploy run: python ./scripts/deploy_docs_status.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} DEPLOY_URL: ${{ steps.deploy.outputs.deployment-url }} COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} RUN_ID: ${{ github.run_id }} IS_DONE: "true" STATE: "success"modified:scripts/deploy_docs_status.py+36 added-12 removed
1 import logging2 import re3+from typing import Literal4 4-from github import Github5+from github import Auth, Github6 from pydantic import BaseModel, SecretStr7 from pydantic_settings import BaseSettings8 9 site_domain = "typer.tiangolo.com"4 unmodified lines14 github_token: SecretStr15 deploy_url: str | None = None16 commit_sha: str17 run_id: int17- is_done: bool = False18+ state: Literal["pending", "success", "error"] = "pending"19 20 21 class LinkData(BaseModel):22 previous_link: str4 unmodified lines27 logging.basicConfig(level=logging.INFO)28 settings = Settings()29 30 logging.info(f"Using config: {settings.model_dump_json()}")30- g = Github(settings.github_token.get_secret_value())31+ g = Github(auth=Auth.Token(settings.github_token.get_secret_value()))32 repo = g.get_repo(settings.github_repository)33 use_pr = next(34 (pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None35 )2 unmodified lines38 return39 commits = list(use_pr.get_commits())40 current_commit = [c for c in commits if c.sha == settings.commit_sha][0]41 run_url = f"https://github.com/{settings.github_repository}/actions/runs/{settings.run_id}"41- if settings.is_done and not settings.deploy_url:42+ if settings.state == "pending":43 current_commit.create_status(43- state="success",44- description="No Docs Changes",44+ state="pending",45+ description="Deploying Docs",46 context="deploy-docs",47 target_url=run_url,48 )48- logging.info("No docs changes found")49+ logging.info("No deploy URL available yet")50 return51+ if settings.state == "error":52+ current_commit.create_status(53+ state="error",54+ description="Error Deploying Docs",55+ context="deploy-docs",56+ target_url=run_url,57+ )58+ logging.info("Error deploying docs")59+ return60+ assert settings.state == "success"61 if not settings.deploy_url:62 current_commit.create_status(52- state="pending",53- description="Deploying Docs",63+ state="success",64+ description="No Docs Changes",65 context="deploy-docs",66 target_url=run_url,67 )57- logging.info("No deploy URL available yet")68+ logging.info("No docs changes found")69 return70+ assert settings.deploy_url71 current_commit.create_status(72 state="success",73 description="Docs Deployed",74 context="deploy-docs",20 unmodified lines95 )96 links.append(link)97 links.sort(key=lambda x: x.preview_link)98 99+ header = "## 📝 Docs preview"100+ message = header87- message = f"📝 Docs preview for commit {settings.commit_sha} at: {deploy_url}"101+ message += f"\n\nLast commit {settings.commit_sha} at: {deploy_url}"102 103 if links:104 message += "\n\n### Modified Pages\n\n"105 for link in links:1 unmodified line107 message += f" - ([before]({link.previous_link}))"108 message += "\n"109 110 print(message)111+ issue = use_pr.as_issue()112+ comments = list(issue.get_comments())113+ for comment in comments:114+ if (115+ comment.body.startswith(header)116+ and comment.user.login == "github-actions[bot]"117+ ):118+ comment.edit(message)119+ break120+ else:97- use_pr.as_issue().create_comment(message)121+ issue.create_comment(message)122 123 logging.info("Finished")124 125 2 unmodified lines