👷 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
Provenance
- Commit
e6b1a2f- Tree
5aedaf6- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
8ab3a35
6 files changed+57 added−92 removed
deleted:.github/actions/comment-docs-preview-in-pr/app/main.py-68 removed
1-import logging2-import sys3-from pathlib import Path4-from typing import Optional5-6-import httpx7-from github import Github8-from github.PullRequest import PullRequest9-from pydantic import BaseModel, BaseSettings, SecretStr, ValidationError10-11-github_api = "https://api.github.com"12-13-14-class Settings(BaseSettings):15- github_repository: str16- github_event_path: Path17- github_event_name: Optional[str] = None18- input_token: SecretStr19- input_deploy_url: str20-21-22-class PartialGithubEventHeadCommit(BaseModel):23- id: str24-25-26-class PartialGithubEventWorkflowRun(BaseModel):27- head_commit: PartialGithubEventHeadCommit28-29-30-class PartialGithubEvent(BaseModel):31- workflow_run: PartialGithubEventWorkflowRun32-33-34-if __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
12345678910111213name: Comment Docs Preview in PRdescription: Comment with the docs URL preview in the PRauthor: Sebastián Ramírez <tiangolo@gmail.com>inputs: token: description: Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }} required: true deploy_url: description: The deployment URL to comment in the PR required: trueruns: using: docker image: Dockerfiledeleted:.github/actions/comment-docs-preview-in-pr/Dockerfile-7 removed
1-FROM python:3.72-3-RUN pip install httpx "pydantic==1.5.1" pygithub4-5-COPY ./app /app6-7-CMD ["python", "/app/main.py"]modified:.github/workflows/deploy-docs.yml+22 added-4 removed
4 unmodified lines56789101112131415161724 unmodified lines4243444546474849505152535455565758594344454660616263644 unmodified lines - Build Docs types: - completed
permissions: deployments: write issues: write pull-requests: write
jobs: deploy-docs: runs-on: ubuntu-latest steps:24 unmodified lines projectName: typertiangolo directory: './site' gitHubToken: ${{ secrets.GITHUB_TOKEN }} branch: ${{ ( github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch == 'master' && 'main' ) || ( github.event.workflow_run.head_sha ) }} - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.11" - uses: actions/cache@v4 id: cache with: path: ${{ env.pythonLocation }} key: ${{ runner.os }}-python-github-actions-${{ env.pythonLocation }}-${{ hashFiles('requirements-github-actions.txt') }}-v01 - name: Install GitHub Actions dependencies if: steps.cache.outputs.cache-hit != 'true' run: pip install -r requirements-github-actions.txt - name: Comment Deploy if: steps.deploy.outputs.url != '' uses: ./.github/actions/comment-docs-preview-in-pr with: token: ${{ secrets.GITHUB_TOKEN }} deploy_url: "${{ steps.deploy.outputs.url }}" run: python ./scripts/comment_docs_deploy_url_in_pr.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} DEPLOY_URL: ${{ steps.deploy.outputs.url }} COMMIT_SHA: ${{ github.event.workflow_run.head_sha }}added:scripts/comment_docs_deploy_url_in_pr.py+31 added
1+import logging2+import sys3+4+from github import Github5+from pydantic import SecretStr6+from pydantic_settings import BaseSettings7+8+9+class Settings(BaseSettings):10+ github_repository: str11+ github_token: SecretStr12+ deploy_url: str13+ commit_sha: str14+15+16+if __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
1+PyGithub>=2.3.0,<3.0.02+pydantic>=2.5.3,<3.0.03+pydantic-settings>=2.1.0,<3.0.04+httpx>=0.27.0,<0.28.0