From b6eef8a54a3db61968e82516480e29e33cad70bc Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 2 Feb 2022 14:17:51 -0600 Subject: [PATCH 1/5] bitbucket: order tags by date --- nvchecker_source/bitbucket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nvchecker_source/bitbucket.py b/nvchecker_source/bitbucket.py index 5919709..c3716d9 100644 --- a/nvchecker_source/bitbucket.py +++ b/nvchecker_source/bitbucket.py @@ -5,7 +5,7 @@ from nvchecker.api import sort_version_keys # doc: https://confluence.atlassian.com/display/BITBUCKET/commits+or+commit+Resource BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' -BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags' +BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags?sort=-target.date' async def get_version(name, conf, *, cache, **kwargs): repo = conf['bitbucket'] From dd87c0e80cb9a2d95e81f5694d22ff91b04fe40b Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Wed, 2 Feb 2022 14:27:14 -0600 Subject: [PATCH 2/5] update bitbucket doc links --- nvchecker_source/bitbucket.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nvchecker_source/bitbucket.py b/nvchecker_source/bitbucket.py index c3716d9..c93a3fe 100644 --- a/nvchecker_source/bitbucket.py +++ b/nvchecker_source/bitbucket.py @@ -1,10 +1,9 @@ # MIT licensed # Copyright (c) 2013-2020 lilydjwg , et al. -from nvchecker.api import sort_version_keys - -# doc: https://confluence.atlassian.com/display/BITBUCKET/commits+or+commit+Resource +# doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commits-get BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' +# doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-get BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags?sort=-target.date' async def get_version(name, conf, *, cache, **kwargs): From 4f06ce78625096d14bd2d10cbab9804dea53003b Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 4 Feb 2022 09:53:03 -0600 Subject: [PATCH 3/5] bitbucket: add types --- nvchecker_source/bitbucket.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/nvchecker_source/bitbucket.py b/nvchecker_source/bitbucket.py index c93a3fe..6aacd03 100644 --- a/nvchecker_source/bitbucket.py +++ b/nvchecker_source/bitbucket.py @@ -1,12 +1,19 @@ # MIT licensed # Copyright (c) 2013-2020 lilydjwg , et al. +from typing import Any, List +from nvchecker.api import VersionResult, Entry, AsyncCache + # doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commits-get BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' # doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-get BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags?sort=-target.date' -async def get_version(name, conf, *, cache, **kwargs): +async def get_version( + name: str, conf: Entry, *, + cache: AsyncCache, + **kwargs: Any, +) -> VersionResult: repo = conf['bitbucket'] br = conf.get('branch', '') use_max_tag = conf.get('use_max_tag', False) @@ -26,7 +33,11 @@ async def get_version(name, conf, *, cache, **kwargs): version = data['values'][0]['date'].split('T', 1)[0].replace('-', '') return version -async def _get_tags(url, *, max_page, cache): +async def _get_tags( + url: str, *, + max_page: int, + cache: AsyncCache, +) -> List[str]: ret = [] for _ in range(max_page): From a8228bb594cef36b1527fc0a51a6328a5561a313 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 4 Feb 2022 10:31:41 -0600 Subject: [PATCH 4/5] bitbucket: use querying and sorting for improved tag search --- docs/usage.rst | 20 ++++++++++++++++++-- nvchecker_source/bitbucket.py | 19 ++++++++++++++++--- tests/test_bitbucket.py | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 52f7536..6c6dfff 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -467,11 +467,27 @@ use_max_tag Set this to ``true`` to check for the max tag on BitBucket. Will return the biggest one sorted by old ``pkg_resources.parse_version``. Will return the tag name instead of date. +use_sorted_tags + If ``true``, tags are queried and sorted according to the ``query`` and + ``sort`` keys. Will return the tag name instead of the date. + +query + A query string use to filter tags when ``use_sorted_tags`` is set (see + `here `__ + for examples). The string does not need to be escaped. + +sort + A field used to sort the tags when ``use_sorted_tags`` is set (see + `here `__ + for examples). Defaults to ``-target.date`` (sorts tags in descending order + by date). + max_page How many pages do we search for the max tag? Default is 3. This works when ``use_max_tag`` is set. -This source supports :ref:`list options` when ``use_max_tag`` is set. +This source supports :ref:`list options` when ``use_max_tag`` or +``use_sorted_tags`` is set. Check GitLab ~~~~~~~~~~~~ @@ -848,7 +864,7 @@ Check `Open Vsx `_ for updates. openvsx The extension's Unique Identifier on open-vsx.org, e.g. ``ritwickdey.LiveServer``. - + Check Visual Studio Code Marketplace ~~~~~~~~~~~~~~~ :: diff --git a/nvchecker_source/bitbucket.py b/nvchecker_source/bitbucket.py index 6aacd03..f934db2 100644 --- a/nvchecker_source/bitbucket.py +++ b/nvchecker_source/bitbucket.py @@ -2,12 +2,14 @@ # Copyright (c) 2013-2020 lilydjwg , et al. from typing import Any, List +from urllib.parse import urlencode + from nvchecker.api import VersionResult, Entry, AsyncCache # doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commits-get BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' # doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-get -BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags?sort=-target.date' +BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags' async def get_version( name: str, conf: Entry, *, @@ -17,8 +19,19 @@ async def get_version( repo = conf['bitbucket'] br = conf.get('branch', '') use_max_tag = conf.get('use_max_tag', False) + use_sorted_tags = conf.get('use_sorted_tags', False) - if use_max_tag: + if use_sorted_tags: + url = BITBUCKET_MAX_TAG % repo + + parameters = {'sort': conf.get('sort', '-target.date')} + if 'query' in conf: + parameters['q'] = conf['query'] + + url += '?' + urlencode(parameters) + data = await _get_tags(url, max_page=1, cache=cache) + + elif use_max_tag: url = BITBUCKET_MAX_TAG % repo max_page = conf.get('max_page', 3) data = await _get_tags(url, max_page=max_page, cache=cache) @@ -27,7 +40,7 @@ async def get_version( url = BITBUCKET_URL % (repo, br) data = await cache.get_json(url) - if use_max_tag: + if use_max_tag or use_sorted_tags: version = data else: version = data['values'][0]['date'].split('T', 1)[0].replace('-', '') diff --git a/tests/test_bitbucket.py b/tests/test_bitbucket.py index 6ba99a5..8efb01d 100644 --- a/tests/test_bitbucket.py +++ b/tests/test_bitbucket.py @@ -24,3 +24,25 @@ async def test_bitbucket_max_tag_with_ignored(get_version): "use_max_tag": True, "ignored": "1.6.0 1.7.0", }) == "v1.5" + +async def test_bitbucket_sorted_tags(get_version): + assert await get_version("example", { + "source": "bitbucket", + "bitbucket": "prawee/git-tag", + "use_sorted_tags": True, + }) == "1.7.0" + + assert await get_version("example", { + "source": "bitbucket", + "bitbucket": "prawee/git-tag", + "use_sorted_tags": True, + "query": 'name~"v"', + }) == "v1.5" + + assert await get_version("example", { + "source": "bitbucket", + "bitbucket": "berkeleylab/gasnet", + "use_sorted_tags": True, + "query": 'name~"CVS/BERKELEY_UPC" AND name!~"rc"', + "prefix": "CVS/BERKELEY_UPC_", + }) == "2_18_0" From 4bf2755b0e5261968c8bda8cf362967871415087 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Sun, 6 Feb 2022 10:17:49 -0600 Subject: [PATCH 5/5] bitbucket: use 'fields' query to restrict returned fields --- docs/usage.rst | 2 +- nvchecker_source/bitbucket.py | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 6c6dfff..86e3434 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -472,7 +472,7 @@ use_sorted_tags ``sort`` keys. Will return the tag name instead of the date. query - A query string use to filter tags when ``use_sorted_tags`` is set (see + A query string use to filter tags when ``use_sorted_tags`` set (see `here `__ for examples). The string does not need to be escaped. diff --git a/nvchecker_source/bitbucket.py b/nvchecker_source/bitbucket.py index f934db2..6039454 100644 --- a/nvchecker_source/bitbucket.py +++ b/nvchecker_source/bitbucket.py @@ -21,29 +21,33 @@ async def get_version( use_max_tag = conf.get('use_max_tag', False) use_sorted_tags = conf.get('use_sorted_tags', False) - if use_sorted_tags: - url = BITBUCKET_MAX_TAG % repo + if use_sorted_tags or use_max_tag: + parameters = {'fields': 'values.name,next'} - parameters = {'sort': conf.get('sort', '-target.date')} - if 'query' in conf: + if use_sorted_tags: + parameters['sort'] = conf.get('sort', '-target.date') + if 'query' in conf: parameters['q'] = conf['query'] + if use_sorted_tags: + url = BITBUCKET_MAX_TAG % repo url += '?' + urlencode(parameters) - data = await _get_tags(url, max_page=1, cache=cache) + + version = 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) - data = await _get_tags(url, max_page=max_page, cache=cache) + version = await _get_tags(url, max_page=max_page, cache=cache) else: url = BITBUCKET_URL % (repo, br) data = await cache.get_json(url) - if use_max_tag or use_sorted_tags: - version = data - else: version = data['values'][0]['date'].split('T', 1)[0].replace('-', '') + return version async def _get_tags( @@ -51,7 +55,7 @@ async def _get_tags( max_page: int, cache: AsyncCache, ) -> List[str]: - ret = [] + ret: List[str] = [] for _ in range(max_page): data = await cache.get_json(url)