Browse Source

Add GitHub Action: Notify Translations (#3715)

pull/3718/head
Sebastián Ramírez 4 years ago
committed by GitHub
parent
commit
d0ced2c7c9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      .github/actions/notify-translations/Dockerfile
  2. 10
      .github/actions/notify-translations/action.yml
  3. 87
      .github/actions/notify-translations/app/main.py
  4. 12
      .github/actions/notify-translations/app/translations.yml
  5. 21
      .github/workflows/notify-translations.yml

7
.github/actions/notify-translations/Dockerfile

@ -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"]

10
.github/actions/notify-translations/action.yml

@ -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'

87
.github/actions/notify-translations/app/main.py

@ -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")

12
.github/actions/notify-translations/app/translations.yml

@ -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

21
.github/workflows/notify-translations.yml

@ -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 }}
Loading…
Cancel
Save