Fix queries for Zuul trigger

Against a live gerrit, we observed that sometimes gerrit may not
return data for a change.  In the case of the zuul trigger's
project-change-merged event, it's best to just ignore errors
for those changes and proceed.

Also, the gerrit trigger was attempting to parse the timing
results from the query.  The test did not catch this because it
did not supply them in the mocked method.  Correct this as well.

Also, double check that the query used was the one expected in
the test.

Change-Id: I792127d29f67f53a419eb94e9e0afb83b6e1bcb2
This commit is contained in:
James E. Blair 2014-08-15 15:24:24 -07:00
parent c878c98977
commit f8ff9937b5
4 changed files with 23 additions and 9 deletions

View File

@ -369,6 +369,7 @@ class FakeGerrit(object):
self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
self.change_number = 0
self.changes = {}
self.queries = []
def addFakeChange(self, project, branch, subject, status='NEW'):
self.change_number += 1
@ -405,7 +406,10 @@ class FakeGerrit(object):
def simpleQuery(self, query):
# This is currently only used to return all open changes for a
# project
return [change.query() for change in self.changes.values()]
self.queries.append(query)
l = [change.query() for change in self.changes.values()]
l.append({"type":"stats","rowCount":1,"runTimeMilliseconds":3})
return l
def startWatching(self, *args, **kw):
pass

View File

@ -102,3 +102,4 @@ class TestZuulTrigger(ZuulTestCase):
"Merge Failed.\n\nThis change was unable to be automatically "
"merged with the current state of the repository. Please rebase "
"your change and upload a new patchset.")
self.assertEqual(self.fake_gerrit.queries[0], "project:org/project status:open")

View File

@ -324,11 +324,18 @@ class Gerrit(object):
return change
def getProjectOpenChanges(self, project):
data = self.gerrit.simpleQuery("project:%s status:open" % project.name)
# This is a best-effort function in case Gerrit is unable to return
# a particular change. It happens.
query = "project:%s status:open" % (project.name,)
self.log.debug("Running query %s to get project open changes" % (query,))
data = self.gerrit.simpleQuery(query)
changes = []
for record in data:
changes.append(self._getChange(record['number'],
record['currentPatchSet']['number']))
for record in data[:-1]:
try:
changes.append(self._getChange(record['number'],
record['currentPatchSet']['number']))
except Exception:
self.log.exception("Unable to query change %s" % (record.get('number'),))
return changes
def updateChange(self, change):

View File

@ -47,7 +47,8 @@ class ZuulTrigger(object):
try:
self._createProjectChangeMergedEvents(change)
except Exception:
self.log.exception("Unable to create project-change-merged events for %s" % (change,))
self.log.exception("Unable to create project-change-merged events for %s" %
(change,))
def onChangeEnqueued(self, change, pipeline):
# Called each time a change is enqueued in a pipeline
@ -55,12 +56,13 @@ class ZuulTrigger(object):
try:
self._createParentChangeEnqueuedEvents(change, pipeline)
except Exception:
self.log.exception("Unable to create parent-change-enqueued events for %s in %s" % (change, pipeline))
self.log.exception("Unable to create parent-change-enqueued events for %s in %s" %
(change, pipeline))
def _createProjectChangeMergedEvents(self, change):
changes = self.sched.triggers['gerrit'].getProjectOpenChanges(change.project)
for change in changes:
self._createProjectChangeMergedEvent(change)
for open_change in changes:
self._createProjectChangeMergedEvent(open_change)
def _createProjectChangeMergedEvent(self, change):
event = TriggerEvent()