Skip to content

Commit

Permalink
fix: comment docs preview in pr
Browse files Browse the repository at this point in the history
  • Loading branch information
lpthong90 committed Jan 1, 2024
1 parent 3c35432 commit 3accde6
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .github/actions/comment-docs-preview-in-pr/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9

RUN pip install httpx "pydantic==1.5.1" pygithub

COPY ./app /app

CMD ["python", "/app/main.py"]
13 changes: 13 additions & 0 deletions .github/actions/comment-docs-preview-in-pr/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Comment Docs Preview in PR
description: Comment with the docs URL preview in the PR
author: 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: true
runs:
using: docker
image: Dockerfile
68 changes: 68 additions & 0 deletions .github/actions/comment-docs-preview-in-pr/app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import logging
import sys
from pathlib import Path
from typing import Union

import httpx
from github import Github
from github.PullRequest import PullRequest
from pydantic import BaseModel, BaseSettings, SecretStr, ValidationError

github_api = "https://api.github.com"


class Settings(BaseSettings):
github_repository: str
github_event_path: Path
github_event_name: Union[str, None] = None
input_token: SecretStr
input_deploy_url: str


class PartialGithubEventHeadCommit(BaseModel):
id: str


class PartialGithubEventWorkflowRun(BaseModel):
head_commit: PartialGithubEventHeadCommit


class PartialGithubEvent(BaseModel):
workflow_run: PartialGithubEventWorkflowRun


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
settings = Settings()
logging.info(f"Using config: {settings.json()}")
g = Github(settings.input_token.get_secret_value())
repo = g.get_repo(settings.github_repository)
try:
event = PartialGithubEvent.parse_file(settings.github_event_path)
except ValidationError as e:
logging.error(f"Error parsing event file: {e.errors()}")
sys.exit(0)
use_pr: Union[PullRequest, None] = None
for pr in repo.get_pulls():
if pr.head.sha == event.workflow_run.head_commit.id:
use_pr = pr
break
if not use_pr:
logging.error(f"No PR found for hash: {event.workflow_run.head_commit.id}")
sys.exit(0)
github_headers = {
"Authorization": f"token {settings.input_token.get_secret_value()}"
}
url = f"{github_api}/repos/{settings.github_repository}/issues/{use_pr.number}/comments"
logging.info(f"Using comments URL: {url}")
response = httpx.post(
url,
headers=github_headers,
json={
"body": f"📝 Docs preview for commit {use_pr.head.sha} at: {settings.input_deploy_url}"
},
)
if not (200 <= response.status_code <= 300):
logging.error(f"Error posting comment: {response.text}")
sys.exit(1)
logging.info("Finished")
13 changes: 7 additions & 6 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ jobs:
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: Comment Deploy
# if: steps.deploy.outputs.url != ''
# uses: ./.github/actions/comment-docs-preview-in-pr
# with:
# token: ${{ secrets.FASTAPI_PREVIEW_DOCS_COMMENT_DEPLOY }}
# deploy_url: "${{ steps.deploy.outputs.url }}"
- name: Comment Deploy
if: steps.deploy.outputs.url != ''
uses: ./.github/actions/comment-docs-preview-in-pr
with:
token: ${{secrets.GITHUB_TOKEN}}
# token: ${{ secrets.FASTAPI_PREVIEW_DOCS_COMMENT_DEPLOY }}
deploy_url: "${{ steps.deploy.outputs.url }}"

0 comments on commit 3accde6

Please sign in to comment.