Fix github dependent pipeline with merge

When enabling merge in a github gate pipeline merge message formatting
fails with an exception [1]. The reason for this is a missing
initialization of the GithubUser data structure which is using lazy
initialization (presumibly due to api rate limiting). This is fixed by
adding the initialization.

This was uncovered in the tests because the FakeGithubConnection
directly returns a data structure instead of a GithubUser object like
the real GithubConnection. This is fixed by returning a real
GithubUser object linked to a FakeGithub object.

[1] 2017-08-02 07:13:21,323 ERROR zuul.DependentPipelineManager: Exception while reporting:
  Traceback (most recent call last):
    File "/usr/lib/python3.6/site-packages/zuul/manager/__init__.py", line 786, in _reportItem
      ret = self.sendReport(actions, item)
    File "/usr/lib/python3.6/site-packages/zuul/manager/__init__.py", line 168, 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 129, in mergePull
      message = self._formatMergeMessage(item.change)
    File "/usr/lib/python3.6/site-packages/zuul/driver/github/githubreporter.py", line 168, in _formatMergeMessage
      if not account:
    File "/usr/lib/python3.6/site-packages/zuul/driver/github/githubconnection.py", line 340, in __len__
      return len(self._data)
  TypeError: object of type 'NoneType' has no len()

Change-Id: Ieb9694ff0c98c8cc4011cd4e36627e8ed647d078
This commit is contained in:
Tobias Henkel 2017-08-02 10:13:30 +02:00
parent 6665c1a358
commit 64e37a0490
2 changed files with 30 additions and 18 deletions

View File

@ -551,6 +551,18 @@ class GithubChangeReference(git.Reference):
_points_to_commits_only = True _points_to_commits_only = True
class FakeGithub(object):
class FakeUser(object):
def __init__(self, login):
self.login = login
self.name = "Github User"
self.email = "github.user@example.com"
def user(self, login):
return self.FakeUser(login)
class FakeGithubPullRequest(object): class FakeGithubPullRequest(object):
def __init__(self, github, number, project, branch, def __init__(self, github, number, project, branch,
@ -879,6 +891,13 @@ class FakeGithubConnection(githubconnection.GithubConnection):
self.merge_failure = False self.merge_failure = False
self.merge_not_allowed_count = 0 self.merge_not_allowed_count = 0
self.reports = [] self.reports = []
self.github_client = FakeGithub()
def getGithubClient(self,
project=None,
user_id=None,
use_app=True):
return self.github_client
def openFakePullRequest(self, project, branch, subject, files=[], def openFakePullRequest(self, project, branch, subject, files=[],
body=None): body=None):
@ -965,14 +984,6 @@ class FakeGithubConnection(githubconnection.GithubConnection):
pr = self.pull_requests[number - 1] pr = self.pull_requests[number - 1]
return pr.reviews return pr.reviews
def getUser(self, login):
data = {
'username': login,
'name': 'Github User',
'email': 'github.user@example.com'
}
return data
def getRepoPermission(self, project, login): def getRepoPermission(self, project, login):
owner, proj = project.split('/') owner, proj = project.split('/')
for pr in self.pull_requests: for pr in self.pull_requests:

View File

@ -326,25 +326,26 @@ class GithubUser(collections.Mapping):
self._data = None self._data = None
def __getitem__(self, key): def __getitem__(self, key):
if self._data is None: self._init_data()
self._data = self._init_data()
return self._data[key] return self._data[key]
def __iter__(self): def __iter__(self):
self._init_data()
return iter(self._data) return iter(self._data)
def __len__(self): def __len__(self):
self._init_data()
return len(self._data) return len(self._data)
def _init_data(self): def _init_data(self):
if self._data is None:
user = self._github.user(self._username) user = self._github.user(self._username)
log_rate_limit(self.log, self._github) log_rate_limit(self.log, self._github)
data = { self._data = {
'username': user.login, 'username': user.login,
'name': user.name, 'name': user.name,
'email': user.email 'email': user.email
} }
return data
class GithubConnection(BaseConnection): class GithubConnection(BaseConnection):