Allow building this project from changes against other projects

As suggested on #zuul, the key is really only:

- listing the "integration" job within a pipeline of the triggering project,
- making the triggered job always run in the working directory of its
  own project (the triggered one), not in the triggering one,
- pick changes from the triggering project(s) into the triggered project.

Change-Id: If5e706f32996e219c4929f56343e7e9181b86275
diff --git a/.zuul.yaml b/.zuul.yaml
index a053701..0b5bc96 100644
--- a/.zuul.yaml
+++ b/.zuul.yaml
@@ -4,6 +4,7 @@
     timeout: 3600
     vars:
       prepare_git_submodules: true
+      zuul_work_dir: "{{ zuul.projects['cesnet-gerrit-czechlight/CzechLight/br2-external'].src_dir }}"
     required-projects: &projects
       - github/buildroot/buildroot
       - CzechLight/dependencies
diff --git a/ci/build.sh b/ci/build.sh
index e3078e2..94b9d01 100755
--- a/ci/build.sh
+++ b/ci/build.sh
@@ -4,16 +4,44 @@
 shopt -s failglob
 
 ZUUL_JOB_NAME=$(jq < ~/zuul-env.json -r '.job')
-ZUUL_PROJECT_SRC_DIR=$HOME/$(jq < ~/zuul-env.json -r '.project.src_dir')
-ZUUL_PROJECT_SHORT_NAME=$(jq < ~/zuul-env.json -r '.project.short_name')
+ZUUL_PROJECT_SRC_DIR=$HOME/$(jq < ~/zuul-env.json -r '.projects["cesnet-gerrit-czechlight/CzechLight/br2-external"].src_dir')
+ZUUL_PROJECT_SHORT_NAME=$(jq < ~/zuul-env.json -r '.projects["cesnet-gerrit-czechlight/CzechLight/br2-external"].short_name')
 CI_PARALLEL_JOBS=$(awk -vcpu=$(getconf _NPROCESSORS_ONLN) 'BEGIN{printf "%.0f", cpu*1.3+1}')
 
 BUILD_DIR=~/build
 mkdir ${BUILD_DIR}
 cd ${BUILD_DIR}
 
+# Dependencies are normally specified via the cla-sysrepo.git repo
 ${ZUUL_PROJECT_SRC_DIR}/dev-setup-git.sh
 
+# If we're being triggered via a change against another repo, use speculatively merged stuff from Zuul, not our submodules
+if [[ $(jq < ~/zuul-env.json -r '.project.name') != 'CzechLight/br2-external' ]]; then
+    # C++ dependencies can be provided either via cla-sysrepo, or via netconf-cli.
+    # Whatever is the latest change in the queue wins.
+    USE_DEPENDENCIES_VIA=$(jq < ~/zuul-env.json -r '[.items[]? | select(.project.name == "CzechLight/cla-sysrepo" or .project.name == "CzechLight/netconf-cli")][-1]?.project.src_dir + ""')
+    if [[ ! -z "${USE_DEPENDENCIES_VIA}" ]]; then
+        sed -i "s|${ZUUL_PROJECT_SRC_DIR}/submodules/cla-sysrepo/submodules/dependencies/|${HOME}/${USE_DEPENDENCIES_VIA}/submodules/dependencies/|g" local.mk
+        # Our Zuul playbook only prepares submodules within CzechLight/br2-external, not submodules of other projects
+        pushd ${HOME}/${USE_DEPENDENCIES_VIA}
+        # ...and before we check out, make sure that relative URLs work, i.e,. no file:///dev/null
+        git config remote.origin.url $(pwd)
+        git submodule update --init --recursive
+        git config remote.origin.url file:///dev/null
+        popd
+    fi
+
+    for PROJECT in cla-sysrepo netconf-cli gammarus; do
+        # If there's a change for ${PROJECT} queued ahead, ensure it gets used.
+        # This means that if our submodules still pin, say, `cla-sysrepo` to some ancient version and we're testing a `netconf-cli` change,
+        # then we will keep using that ancient `cla-sysrepo`. Hopefully this reduces the number of false alerts.
+        DEPSRCDIR=$(jq < ~/zuul-env.json -r "[.items[]? | select(.project.name == \"CzechLight/${PROJECT}\")][-1]?.project.src_dir + \"\"")
+        if [[ ! -z "${DEPSRCDIR}" ]]; then
+            sed -i "s|${ZUUL_PROJECT_SRC_DIR}/submodules/${PROJECT}|${HOME}/${DEPSRCDIR}|g" local.mk
+        fi
+    done
+fi
+
 if [[ "${ZUUL_JOB_NAME}" =~ clearfog ]]; then
     make czechlight_clearfog_defconfig
 elif [[ "${ZUUL_JOB_NAME}" =~ beagleboneblack ]]; then