Include bzlmod from plugin's external deps and its extra components

Allow importing the bzlmod of extra external components of a plugin,
defined in the extraPlugin, extraModule and extraGhRepo, including their
modules, rather than creating a symlink to it.

This change leverages the ability to have multi-level dependencies and
compose them using a module definition rather than a bazel script
fragment, as we had to do before the adoption of bzlmod.

For older builds, before the bzlmod adoption, the system doesn't change
and the dependent plugins, lib and repos cannot contribute their
external dependencies.

Change-Id: I5080f0682879b5c03f8cf2bfb87bfb8087ce7676
diff --git a/jenkins/gerrit-bazel-build-plugin-branch.sh b/jenkins/gerrit-bazel-build-plugin-branch.sh
index 0740ad9..ac8ffad 100644
--- a/jenkins/gerrit-bazel-build-plugin-branch.sh
+++ b/jenkins/gerrit-bazel-build-plugin-branch.sh
@@ -22,7 +22,7 @@
   pushd plugins && ln -s {repo}/{sourcePath} . && popd
 fi
 
-for file in external_plugin_deps.bzl external_plugin_deps.MODULE.bazel external_package.json
+for file in external_plugin_deps.bzl external_package.json
 do
   if [ -f plugins/{name}/$file ]
   then
@@ -30,6 +30,16 @@
   fi
 done
 
+includeExternalBzlModDeps() {{
+  local pluginName=$1
+  if [ -f plugins/$pluginName/external_plugin_deps.MODULE.bazel ]
+  then
+    echo 'include("//plugins/'$pluginName':external_plugin_deps.MODULE.bazel")' >> plugins/external_plugin_deps.MODULE.bazel
+  fi
+}}
+
+includeExternalBzlModDeps {name}
+
 PLUGIN_SCM_BASE_URL="https://gerrit.googlesource.com/a"
 for extraPlugin in {extra-plugins}
 do
@@ -39,6 +49,7 @@
     pushd plugins
     ln -s ../../$extraPlugin .
     popd
+    includeExternalBzlModDeps $extraPlugin
 done
 for extraModule in {extra-modules}
 do
@@ -48,6 +59,7 @@
     pushd plugins
     ln -s ../../$extraModule .
     popd
+    includeExternalBzlModDeps $extraModule
 done
 
 GH_PLUGIN_SCM_BASE_URL="https://review.gerrithub.io/a/{organization}"
@@ -59,6 +71,7 @@
     pushd plugins
     ln -s ../../$extraGhRepo .
     popd
+    includeExternalBzlModDeps $extraGhRepo
 done
 
 TARGETS=$(echo "{targets}" | sed -e 's/{{name}}/{name}/g')
diff --git a/jenkins/gerrit-bazel-build-plugin.sh b/jenkins/gerrit-bazel-build-plugin.sh
index be4ddd6..370c08c 100644
--- a/jenkins/gerrit-bazel-build-plugin.sh
+++ b/jenkins/gerrit-bazel-build-plugin.sh
@@ -21,7 +21,7 @@
   pushd plugins && ln -s {repo}/{sourcePath} . && popd
 fi
 
-for file in external_plugin_deps.bzl external_plugin_deps.MODULE.bazel external_package.json
+for file in external_plugin_deps.bzl external_package.json
 do
   if [ -f plugins/{name}/$file ]
   then
@@ -29,6 +29,16 @@
   fi
 done
 
+includeExternalBzlModDeps() {{
+  local pluginName=$1
+  if [ -f plugins/$pluginName/external_plugin_deps.MODULE.bazel ]
+  then
+    echo 'include("//plugins/'$pluginName':external_plugin_deps.MODULE.bazel")' >> plugins/external_plugin_deps.MODULE.bazel
+  fi
+}}
+
+includeExternalBzlModDeps {name}
+
 PLUGIN_SCM_BASE_URL="https://gerrit.googlesource.com/a"
 for extraPlugin in {extra-plugins}
 do
@@ -38,6 +48,7 @@
     pushd plugins
     ln -s ../../$extraPlugin .
     popd
+    includeExternalBzlModDeps $extraPlugin
 done
 for extraModule in {extra-modules}
 do
@@ -47,6 +58,7 @@
     pushd plugins
     ln -s ../../$extraModule .
     popd
+    includeExternalBzlModDeps $extraModule
 done
 
 GH_PLUGIN_SCM_BASE_URL="https://review.gerrithub.io/a/{organization}"
@@ -58,6 +70,7 @@
     pushd plugins
     ln -s ../../$extraGhRepo .
     popd
+    includeExternalBzlModDeps $extraGhRepo
 done
 
 TARGETS=$(echo "{targets}" | sed -e 's/{{name}}/{name}/g')