refactor: factor Gerrit metadata generation into templates

Since the addition of the `expand_template` utility function to our Bash
utilities, we can expand Bash expressions in arbitrary files. In
particular, this allows us to generate YAML expressions within our LAVA
job templates, and begin reducing the amount of external script-based
templating we do.

This change begins with the Gerrit metadata section shared by both of
the FVP LAVA job templates, by removing the template-specific scripts
which generate the relevant YAML, and integrating it directly into the
templates.

Change-Id: I1e28791069e8194f967cb5b0df12a19db2a0f590
Signed-off-by: Chris Kay <chris.kay@arm.com>
diff --git a/fvp_utils.sh b/fvp_utils.sh
index 8985846..7854081 100644
--- a/fvp_utils.sh
+++ b/fvp_utils.sh
@@ -284,15 +284,33 @@
     fi
 }
 
+# Generates the template for YAML-based LAVA job definitions from a file
+# corresponding to the currently-selected payload, e.g.:
+#
+# - `lava-templates/fvp-linux.yaml`
+# - `lava-templates/fvp-tftf.yaml`
+#
+# The job definition template is itself expanded with visibility of all
+# variables that are available from within the function, including those with
+# local scope.
+#
+# TODO: Move the Gerrit metadata generation to to the main YAML generation phase
+#   so that we can template the file in one phase.
 gen_fvp_yaml_template() {
-    local yaml_template_file="$workspace/fvp_template.yaml"
+    if [ -n "${GERRIT_CHANGE_NUMBER}" ]; then
+        local gerrit_url="https://review.trustedfirmware.org/c/${GERRIT_CHANGE_NUMBER}/${GERRIT_PATCHSET_NUMBER}"
+    elif [ -n "${GERRIT_REFSPEC}" ]; then
+        local gerrit_url=$(echo ${GERRIT_REFSPEC} |
+            awk -F/ '{print "https://review.trustedfirmware.org/c/" $4 "/" $5}')
+    fi
 
-    # must parameters for yaml generation
-    local payload_type="${payload_type:?}"
+    local yaml_template_file="${workspace}/fvp_template.yaml"
 
-    "$ci_root/script/gen_fvp_${payload_type}_yaml.sh" > "$yaml_template_file"
+    pushd "${ci_root}/script/lava-templates"
+    expand_template "fvp-${payload_type:?}.yaml" > "$yaml_template_file"
+    popd
 
-    archive_file "$yaml_template_file"
+    archive_file "${yaml_template_file}"
 }
 
 gen_fvp_yaml() {
diff --git a/script/gen_fvp_linux_yaml.sh b/script/gen_fvp_linux_yaml.sh
deleted file mode 100755
index 68ef6eb..0000000
--- a/script/gen_fvp_linux_yaml.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env bash
-#
-# Copyright (c) 2019-2021 Arm Limited. All rights reserved.
-#
-# SPDX-License-Identifier: BSD-3-Clause
-#
-
-# Generate a FVP-TFTF model agnostic YAML template. Note that this template
-# is not ready to be sent to LAVA by Jenkins. So in order to produce complete
-# file, variables in {UPPERCASE} must be replaced to correct values. This
-# file also includes references to ${UPPERCASE} which are just normal shell
-# variables, replaced on spot.
-
-ci_root="$(readlink -f "$(dirname "$0")/..")"
-source "$ci_root/utils.sh"
-
-. $(dirname $0)/gen_gerrit_meta.sh
-
-expand_template "$(dirname "$0")/lava-templates/fvp-linux.yaml"
diff --git a/script/gen_fvp_tftf_yaml.sh b/script/gen_fvp_tftf_yaml.sh
deleted file mode 100755
index 5c024ca..0000000
--- a/script/gen_fvp_tftf_yaml.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env bash
-#
-# Copyright (c) 2019-2021 Arm Limited. All rights reserved.
-#
-# SPDX-License-Identifier: BSD-3-Clause
-#
-
-# Generate a FVP-TFTF model agnostic YAML template. Note that this template
-# is not ready to be sent to LAVA by Jenkins. So in order to produce complete
-# file, variables in {UPPERCASE} must be replaced to correct values. This
-# file also includes references to ${UPPERCASE} which are just normal shell
-# variables, replaced on spot.
-
-ci_root="$(readlink -f "$(dirname "$0")/..")"
-source "$ci_root/utils.sh"
-
-. $(dirname $0)/gen_gerrit_meta.sh
-
-expand_template "$(dirname "$0")/lava-templates/fvp-tftf.yaml"
diff --git a/script/gen_gerrit_meta.sh b/script/gen_gerrit_meta.sh
deleted file mode 100644
index 1c3e93b..0000000
--- a/script/gen_gerrit_meta.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#
-# Copyright (c) 2021 Arm Limited. All rights reserved.
-#
-# SPDX-License-Identifier: BSD-3-Clause
-#
-
-# Generate Gerrit-related metadata for LAVA job definitions. This is
-# include file is supposed to be sourced from gen_*_yaml.sh files.
-
-
-if [ -n "${GERRIT_CHANGE_NUMBER}" ]; then
-    gerrit_url="https://review.trustedfirmware.org/c/${GERRIT_CHANGE_NUMBER}/${GERRIT_PATCHSET_NUMBER}"
-elif [ -n "${GERRIT_REFSPEC}" ]; then
-    gerrit_url=$(echo ${GERRIT_REFSPEC} |  awk -F/ '{print "https://review.trustedfirmware.org/c/" $4 "/" $5}')
-fi
-
-if [ -n "${gerrit_url}" ]; then
-    gerrit_meta="\
-  gerrit_url: ${gerrit_url}"
-fi
diff --git a/script/lava-templates/fvp-linux.yaml b/script/lava-templates/fvp-linux.yaml
index 2f0527a..711499e 100644
--- a/script/lava-templates/fvp-linux.yaml
+++ b/script/lava-templates/fvp-linux.yaml
@@ -2,7 +2,12 @@
   test_config: {TEST_CONFIG}
   fvp_model: {MODEL}
   build_url: ${BUILD_URL}
-${gerrit_meta}
+
+$(if [ -n "${gerrit_url}" ]; then
+	cat <<-YAML
+  gerrit_url: "${gerrit_url}"
+	YAML
+fi)
 
 device_type: fvp
 job_name: fvp-linux-{TEST_CONFIG}
diff --git a/script/lava-templates/fvp-tftf.yaml b/script/lava-templates/fvp-tftf.yaml
index a21a12c..9a337a3 100644
--- a/script/lava-templates/fvp-tftf.yaml
+++ b/script/lava-templates/fvp-tftf.yaml
@@ -2,7 +2,12 @@
   test_config: {TEST_CONFIG}
   fvp_model: {MODEL}
   build_url: ${BUILD_URL}
-${gerrit_meta}
+
+$(if [ -n "${gerrit_url}" ]; then
+	cat <<-YAML
+  gerrit_url: "${gerrit_url}"
+	YAML
+fi)
 
 device_type: fvp
 job_name: {TEST_CONFIG}