From 55b3f671c876abf2646d0afec9659b153ac01519 Mon Sep 17 00:00:00 2001 From: Daniel Peukert Date: Wed, 18 Oct 2023 21:46:28 +0200 Subject: [PATCH] implement second batch of URL results for sources --- nvchecker_source/bitbucket.py | 21 +++++++++++------- nvchecker_source/gitea.py | 16 +++++++++---- nvchecker_source/github.py | 42 ++++++++++++++++++++++++----------- nvchecker_source/gitlab.py | 17 +++++++++----- 4 files changed, 66 insertions(+), 30 deletions(-) diff --git a/nvchecker_source/bitbucket.py b/nvchecker_source/bitbucket.py index 211ff04..183497d 100644 --- a/nvchecker_source/bitbucket.py +++ b/nvchecker_source/bitbucket.py @@ -22,7 +22,7 @@ async def get_version( use_sorted_tags = conf.get('use_sorted_tags', False) if use_sorted_tags or use_max_tag: - parameters = {'fields': 'values.name,next'} + parameters = {'fields': 'values.name,values.links.html.href,next'} if use_sorted_tags: parameters['sort'] = conf.get('sort', '-target.date') @@ -33,22 +33,22 @@ async def get_version( url = BITBUCKET_MAX_TAG % repo url += '?' + urlencode(parameters) - version = await _get_tags(url, max_page=1, cache=cache) + return await _get_tags(url, max_page=1, cache=cache) elif use_max_tag: url = BITBUCKET_MAX_TAG % repo url += '?' + urlencode(parameters) max_page = conf.get('max_page', 3) - version = await _get_tags(url, max_page=max_page, cache=cache) + return await _get_tags(url, max_page=max_page, cache=cache) else: url = BITBUCKET_URL % (repo, br) data = await cache.get_json(url) - - version = data['values'][0]['date'].split('T', 1)[0].replace('-', '') - - return version + return RichResult( + version = data['values'][0]['date'].split('T', 1)[0].replace('-', ''), + url = data['values'][0]['links']['html']['href'], + ) async def _get_tags( url: str, *, @@ -59,7 +59,12 @@ async def _get_tags( for _ in range(max_page): data = await cache.get_json(url) - ret.extend(x['name'] for x in data['values']) + ret.extend([ + RichResult( + version = tag['name'], + url = tag['links']['html']['href'], + ) for tag in data['values'] + ]) if 'next' in data: url = data['next'] else: diff --git a/nvchecker_source/gitea.py b/nvchecker_source/gitea.py index 50da908..0405554 100644 --- a/nvchecker_source/gitea.py +++ b/nvchecker_source/gitea.py @@ -9,7 +9,8 @@ GITEA_URL = 'https://%s/api/v1/repos/%s/commits' GITEA_MAX_TAG = 'https://%s/api/v1/repos/%s/tags' from nvchecker.api import ( - VersionResult, Entry, AsyncCache, KeyManager, + VersionResult, RichResult, Entry, + AsyncCache, KeyManager, ) async def get_version( @@ -42,7 +43,14 @@ async def get_version( data = await cache.get_json(url, headers = headers) if use_max_tag: - version = [tag["name"] for tag in data] + return [ + RichResult( + version = tag['name'], + url = f'https://{host}/{conf["gitea"]}/releases/tag/{tag["name"]}', + ) for tag in data + ] else: - version = data[0]['commit']['committer']['date'].split('T', 1)[0].replace('-', '') - return version + return RichResult( + version = data[0]['commit']['committer']['date'].split('T', 1)[0].replace('-', ''), + url = data[0]['html_url'], + ) diff --git a/nvchecker_source/github.py b/nvchecker_source/github.py index 0d512c2..e4f1b39 100644 --- a/nvchecker_source/github.py +++ b/nvchecker_source/github.py @@ -3,13 +3,13 @@ import time from urllib.parse import urlencode -from typing import Tuple +from typing import List, Tuple, Union import structlog from nvchecker.api import ( VersionResult, Entry, AsyncCache, KeyManager, - TemporaryError, session, GetVersionError, + TemporaryError, session, RichResult, GetVersionError, ) logger = structlog.get_logger(logger_name=__name__) @@ -49,6 +49,7 @@ QUERY_LATEST_RELEASE_WITH_PRERELEASES = ''' edges {{ node {{ name + url }} }} }} @@ -56,7 +57,7 @@ QUERY_LATEST_RELEASE_WITH_PRERELEASES = ''' }} ''' -async def get_latest_tag(key: Tuple[str, str, str]) -> str: +async def get_latest_tag(key: Tuple[str, str, str]) -> RichResult: repo, query, token = key owner, reponame = repo.split('/') headers = { @@ -80,9 +81,13 @@ async def get_latest_tag(key: Tuple[str, str, str]) -> str: if not refs: raise GetVersionError('no tag found') - return refs[0]['node']['name'] + version = refs[0]['node']['name'] + return RichResult( + version = version, + url = f'https://github.com/{repo}/releases/tag/{version}', + ) -async def get_latest_release_with_prereleases(key: Tuple[str, str]) -> str: +async def get_latest_release_with_prereleases(key: Tuple[str, str]) -> RichResult: repo, token = key owner, reponame = repo.split('/') headers = { @@ -105,7 +110,10 @@ async def get_latest_release_with_prereleases(key: Tuple[str, str]) -> str: if not refs: raise GetVersionError('no release found') - return refs[0]['node']['name'] + return RichResult( + version = refs[0]['node']['name'], + url = refs[0]['node']['url'], + ) async def get_version_real( name: str, conf: Entry, *, @@ -160,7 +168,12 @@ async def get_version_real( data = await cache.get_json(url, headers = headers) if use_max_tag: - tags = [ref['ref'].split('/', 2)[-1] for ref in data] + tags: List[Union[str, RichResult]] = [ + RichResult( + version = ref['ref'].split('/', 2)[-1], + url = f'https://github.com/{repo}/releases/tag/{ref["ref"].split("/", 2)[-1]}', + ) for ref in data + ] if not tags: raise GetVersionError('No tag found in upstream repository.') return tags @@ -168,14 +181,17 @@ async def get_version_real( if use_latest_release: if 'tag_name' not in data: raise GetVersionError('No release found in upstream repository.') - version = data['tag_name'] + return RichResult( + version = data['tag_name'], + url = data['html_url'], + ) else: - # YYYYMMDD.HHMMSS - version = data[0]['commit']['committer']['date'] \ - .rstrip('Z').replace('-', '').replace(':', '').replace('T', '.') - - return version + return RichResult( + # YYYYMMDD.HHMMSS + version = data[0]['commit']['committer']['date'].rstrip('Z').replace('-', '').replace(':', '').replace('T', '.'), + url = data[0]['html_url'], + ) def check_ratelimit(exc, name): res = exc.response diff --git a/nvchecker_source/gitlab.py b/nvchecker_source/gitlab.py index 844d93b..de4d4da 100644 --- a/nvchecker_source/gitlab.py +++ b/nvchecker_source/gitlab.py @@ -6,8 +6,8 @@ import urllib.parse import structlog from nvchecker.api import ( - VersionResult, Entry, AsyncCache, KeyManager, - TemporaryError, + VersionResult, RichResult, Entry, + AsyncCache, KeyManager, TemporaryError, ) GITLAB_URL = 'https://%s/api/v4/projects/%s/repository/commits' @@ -52,10 +52,17 @@ async def get_version_real( data = await cache.get_json(url, headers = headers) if use_max_tag: - version = [tag["name"] for tag in data] + return [ + RichResult( + version = tag['name'], + url = f'https://{host}/{conf["gitlab"]}/-/tags/{tag["name"]}', + ) for tag in data + ] else: - version = data[0]['created_at'].split('T', 1)[0].replace('-', '') - return version + return RichResult( + version = data[0]['created_at'].split('T', 1)[0].replace('-', ''), + url = data[0]['web_url'], + ) def check_ratelimit(exc, name): res = exc.response