Skip to content

Commit

Permalink
Agrega comentario a PR con entradas faltantes (#2726)
Browse files Browse the repository at this point in the history
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
rtobar#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
rtobar#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 <[email protected]>
  • Loading branch information
rtobar authored Nov 6, 2023
1 parent 811360b commit 7680210
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/pr-comment.yml
Original file line number Diff line number Diff line change
@@ -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<<EOF'
python scripts/list_missing_entries.py --github $CHANGED_PO_FILES
echo EOF
} >> "$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
55 changes: 55 additions & 0 deletions scripts/list_missing_entries.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7680210

Please sign in to comment.