Merge "Update gerrit change attributes even if merged"
diff --git a/tests/base.py b/tests/base.py
index 1b82944..179f4f4 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -369,6 +369,7 @@
         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 @@
     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
@@ -942,9 +946,6 @@
         self.config.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
 
     def assertFinalState(self):
-        # Make sure that the change cache is cleared
-        self.assertEqual(len(self.gerrit._change_cache.keys()), 0,
-                         "Change cache should have been cleared")
         # Make sure that git.Repo objects have been garbage collected.
         repos = []
         gc.collect()
diff --git a/tests/test_zuultrigger.py b/tests/test_zuultrigger.py
index eb8fdc5..9e9bc61 100644
--- a/tests/test_zuultrigger.py
+++ b/tests/test_zuultrigger.py
@@ -102,3 +102,4 @@
             "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")
diff --git a/zuul/trigger/gerrit.py b/zuul/trigger/gerrit.py
index 2be3799..4d4deb8 100644
--- a/zuul/trigger/gerrit.py
+++ b/zuul/trigger/gerrit.py
@@ -279,13 +279,9 @@
     def maintainCache(self, relevant):
         # This lets the user supply a list of change objects that are
         # still in use.  Anything in our cache that isn't in the supplied
-        # list should be same to remove from the cache.
-        remove = []
-        for key, change in self._change_cache.items():
-            if change not in relevant:
-                remove.append(key)
-        for key in remove:
-            del self._change_cache[key]
+        # list should be safe to remove from the cache.
+        # TODO(jeblair): consider removing this feature
+        return
 
     def postConfig(self):
         pass
@@ -324,11 +320,18 @@
         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):
diff --git a/zuul/trigger/zuultrigger.py b/zuul/trigger/zuultrigger.py
index 436311b..27098ab 100644
--- a/zuul/trigger/zuultrigger.py
+++ b/zuul/trigger/zuultrigger.py
@@ -47,7 +47,8 @@
             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 @@
             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()