add support for GitHub releases

Original author is isamazingfate, in this pull request:
https://github.com/lilydjwg/nvchecker/pull/15

This closes #15.
This commit is contained in:
lilydjwg 2015-11-04 20:10:13 +08:00
parent 15b800d5a8
commit 512399cef5
2 changed files with 17 additions and 4 deletions

View File

@ -127,6 +127,11 @@ branch
An environment variable ``NVCHECKER_GITHUB_TOKEN`` can be set to a GitHub OAuth token in order to request more frequently than anonymously.
use_latest_release
Set this to ``true`` to check for the latest relase on GitHub. An annotated
tag creates a "release" on GitHub. It's not the same with git tags, which
includes both annotated tags and lightweight ones.
Check BitBucket
---------------
Check `BitBucket <https://bitbucket.org/>`_ for updates. The version returned is in date format ``%Y%m%d``, e.g. ``20130701``.

View File

@ -5,19 +5,27 @@ from functools import partial
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
GITHUB_URL = 'https://api.github.com/repos/%s/commits?sha=%s'
GITHUB_LATEST_RELEASE = 'https://api.github.com/repos/%s/releases/latest'
def get_version(name, conf, callback):
repo = conf.get('github')
br = conf.get('branch', 'master')
use_latest_release = conf.getboolean('use_latest_release', False)
if use_latest_release:
url = GITHUB_LATEST_RELEASE % repo
else:
url = GITHUB_URL % (repo, br)
headers = {'Accept': "application/vnd.github.quicksilver-preview+json"}
if 'NVCHECKER_GITHUB_TOKEN' in os.environ:
headers['Authorization'] = 'token %s' % os.environ['NVCHECKER_GITHUB_TOKEN']
request = HTTPRequest(url, headers=headers, user_agent='lilydjwg/nvchecker')
AsyncHTTPClient().fetch(request,
callback=partial(_github_done, name, callback))
callback=partial(_github_done, name, use_tags, callback))
def _github_done(name, callback, res):
def _github_done(name, use_tags, callback, res):
data = json.loads(res.body.decode('utf-8'))
if use_tags:
version = data['tag_name']
else:
version = data[0]['commit']['committer']['date'].split('T', 1)[0].replace('-', '')
callback(name, version)