diff --git a/README.rst b/README.rst
index 933b30a..a441082 100644
--- a/README.rst
+++ b/README.rst
@@ -36,6 +36,7 @@ Contents
* `Check GitHub <#check-github>`_
* `Check BitBucket <#check-bitbucket>`_
* `Check GitLab <#check-gitlab>`_
+ * `Check Gitea <#check-gitea>`_
* `Check PyPI <#check-pypi>`_
* `Check RubyGems <#check-rubygems>`_
* `Check NPM Registry <#check-npm-registry>`_
@@ -332,6 +333,42 @@ anonymously.
This source supports `list options`_ when ``use_max_tag`` is set.
+Check Gitea
+-------------
+Check `Gitea `_ for updates. The version returned is in date format ``%Y%m%d``, e.g. ``20130701``,
+unless ``use_max_tag`` is used. See below.
+
+gitea
+ The gitea repository, with author, e.g. ``gitea/tea``.
+
+branch
+ Which branch to track? Default: ``master``.
+
+use_max_tag
+ Set this to ``true`` to check for the max tag on Gitea. Will return the biggest one
+ sorted by ``pkg_resources.parse_version``. Will return the tag name instead of date.
+
+host
+ Hostname for self-hosted Gitea instance.
+
+token
+ Gitea authorization token used to call the API.
+
+ignored_tags, sort_version_key
+ Deprecated. Use `list options`_ instead.
+
+To set an authorization token, you can set:
+
+- a key named ``gitea_{host}`` in the keyfile (where ``host`` is formed the
+ same as the environment variable, but all lowercased).
+- an environment variable ``NVCHECKER_GITEA_TOKEN_{host}`` must provide that
+ token. The ``host`` part is the uppercased version of the ``host`` setting,
+ with dots (``.``) and slashes (``/``) replaced by underscores (``_``), e.g.
+ ``NVCHECKER_GITEA_TOKEN_GITEA_COM``.
+- the token option
+
+This source supports `list options`_ when ``use_max_tag`` is set.
+
Check BitBucket
---------------
Check `BitBucket `_ for updates. The version returned
diff --git a/nvchecker/get_version.py b/nvchecker/get_version.py
index 79a9f07..3e7750b 100644
--- a/nvchecker/get_version.py
+++ b/nvchecker/get_version.py
@@ -15,7 +15,7 @@ handler_precedence = (
'gems', 'pacman',
'cmd', 'bitbucket', 'regex', 'manual', 'vcs',
'cratesio', 'npm', 'hackage', 'cpan', 'gitlab', 'packagist',
- 'repology', 'anitya', 'android_sdk', 'sparkle',
+ 'repology', 'anitya', 'android_sdk', 'sparkle', 'gitea'
)
def substitute_version(version, name, conf):
diff --git a/nvchecker/source/gitea.py b/nvchecker/source/gitea.py
new file mode 100644
index 0000000..0e8306a
--- /dev/null
+++ b/nvchecker/source/gitea.py
@@ -0,0 +1,56 @@
+# MIT licensed
+# Copyright (c) 2013-2018 lilydjwg , 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
diff --git a/tests/test_gitea.py b/tests/test_gitea.py
new file mode 100644
index 0000000..0402e04
--- /dev/null
+++ b/tests/test_gitea.py
@@ -0,0 +1,25 @@
+# MIT licensed
+# Copyright (c) 2013-2017 lilydjwg , 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"