Add use_max_tag support for GitHub source

This commit is contained in:
Felix Yan 2015-11-05 11:25:57 +08:00
parent 4be2d6c09d
commit 07d75c4c5d
3 changed files with 28 additions and 3 deletions

View File

@ -126,10 +126,15 @@ branch
Which branch to track? Default: ``master``. Which branch to track? Default: ``master``.
use_latest_release use_latest_release
Set this to ``true`` to check for the latest relase on GitHub. An annotated Set this to ``true`` to check for the latest release on GitHub. An annotated
tag creates a "release" on GitHub. It's not the same with git tags, which tag creates a "release" on GitHub. It's not the same with git tags, which
includes both annotated tags and lightweight ones. includes both annotated tags and lightweight ones.
use_max_tag
Set this to ``true`` to check for the max tag on GitHub. Unlike ``use_latest_release``,
this option includes both annotated tags and lightweight ones, and return the biggest one
sorted by ``pkg_resources.parse_version``.
An environment variable ``NVCHECKER_GITHUB_TOKEN`` can be set to a GitHub OAuth token in order to request more frequently than anonymously. An environment variable ``NVCHECKER_GITHUB_TOKEN`` can be set to a GitHub OAuth token in order to request more frequently than anonymously.
Check BitBucket Check BitBucket

View File

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

12
tests/test_github.py Normal file
View File

@ -0,0 +1,12 @@
from tests.helper import ExternalVersionTestCase
class GitHubTest(ExternalVersionTestCase):
def test_github(self):
self.assertEqual(self.sync_get_version("example", {"github": "harry-sanabria/ReleaseTestRepo"}), "20140122")
def test_github_latest_release(self):
self.assertEqual(self.sync_get_version("example", {"github": "harry-sanabria/ReleaseTestRepo", "use_latest_release": 1}), "release3")
def test_github_max_tag(self):
self.assertEqual(self.sync_get_version("example", {"github": "harry-sanabria/ReleaseTestRepo", "use_max_tag": 1}), "second_release")