Introduce gerritJGitServlet4Pipeline library

The Bazel-based build of JGit has historically been unmonitored, leading
to unnoticed breakages due to lack of CI validation. Currently, the
Eclipse CI pipeline only verifies Maven builds, which does not cover all
scenarios.

Introduce a new `gerritJGitServlet4Pipeline` and a job to leverage it
ensure that JGit servlet-4 branch builds with Gerrit so that we can spot
JGit servlet-4 changes that are incompatible with Gerrit before they get
merged.

Change-Id: I5f508fc2e15ab98c7cadd5e88fee14d459d90c42
diff --git a/vars/gerritJGitServlet4Pipeline.groovy b/vars/gerritJGitServlet4Pipeline.groovy
new file mode 100644
index 0000000..b2f5165
--- /dev/null
+++ b/vars/gerritJGitServlet4Pipeline.groovy
@@ -0,0 +1,178 @@
+#!/usr/bin/env groovy
+
+// Copyright (C) 2025 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * Declarative pipeline for building and testing JGit servlet-4 branch against Gerrit stable-3.12 and Gerrit master.
+ *  Parameters: None
+ * Usage in a Jenkinsfile:
+ *   gerritJGitServlet4Pipeline()
+ */
+
+def call(Map cfg = [:]) {
+
+  echo "Starting pipeline for JGit servlet-4"
+  echo "Change : ${env.GERRIT_CHANGE_NUMBER}/${GERRIT_PATCHSET_NUMBER} '${env.GERRIT_CHANGE_SUBJECT}'"
+  echo "Change URL: ${env.GERRIT_CHANGE_URL}"
+
+  pipeline {
+    agent { label 'bazel-debian' }
+    options {
+      skipDefaultCheckout true
+    }
+    stages {
+      stage('Checkout JGit') {
+        steps {
+          dir('jgit') {
+            checkout scm
+          }
+        }
+      }
+      stage('Build JGit') {
+        steps {
+          dir('jgit') {
+            sh '''
+              . set-java.sh 21
+              java -version
+              bazelisk build all
+            '''
+          }
+        }
+      }
+
+      stage('Test JGit') {
+        steps {
+          dir('jgit') {
+            sh '''
+              echo "Running JGit tests..."
+              . set-java.sh 21
+              bazelisk test //...
+            '''
+          }
+        }
+      }
+
+      stage('Checkout Gerrit stable-3.12') {
+        steps {
+          sh '''
+            git clone -b stable-3.12 --recursive https://gerrit.googlesource.com/gerrit
+          '''
+        }
+      }
+
+      stage('Build Gerrit stable-3.12') {
+        steps {
+          script {
+            def jgitSourceDir = "${env.WORKSPACE}/jgit"
+            def jgitTargetDir = "${env.WORKSPACE}/gerrit/modules/jgit"
+            echo "Replacing gerrit jgit module with jgit change ${env.GERRIT_CHANGE_NUMBER}..."
+
+            sh """
+                echo "${jgitSourceDir} -> ${jgitTargetDir}"
+                mv ${jgitTargetDir} /tmp/jgit-3.12
+                ln -sfn ${jgitSourceDir} ${jgitTargetDir}
+            """
+
+            dir('gerrit') {
+              sh '''
+                . set-java.sh 21
+                  bazelisk build release
+              '''
+            }
+          }
+        }
+      }
+      stage('Test Gerrit stable-3.12') {
+        steps {
+          dir('gerrit') {
+            sh '''
+              . set-java.sh 21
+              echo "running gerrit stable-3.12 tests..."
+              export BAZEL_OPTS="--config=remote_bb \
+                   --jobs=50 \
+                   --remote_header=x-buildbuddy-api-key=$BB_API_KEY \
+                   --flaky_test_attempts 3 \
+                   --test_timeout 3600 \
+                   --test_tag_filters=-flaky"
+              bazelisk test $BAZEL_OPTS //...
+            '''
+          }
+        }
+      }
+      stage('Checkout Gerrit master') {
+        steps {
+          dir('gerrit') {
+            sh '''
+              git reset --hard origin/master
+              git checkout master
+              git submodule update --init --recursive
+            '''
+          }
+        }
+      }
+      stage('Build Gerrit master') {
+        steps {
+          script {
+            def jgitSourceDir = "${env.WORKSPACE}/jgit"
+            def jgitTargetDir = "${env.WORKSPACE}/gerrit/modules/jgit"
+            echo "Replacing gerrit jgit module with jgit change ${env.GERRIT_CHANGE_NUMBER}..."
+
+            sh """
+                echo "${jgitSourceDir} -> ${jgitTargetDir}"
+                mv ${jgitTargetDir} /tmp/jgit-master
+                ln -sfn ${jgitSourceDir} ${jgitTargetDir}
+            """
+
+            dir('gerrit') {
+              sh """
+                . set-java.sh 21
+                bazelisk build release
+              """
+            }
+          }
+        }
+      }
+      stage('Test Gerrit master') {
+        steps {
+          dir('gerrit') {
+            sh '''
+              . set-java.sh 21
+              echo "running gerrit master tests..."
+              export BAZEL_OPTS="--config=remote_bb \
+                   --jobs=50 \
+                   --remote_header=x-buildbuddy-api-key=$BB_API_KEY \
+                   --flaky_test_attempts 3 \
+                   --test_timeout 3600 \
+                   --test_tag_filters=-flaky"
+              bazelisk test $BAZEL_OPTS //...
+            '''
+          }
+        }
+      }
+    }
+
+    post {
+      success {
+        gerritReview labels: ["Code-Review": 1], message: "Gerrit successfully builds with this JGit change.\nBuild: ${env.BUILD_URL}"
+      }
+      unstable {
+        gerritReview labels: ["Code-Review": -1], message: "Gerrit build with this JGit change is unstable.\nBuild: ${env.BUILD_URL}"
+      }
+      failure {
+        gerritReview labels: ["Code-Review": -1], message: "Gerrit does not build successfully with this JGit change.\nBuild: ${env.BUILD_URL}"
+      }
+    }
+  }
+}