port the gitea source

This commit is contained in:
lilydjwg 2020-08-18 16:22:14 +08:00
parent c0d836ba65
commit 71015be582
6 changed files with 92 additions and 86 deletions

View File

@ -1,56 +0,0 @@
# MIT licensed
# Copyright (c) 2013-2018 lilydjwg <lilydjwg@gmail.com>, et al.
import os
import urllib.parse
import structlog
from . import session, HTTPError
logger = structlog.get_logger(logger_name=__name__)
GITEA_URL = 'https://%s/api/v1/repos/%s/commits?sha=%s'
GITEA_MAX_TAG = 'https://%s/api/v1/repos/%s/tags'
async def get_version(name, conf, **kwargs):
try:
return await get_version_real(name, conf, **kwargs)
except HTTPError as e:
raise
async def get_version_real(name, conf, **kwargs):
repo = urllib.parse.quote(conf.get('gitea'))
br = conf.get('branch', 'master')
host = conf.get('host', "gitea.com")
use_max_tag = conf.getboolean('use_max_tag', False)
ignored_tags = conf.get("ignored_tags", "").split()
if use_max_tag:
url = GITEA_MAX_TAG % (host, repo)
else:
url = GITEA_URL % (host, repo, br)
# Load token from config
token = conf.get('token')
# Load token from environ
if token is None:
env_name = "NVCHECKER_GITEA_TOKEN_" + host.upper().replace(".", "_").replace("/", "_")
token = os.environ.get(env_name)
# Load token from keyman
if token is None and 'keyman' in kwargs:
key_name = 'gitea_' + host.lower().replace('.', '_').replace("/", "_")
token = kwargs['keyman'].get_key(key_name)
# Set private token if token exists.
headers = {}
if token:
headers["Authorization"] = "token %s" % token
async with session.get(url, headers=headers) as res:
data = await res.json()
if use_max_tag:
version = [tag["name"] for tag in data if tag["name"] not in ignored_tags]
else:
version = data[0]['commit']['committer']['date'].split('T', 1)[0].replace('-', '')
return version

View File

@ -5,6 +5,7 @@ from .httpclient import session # type: ignore
from .core import GetVersionError
from .util import (
Entry, BaseWorker, RawResult, VersionResult,
AsyncCache, KeyManager,
)
from .sortversion import sort_version_keys
from .ctxvars import tries, proxy

View File

@ -91,14 +91,20 @@ class AsyncCache:
self.cache = {}
self.lock = asyncio.Lock()
async def _get_json(self, key: Tuple[str, str]) -> Any:
url = key[1]
res = await session.get(url)
async def _get_json(
self, key: Tuple[str, str, Tuple[Tuple[str, str], ...]],
) -> Any:
_, url, headers = key
res = await session.get(url, headers=dict(headers))
return res.json()
async def get_json(self, url: str) -> Any:
async def get_json(
self, url: str, *,
headers: Dict[str, str] = {},
) -> Any:
key = '_jsonurl', url, tuple(sorted(headers.items()))
return await self.get(
('_jsonurl', url), self._get_json) # type: ignore
key , self._get_json) # type: ignore
async def get(
self,

44
nvchecker_source/gitea.py Normal file
View File

@ -0,0 +1,44 @@
# MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import urllib.parse
GITEA_URL = 'https://%s/api/v1/repos/%s/commits?sha=%s'
GITEA_MAX_TAG = 'https://%s/api/v1/repos/%s/tags'
from nvchecker.api import (
VersionResult, Entry, AsyncCache, KeyManager,
)
async def get_version(
name: str, conf: Entry, *,
cache: AsyncCache, keymanager: KeyManager,
) -> VersionResult:
repo = urllib.parse.quote(conf['gitea'])
br = conf.get('branch', 'master')
host = conf.get('host', 'gitea.com')
use_max_tag = conf.get('use_max_tag', False)
if use_max_tag:
url = GITEA_MAX_TAG % (host, repo)
else:
url = GITEA_URL % (host, repo, br)
# Load token from config
token = conf.get('token')
# Load token from keyman
if token is None:
key_name = 'gitea_' + host.lower().replace('.', '_').replace("/", "_")
token = keymanager.get_key(key_name)
# Set private token if token exists.
headers = {}
if token:
headers["Authorization"] = 'token %s' % token
data = await cache.get_json(url, headers = headers)
if use_max_tag:
version = [tag["name"] for tag in data]
else:
version = data[0]['commit']['committer']['date'].split('T', 1)[0].replace('-', '')
return version

View File

@ -1,25 +0,0 @@
# MIT licensed
# Copyright (c) 2013-2017 lilydjwg <lilydjwg@gmail.com>, et al.
import os
import pytest
pytestmark = [pytest.mark.asyncio,
pytest.mark.needs_net]
async def test_gitea(get_version):
ver = await get_version("example",
{"gitea": "gitea/tea"})
assert len(ver) == 8
assert ver.isdigit()
async def test_gitea_max_tag(get_version):
assert await get_version("example", {"gitea": "gitea/tea", "use_max_tag": 1}) == "v0.4.0"
async def test_gitea_max_tag_with_ignored_tags(get_version):
assert await get_version("example", {"gitea": "gitea/tea", "use_max_tag": 1, "ignored_tags": "v0.4.0"}) == "v0.3.1"
async def test_gitea_max_tag_with_include(get_version):
assert await get_version("example", {
"gitea": "gitea/tea", "use_max_tag": 1,
"include_regex": r'v0\.3.*',
}) == "v0.3.1"

36
tests/test_gitea.py Normal file
View File

@ -0,0 +1,36 @@
# MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import pytest
pytestmark = [pytest.mark.asyncio,
pytest.mark.needs_net]
async def test_gitea(get_version):
ver = await get_version("example", {
"source": "gitea",
"gitea": "gitea/tea"})
assert len(ver) == 8
assert ver.isdigit()
async def test_gitea_max_tag(get_version):
assert await get_version("example", {
"source": "gitea",
"gitea": "gitea/tea",
"use_max_tag": True,
}) == "v0.4.0"
async def test_gitea_max_tag_with_ignored_tags(get_version):
assert await get_version("example", {
"source": "gitea",
"gitea": "gitea/tea",
"use_max_tag": True,
"ignored": "v0.4.0",
}) == "v0.3.1"
async def test_gitea_max_tag_with_include(get_version):
assert await get_version("example", {
"source": "gitea",
"gitea": "gitea/tea",
"use_max_tag": True,
"include_regex": r'v0\.3.*',
}) == "v0.3.1"