diff --git a/README.rst b/README.rst index f129378..f769730 100644 --- a/README.rst +++ b/README.rst @@ -142,6 +142,10 @@ bitbucket branch Which branch to track? Default is the repository's default. +use_max_tag + Set this to ``true`` to check for the max tag on BitBucket. Will return the biggest one + sorted by ``pkg_resources.parse_version``. + Check GitCafe ------------- Check `GitCafe `_ for updates. The version returned is in date format ``%Y%m%d``, e.g. ``20130701``. diff --git a/nvchecker/source/bitbucket.py b/nvchecker/source/bitbucket.py index 7124927..5179e69 100644 --- a/nvchecker/source/bitbucket.py +++ b/nvchecker/source/bitbucket.py @@ -2,20 +2,31 @@ import os import json from functools import partial +from pkg_resources import parse_version from tornado.httpclient import AsyncHTTPClient, HTTPRequest # doc: https://confluence.atlassian.com/display/BITBUCKET/commits+or+commit+Resource BITBUCKET_URL = 'https://bitbucket.org/api/2.0/repositories/%s/commits/%s' +BITBUCKET_MAX_TAG = 'https://bitbucket.org/api/1.0/repositories/%s/tags' def get_version(name, conf, callback): repo = conf.get('bitbucket') br = conf.get('branch', '') - url = BITBUCKET_URL % (repo, br) + use_max_tag = conf.getboolean('use_max_tag', False) + if use_max_tag: + url = BITBUCKET_MAX_TAG % repo + else: + url = BITBUCKET_URL % (repo, br) request = HTTPRequest(url, user_agent='lilydjwg/nvchecker') AsyncHTTPClient().fetch(request, - callback=partial(_bitbucket_done, name, callback)) + callback=partial(_bitbucket_done, name, use_max_tag, callback)) -def _bitbucket_done(name, callback, res): +def _bitbucket_done(name, use_max_tag, callback, res): data = json.loads(res.body.decode('utf-8')) - version = data['values'][0]['date'].split('T', 1)[0].replace('-', '') + if use_max_tag: + data = list(data) + data.sort(key=parse_version) + version = data[-1] + else: + version = data['values'][0]['date'].split('T', 1)[0].replace('-', '') callback(name, version) diff --git a/tests/test_bitbucket.py b/tests/test_bitbucket.py new file mode 100644 index 0000000..69fb242 --- /dev/null +++ b/tests/test_bitbucket.py @@ -0,0 +1,9 @@ +from tests.helper import ExternalVersionTestCase + + +class BitBucketTest(ExternalVersionTestCase): + def test_bitbucket(self): + self.assertEqual(self.sync_get_version("example", {"bitbucket": "prawee/git-tag"}), "20150303") + + def test_bitbucket_max_tag(self): + self.assertEqual(self.sync_get_version("example", {"bitbucket": "prawee/git-tag", "use_max_tag": 1}), "1.7.0")