From c45e3d7c7317e2cfe5670ed9d965c90bd568188a Mon Sep 17 00:00:00 2001 From: make-ing Date: Wed, 12 Apr 2017 10:42:23 +0200 Subject: [PATCH] # This is a combination of 2 commits. # This is the 1st commit message: added our own mrbeam2 branches to allow for tag lookup. # This is the commit message #2: Added check_type 'bitbucket_commit' to softwareupdate plugin --- .versioneer-lookup | 2 + .../plugins/softwareupdate/__init__.py | 8 ++- .../softwareupdate/version_checks/__init__.py | 4 +- .../version_checks/bitbucket_commit.py | 56 +++++++++++++++++++ 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py diff --git a/.versioneer-lookup b/.versioneer-lookup index efcad5d0..9f0f950c 100644 --- a/.versioneer-lookup +++ b/.versioneer-lookup @@ -8,6 +8,8 @@ # the lookup table does not apply to the matched branches # master and rc shall not use the lookup table, only tags +mrbeam2-dev +mrbeam2-stable master rc/.* prerelease diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index b3a0b7f3..819f4177 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -825,7 +825,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, self._settings.load() # persist the new version if necessary for check type - if check["type"] == "github_commit": + if check["type"] in ["github_commit", "bitbucket_commit"]: dummy_default = dict(plugins=dict()) dummy_default["plugins"][self._identifier] = dict(checks=dict()) dummy_default["plugins"][self._identifier]["checks"][target] = dict(current=None) @@ -882,7 +882,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, release_branches += [x["branch"] for x in check["prerelease_branches"]] result["released_version"] = not release_branches or BRANCH in release_branches - if check["type"] == "github_commit": + if check["type"] in ["github_commit", "bitbucket_commit"]: result["current"] = REVISION if REVISION else "unknown" else: result["current"] = VERSION @@ -936,7 +936,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, # displayVersion AND current missing or None result["displayVersion"] = u"unknown" - if check["type"] in ("github_commit",): + if check["type"] in ["github_commit", "bitbucket_commit"]: result["current"] = check.get("current", None) else: result["current"] = check.get("current", check.get("displayVersion", None)) @@ -972,6 +972,8 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, return version_checks.github_release elif check_type == "github_commit": return version_checks.github_commit + elif check_type == "bitbucket_commit": + return version_checks.bitbucket_commit elif check_type == "git_commit": return version_checks.git_commit elif check_type == "commandline": diff --git a/src/octoprint/plugins/softwareupdate/version_checks/__init__.py b/src/octoprint/plugins/softwareupdate/version_checks/__init__.py index 2597bd1f..fb9784f5 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/__init__.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/__init__.py @@ -5,7 +5,7 @@ __author__ = "Gina Häußge " __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License" -from . import commandline, git_commit, github_commit, github_release, python_checker +from . import commandline, git_commit, github_commit, github_release, bitbucket_commit, python_checker def log_github_ratelimit(logger, r): ratelimit = r.headers["X-RateLimit-Limit"] if "X-RateLimit-Limit" in r.headers else "?" @@ -17,4 +17,4 @@ def log_github_ratelimit(logger, r): except: reset = "?" - logger.debug("Github rate limit: %s/%s, reset at %s" % (remaining, ratelimit, reset)) \ No newline at end of file + logger.debug("Github rate limit: %s/%s, reset at %s" % (remaining, ratelimit, reset)) diff --git a/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py b/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py new file mode 100644 index 00000000..bd042c71 --- /dev/null +++ b/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py @@ -0,0 +1,56 @@ +# coding=utf-8 +from __future__ import absolute_import, division, print_function + +__author__ = "Gina Häußge " +__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' +__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License" + +import requests +import logging + +from ..exceptions import ConfigurationInvalid + +BRANCH_HEAD_URL = "https://api.bitbucket.org/2.0/repositories/{user}/{repo}/commits/{branch}?pagelen=1" + +logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.bitbucket_commit") + +def _get_latest_commit(user, repo, branch): + result = None + r = requests.get(BRANCH_HEAD_URL.format(user=user, repo=repo, branch=branch)) + + if not r.status_code == requests.codes.ok: + return None + + reference = r.json() + if not "values" in reference \ + or len(reference["values"]) < 1 \ + or not 'hash' in reference["values"][0]: + return None + + return reference["values"][0]['hash'] + + +def get_latest(target, check): + if "user" not in check or "repo" not in check: + raise ConfigurationInvalid("Update configuration for %s of type bitbucket_commit needs all of user and repo" % target) + + branch = "master" + if "branch" in check: + branch = check["branch"] + + current = None + if "current" in check: + current = check["current"] + + remote_commit = _get_latest_commit(check["user"], check["repo"], branch) + + information = dict( + local=dict(name="Commit {commit}".format(commit=current if current is not None else "unknown"), value=current), + remote=dict(name="Commit {commit}".format(commit=remote_commit if remote_commit is not None else "unknown"), value=remote_commit) + ) + is_current = (current is not None and current == remote_commit) or remote_commit is None + + logger.debug("Target: %s, local: %s, remote: %s" % (target, current, remote_commit)) + + return information, is_current +