66
77permissions :
88 contents : read
9+ pull-requests : read
910
1011jobs :
1112 define-comment :
@@ -14,59 +15,120 @@ jobs:
1415 outputs :
1516 any_changed : ${{ steps.changed-files.outputs.any_changed }}
1617 comment : ${{ steps.create-pr-comment.outputs.comment }}
18+
1719 steps :
20+ # Obtener únicamente los archivos del repositorio original
1821 - uses : actions/checkout@v6
1922 with :
20- ref : ${{ github.event.pull_request.head.sha }}
23+ path : base-branch
2124 persist-credentials : false
22- allow-unsafe-pr-checkout : true
2325
2426 - name : Preparar Python v3.13
2527 uses : actions/setup-python@v7
2628 with :
2729 python-version : " 3.13"
2830 cache : " pip"
31+ cache-dependency-path : base-branch/requirements-own.txt
2932
30- # checkout these files from the base branch to guarantee they haven't been
31- # modified by the PR
32- - uses : actions/checkout@v6
33+ # Obtener los archivos .po modificados mediante la API
34+ - name : Obtiene lista de archivos con cambios
35+ id : changed-files
36+ uses : tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6
3337 with :
34- path : base-branch
35- sparse-checkout-cone-mode : false
36- sparse-checkout : |
37- requirements-own.txt
38- scripts/list_missing_entries.py
38+ files : |
39+ **/*.po
3940
4041 - name : Instalar dependencias
42+ if : steps.changed-files.outputs.any_changed == 'true'
4143 run : |
4244 python -m pip install -r base-branch/requirements-own.txt
4345
44- - name : Obtiene lista de archivos con cambios
45- id : changed-files
46- uses : tj-actions/changed-files@v47
47- with :
48- files : |
49- **/*.po
46+ # Descargar los archivos modificados como datos, sin ejecutar su código
47+ - name : Descargar archivos PO de la Pull Request
48+ if : steps.changed-files.outputs.any_changed == 'true'
49+ env :
50+ GH_TOKEN : ${{ github.token }}
51+ GH_REPOSITORY : ${{ github.repository }}
52+ PR_HEAD_SHA : ${{ github.event.pull_request.head.sha }}
53+ CHANGED_PO_FILES : ${{ steps.changed-files.outputs.all_changed_files }}
54+ run : |
55+ python - <<'PY'
56+ import base64
57+ import json
58+ import os
59+ import pathlib
60+ import urllib.parse
61+ import urllib.request
62+
63+ repository = os.environ["GH_REPOSITORY"]
64+ commit = os.environ["PR_HEAD_SHA"]
65+ token = os.environ["GH_TOKEN"]
66+ files = os.environ["CHANGED_PO_FILES"].splitlines()
67+
68+ api_url = os.environ["GITHUB_API_URL"]
69+
70+ for filename in files:
71+ path = pathlib.PurePosixPath(filename)
72+
73+ if path.is_absolute() or ".." in path.parts:
74+ raise ValueError(f"Ruta no permitida: {filename}")
75+
76+ if path.suffix != ".po":
77+ raise ValueError(f"Archivo no permitido: {filename}")
78+
79+ url = (
80+ f"{api_url}/repos/{repository}/contents/"
81+ f"{urllib.parse.quote(filename, safe='/')}"
82+ f"?ref={commit}"
83+ )
84+
85+ request = urllib.request.Request(
86+ url,
87+ headers={
88+ "Authorization": f"Bearer {token}",
89+ "Accept": "application/vnd.github+json",
90+ "X-GitHub-Api-Version": "2022-11-28",
91+ },
92+ )
93+
94+ with urllib.request.urlopen(request) as response:
95+ data = json.loads(response.read())
96+
97+ if data.get("encoding") != "base64":
98+ raise ValueError(f"Formato inesperado: {filename}")
99+
100+ destination = pathlib.Path("pr-files") / filename
101+ destination.parent.mkdir(parents=True, exist_ok=True)
102+ destination.write_bytes(
103+ base64.b64decode(data["content"])
104+ )
105+ PY
50106
51107 - name : Calcular entradas faltantes
52108 if : steps.changed-files.outputs.any_changed == 'true'
53109 id : create-pr-comment
54110 env :
55111 CHANGED_PO_FILES : ${{ steps.changed-files.outputs.all_changed_files }}
56112 run : |
113+ cd pr-files
114+
115+ mapfile -t PO_FILES <<< "$CHANGED_PO_FILES"
116+
57117 {
58118 echo 'comment<<EOF'
59- python base-branch/scripts/list_missing_entries.py --github $CHANGED_PO_FILES
119+ python ../ base-branch/scripts/list_missing_entries.py --github-mode "${PO_FILES[@]}"
60120 echo EOF
61121 } >> "$GITHUB_OUTPUT"
62122
63123 write-comment :
64124 runs-on : ubuntu-22.04
65125 needs : [define-comment]
66126 if : needs.define-comment.outputs.any_changed == 'true'
127+
67128 permissions :
68129 issues : write
69130 pull-requests : write
131+
70132 steps :
71133 - name : Agregar comentario con entradas faltantes
72134 uses : thollander/actions-comment-pull-request@v3
0 commit comments