From 7680210a8d086d11436017e64b832b546794f547 Mon Sep 17 00:00:00 2001 From: rtobar Date: Tue, 7 Nov 2023 05:30:18 +0800 Subject: [PATCH] Agrega comentario a PR con entradas faltantes (#2726) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Este PR agrega una nueva acción que agrega un comentario a los PRs que editan archivos .po indicando qué entradas aún no tienen una traducción. Un ejemplo de esto funcionando se puede ver en https://github.com/rtobar/python-docs-es/pull/3. Hay varios comentarios automáticos porque use ese PR para debuguear el proceso, pero la idea es que finalmente haya un solo comentario que se va actualizando (como se puede ver en el último comentario, que está editado). Otro ejemplo se puede ver en https://github.com/rtobar/python-docs-es/pull/5, donde el PR viene desde un fork en vez de venir desde el mismo repositorio. Otra opción habría sido agregar un check a CI que falle si faltan entradas, pero en 3.11 ya tuvimos el caso en que *tuvimos* que dejar entradas fuzzy a propósito. --------- Signed-off-by: Rodrigo Tobar --- .github/workflows/pr-comment.yml | 45 ++++++++++++++++++++++++++ scripts/list_missing_entries.py | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 .github/workflows/pr-comment.yml create mode 100644 scripts/list_missing_entries.py diff --git a/.github/workflows/pr-comment.yml b/.github/workflows/pr-comment.yml new file mode 100644 index 0000000000..7be6faccd1 --- /dev/null +++ b/.github/workflows/pr-comment.yml @@ -0,0 +1,45 @@ +name: Agrega comentario a PR + +on: + pull_request_target: + +jobs: + pr-comment: + name: Entradas sin traducción + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.merge_commit_sha }} + persist-credentials: false + - name: Preparar Python v3.11 + uses: actions/setup-python@v4 + with: + python-version: "3.11" + cache: "pip" + - name: Instalar dependencias + run: | + python -m pip install -r requirements.txt + - name: Obtiene lista de archivos con cambios + id: changed-files + uses: tj-actions/changed-files@v39 + with: + files: | + **/*.po + - name: Calcular entradas faltantes + if: steps.changed-files.outputs.any_changed == 'true' + id: create-pr-comment + env: + CHANGED_PO_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + run: | + { + echo 'comment<> "$GITHUB_OUTPUT" + - name: Agregar comentario con entradas faltantes + if: steps.changed-files.outputs.any_changed == 'true' + uses: thollander/actions-comment-pull-request@v2 + with: + message: ${{ steps.create-pr-comment.outputs.comment }} + comment_tag: missing-entries diff --git a/scripts/list_missing_entries.py b/scripts/list_missing_entries.py new file mode 100644 index 0000000000..4c51b37a63 --- /dev/null +++ b/scripts/list_missing_entries.py @@ -0,0 +1,55 @@ +import argparse +import dataclasses +import enum +import glob +import itertools +import os + +import polib +import tabulate + + +class MissingReason(enum.StrEnum): + FUZZY = "fuzzy" + UNTRANSLATED = "untranslated" + + @staticmethod + def from_poentry(poentry: polib.POEntry): + if poentry.fuzzy: + return MissingReason.FUZZY + assert not poentry.translated() + return MissingReason.UNTRANSLATED + +@dataclasses.dataclass +class MissingEntry: + reason: MissingReason + file: str + line: int + + @staticmethod + def from_poentry(pofilename: str, poentry: polib.POEntry) -> "MissingEntry": + return MissingEntry(MissingReason.from_poentry(poentry), pofilename, poentry.linenum) + + +def find_missing_entries(filename: str) -> list[MissingEntry]: + po = polib.pofile(filename) + fuzzy = po.fuzzy_entries() + untranslated = po.untranslated_entries() + return [MissingEntry.from_poentry(filename, entry) for entry in fuzzy + untranslated] + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("files", nargs="+") + parser.add_argument("-g", "--github-mode", help="Produce output as a GitHub comment", action='store_true') + opts = parser.parse_args() + missing_entries = list(itertools.chain.from_iterable(map(find_missing_entries, opts.files))) + if not missing_entries: + print(f"All entries translated, horray!{' :tada:' if opts.github_mode else ''}") + else: + missing_entries.sort(key = lambda entry: (entry.file, entry.line)) + print("Entries missing translation, details follow:\n") + print(tabulate.tabulate(missing_entries,headers=["Reason", "File", "Line"], tablefmt="github")) + + +if __name__ == "__main__": + main()