From 4c6dfed137fadc4a00d529b18cb937e9715a07b0 Mon Sep 17 00:00:00 2001 From: Andy Werner Date: Mon, 3 Jul 2017 16:26:58 +0200 Subject: [PATCH] SW Update check type 'bitbucket_commit' now supports API credentials for private repos --- .../version_checks/bitbucket_commit.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py b/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py index 969473d0..81d5277a 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py @@ -6,6 +6,7 @@ __copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms import requests import logging +import base64 from ..exceptions import ConfigurationInvalid @@ -13,8 +14,14 @@ BRANCH_HEAD_URL = "https://api.bitbucket.org/2.0/repositories/{user}/{repo}/comm logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.bitbucket_commit") -def _get_latest_commit(user, repo, branch): - r = requests.get(BRANCH_HEAD_URL.format(user=user, repo=repo, branch=branch)) + +def _get_latest_commit(user, repo, branch, api_user=None, api_password=None): + url = BRANCH_HEAD_URL.format(user=user, repo=repo, branch=branch) + headers = {} + if api_user is not None and api_password is not None: + headers['authorization'] = 'Basic {}'.format( + base64.b64encode(b"{user}:{pw}".format(user=api_user, pw=api_password))) + r = requests.get(url, headers=headers) if not r.status_code == requests.codes.ok: return None @@ -34,11 +41,14 @@ def get_latest(target, check): if "branch" in check: branch = check["branch"] + api_user = check["api_user"] if 'api_user' in check else None + api_password = check["api_password"] if 'api_password' in check else None + current = None if "current" in check: current = check["current"] - remote_commit = _get_latest_commit(check["user"], check["repo"], branch) + remote_commit = _get_latest_commit(check["user"], check["repo"], branch, api_user, api_password) information = dict( local=dict(name="Commit {commit}".format(commit=current if current is not None else "unknown"), value=current),