From 1fd3f96d0ee57677ae3bbd72ba7c0af25209a62c Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Thu, 28 Mar 2024 07:09:30 +0100 Subject: [PATCH] feat(translate_apps): use testing branch if it exists --- .../apps_translations_to_apps.py | 21 +++++++++++++++++-- tools/translate_apps/base.py | 16 ++++++++++++++ .../push_or_update_apps_on_repository.py | 8 +++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/tools/translate_apps/apps_translations_to_apps.py b/tools/translate_apps/apps_translations_to_apps.py index b4ec6948..ade5cc64 100644 --- a/tools/translate_apps/apps_translations_to_apps.py +++ b/tools/translate_apps/apps_translations_to_apps.py @@ -5,7 +5,7 @@ from pathlib import Path import tomlkit -from base import Repository, login, token +from base import Repository, login, token, WORKING_BRANCH def extract_strings_to_translate_from_apps(apps, translations_repository): @@ -43,6 +43,23 @@ def extract_strings_to_translate_from_apps(apps, translations_repository): if not repository.file_exists("manifest.toml"): continue + if repository.run_command_as_if(["git", "rev-parse", "--verify", "origin/testing"]): + repository.run_command(["git", "checkout", "-b", WORKING_BRANCH, "--track", "origin/testing"]) + if repository.run_command_as_if( + ["git", "rev-parse", "--verify", "origin/testing"] + ): + repository.run_command( + [ + "git", + "checkout", + "-b", + WORKING_BRANCH, + "--track", + "origin/testing", + ] + ) + repository.run_command(["git", "checkout", "-b", WORKING_BRANCH]) + manifest = tomlkit.loads(repository.read_file("manifest.toml")) for translation in translations_path.glob("*.json"): @@ -77,7 +94,7 @@ def extract_strings_to_translate_from_apps(apps, translations_repository): repository.run_command("git diff") repository.run_command("git add manifest.toml") repository.run_command(["git", "commit", "-m", "feat(i18n): update translations for manifest.toml"]) - repository.run_command(["git", "push", "-f", "origin", "master:manifest_toml_i18n"]) + repository.run_command(["git", "push", "-f", "origin", f"{WORKING_BRANCH}:manifest_toml_i18n"]) # if no PR exist, create one if not repository.run_command("hub pr list -h manifest_toml_i18n", capture_output=True): diff --git a/tools/translate_apps/base.py b/tools/translate_apps/base.py index b0f5b957..f98beb9e 100644 --- a/tools/translate_apps/base.py +++ b/tools/translate_apps/base.py @@ -21,6 +21,8 @@ my_env["GIT_COMMITTER_EMAIL"] = "yunohost@yunohost.org" my_env["GITHUB_USER"] = login my_env["GITHUB_TOKEN"] = token +WORKING_BRANCH = "manifest_toml_i18n" + class Repository: def __init__(self, url, branch): @@ -63,6 +65,20 @@ class Repository: print(f"\033[1;31m>>\033[0m \033[0;34m{command}\033[0m") return subprocess.check_call(**kwargs) + def run_command_as_if(self, command: Union[str, list]) -> bool: + if isinstance(command, str): + kwargs = { + "args": f"cd {self.path} && {command}", + "shell": True, + "env": my_env, + } + + elif isinstance(command, list): + kwargs = {"args": command, "cwd": self.path, "env": my_env} + + print(f"\033[1;31m>>\033[0m \033[0;34m{command}\033[0m") + return subprocess.run(**kwargs).returncode == 0 + def file_exists(self, file_name: str) -> bool: return (self.path / file_name).exists() diff --git a/tools/translate_apps/push_or_update_apps_on_repository.py b/tools/translate_apps/push_or_update_apps_on_repository.py index e8119ae3..236cbebb 100644 --- a/tools/translate_apps/push_or_update_apps_on_repository.py +++ b/tools/translate_apps/push_or_update_apps_on_repository.py @@ -41,6 +41,14 @@ def extract_strings_to_translate_from_apps(apps, translations_repository): if not repository.file_exists("manifest.toml"): continue + # base our work on the testing branch if it exists + if repository.run_command_as_if( + ["git", "rev-parse", "--verify", "origin/testing"] + ): + repository.run_command( + ["git", "checkout", "-b", "testing", "--track", "origin/testing"] + ) + manifest = tomlkit.loads(repository.read_file("manifest.toml")) translations_path = Path(f"translations/apps/{app}/manifest/")