]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✨ Add GitHub Action: Notify Translations (#3715)
authorSebastián Ramírez <tiangolo@gmail.com>
Wed, 18 Aug 2021 13:07:08 +0000 (15:07 +0200)
committerGitHub <noreply@github.com>
Wed, 18 Aug 2021 13:07:08 +0000 (15:07 +0200)
.github/actions/notify-translations/Dockerfile [new file with mode: 0644]
.github/actions/notify-translations/action.yml [new file with mode: 0644]
.github/actions/notify-translations/app/main.py [new file with mode: 0644]
.github/actions/notify-translations/app/translations.yml [new file with mode: 0644]
.github/workflows/notify-translations.yml [new file with mode: 0644]

diff --git a/.github/actions/notify-translations/Dockerfile b/.github/actions/notify-translations/Dockerfile
new file mode 100644 (file)
index 0000000..fa4197e
--- /dev/null
@@ -0,0 +1,7 @@
+FROM python:3.7
+
+RUN pip install httpx PyGithub "pydantic==1.5.1" "pyyaml>=5.3.1,<6.0.0"
+
+COPY ./app /app
+
+CMD ["python", "/app/main.py"]
diff --git a/.github/actions/notify-translations/action.yml b/.github/actions/notify-translations/action.yml
new file mode 100644 (file)
index 0000000..c357997
--- /dev/null
@@ -0,0 +1,10 @@
+name: "Notify Translations"
+description: "Notify in the issue for a translation when there's a new PR available"
+author: "Sebastián Ramírez <tiangolo@gmail.com>"
+inputs:
+  token:
+    description: 'Token, to read the GitHub API. Can be passed in using {{ secrets.GITHUB_TOKEN }}'
+    required: true
+runs:
+  using: 'docker'
+  image: 'Dockerfile'
diff --git a/.github/actions/notify-translations/app/main.py b/.github/actions/notify-translations/app/main.py
new file mode 100644 (file)
index 0000000..7985051
--- /dev/null
@@ -0,0 +1,87 @@
+import logging
+from pathlib import Path
+from typing import Dict, Optional
+
+import yaml
+from github import Github
+from pydantic import BaseModel, BaseSettings, SecretStr
+
+awaiting_label = "awaiting review"
+lang_all_label = "lang-all"
+approved_label = "approved-2"
+translations_path = Path(__file__).parent / "translations.yml"
+
+
+class Settings(BaseSettings):
+    github_repository: str
+    input_token: SecretStr
+    github_event_path: Path
+    github_event_name: Optional[str] = None
+    input_debug: Optional[bool] = False
+
+
+class PartialGitHubEventIssue(BaseModel):
+    number: int
+
+
+class PartialGitHubEvent(BaseModel):
+    pull_request: PartialGitHubEventIssue
+
+
+if __name__ == "__main__":
+    settings = Settings()
+    if settings.input_debug:
+        logging.basicConfig(level=logging.DEBUG)
+    else:
+        logging.basicConfig(level=logging.INFO)
+    logging.debug(f"Using config: {settings.json()}")
+    g = Github(settings.input_token.get_secret_value())
+    repo = g.get_repo(settings.github_repository)
+    if not settings.github_event_path.is_file():
+        raise RuntimeError(
+            f"No github event file available at: {settings.github_event_path}"
+        )
+    contents = settings.github_event_path.read_text()
+    github_event = PartialGitHubEvent.parse_raw(contents)
+    translations_map: Dict[str, int] = yaml.safe_load(translations_path.read_text())
+    logging.debug(f"Using translations map: {translations_map}")
+    pr = repo.get_pull(github_event.pull_request.number)
+    logging.debug(f"Processing PR: {pr.number}")
+    if pr.state == "open":
+        logging.debug(f"PR is open: {pr.number}")
+        label_strs = set([label.name for label in pr.get_labels()])
+        if lang_all_label in label_strs and awaiting_label in label_strs:
+            logging.info(
+                f"This PR seems to be a language translation and awaiting reviews: {pr.number}"
+            )
+            if approved_label in label_strs:
+                message = (
+                    f"It seems this PR already has the approved label: {pr.number}"
+                )
+                logging.error(message)
+                raise RuntimeError(message)
+            langs = []
+            for label in label_strs:
+                if label.startswith("lang-") and not label == lang_all_label:
+                    langs.append(label[5:])
+            for lang in langs:
+                if lang in translations_map:
+                    num = translations_map[lang]
+                    logging.info(f"Found a translation issue for language: {lang} in issue: {num}")
+                    issue = repo.get_issue(num)
+                    message = f"Good news everyone! 😉 There's a new translation PR to be reviewed: #{pr.number} 🎉"
+                    already_notified = False
+                    logging.info(f"Checking current comments in issue: {num} to see if already notified about this PR: {pr.number}")
+                    for comment in issue.get_comments():
+                        if message in comment.body:
+                            already_notified = True
+                    if not already_notified:
+                        logging.info(f"Writing comment in issue: {num} about PR: {pr.number}")
+                        issue.create_comment(message)
+                    else:
+                        logging.info(f"Issue: {num} was already notified of PR: {pr.number}")
+    else:
+        logging.info(
+            f"Changing labels in a closed PR doesn't trigger comments, PR: {pr.number}"
+        )
+    logging.info("Finished")
diff --git a/.github/actions/notify-translations/app/translations.yml b/.github/actions/notify-translations/app/translations.yml
new file mode 100644 (file)
index 0000000..2ac9556
--- /dev/null
@@ -0,0 +1,12 @@
+pt: 1211
+es: 1218
+zh: 1228
+ru: 1362
+it: 1556
+ja: 1572
+uk: 1748
+tr: 1892
+fr: 1972
+ko: 2017
+sq: 2041
+pl: 3169
diff --git a/.github/workflows/notify-translations.yml b/.github/workflows/notify-translations.yml
new file mode 100644 (file)
index 0000000..7e414ab
--- /dev/null
@@ -0,0 +1,21 @@
+name: Notify Translations
+
+on:
+  pull_request_target:
+    types:
+      - labeled
+
+jobs:
+  notify-translations:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+      # Allow debugging with tmate
+      - name: Setup tmate session
+        uses: mxschmitt/action-tmate@v3
+        if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}
+        with:
+          limit-access-to-actor: true
+      - uses: ./.github/actions/notify-translations
+        with:
+          token: ${{ secrets.GITHUB_TOKEN }}