Skip to content

Commit 3620d73

Browse files
Add Last-Translators to TX pull commit message (#88)
1 parent ee9daeb commit 3620d73

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

.github/workflows/update-lint-and-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
run: >
5757
! git diff -I'^"POT-Creation-Date: ' -I'^"Language-Team: ' -I'^# ' -I'^"Last-Translator: ' -I'^"Project-Id-Version: ' --exit-code && echo "SIGNIFICANT_CHANGES=1" >> "$GITHUB_ENV" || exit 0
5858
- run: git add .
59-
- run: git commit -m 'Update translation from Transifex'
59+
- run: git commit -m "$(python manage_translation.py generate_commit_msg)"
6060
if: env.SIGNIFICANT_CHANGES
6161
- name: Push commit
6262
uses: ad-m/github-push-action@master

manage_translation.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# files.
1212
# * recreate_tx_config: recreate configuration for all resources.
1313
# * warn_about_files_to_delete: lists files that are not available upstream
14+
# * generate_commit_msg: generates commit message with co-authors
1415

1516
from argparse import ArgumentParser
1617
import os
@@ -19,13 +20,13 @@
1920
from difflib import SequenceMatcher
2021
from logging import info
2122
from pathlib import Path
22-
from subprocess import call
23+
from subprocess import call, run, CalledProcessError
2324
import sys
2425
from tempfile import TemporaryDirectory
2526
from typing import Self, Generator, Iterable
2627
from warnings import warn
2728

28-
from polib import pofile
29+
from polib import pofile, POFile
2930
from transifex.api import transifex_api
3031

3132
LANGUAGE = 'pl'
@@ -202,8 +203,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool:
202203
)
203204

204205

206+
def generate_commit_msg():
207+
"""Generate a commit message
208+
Parses staged files and generates a commit message with Last-Translator's as
209+
co-authors.
210+
"""
211+
translators: set[str] = set()
212+
213+
result = run(
214+
['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'],
215+
capture_output=True,
216+
text=True,
217+
check=True,
218+
)
219+
staged = [
220+
filename for filename in result.stdout.splitlines() if filename.endswith('.po')
221+
]
222+
223+
for file in staged:
224+
staged_file = run(
225+
['git', 'show', f':{file}'], capture_output=True, text=True, check=True
226+
).stdout
227+
try:
228+
old_file = run(
229+
['git', 'show', f'HEAD:{file}'],
230+
capture_output=True,
231+
text=True,
232+
check=True,
233+
).stdout
234+
except CalledProcessError:
235+
old_file = ''
236+
237+
new_po = pofile(staged_file)
238+
old_po = pofile(old_file) if old_file else POFile()
239+
old_entries = {entry.msgid: entry.msgstr for entry in old_po}
240+
241+
for entry in new_po:
242+
if entry.msgstr and (
243+
entry.msgid not in old_entries
244+
or old_entries[entry.msgid] != entry.msgstr
245+
):
246+
translator = new_po.metadata.get('Last-Translator')
247+
translator = translator.split(',')[0].strip()
248+
if translator:
249+
translators.add(f'Co-Authored-By: {translator}')
250+
break
251+
252+
print('Update translation from Transifex\n\n' + '\n'.join(translators))
253+
254+
205255
if __name__ == '__main__':
206-
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'warn_about_files_to_delete')
256+
RUNNABLE_SCRIPTS = (
257+
'fetch',
258+
'recreate_tx_config',
259+
'warn_about_files_to_delete',
260+
'generate_commit_msg',
261+
)
207262

208263
parser = ArgumentParser()
209264
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)

0 commit comments

Comments
 (0)