Skip to content
Myr

👷 Refactor GitHub Action to comment docs deployment URLs and update token, preparing for GitHub org (#896)

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 8ab3a35 · Browse files · 6 files+57 added−92 removed
This commit

State

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

Diff stats

6 files+57 added−92 removed

  • other:+57 added−92 removed

6 files+57 added−92 removed

deleted:.github/actions/comment-docs-preview-in-pr/app/main.py-68 removed
1import logging2import sys3from pathlib import Path4from typing import Optional56import httpx7from github import Github8from github.PullRequest import PullRequest9from pydantic import BaseModel, BaseSettings, SecretStr, ValidationError1011github_api = "https://api.github.com"121314class Settings(BaseSettings):15 github_repository: str16 github_event_path: Path17 github_event_name: Optional[str] = None18 input_token: SecretStr19 input_deploy_url: str202122class PartialGithubEventHeadCommit(BaseModel):23 id: str242526class PartialGithubEventWorkflowRun(BaseModel):27 head_commit: PartialGithubEventHeadCommit282930class PartialGithubEvent(BaseModel):31 workflow_run: PartialGithubEventWorkflowRun323334if __name__ == "__main__":35 logging.basicConfig(level=logging.INFO)36 settings = Settings()37 logging.info(f"Using config: {settings.json()}")38 g = Github(settings.input_token.get_secret_value())39 repo = g.get_repo(settings.github_repository)40 try:41 event = PartialGithubEvent.parse_file(settings.github_event_path)42 except ValidationError as e:43 logging.error(f"Error parsing event file: {e.errors()}")44 sys.exit(0)45 use_pr: Optional[PullRequest] = None46 for pr in repo.get_pulls():47 if pr.head.sha == event.workflow_run.head_commit.id:48 use_pr = pr49 break50 if not use_pr:51 logging.error(f"No PR found for hash: {event.workflow_run.head_commit.id}")52 sys.exit(0)53 github_headers = {54 "Authorization": f"token {settings.input_token.get_secret_value()}"55 }56 url = f"{github_api}/repos/{settings.github_repository}/issues/{use_pr.number}/comments"57 logging.info(f"Using comments URL: {url}")58 response = httpx.post(59 url,60 headers=github_headers,61 json={62 "body": f"📝 Docs preview for commit {use_pr.head.sha} at: {settings.input_deploy_url}"63 },64 )65 if not (200 <= response.status_code <= 300):66 logging.error(f"Error posting comment: {response.text}")67 sys.exit(1)68 logging.info("Finished")
deleted:.github/actions/comment-docs-preview-in-pr/action.yml-13 removed
deleted:.github/actions/comment-docs-preview-in-pr/Dockerfile-7 removed
1FROM python:3.723RUN pip install httpx "pydantic==1.5.1" pygithub45COPY ./app /app67CMD ["python", "/app/main.py"]
modified:.github/workflows/deploy-docs.yml+22 added-4 removed
added:scripts/comment_docs_deploy_url_in_pr.py+31 added
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:requirements-github-actions.txt+4 added
1PyGithub>=2.3.0,<3.0.02pydantic>=2.5.3,<3.0.03pydantic-settings>=2.1.0,<3.0.04httpx>=0.27.0,<0.28.0