diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index fd8872d45..ab6958b79 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -1861,6 +1861,12 @@ class SoftwareUpdatePlugin( ) update_possible = False error = "network" + except exceptions.ApiCheckError as exc: + self._logger.warning( + f"Could not check {target} for updates due to an API error: {exc}" + ) + update_possible = False + error = "api" except exceptions.RateLimitCheckError as exc: self._logger.warning( "Could not check {} for updates due to running into a rate limit: {}".format( diff --git a/src/octoprint/plugins/softwareupdate/exceptions.py b/src/octoprint/plugins/softwareupdate/exceptions.py index 93fb909b7..29cc72d28 100644 --- a/src/octoprint/plugins/softwareupdate/exceptions.py +++ b/src/octoprint/plugins/softwareupdate/exceptions.py @@ -42,6 +42,14 @@ class CheckError(Exception): return self.message +class ApiCheckError(CheckError): + API = "API" + + def __init__(self, status_code, message): + self.status_code = status_code + super().__init__(f"{self.API} error: {message} (HTTP {status_code})") + + class RateLimitCheckError(CheckError): def __init__(self, message, remaining=None, limit=None, reset=None): super().__init__(message) diff --git a/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js b/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js index 3505bc5a6..2de85091f 100644 --- a/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js +++ b/src/octoprint/plugins/softwareupdate/static/js/softwareupdate.js @@ -64,6 +64,7 @@ $(function () { self.config_releaseChannel = ko.observable(); self.config_pipEnableCheck = ko.observable(); self.config_minimumFreeStorage = ko.observable(); + self.config_githubToken = ko.observable(); self.error_checkoutFolder = ko.pureComputed(function () { return ( @@ -243,6 +244,8 @@ $(function () { var target = $(event.target); target.prepend(' '); + var githubToken = self.config_githubToken(); + var data = { plugins: { softwareupdate: { @@ -258,6 +261,9 @@ $(function () { } } }; + if (githubToken) { + data.plugins.softwareupdate.credentials = {github: githubToken}; + } self.settings.saveData(data, { success: function () { self.configurationDialog.modal("hide"); @@ -266,6 +272,7 @@ $(function () { }, complete: function () { $("i.fa-spinner", target).remove(); + self.config_githubToken(""); }, sending: true }); @@ -661,7 +668,14 @@ $(function () { return gettext("Cannot check for update, need online connection"); } case "network": { - return gettext("Network error while checking for update"); + return gettext( + "Network error while checking for update, please check the logs" + ); + } + case "api": { + return gettext( + "API error while checking for update, please check the logs" + ); } case "ratelimit": { return gettext( @@ -669,7 +683,9 @@ $(function () { ); } case "check": { - return gettext("Check internal error while checking for update"); + return gettext( + "Check internal error while checking for update, please check the logs" + ); } case "unknown": { return gettext( diff --git a/src/octoprint/plugins/softwareupdate/templates/snippets/githubToken.jinja2 b/src/octoprint/plugins/softwareupdate/templates/snippets/githubToken.jinja2 new file mode 100644 index 000000000..0a03630d9 --- /dev/null +++ b/src/octoprint/plugins/softwareupdate/templates/snippets/githubToken.jinja2 @@ -0,0 +1,7 @@ +
+ +
+ + {{ _('Set this if you keep running into rate limiting issues. See here for how to obtain a token.', url="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token") }} +
+
diff --git a/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 b/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 index 2ac61e04b..b8d8df41e 100644 --- a/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 +++ b/src/octoprint/plugins/softwareupdate/templates/softwareupdate_settings.jinja2 @@ -171,6 +171,7 @@ {% include "snippets/checkoutFolder.jinja2" %} {% include "snippets/pipTarget.jinja2" %} {% include "snippets/pipEnableCheck.jinja2" %} + {% include "snippets/githubToken.jinja2" %}
diff --git a/src/octoprint/plugins/softwareupdate/version_checks/__init__.py b/src/octoprint/plugins/softwareupdate/version_checks/__init__.py index 737fbe3ce..12f565452 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/__init__.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/__init__.py @@ -2,7 +2,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 ..exceptions import RateLimitCheckError +from ..exceptions import ApiCheckError, RateLimitCheckError from . import ( # noqa: F401 always_current, bitbucket_commit, @@ -18,16 +18,36 @@ from . import ( # noqa: F401 ) -class GithubRateLimitCheckError(RateLimitCheckError): +class GitHubRateLimitCheckError(RateLimitCheckError): def __init__(self, remaining, ratelimit, reset): if reset: - message = f"Github rate limit exceeded, reset at {reset}" + message = f"GitHub rate limit exceeded, reset at {reset}" else: - message = "Github rate limit exceeded" + message = "GitHub rate limit exceeded" super().__init__(message, remaining=remaining, limit=ratelimit, reset=reset) -def log_github_ratelimit(logger, r): +class GitHubApiError(ApiCheckError): + API = "GitHub API" + + +def check_github_apiresponse(logger, r, ok_codes=None): + if ok_codes is None: + ok_codes = (200,) + + if r.status_code not in ok_codes: + try: + data = r.json() + message = data.get("message", "Unknown error") + except Exception: + message = "Not a valid JSON response" + + exc = GitHubApiError(r.status_code, message) + logger.error(exc.message) + raise exc + + +def check_github_ratelimit(logger, r): try: ratelimit = int(r.headers.get("X-RateLimit-Limit", None)) except Exception: @@ -55,4 +75,4 @@ def log_github_ratelimit(logger, r): ) if remaining == 0: - raise GithubRateLimitCheckError(remaining, ratelimit, reset) + raise GitHubRateLimitCheckError(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 index 45a51d824..37f3b6173 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py @@ -6,6 +6,8 @@ import logging import requests +from . import ApiCheckError + BRANCH_HEAD_URL = ( "https://api.bitbucket.org/2.0/repositories/{user}/{repo}/commit/{branch}" ) @@ -15,6 +17,26 @@ logger = logging.getLogger( ) +class BitbucketApiError(ApiCheckError): + API = "Bitbucket API" + + +def check_bitbucket_api_response(logger, r, ok_codes=None): + if ok_codes is None: + ok_codes = [requests.codes.ok] + + if r.status_code not in ok_codes: + try: + data = r.json() + message = data.get("message", "Unknown error") + except Exception: + message = "Not a valid JSON response" + + exc = BitbucketApiError(r.status_code, message) + logger.error(exc.message) + raise exc + + def _get_latest_commit(user, repo, branch, api_user=None, api_password=None): from ..exceptions import NetworkError @@ -31,12 +53,11 @@ def _get_latest_commit(user, repo, branch, api_user=None, api_password=None): except requests.ConnectionError as exc: raise NetworkError(cause=exc) - if not r.status_code == requests.codes.ok: - return None + check_bitbucket_api_response(logger, r) reference = r.json() if "hash" not in reference: - return None + raise BitbucketApiError(r.status_code, "No commit hash found in response") return reference["hash"] diff --git a/src/octoprint/plugins/softwareupdate/version_checks/github_commit.py b/src/octoprint/plugins/softwareupdate/version_checks/github_commit.py index 551dd7d69..b4c88f0e3 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/github_commit.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/github_commit.py @@ -30,12 +30,10 @@ def _get_latest_commit(user, repo, branch, apikey=None): except requests.ConnectionError as exc: raise NetworkError(cause=exc) - from . import log_github_ratelimit + from . import check_github_apiresponse, check_github_ratelimit - log_github_ratelimit(logger, r) - - if not r.status_code == requests.codes.ok: - return None + check_github_ratelimit(logger, r) + check_github_apiresponse(logger, r) reference = r.json() if "object" not in reference or "sha" not in reference["object"]: diff --git a/src/octoprint/plugins/softwareupdate/version_checks/github_release.py b/src/octoprint/plugins/softwareupdate/version_checks/github_release.py index c755bf639..a51a48760 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/github_release.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/github_release.py @@ -105,8 +105,6 @@ def _get_latest_release( ): from ..exceptions import NetworkError - nothing = None, None, None - headers = {} if apikey: auth = "token " + apikey @@ -119,12 +117,10 @@ def _get_latest_release( except requests.ConnectionError as exc: raise NetworkError(cause=exc) - from . import log_github_ratelimit + from . import check_github_apiresponse, check_github_ratelimit - log_github_ratelimit(logger, r) - - if not r.status_code == requests.codes.ok: - return nothing + check_github_ratelimit(logger, r) + check_github_apiresponse(logger, r) releases = r.json() @@ -172,26 +168,6 @@ def _get_sanitized_version(version_string): return version_string -def _get_base_from_version_tuple(version_tuple): - """ - Reduces version tuple to base version. - - Tests: - - >>> _get_base_from_version_tuple(("1", "2", "15")) - ('1', '2', '15') - >>> _get_base_from_version_tuple(("1", "2", "15", "*", "dev12")) - ('1', '2', '15') - """ - - base_version = [] - for part in version_tuple: - if part.startswith("*"): - break - base_version.append(part) - return tuple(base_version) - - def _get_comparable_version_semantic(version_string, force_base=True): import semantic_version