diff --git a/.github/workflows/build-artifact.yml b/.github/workflows/build-artifact.yml index 6b66073..e8bbf11 100644 --- a/.github/workflows/build-artifact.yml +++ b/.github/workflows/build-artifact.yml @@ -43,6 +43,7 @@ jobs: - name: Update Env for custom build run: | echo "${{ secrets.ENVS }}" >> .env + echo "GITHUB_REPOSITORY=${{ github.repository }}" >> .env - name: Install Dot-Env if: ${{ inputs.PREFERRED_PATCH_APPS }} diff --git a/main.py b/main.py index be9717f..cd25dd7 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ from src.downloader.download import Downloader from src.exceptions import AppNotFoundError, BuilderError, PatchesJsonLoadError, PatchingFailedError from src.parser import Parser from src.patches import Patches -from src.utils import check_java, delete_old_changelog, save_patch_info, write_changelog_to_file +from src.utils import check_java, delete_old_changelog, load_older_updates, save_patch_info, write_changelog_to_file def get_app(config: RevancedConfig, app_name: str) -> APP: @@ -26,10 +26,12 @@ def main() -> None: env = Env() env.read_env() config = RevancedConfig(env) + updates_info = {} Downloader.extra_downloads(config) if not config.dry_run: check_java() delete_old_changelog() + updates_info = load_older_updates(env) logger.info(f"Will Patch only {config.apps}") for possible_app in config.apps: @@ -43,7 +45,7 @@ def main() -> None: app.download_apk_for_patching(config) parser.include_exclude_patch(app, app_all_patches, patcher.patches_dict) logger.info(app) - save_patch_info(app) + updates_info = save_patch_info(app, updates_info) parser.patch_app(app) except AppNotFoundError as e: logger.info(e) @@ -53,7 +55,7 @@ def main() -> None: logger.exception(e) except BuilderError as e: logger.exception(f"Failed to build {possible_app} because of {e}") - write_changelog_to_file() + write_changelog_to_file(updates_info) if __name__ == "__main__": diff --git a/src/manager/github.py b/src/manager/github.py index 86e847c..1288b0b 100644 --- a/src/manager/github.py +++ b/src/manager/github.py @@ -10,15 +10,17 @@ from environs import Env from src.app import APP from src.manager.release_manager import ReleaseManager -from src.utils import branch_name, updates_file +from src.utils import branch_name, updates_file, updates_file_url class GitHubManager(ReleaseManager): """Release manager with GitHub.""" def __init__(self: Self, env: Env) -> None: - self.update_file_url = ( - f"https://raw.githubusercontent.com/{env.str('GITHUB_REPOSITORY')}/{branch_name}/{updates_file}" + self.update_file_url = updates_file_url.format( + github_repository=env.str("GITHUB_REPOSITORY"), + branch_name=branch_name, + updates_file=updates_file, ) def get_last_version(self: Self, app: APP, resource_name: str) -> str: diff --git a/src/utils.py b/src/utils.py index 0436083..d09c94e 100644 --- a/src/utils.py +++ b/src/utils.py @@ -6,12 +6,14 @@ import re import subprocess import sys import time +import urllib.error +import urllib.request from datetime import datetime -from json import JSONDecodeError from pathlib import Path from typing import TYPE_CHECKING, Any import requests +from environs import Env from loguru import logger from pytz import timezone from requests import Response, Session @@ -45,6 +47,7 @@ request_timeout = 60 session = Session() session.headers["User-Agent"] = request_header["User-Agent"] updates_file = "updates.json" +updates_file_url = "https://raw.githubusercontent.com/{github_repository}/{branch_name}/{updates_file}" changelogs: dict[str, dict[str, str]] = {} time_zone = "Asia/Kolkata" app_version_key = "app_version" @@ -99,7 +102,7 @@ def format_changelog(name: str, response: dict[str, str]) -> dict[str, str]: } -def write_changelog_to_file() -> None: +def write_changelog_to_file(updates_info: dict[str, Any]) -> None: """The function `write_changelog_to_file` writes a given changelog json to a file.""" markdown_table = inspect.cleandoc( """ @@ -124,6 +127,7 @@ def write_changelog_to_file() -> None: with Path(changelog_file).open("w", encoding="utf_8") as file1: file1.write(markdown_table) Path(changelog_json_file).write_text(json.dumps(changelogs, indent=4) + "\n") + Path(updates_file).write_text(json.dumps(updates_info, indent=4, default=str) + "\n") def get_parent_repo() -> str: @@ -242,16 +246,24 @@ def datetime_to_ms_epoch(dt: datetime) -> int: return int(round(microseconds / float(1000))) -def save_patch_info(app: "APP") -> None: - """Save version info a patching resources used to a file.""" +def load_older_updates(env: Env) -> dict[str, Any]: + """Load older updated from updates.json.""" + update_file_url = updates_file_url.format( + github_repository=env.str("GITHUB_REPOSITORY"), + branch_name=branch_name, + updates_file=updates_file, + ) try: - with Path(updates_file).open() as file: - old_version = json.load(file) - except (JSONDecodeError, FileNotFoundError): - # Handle the case when the file is empty - old_version = {} # or any default value you want to assign + with urllib.request.urlopen(update_file_url) as url: + return json.load(url) # type: ignore[no-any-return] + except urllib.error.URLError as e: + logger.error(f"Failed to retrieve update file: {e}") + return {} - old_version[app.app_name] = { + +def save_patch_info(app: "APP", updates_info: dict[str, Any]) -> dict[str, Any]: + """Save version info a patching resources used to a file.""" + updates_info[app.app_name] = { app_version_key: app.app_version, integration_version_key: app.resource["integrations"]["version"], patches_version_key: app.resource["patches"]["version"], @@ -261,4 +273,4 @@ def save_patch_info(app: "APP") -> None: "date_patched": datetime.now(timezone(time_zone)), "app_dump": app.for_dump(), } - Path(updates_file).write_text(json.dumps(old_version, indent=4, default=str) + "\n") + return updates_info