]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✨ Add GitHub Action to watch for missing preview docs (#1740)
authorSebastián Ramírez <tiangolo@gmail.com>
Mon, 20 Jul 2020 14:33:17 +0000 (16:33 +0200)
committerGitHub <noreply@github.com>
Mon, 20 Jul 2020 14:33:17 +0000 (16:33 +0200)
* 📝 Update release notes

* 🔊 Make curl verbose when triggering docs preview

* 🔧 Update GitHub Actions circus to use commit hash

* ✨ Add PR docs preview watcher

.github/actions/get-artifact/action.yml
.github/actions/watch-previews/Dockerfile [new file with mode: 0644]
.github/actions/watch-previews/action.yml [new file with mode: 0644]
.github/actions/watch-previews/app/main.py [new file with mode: 0644]
.github/workflows/build-docs.yml
.github/workflows/preview-docs.yml
.github/workflows/watch-docs-previews.yml [new file with mode: 0644]
docs/en/docs/release-notes.md
scripts/docs-comment-deploy.sh
scripts/trigger-docs-preview.sh

index e19b7c9831cf5cb7a9a3ff33fb935f4eb091fa69..bff398d451a564a28718bc1aa3ca980b2c802f77 100644 (file)
@@ -1,4 +1,4 @@
-name: "Deploy Artifact to Netlify"
+name: "Get Artifact"
 description: "Get artifact, possibly uploaded by a PR, useful to deploy docs previews"
 author: "Sebastián Ramírez <tiangolo@gmail.com>"
 inputs:
diff --git a/.github/actions/watch-previews/Dockerfile b/.github/actions/watch-previews/Dockerfile
new file mode 100644 (file)
index 0000000..b8cc64d
--- /dev/null
@@ -0,0 +1,7 @@
+FROM python:3.7
+
+RUN pip install httpx PyGithub "pydantic==1.5.1"
+
+COPY ./app /app
+
+CMD ["python", "/app/main.py"]
diff --git a/.github/actions/watch-previews/action.yml b/.github/actions/watch-previews/action.yml
new file mode 100644 (file)
index 0000000..5c09ad4
--- /dev/null
@@ -0,0 +1,10 @@
+name: "Watch docs previews in PRs"
+description: "Check PRs and trigger new docs deploys"
+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
+runs:
+  using: 'docker'
+  image: 'Dockerfile'
diff --git a/.github/actions/watch-previews/app/main.py b/.github/actions/watch-previews/app/main.py
new file mode 100644 (file)
index 0000000..fca8a78
--- /dev/null
@@ -0,0 +1,94 @@
+import logging
+from datetime import datetime
+from pathlib import Path
+from typing import List, Optional
+
+import httpx
+from github import Github
+from github.NamedUser import NamedUser
+from pydantic import BaseModel, BaseSettings, SecretStr
+
+github_api = "https://api.github.com"
+netlify_api = "https://api.netlify.com"
+
+
+class Settings(BaseSettings):
+    input_token: SecretStr
+    github_repository: str
+    github_event_path: Path
+    github_event_name: Optional[str] = None
+
+
+class Artifact(BaseModel):
+    id: int
+    node_id: str
+    name: str
+    size_in_bytes: int
+    url: str
+    archive_download_url: str
+    expired: bool
+    created_at: datetime
+    updated_at: datetime
+
+
+class ArtifactResponse(BaseModel):
+    total_count: int
+    artifacts: List[Artifact]
+
+
+def get_message(commit: str) -> str:
+    return f"Docs preview for commit {commit} at"
+
+
+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)
+    owner: NamedUser = repo.owner
+    headers = {"Authorization": f"token {settings.input_token.get_secret_value()}"}
+    prs = list(repo.get_pulls(state="open"))
+    for pr in prs:
+        logging.info("-----")
+        logging.info(f"Processing PR #{pr.number}: {pr.title}")
+        pr_comments = list(pr.get_issue_comments())
+        pr_commits = list(pr.get_commits())
+        last_commit = pr_commits[0]
+        for pr_commit in pr_commits:
+            if pr_commit.commit.author.date > last_commit.commit.author.date:
+                last_commit = pr_commit
+        commit = last_commit.commit.sha
+        logging.info(f"Last commit: {commit}")
+        message = get_message(commit)
+        notified = False
+        for pr_comment in pr_comments:
+            if message in pr_comment.body:
+                notified = True
+        logging.info(f"Docs preview was notified: {notified}")
+        if not notified:
+            response = httpx.get(
+                f"{github_api}/repos/{settings.github_repository}/actions/artifacts",
+                headers=headers,
+            )
+            data = response.json()
+            artifacts_response = ArtifactResponse.parse_obj(data)
+            use_artifact: Optional[Artifact] = None
+            for artifact in artifacts_response.artifacts:
+                if artifact.name == settings.input_name:
+                    use_artifact = artifact
+                    break
+            if use_artifact:
+                logging.info(f"Existing artifact: {use_artifact.name}")
+                response = httpx.post(
+                    "https://api.github.com/repos/tiangolo/fastapi/actions/workflows/preview-docs.yml/dispatches",
+                    headers=headers,
+                    json={
+                        "ref": "master",
+                        "inputs": {"pr": f"{pr.number}", "name": f"docs-zip-{commit}"},
+                    },
+                )
+                logging.info(
+                    f"Trigger sent, response status: {response.status_code} - content: {response.content}"
+                )
+    logging.info("Finished")
index 2b90c54b4c4b4bc78183b7ae8c4b5a5285cd6fa9..c541526992e0ccd4b154c1a109d5022908b59692 100644 (file)
@@ -31,7 +31,7 @@ jobs:
           GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
         run: bash ./scripts/trigger-docs-preview.sh
       - name: Deploy to Netlify
-        uses: nwtgck/actions-netlify@v1.0.3
+        uses: nwtgck/actions-netlify@v1.1.5
         with:
           publish-dir: './site'
           production-branch: master
index 687112c7a11c74f8d5a7b9a4c2b2a4e2ec40e64d..b2f2db9a3d0026e1804128cbd3f5be0006cfe62b 100644 (file)
@@ -8,6 +8,9 @@ on:
       name:
         description: Artifact name for zip file with docs
         required: true
+      commit:
+        description: Commit SHA hash
+        required: true
 
 jobs:
   deploy:
@@ -32,8 +35,9 @@ jobs:
           NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
           NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
       - name: Comment Deploy
-        run: bash ./scripts/docs-comment-deploy.sh
         env:
           PR: "${{ github.event.inputs.pr }}"
           DEPLOY_URL: "${{ steps.netlify.outputs.deploy-url }}"
           GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
+          COMMIT: "${{ github.event.inputs.commit }}"
+        run: bash ./scripts/docs-comment-deploy.sh
diff --git a/.github/workflows/watch-docs-previews.yml b/.github/workflows/watch-docs-previews.yml
new file mode 100644 (file)
index 0000000..e73fd53
--- /dev/null
@@ -0,0 +1,13 @@
+name: Deploy Docs
+on:
+  schedule:
+    - cron: "0 * * * *"
+
+jobs:
+  deploy:
+    runs-on: ubuntu-18.04
+    steps:
+      - uses: actions/checkout@v2
+      - uses: ./.github/actions/watch-previews
+        with:
+          token: ${{ secrets.GITHUB_TOKEN }}
index 1f8a25fe70129294dde3657966b684ae5f67fa7d..c2a69cde65109aff5dd94376eb8aff2a3207008f 100644 (file)
@@ -2,6 +2,7 @@
 
 ## Latest changes
 
+* Add custom GitHub Action to get artifact with docs preview. PR [#1739](https://github.com/tiangolo/fastapi/pull/1739).
 * Add new GitHub Actions to preview docs from PRs. PR [#1738](https://github.com/tiangolo/fastapi/pull/1738).
 * Add XML test coverage to support GitHub Actions. PR [#1737](https://github.com/tiangolo/fastapi/pull/1737).
 * Update badges and remove Travis now that GitHub Actions is the main CI. PR [#1736](https://github.com/tiangolo/fastapi/pull/1736).
index fa4214b8ad620a34518ea48a6dd2cfc1ce108de8..7bd77455bb580597352443fad551a8c3202bca84 100644 (file)
@@ -6,8 +6,9 @@ set -e
 PR=${PR:?Variable not set}
 DEPLOY_URL=${DEPLOY_URL:?Variable not set}
 GITHUB_TOKEN=${GITHUB_TOKEN:?Variable not set}
+COMMIT=${COMMIT:?Variable not set}
 
 curl \
     -H "Authorization: token ${GITHUB_TOKEN}" \
     https://api.github.com/repos/tiangolo/fastapi/issues/${PR}/comments \
-    -d '{"body": "📝 Docs preview: '"${DEPLOY_URL}"'"}'
+    -d '{"body": "📝 Docs preview for commit '"${COMMIT} at: ${DEPLOY_URL}"'"}'
index 7dbb8063d6af9517583cad2b46d8fc5f8aae390c..d08d7d49eb4f27a4d8ddfb465f87d4caf8e38f5e 100644 (file)
@@ -13,7 +13,7 @@ fi
 NAME=${NAME:?Variable not set}
 GITHUB_TOKEN=${GITHUB_TOKEN:?Variable not set}
 
-curl \
+curl -v \
   -X POST \
   -H "Authorization: token ${GITHUB_TOKEN}" \
   -H "Accept: application/vnd.github.v3+json" \