diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index abe4038e..5bac02c3 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -827,7 +827,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) @@ -884,7 +884,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 @@ -938,7 +938,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)) @@ -974,6 +974,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..e4487752 --- /dev/null +++ b/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py @@ -0,0 +1,54 @@ +# 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}/commit/{branch}" + +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 "hash" in reference: + return None + + return reference["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 +