Merge branch 'maintenance' into devel
This commit is contained in:
commit
5f47af505a
9 files changed
+96
-43
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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('<i class="fa fa-spinner fa-spin"></i> ');
|
||||
|
||||
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(
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<div class="control-group">
|
||||
<label class="control-label">{{ _('GitHub Access Token') }}</label>
|
||||
<div class="controls">
|
||||
<input type="text" class="input-block-level" data-bind="value: config_githubToken" placeholder="{{ _('Enter a new token...')|edq }}">
|
||||
<span class="help-block">{{ _('Set this if you keep running into rate limiting issues. <a href="%(url)s" target="_blank" rel="noopener noreferer">See here for how to obtain a token</a>.', url="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token") }}</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -171,6 +171,7 @@
|
||||
{% include "snippets/checkoutFolder.jinja2" %}
|
||||
{% include "snippets/pipTarget.jinja2" %}
|
||||
{% include "snippets/pipEnableCheck.jinja2" %}
|
||||
{% include "snippets/githubToken.jinja2" %}
|
||||
<div class="control-group">
|
||||
<label class="control-label">{{ _('Version cache TTL') }}</label>
|
||||
<div class="controls">
|
||||
|
||||
@@ -2,7 +2,7 @@ __author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__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)
|
||||
@@ -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"]
|
||||
|
||||
|
||||
@@ -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"]:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user