lava-submit.jpl: Rework detection of "per-patch" runs

As those are defined as started by the tf-m-static parent job, check just
for that. Checking for tf-m-build-and-test doesn't make sense, as that job
is run for manual testing purposes, when adhoc filtering is not required
and confusing.

When checking for the job name, using .endsWith() method, to accommodate
per-user job copies on staging (user-tf-m-static).

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
Change-Id: I82475fbcc8719abefa92b389283e240be2c9dd30
diff --git a/jenkins/lava-submit.jpl b/jenkins/lava-submit.jpl
index efcc755..d2ed238 100644
--- a/jenkins/lava-submit.jpl
+++ b/jenkins/lava-submit.jpl
@@ -42,11 +42,27 @@
     }
     stage("LAVA") {
       def device_type = ""
-      def upstreamProject = getUpstreamJob()[0].upstreamProject
-      if (upstreamProject == "tf-m-build-and-test") {
-        device_type = "--physical-board-only "
-        print("Run test cases only on physical boards in per-patch.")
+
+      // For builds running per-patch (started by the tf-m-static job),
+      // run tests only on physical boards to improve turnaround/performance.
+      def upstream_job = getUpstreamJob()[0]
+      if (upstream_job instanceof hudson.model.Cause.UpstreamCause) {
+        def gp_causes = upstream_job.getUpstreamCauses()
+        if (gp_causes.size() > 0 && gp_causes[0] instanceof hudson.model.Cause.UpstreamCause) {
+          print("Grand-parent project cause: ")
+          println(gp_causes[0].upstreamProject)
+          if (gp_causes[0].upstreamProject.endsWith("tf-m-static")) {
+            device_type = "--physical-board-only "
+            print("Run test cases only on physical boards in tf-m-static.")
+          }
+        } else {
+          println("No grand-parent project cause.")
+        }
       }
+      // Workaround for Groovy CPS idiosyncrasies. See e.g.
+      // https://blog.thesparktree.com/you-dont-know-jenkins-part-3#solutions
+      upstream_job = null
+      gp_causes = null
 
       withCredentials([usernamePassword(credentialsId: env.LAVA_CREDENTIALS, passwordVariable: 'LAVA_TOKEN', usernameVariable: 'LAVA_USER')]) {
         print("Generating LAVA jobs...")