2017-02-28 07:24:53 +00:00
|
|
|
# MIT licensed
|
2020-08-14 10:25:48 +00:00
|
|
|
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
2017-02-28 07:24:53 +00:00
|
|
|
|
2023-10-17 23:59:30 +00:00
|
|
|
from typing import Any, List, Union
|
2022-02-04 16:31:41 +00:00
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
2023-10-17 23:59:30 +00:00
|
|
|
from nvchecker.api import VersionResult, RichResult, Entry, AsyncCache
|
2022-02-04 15:53:03 +00:00
|
|
|
|
2022-02-02 20:27:14 +00:00
|
|
|
# doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-commits-get
|
2015-08-04 08:12:46 +00:00
|
|
|
BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s'
|
2022-02-02 20:27:14 +00:00
|
|
|
# doc: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-tags-get
|
2022-02-04 16:31:41 +00:00
|
|
|
BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/2.0/repositories/%s/refs/tags'
|
2015-08-04 08:12:46 +00:00
|
|
|
|
2022-02-04 15:53:03 +00:00
|
|
|
async def get_version(
|
|
|
|
name: str, conf: Entry, *,
|
|
|
|
cache: AsyncCache,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> VersionResult:
|
2020-08-20 07:39:21 +00:00
|
|
|
repo = conf['bitbucket']
|
2015-08-04 08:12:46 +00:00
|
|
|
br = conf.get('branch', '')
|
2020-08-17 08:31:59 +00:00
|
|
|
use_max_tag = conf.get('use_max_tag', False)
|
2022-02-04 16:31:41 +00:00
|
|
|
use_sorted_tags = conf.get('use_sorted_tags', False)
|
|
|
|
|
2022-02-06 16:17:49 +00:00
|
|
|
if use_sorted_tags or use_max_tag:
|
2023-10-18 19:46:28 +00:00
|
|
|
parameters = {'fields': 'values.name,values.links.html.href,next'}
|
2022-02-04 16:31:41 +00:00
|
|
|
|
2022-02-06 16:17:49 +00:00
|
|
|
if use_sorted_tags:
|
|
|
|
parameters['sort'] = conf.get('sort', '-target.date')
|
|
|
|
if 'query' in conf:
|
2022-02-04 16:31:41 +00:00
|
|
|
parameters['q'] = conf['query']
|
|
|
|
|
2022-02-06 16:17:49 +00:00
|
|
|
if use_sorted_tags:
|
|
|
|
url = BITBUCKET_MAX_TAG % repo
|
2022-02-04 16:31:41 +00:00
|
|
|
url += '?' + urlencode(parameters)
|
2022-02-06 16:17:49 +00:00
|
|
|
|
2023-10-18 19:46:28 +00:00
|
|
|
return await _get_tags(url, max_page=1, cache=cache)
|
2019-07-04 08:39:49 +00:00
|
|
|
|
2022-02-04 16:31:41 +00:00
|
|
|
elif use_max_tag:
|
2015-11-05 04:12:03 +00:00
|
|
|
url = BITBUCKET_MAX_TAG % repo
|
2022-02-06 16:17:49 +00:00
|
|
|
url += '?' + urlencode(parameters)
|
|
|
|
|
2020-08-17 08:31:59 +00:00
|
|
|
max_page = conf.get('max_page', 3)
|
2023-10-18 19:46:28 +00:00
|
|
|
return await _get_tags(url, max_page=max_page, cache=cache)
|
2019-07-04 08:39:49 +00:00
|
|
|
|
2015-11-05 04:12:03 +00:00
|
|
|
else:
|
|
|
|
url = BITBUCKET_URL % (repo, br)
|
2020-08-14 10:25:48 +00:00
|
|
|
data = await cache.get_json(url)
|
2023-10-18 19:46:28 +00:00
|
|
|
return RichResult(
|
|
|
|
version = data['values'][0]['date'].split('T', 1)[0].replace('-', ''),
|
|
|
|
url = data['values'][0]['links']['html']['href'],
|
|
|
|
)
|
2019-07-04 08:39:49 +00:00
|
|
|
|
2022-02-04 15:53:03 +00:00
|
|
|
async def _get_tags(
|
|
|
|
url: str, *,
|
|
|
|
max_page: int,
|
|
|
|
cache: AsyncCache,
|
2023-10-17 23:59:30 +00:00
|
|
|
) -> VersionResult:
|
|
|
|
ret: List[Union[str, RichResult]] = []
|
2019-07-04 08:39:49 +00:00
|
|
|
|
2019-07-04 09:59:19 +00:00
|
|
|
for _ in range(max_page):
|
2020-08-14 10:25:48 +00:00
|
|
|
data = await cache.get_json(url)
|
2023-10-18 19:46:28 +00:00
|
|
|
ret.extend([
|
|
|
|
RichResult(
|
|
|
|
version = tag['name'],
|
|
|
|
url = tag['links']['html']['href'],
|
|
|
|
) for tag in data['values']
|
|
|
|
])
|
2019-07-04 08:39:49 +00:00
|
|
|
if 'next' in data:
|
|
|
|
url = data['next']
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
return ret
|