From 5f1a16d36c7034a729bd07723a28520051e27f02 Mon Sep 17 00:00:00 2001 From: OniriCorpe Date: Wed, 14 Feb 2024 03:40:26 +0100 Subject: [PATCH] Initial support for Gitea & Forgejo --- .../autoupdate_app_sources.py | 29 +++++++++++------- autoupdate_app_sources/rest_api.py | 30 +++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/autoupdate_app_sources/autoupdate_app_sources.py b/autoupdate_app_sources/autoupdate_app_sources.py index b42dc78..92b758c 100644 --- a/autoupdate_app_sources/autoupdate_app_sources.py +++ b/autoupdate_app_sources/autoupdate_app_sources.py @@ -10,7 +10,7 @@ from datetime import datetime import requests import toml -from rest_api import GithubAPI, GitlabAPI, RefType +from rest_api import GithubAPI, GitlabAPI, GiteaForgejoAPI, RefType STRATEGIES = [ "latest_github_release", @@ -18,7 +18,10 @@ STRATEGIES = [ "latest_github_commit", "latest_gitlab_release", "latest_gitlab_tag", - "latest_gitlab_commit" + "latest_gitlab_commit", + "latest_giteaforgejo_release", + "latest_giteaforgejo_tag", + "latest_giteaforgejo_commit" ] if "--commit-and-create-PR" not in sys.argv: @@ -173,7 +176,7 @@ class AppAutoUpdater: print(f"\n Checking {source} ...") - if strategy == "latest_github_release" or strategy == "latest_gitlab_release": + if "_release" in strategy: ( new_version, new_asset_urls, @@ -240,7 +243,7 @@ class AppAutoUpdater: return bool(todos) if "main" in todos: - if strategy == "latest_github_release": + if "_release" in strategy: title = f"Upgrade to v{new_version}" message = f"Upgrade to v{new_version}\nChangelog: {changelog_url}" else: @@ -302,8 +305,10 @@ class AppAutoUpdater: api = GithubAPI(upstream, auth=auth) elif "gitlab" in strategy: api = GitlabAPI(upstream) + elif "gitea" in strategy or "forgejo" in strategy: + api = GiteaForgejoAPI(upstream) - if strategy == "latest_github_release" or strategy == "latest_gitlab_release": + if "_release" in strategy: releases = api.releases() tags = [ release["tag_name"] @@ -323,7 +328,11 @@ class AppAutoUpdater: for a in latest_release["assets"] if not a["name"].endswith(".md5") } - latest_release_html_url = latest_release["html_url"] + if strategy == "latest_gitlab_release": + # gitlab's API is different for that + latest_release_html_url = latest_release["_links"]["self"] + else: + latest_release_html_url = latest_release["html_url"] if asset == "tarball": latest_tarball = ( api.url_for_ref(latest_version_orig, RefType.tags) @@ -373,10 +382,10 @@ class AppAutoUpdater: latest_release_html_url, ) - elif strategy == "latest_github_tag" or strategy == "latest_gitlab_tag": + elif "_tag" in strategy: if asset != "tarball": raise Exception( - "For the latest_github_tag strategy, only asset = 'tarball' is supported" + "For the latest tag strategy, only asset = 'tarball' is supported" ) tags = api.tags() latest_version_orig, latest_version = filter_and_get_latest_tag( @@ -385,10 +394,10 @@ class AppAutoUpdater: latest_tarball = api.url_for_ref(latest_version_orig, RefType.tags) return latest_version, latest_tarball - elif strategy == "latest_github_commit" or strategy == "latest_gitlab_commit": + elif "_commit" in strategy: if asset != "tarball": raise Exception( - "For the latest_github_release strategy, only asset = 'tarball' is supported" + "For the latest release strategy, only asset = 'tarball' is supported" ) commits = api.commits() latest_commit = commits[0] diff --git a/autoupdate_app_sources/rest_api.py b/autoupdate_app_sources/rest_api.py index ccc6b4d..22374bb 100644 --- a/autoupdate_app_sources/rest_api.py +++ b/autoupdate_app_sources/rest_api.py @@ -111,3 +111,33 @@ class GitlabAPI: def url_for_ref(self, ref: str, ref_type: RefType) -> str: return f"{self.upstream}/api/v4/projects/{self.project_id}/repository/archive.tar.gz/?sha={ref}" + + +class GiteaForgejoAPI: + def __init__(self, upstream: str): + split = re.search("(?Phttps?://.+)/(?P[^/]+)/(?P[^/]+)/?$", upstream) + self.upstream = split.group("host") + self.upstream_repo = f"{split.group('group')}/{split.group('project')}" + self.project_id = self.find_project_id(self.upstream_repo) + + def internal_api(self, uri: str): + url = f"{self.upstream}/api/v1/{uri}" + r = requests.get(url) + assert r.status_code == 200, r + return r.json() + + def tags(self) -> List[str]: + """Get a list of tags for project.""" + return self.internal_api(f"repos/{self.upstream_repo}/tags") + + def commits(self) -> List[str]: + """Get a list of commits for project.""" + return self.internal_api(f"repos/{self.upstream_repo}/commits") + + def releases(self) -> List[str]: + """Get a list of releases for project.""" + return self.internal_api(f"repos/{self.upstream_repo}/releases") + + def url_for_ref(self, ref: str, ref_type: RefType) -> str: + """Get a URL for a ref.""" + return f"{self.upstream}/{self.upstream_repo}/archive/{ref}.tar.gz"