Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Last-Translators to commit message #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 3.14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/update-lint-and-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
run: >
! 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
- run: git add .
- run: git commit -m 'Update translation from Transifex'
- run: git commit -m '$(python manage_translation.py generate_commit_msg)'
if: env.SIGNIFICANT_CHANGES
- name: Push commit
uses: ad-m/github-push-action@master
Expand Down
42 changes: 36 additions & 6 deletions manage_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# files.
# * recreate_tx_config: recreate configuration for all resources.
# * warn_about_files_to_delete: lists files that are not available upstream
# * generate_commit_msg: generates commit message with co-authors

from argparse import ArgumentParser
import os
Expand All @@ -19,7 +20,7 @@
from difflib import SequenceMatcher
from logging import info
from pathlib import Path
from subprocess import call
from subprocess import call, run, CalledProcessError
import sys
from tempfile import TemporaryDirectory
from typing import Self, Generator, Iterable
Expand All @@ -29,6 +30,8 @@
from transifex.api import transifex_api

LANGUAGE = 'pl'
PROJECT_SLUG = 'python-newest'
VERSION = '3.14'


def fetch():
Expand All @@ -49,10 +52,6 @@ def _call(command: str):
exit(return_code)


PROJECT_SLUG = 'python-newest'
VERSION = '3.14'


def recreate_tx_config():
"""
Regenerate Transifex client config for all resources.
Expand Down Expand Up @@ -187,8 +186,39 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool:
return any(entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes)


def generate_commit_msg():
"""Generate a commit message
Parses staged files and generates a commit message with Last-Translator's as
co-authors.
"""
translators: set[str] = set()

result = run(["git", "diff", "--cached", "--name-only", "--diff-filter=ACM"], capture_output=True, text=True, check=True)
staged = [filename for filename in result.stdout.splitlines() if filename.endswith(".po")]

for file in staged:
staged_file = run(["git", "show", f":{file}"], capture_output=True, text=True, check=True).stdout
try:
old_file = run(["git", "show", f"HEAD:{file}"], capture_output=True, text=True, check=True).stdout
except CalledProcessError:
old_file = ""

new_po = pofile(staged_file)
old_po = pofile(old_file) if old_file else POFile()
old_entries = {entry.msgid: entry.msgstr for entry in old_po}

for entry in new_po:
if entry.msgstr and (entry.msgid not in old_entries or old_entries[entry.msgid] != entry.msgstr):
translator = new_po.metadata.get("Last-Translator")
translator = translator.split(",")[0].strip()
if translator:
translators.add(f'Co-Authored-By: {translator}')
break

print('Update translation from Transifex\n\n' + "\n".join(translators))

if __name__ == "__main__":
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'warn_about_files_to_delete')
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'warn_about_files_to_delete', 'generate_commit_msg')

parser = ArgumentParser()
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)
Expand Down