nvchecker/nvchecker_source/bitbucket.py

74 lines
2.1 KiB
Python
Raw Normal View History

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
from typing import Any, List, Union
from urllib.parse import urlencode
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
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)
use_sorted_tags = conf.get('use_sorted_tags', False)
if use_sorted_tags or use_max_tag:
parameters = {'fields': 'values.name,values.links.html.href,next'}
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)
return await _get_tags(url, max_page=1, cache=cache)
elif use_max_tag:
url = BITBUCKET_MAX_TAG % repo
url += '?' + urlencode(parameters)
2020-08-17 08:31:59 +00:00
max_page = conf.get('max_page', 3)
return await _get_tags(url, max_page=max_page, cache=cache)
else:
url = BITBUCKET_URL % (repo, br)
2020-08-14 10:25:48 +00:00
data = await cache.get_json(url)
return RichResult(
version = data['values'][0]['date'].split('T', 1)[0].replace('-', ''),
url = data['values'][0]['links']['html']['href'],
)
2022-02-04 15:53:03 +00:00
async def _get_tags(
url: str, *,
max_page: int,
cache: AsyncCache,
) -> VersionResult:
ret: List[Union[str, RichResult]] = []
for _ in range(max_page):
2020-08-14 10:25:48 +00:00
data = await cache.get_json(url)
ret.extend([
RichResult(
version = tag['name'],
url = tag['links']['html']['href'],
) for tag in data['values']
])
if 'next' in data:
url = data['next']
else:
break
return ret