diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index 7e0d57e4..a11cdd70 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -114,7 +114,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, # This used to be part of the settings migration (version 2) due to a bug - it can't # stay there though since it interferes with manual entries to the checks not # originating from within a plugin. Hence we do that step now here. - if "type" not in effective_config or effective_config["type"] != "github_commit": + if "type" not in effective_config or effective_config["type"] not in ["github_commit", "bitbucket_commit"]: deletables = ["current", "displayVersion"] else: deletables = [] @@ -384,7 +384,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, configured_checks = self._settings.get(["checks"], incl_defaults=False) if configured_checks is not None and "octoprint" in configured_checks: octoprint_check = dict(configured_checks["octoprint"]) - if "type" not in octoprint_check or octoprint_check["type"] != "github_commit": + if "type" not in octoprint_check or octoprint_check["type"] not in ["github_commit", "bitbucket_commit"]: deletables=["current", "displayName", "displayVersion"] else: deletables=[] @@ -831,7 +831,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) @@ -888,7 +888,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 @@ -942,7 +942,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)) @@ -978,6 +978,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 +