2017-02-28 07:24:53 +00:00
|
|
|
# MIT licensed
|
2020-08-18 08:32:47 +00:00
|
|
|
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
2017-02-28 07:24:53 +00:00
|
|
|
|
2015-11-05 07:31:56 +00:00
|
|
|
import urllib.parse
|
|
|
|
|
2018-04-11 08:18:41 +00:00
|
|
|
import structlog
|
|
|
|
|
2020-08-18 08:32:47 +00:00
|
|
|
from nvchecker.api import (
|
2023-10-18 19:46:28 +00:00
|
|
|
VersionResult, RichResult, Entry,
|
|
|
|
AsyncCache, KeyManager, TemporaryError,
|
2020-08-18 08:32:47 +00:00
|
|
|
)
|
2018-04-11 08:18:41 +00:00
|
|
|
|
2021-03-18 05:18:03 +00:00
|
|
|
GITLAB_URL = 'https://%s/api/v4/projects/%s/repository/commits'
|
2018-06-08 16:02:02 +00:00
|
|
|
GITLAB_MAX_TAG = 'https://%s/api/v4/projects/%s/repository/tags'
|
2015-11-05 07:31:56 +00:00
|
|
|
|
2020-08-18 08:32:47 +00:00
|
|
|
logger = structlog.get_logger(logger_name=__name__)
|
|
|
|
|
2018-05-05 15:39:02 +00:00
|
|
|
async def get_version(name, conf, **kwargs):
|
2018-06-10 13:24:22 +00:00
|
|
|
try:
|
|
|
|
return await get_version_real(name, conf, **kwargs)
|
2020-08-18 08:32:47 +00:00
|
|
|
except TemporaryError as e:
|
2018-06-10 13:24:22 +00:00
|
|
|
check_ratelimit(e, name)
|
|
|
|
|
2020-08-18 08:32:47 +00:00
|
|
|
async def get_version_real(
|
|
|
|
name: str, conf: Entry, *,
|
|
|
|
cache: AsyncCache, keymanager: KeyManager,
|
2020-08-20 06:56:40 +00:00
|
|
|
**kwargs,
|
2020-08-18 08:32:47 +00:00
|
|
|
) -> VersionResult:
|
|
|
|
repo = urllib.parse.quote_plus(conf['gitlab'])
|
2021-03-18 05:18:03 +00:00
|
|
|
br = conf.get('branch')
|
2015-11-05 07:31:56 +00:00
|
|
|
host = conf.get('host', "gitlab.com")
|
2020-08-18 08:32:47 +00:00
|
|
|
use_max_tag = conf.get('use_max_tag', False)
|
2015-11-05 07:31:56 +00:00
|
|
|
|
|
|
|
if use_max_tag:
|
|
|
|
url = GITLAB_MAX_TAG % (host, repo)
|
|
|
|
else:
|
2021-03-18 05:18:03 +00:00
|
|
|
url = GITLAB_URL % (host, repo)
|
|
|
|
if br:
|
|
|
|
url += '?ref_name=%s' % br
|
2015-11-05 07:31:56 +00:00
|
|
|
|
2018-06-10 13:24:22 +00:00
|
|
|
# Load token from config
|
|
|
|
token = conf.get('token')
|
|
|
|
# Load token from keyman
|
2020-08-18 08:32:47 +00:00
|
|
|
if token is None:
|
2024-03-02 09:46:46 +00:00
|
|
|
token = keymanager.get_key(host.lower(), 'gitlab_' + host.lower())
|
2018-06-10 13:24:22 +00:00
|
|
|
|
2018-09-27 13:40:21 +00:00
|
|
|
# Set private token if token exists.
|
2018-06-10 13:24:22 +00:00
|
|
|
headers = {}
|
|
|
|
if token:
|
|
|
|
headers["PRIVATE-TOKEN"] = token
|
|
|
|
|
2020-08-18 08:32:47 +00:00
|
|
|
data = await cache.get_json(url, headers = headers)
|
2015-11-05 07:31:56 +00:00
|
|
|
if use_max_tag:
|
2023-10-18 19:46:28 +00:00
|
|
|
return [
|
|
|
|
RichResult(
|
|
|
|
version = tag['name'],
|
2024-03-02 10:23:48 +00:00
|
|
|
revision = tag['commit']['id'],
|
2023-10-18 19:46:28 +00:00
|
|
|
url = f'https://{host}/{conf["gitlab"]}/-/tags/{tag["name"]}',
|
|
|
|
) for tag in data
|
|
|
|
]
|
2015-11-05 07:31:56 +00:00
|
|
|
else:
|
2023-10-18 19:46:28 +00:00
|
|
|
return RichResult(
|
|
|
|
version = data[0]['created_at'].split('T', 1)[0].replace('-', ''),
|
2024-03-02 10:23:48 +00:00
|
|
|
revision = data[0]['id'],
|
2023-10-18 19:46:28 +00:00
|
|
|
url = data[0]['web_url'],
|
|
|
|
)
|
2018-06-10 13:24:22 +00:00
|
|
|
|
|
|
|
def check_ratelimit(exc, name):
|
|
|
|
res = exc.response
|
2018-06-19 10:29:38 +00:00
|
|
|
if not res:
|
|
|
|
raise
|
|
|
|
|
|
|
|
# default -1 is used to re-raise the exception
|
2018-09-25 04:33:37 +00:00
|
|
|
n = int(res.headers.get('RateLimit-Remaining', -1))
|
2018-06-10 13:24:22 +00:00
|
|
|
if n == 0:
|
2020-08-18 08:32:47 +00:00
|
|
|
logger.error('gitlab rate limited. Wait some time '
|
|
|
|
'or get an API token to increase the allowance if not yet',
|
2018-06-10 13:24:22 +00:00
|
|
|
name = name)
|
|
|
|
else:
|
|
|
|
raise
|