8000 Agrega comentario a PR con entradas faltantes (#2726) · python/python-docs-es@7680210 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7680210

Browse files
authored
Agrega comentario a PR con entradas faltantes (#2726)
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 <rtobar@icrar.org>
1 parent 811360b commit 7680210

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

.github/workflows/pr-comment.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Agrega comentario a PR
2+
3+
on:
4+
pull_request_target:
5+
6+
jobs:
7+
pr-comment:
8+
name: Entradas sin traducción
9+
runs-on: ubuntu-22.04
10+
steps:
11+
- uses: actions/checkout@v4
12+
with:
13+
ref: ${{ github.event.pull_request.merge_commit_sha }}
14+
persist-credentials: false
15+
- name: Preparar Python v3.11
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: "3.11"
19+
cache: "pip"
20+
- name: Instalar dependencias
21+
run: |
22+
python -m pip install -r requirements.txt
23+
- name: Obtiene lista de archivos con cambios
24+
id: changed-files
25+
uses: tj-actions/changed-files@v39
26+
with:
27+
files: |
28+
**/*.po
29+
- name: Calcular entradas faltantes
30+
if: steps.changed-files.outputs.any_changed == 'true'
31+
id: create-pr-comment
32+
env:
33+
CHANGED_PO_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
34+
run: |
35+
{
36+
echo 'comment<<EOF'
37+
python scripts/list_missing_entries.py --github $CHANGED_PO_FILES
38+
echo EOF
39+
} >> "$GITHUB_OUTPUT"
40+
- name: Agregar comentario con entradas faltantes
41+
if: steps.changed-files.outputs.any_changed == 'true'
42+
uses: thollander/actions-comment-pull-request@v2
43+
with:
44+
message: ${{ steps.create-pr-comment.outputs.comment }}
45+
comment_tag: missing-entries

scripts/list_missing_entries.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import argparse
2+
import dataclasses
3+
import enum
4+
import glob
5+
import itertools
6+
import os
7+
8+
import polib
9+
import tabulate
10+
11+
12+
class MissingReason(enum.StrEnum):
13+
FUZZY = "fuzzy"
14+
UNTRANSLATED = "untranslated"
15+
16+
@staticmethod
17+
def from_poentry(poentry: polib.POEntry):
18+
if poentry.fuzzy:
19+
return MissingReason.FUZZY
20+
assert not poentry.translated()
21+
return MissingReason.UNTRANSLATED
22+
23+
@dataclasses.dataclass
24+
class MissingEntry:
25+
reason: MissingReason
26+
file: str
27+
line: int
28+
29+
@staticmethod
30+
def from_poentry(pofilename: str, poentry: polib.POEntry) -> "MissingEntry":
31+
return MissingEntry(MissingReason.from_poentry(poentry), pofilename, poentry.linenum)
32+
33+
34+
def find_missing_entries(filename: str) -> list[MissingEntry]:
35+
po = polib.pofile(filename)
36+
fuzzy = po.fuzzy_entries()
37+
untranslated = po.untranslated_entries()
38+
return [MissingEntry.from_poentry(filename, entry) for entry in fuzzy + untranslated]
39+
40+
def main():
41+
parser = argparse.ArgumentParser()
42+
parser.add_argument("files", nargs="+")
43+
parser.add_argument("-g", "--github-mode", help="Produce output as a GitHub comment", action='store_true')
44+
opts = parser.parse_args()
45+
missing_entries = list(itertools.chain.from_iterable(map(find_missing_entries, opts.files)))
46+
if not missing_entries:
47+
print(f"All entries translated, horray!{' :tada:' if opts.github_mode else ''}")
48+
else:
49+
missing_entries.sort(key = lambda entry: (entry.file, entry.line))
50+
print("Entries missing translation, details follow:\n")
51+
print(tabulate.tabulate(missing_entries,headers=["Reason", "File", "Line"], tablefmt="github"))
52+
53+
54+
if __name__ == "__main__":
55+
main()

0 commit comments

Comments
 (0)
0