From 512399cef58359f42cafa8c7ca6ebbd6ad580503 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Wed, 4 Nov 2015 20:10:13 +0800 Subject: [PATCH] add support for GitHub releases Original author is isamazingfate, in this pull request: https://github.com/lilydjwg/nvchecker/pull/15 This closes #15. --- README.rst | 5 +++++ nvchecker/source/github.py | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 6319cba..6f5e42a 100644 --- a/README.rst +++ b/README.rst @@ -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 `_ for updates. The version returned is in date format ``%Y%m%d``, e.g. ``20130701``. diff --git a/nvchecker/source/github.py b/nvchecker/source/github.py index 6af57ee..67f45e6 100644 --- a/nvchecker/source/github.py +++ b/nvchecker/source/github.py @@ -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') - url = GITHUB_URL % (repo, br) + 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')) - version = data[0]['commit']['committer']['date'].split('T', 1)[0].replace('-', '') + if use_tags: + version = data['tag_name'] + else: + version = data[0]['commit']['committer']['date'].split('T', 1)[0].replace('-', '') callback(name, version)