Merge "Enforce ref only for gerrit events that supply a ref"
diff --git a/tests/fixtures/layouts/bad_misplaced_ref.yaml b/tests/fixtures/layouts/bad_misplaced_ref.yaml
new file mode 100644
index 0000000..f009c39
--- /dev/null
+++ b/tests/fixtures/layouts/bad_misplaced_ref.yaml
@@ -0,0 +1,13 @@
+pipelines:
+  - name: 'check'
+    manager: IndependentPipelineManager
+    trigger:
+      gerrit:
+        - event: patchset-created
+          ref: /some/ref/path
+
+projects:
+  - name: org/project
+    merge-mode: cherry-pick
+    check:
+      - project-check
diff --git a/zuul/layoutvalidator.py b/zuul/layoutvalidator.py
index 9a4e00f..6969653 100644
--- a/zuul/layoutvalidator.py
+++ b/zuul/layoutvalidator.py
@@ -18,6 +18,7 @@
 import voluptuous as v
 import string
 
+from zuul.trigger import gerrit
 
 # Several forms accept either a single item or a list, this makes
 # specifying that in the schema easy (and explicit).
@@ -271,3 +272,7 @@
         if 'project-templates' in data:
             self.checkDuplicateNames(
                 data['project-templates'], ['project-templates'])
+        for pipeline in data['pipelines']:
+            if 'gerrit' in pipeline['trigger']:
+                gerrit.validate_trigger(pipeline['trigger'])
+
diff --git a/zuul/trigger/gerrit.py b/zuul/trigger/gerrit.py
index bf591c4..368e37d 100644
--- a/zuul/trigger/gerrit.py
+++ b/zuul/trigger/gerrit.py
@@ -16,6 +16,7 @@
 import threading
 import time
 import urllib2
+import voluptuous
 from zuul.lib import gerrit
 from zuul.model import TriggerEvent, Change, Ref, NullChange
 
@@ -412,3 +413,14 @@
         if sha:
             url += ';a=commitdiff;h=' + sha
         return url
+
+
+def validate_trigger(trigger_data):
+    """Validates the layout's trigger data."""
+    events_with_ref = ('ref-updated', )
+    for event in trigger_data['gerrit']:
+        if event['event'] not in events_with_ref and event.get('ref', False):
+            raise voluptuous.Invalid(
+                "The event %s does not include ref information, Zuul cannot "
+                "use ref filter 'ref: %s'" % (event['event'], event['ref']))
+