Fix lazy initialization of GithubUser with apps

When in app mode the lazy initialization of the GithubUser object can
fail because the github client is eager initialized. This is the case
if the job takes longer than the lifetime of the auth token which
crashes reporting than with [1].

[1] Trace:
2018-01-31 09:21:55,354 ERROR zuul.Pipeline.xpad.gate: Exception while reporting:
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/zuul/manager/__init__.py", line 856, in _reportItem
    ret = self.sendReport(actions, item)
  File "/usr/lib/python3.6/site-packages/zuul/manager/__init__.py", line 175, in sendReport
    ret = reporter.report(item)
  File "/usr/lib/python3.6/site-packages/zuul/driver/github/githubreporter.py", line 73, in report
    self.mergePull(item)
  File "/usr/lib/python3.6/site-packages/zuul/driver/github/githubreporter.py", line 137, in mergePull
    message = self._formatMergeMessage(item.change)
  File "/usr/lib/python3.6/site-packages/zuul/driver/github/githubreporter.py", line 175, in _formatMergeMessage
    if not account:
  File "/usr/lib/python3.6/site-packages/zuul/driver/github/githubconnection.py", line 377, in __len__
    self._init_data()
  File "/usr/lib/python3.6/site-packages/zuul/driver/github/githubconnection.py", line 382, in _init_data
    user = self._github.user(self._username)
  File "/usr/lib/python3.6/site-packages/github3/github.py", line 1657, in user
    json = self._json(self._get(url), 200)
  File "/usr/lib/python3.6/site-packages/github3/models.py", line 211, in _json
    if self._boolean(response, status_code, 404) and response.content:
  File "/usr/lib/python3.6/site-packages/github3/models.py", line 233, in _boolean
    raise exceptions.error_for(response)
github3.exceptions.AuthenticationFailed: 401 Bad credentials

Change-Id: Id38520b790d7d7c6ce72bbcc8a736ed7485b5ae1
This commit is contained in:
Tobias Henkel 2018-01-31 15:09:08 +01:00
parent 22c5b7155b
commit dea5fec143
No known key found for this signature in database
GPG Key ID: 03750DEC158E5FA2
1 changed files with 9 additions and 7 deletions

View File

@ -343,7 +343,7 @@ class GithubEventConnector(threading.Thread):
if login:
# TODO(tobiash): it might be better to plumb in the installation id
project = body.get('repository', {}).get('full_name')
return self.connection.getUser(login, project=project)
return self.connection.getUser(login, project)
def run(self):
while True:
@ -360,10 +360,11 @@ class GithubEventConnector(threading.Thread):
class GithubUser(collections.Mapping):
log = logging.getLogger('zuul.GithubUser')
def __init__(self, github, username):
self._github = github
def __init__(self, username, connection, project):
self._connection = connection
self._username = username
self._data = None
self._project = project
def __getitem__(self, key):
self._init_data()
@ -379,9 +380,10 @@ class GithubUser(collections.Mapping):
def _init_data(self):
if self._data is None:
user = self._github.user(self._username)
github = self._connection.getGithubClient(self._project)
user = github.user(self._username)
self.log.debug("Initialized data for user %s", self._username)
log_rate_limit(self.log, self._github)
log_rate_limit(self.log, github)
self._data = {
'username': user.login,
'name': user.name,
@ -972,8 +974,8 @@ class GithubConnection(BaseConnection):
log_rate_limit(self.log, github)
return reviews
def getUser(self, login, project=None):
return GithubUser(self.getGithubClient(project), login)
def getUser(self, login, project):
return GithubUser(login, self, project)
def getUserUri(self, login):
return 'https://%s/%s' % (self.server, login)