Fix branch detection in prepare-gerrit-repos

This corrects two bugs:

1) We need to detect the branch of Gerrit that is being checked out
in order to determine whether each submodule has that branch.  We were
using "zuul.branch" which is the branch of the change being tested,
but that only works for branched source code repos.  It doesn't work
for jobs in unbranched repos like zuul/jobs.  To allow this to run
there, let's just directly check the branch of Gerrit that is checked
out.

2) A logic error caused the repo_has_dependent_change fact to search
for dependent changes that matched the repo of the change under test,
but we actually need to search for changes for the repo we're checking
out.

Change-Id: I4e09c6c8ba752ae1af2907a20153d21d9f769e1c
diff --git a/roles/prepare-gerrit-repos/README.rst b/roles/prepare-gerrit-repos/README.rst
index 81ec7bb..de7ca4b 100644
--- a/roles/prepare-gerrit-repos/README.rst
+++ b/roles/prepare-gerrit-repos/README.rst
@@ -65,3 +65,10 @@
       gerrit_project_mapping:
         gerrit: ''
         jgit: modules/jgit
+
+.. zuul:rolevar:: gerrit_project_name
+   :default: gerrit.googlesource.com/gerrit
+
+   The canonical name of the Gerrit repository.  This role uses this
+   value to look up the branch of Gerrit which is checked out in order
+   to detect whether or not sub-projects contain the same branch.
diff --git a/roles/prepare-gerrit-repos/defaults/main.yaml b/roles/prepare-gerrit-repos/defaults/main.yaml
index ce0cbe7..8a6c340 100644
--- a/roles/prepare-gerrit-repos/defaults/main.yaml
+++ b/roles/prepare-gerrit-repos/defaults/main.yaml
@@ -1,3 +1,4 @@
+gerrit_project_name: gerrit.googlesource.com/gerrit
 gerrit_project_mapping:
   gerrit: ''
   jgit: modules/jgit
diff --git a/roles/prepare-gerrit-repos/tasks/repo.yaml b/roles/prepare-gerrit-repos/tasks/repo.yaml
index f4da53f..bce358c 100644
--- a/roles/prepare-gerrit-repos/tasks/repo.yaml
+++ b/roles/prepare-gerrit-repos/tasks/repo.yaml
@@ -4,12 +4,13 @@
 
 - name: Check if zuul.branch exists in repo
   set_fact:
-    # If zuul checked out the branch we're testing, then it exists.
-    project_branch_exists: "{{ zuul.branch == project.checkout }}"
+    # If zuul checked out the branch we're testing (ie, gerrit's
+    # branch), then it exists.
+    project_branch_exists: "{{ zuul.projects[gerrit_project_name].checkout == project.checkout }}"
 
 - name: Check if repo has a dependent change
   set_fact:
-    repo_has_dependent_change: "{{ zuul['items'] | selectattr('project.canonical_name', 'eq', zuul.project.canonical_name) | list | length | bool }}"
+    repo_has_dependent_change: "{{ zuul['items'] | selectattr('project.canonical_name', 'eq', project.canonical_name) | list | length | bool }}"
 
 - name: Check for unsatisfiable source repo condition
   when:
@@ -19,17 +20,19 @@
   fail:
     msg: >-
       The repository {{ project.name }} does not contain the branch
-      under test ({{ zuul.branch }}), but this change depends on a
-      change to that project and branch.  While Zuul is able to check
-      out the repos in the requested state, the branch mismatch means
-      that Gerrit's submodule subscription would not automatically
-      update the submodule pointer, and the merged state would not
-      reflect the tested state.
+      under test ({{ zuul.projects[gerrit_project_name].checkout }}),
+      but this change depends on a change to that project and branch.
+      While Zuul is able to check out the repos in the requested
+      state, the branch mismatch means that Gerrit's submodule
+      subscription would not automatically update the submodule
+      pointer, and the merged state would not reflect the tested
+      state.
 
       This configuration would be testable by creating a {{
-      zuul.branch }} branch in the {{ project.name }} repo.
-      Alternatively, you can merge the dependent change, manually
-      update the submodule pointer, then test this change again.
+      zuul.projects[gerrit_project_name].checkout }} branch in the {{
+      project.name }} repo.  Alternatively, you can merge the
+      dependent change, manually update the submodule pointer, then
+      test this change again.
 
 # If there is no matching branch we need to check out the actual sha
 # defined in the parent repo.