diff --git a/gerrit_util.py b/gerrit_util.py index c8d838c7e..984fa1014 100755 --- a/gerrit_util.py +++ b/gerrit_util.py @@ -14,6 +14,7 @@ import json import logging import netrc import os +import re import time import urllib from cStringIO import StringIO @@ -38,6 +39,10 @@ class GerritError(Exception): self.message = '(%d) %s' % (self.http_status, self.message) +class GerritAuthenticationError(GerritError): + """Exception class for authentication errors during Gerrit communication.""" + + def _QueryString(param_dict, first_param=None): """Encodes query parameters in the key:val[+key:val...] format specified here: @@ -115,6 +120,17 @@ def ReadHttpResponse(conn, expect_status=200, ignore_404=True): sleep_time = 0.5 for idx in range(TRY_LIMIT): response = conn.getresponse() + + # Check if this is an authentication issue. + www_authenticate = response.getheader('www-authenticate') + if (response.status in (httplib.UNAUTHORIZED, httplib.FOUND) and + www_authenticate): + auth_match = re.search('realm="([^"]+)"', www_authenticate, re.I) + host = auth_match.group(1) if auth_match else conn.req_host + reason = ('Authentication failed. Please make sure your .netrc file ' + 'has credentials for %s' % host) + raise GerritAuthenticationError(response.status, reason) + # If response.status < 500 then the result is final; break retry loop. if response.status < 500: break diff --git a/trychange.py b/trychange.py index fc9c1eb1f..7ad2fa126 100755 --- a/trychange.py +++ b/trychange.py @@ -779,8 +779,12 @@ def _SendChangeGerrit(bot_spec, options): change_id = GetChangeId(head_sha) - # Check that the uploaded revision matches the local one. - changes = gerrit_util.GetChangeCurrentRevision(gerrit_host, change_id) + try: + # Check that the uploaded revision matches the local one. + changes = gerrit_util.GetChangeCurrentRevision(gerrit_host, change_id) + except gerrit_util.GerritAuthenticationError, e: + raise NoTryServerAccess(e.message) + assert len(changes) <= 1, 'Multiple changes with id %s' % change_id if not changes: raise Error('A change %s was not found on the server. Was it uploaded?' %