From 15340bc8eb9743fa0ef1e96956d5736d68714384 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Tue, 2 Apr 2024 07:10:39 +0200 Subject: [PATCH] fix(translate_apps): testing branch existance needed to be checked before cloning --- translate_apps/apps_translations_to_apps.py | 29 +++++++++---------- translate_apps/base.py | 15 ++++++++++ .../push_or_update_apps_on_repository.py | 13 +++------ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/translate_apps/apps_translations_to_apps.py b/translate_apps/apps_translations_to_apps.py index 0f745e56..6d77c17d 100644 --- a/translate_apps/apps_translations_to_apps.py +++ b/translate_apps/apps_translations_to_apps.py @@ -5,7 +5,7 @@ import tomlkit -from base import Repository, login, token, WORKING_BRANCH +from base import Repository, login, token, WORKING_BRANCH, get_repository_branches def extract_strings_to_translate_from_apps(apps, translations_repository): @@ -39,26 +39,25 @@ def extract_strings_to_translate_from_apps(apps, translations_repository): translations_path = translations_repository.path / translations_path + if "testing" in get_repository_branches(repository_uri, token): + branch = "testing" + with Repository( f"https://{login}:{token}@github.com/{repository_uri}", branch ) as 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", - ] - ) - branch = "testing" - else: - repository.run_command(["git", "checkout", "-b", WORKING_BRANCH]) + repository.run_command( + [ + "git", + "checkout", + "-b", + WORKING_BRANCH, + "--track", + "origin/{branch}", + ] + ) manifest = tomlkit.loads(repository.read_file("manifest.toml")) diff --git a/translate_apps/base.py b/translate_apps/base.py index f98beb9e..d739da8d 100644 --- a/translate_apps/base.py +++ b/translate_apps/base.py @@ -2,6 +2,8 @@ import tempfile import subprocess +import requests + from typing import Union from pathlib import Path @@ -24,6 +26,19 @@ WORKING_BRANCH = "manifest_toml_i18n" +def get_repository_branches(repository, token): + branches = requests.get( + f"https://api.github.com/repos/{repository}/branches", + headers={ + "Authorization": f"Bearer {token}", + "X-GitHub-Api-Version": "2022-11-28", + "Accept": "application/vnd.github+json", + }, + ).json() + + return {x["name"] for x in branches} + + class Repository: def __init__(self, url, branch): self.url = url diff --git a/translate_apps/push_or_update_apps_on_repository.py b/translate_apps/push_or_update_apps_on_repository.py index afdbfefd..453d8939 100644 --- a/translate_apps/push_or_update_apps_on_repository.py +++ b/translate_apps/push_or_update_apps_on_repository.py @@ -7,7 +7,7 @@ import wlc import tomlkit -from base import Repository, login, token, weblate_token +from base import Repository, login, token, weblate_token, get_repository_branches def get_weblate_component(weblate, component_path): @@ -40,20 +40,15 @@ def extract_strings_to_translate_from_apps(apps, translations_repository): print("=" * len(app)) print(f"{repository_uri} -> branch '{branch}'") + if "testing" in get_repository_branches(repository_uri, token): + branch = "testing" + with Repository( f"https://{login}:{token}@github.com/{repository_uri}", branch ) as 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/")