update remote on ref-update

Whenever receiving a ref-update event, for example when a tag is
created, deleted or changed, the local Zuul git repository would not
reflect the change.

This patch force a git remote update on the local git repository so the
Jenkins git plugin can actually find the new tag. This is done before
enqueing the change in the pipeline to only update the remote once.

Change-Id: I87a29aa15855bfb367f03da3e15cd8a733b757fd
Reviewed-on: https://review.openstack.org/19928
Approved: James E. Blair <corvus@inaugust.com>
Reviewed-by: James E. Blair <corvus@inaugust.com>
Tested-by: Jenkins
diff --git a/zuul/merger.py b/zuul/merger.py
index ed89ec8..7ad7eed 100644
--- a/zuul/merger.py
+++ b/zuul/merger.py
@@ -149,6 +149,14 @@
         r.recreateRepoObject()
         return r
 
+    def updateRepo(self, project):
+        repo = self.getRepo(project)
+        try:
+            self.log.info("Updating local repository %s", project)
+            repo.update()
+        except:
+            self.log.exception("Unable to update %s", project)
+
     def _mergeChange(self, change, ref, target_ref, mode):
         repo = self.getRepo(change.project)
         try:
diff --git a/zuul/scheduler.py b/zuul/scheduler.py
index b52e97c..2f5795c 100644
--- a/zuul/scheduler.py
+++ b/zuul/scheduler.py
@@ -403,6 +403,15 @@
             self.trigger_event_queue.task_done()
             return
 
+        # Preprocessing for ref-update events
+        if hasattr(event, 'refspec'):
+            # Make sure the local git repo is up-to-date with the remote one.
+            # We better have the new ref before enqueuing the changes.
+            # This is done before enqueuing the changes to avoid calling an
+            # update per pipeline accepting the change.
+            self.log.info("Fetching references for %s" % project)
+            self.merger.updateRepo(project)
+
         for pipeline in self.pipelines.values():
             change = event.getChange(project, self.trigger)
             if event.type == 'patchset-created':