Extract original repo from DelegateRepository

To access the original repository in classes extending the
DelegateRepository class, the delegate() method needs to be invoked.

However, this method is package protected in Gerrit core.

As a workaround we added a DelegateRepositoryUnwrapper class in the
same namespace of DelegateRepository to get hold of the original repository.

Adding the DelegateRepositoryUnwrapper jar in the Gerrit lib allows
the new class to be picked up by the class loader.

Bug: Issue 15997

Change-Id: Ia3fbdc7e9629072c0019c44fbdfc201aa5b317b9
diff --git a/BUILD b/BUILD
index 1052170..613eda5 100644
--- a/BUILD
+++ b/BUILD
@@ -7,10 +7,13 @@
     "gerrit_plugin",
 )
 
+DELEGATE_REPOSITORY_UNWRAPPER_SRCS = ["src/main/java/com/google/gerrit/server/git/DelegateRepositoryUnwrapper.java"]
+
 gerrit_plugin(
     name = "git-repo-metrics",
     srcs = glob(
         ["src/main/java/**/*.java"],
+        exclude = DELEGATE_REPOSITORY_UNWRAPPER_SRCS,
     ),
     manifest_entries = [
         "Gerrit-PluginName: git-repo-metrics",
@@ -21,7 +24,23 @@
     ],
     resources = glob(
         ["src/main/resources/**/*"],
-    )
+    ),
+    deps = [
+        ":delegaterepositoryunwrapper-neverlink",
+    ],
+)
+
+java_library(
+    name = "delegaterepositoryunwrapper",
+    srcs = DELEGATE_REPOSITORY_UNWRAPPER_SRCS,
+    deps = PLUGIN_DEPS,
+)
+
+java_library(
+    name = "delegaterepositoryunwrapper-neverlink",
+    srcs = DELEGATE_REPOSITORY_UNWRAPPER_SRCS,
+    neverlink = True,
+    deps = PLUGIN_DEPS,
 )
 
 junit_tests(
@@ -48,5 +67,5 @@
 java_library(
     name = "git-repo-metrics__plugin_deps",
     visibility = ["//visibility:public"],
-    exports = PLUGIN_DEPS
+    exports = PLUGIN_DEPS,
 )
diff --git a/README.md b/README.md
index 780f1c0..ae86d4a 100644
--- a/README.md
+++ b/README.md
@@ -14,19 +14,20 @@
 git clone --recursive https://gerrit.googlesource.com/gerrit
 git clone https://gerrit.googlesource.com/plugins/git-repo-metrics
 pushd gerrit/plugins && ln -s ../../git-repo-metrics . && popd
-cd gerrit && bazel build plugins/git-repo-metrics
+cd gerrit && bazel build //plugins/git-repo-metrics:all
 ```
 
-The output plugin jar is created in:
+Two jar will be created:
 
 ```
-bazel-genfiles/plugins/git-repo-metrics/git-repo-metrics.jar
+bazel-bin/plugins/git-repo-metrics/git-repo-metrics.jar
+bazel-bin/plugins/git-repo-metrics/libdelegaterepositoryunwrapper.jar
 ```
 
 ## How to install
 
-Copy the git-repo-metrics.jar into the Gerrit's /plugins directory and wait for the plugin to be automatically
-loaded.
+Copy the `git-repo-metrics.jar` into the Gerrit's `<gerrit_site>/plugins` directory and the `libdelegaterepositoryunwrapper.jar`
+into the `<gerrit_site>/lib` directory. Restart your Gerrit instance and the plugin will be loaded.
 
 ## Configuration
 
diff --git a/src/main/java/com/google/gerrit/server/git/DelegateRepositoryUnwrapper.java b/src/main/java/com/google/gerrit/server/git/DelegateRepositoryUnwrapper.java
new file mode 100644
index 0000000..ad07087
--- /dev/null
+++ b/src/main/java/com/google/gerrit/server/git/DelegateRepositoryUnwrapper.java
@@ -0,0 +1,25 @@
+// Copyright (C) 2022 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.
+
+package com.google.gerrit.server.git;
+
+import org.eclipse.jgit.lib.Repository;
+
+public class DelegateRepositoryUnwrapper {
+  public static Repository unwrap(DelegateRepository delegateRepository) {
+    return delegateRepository.delegate();
+  }
+
+  public DelegateRepositoryUnwrapper() {}
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTask.java b/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTask.java
index ed120e5..51fb927 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTask.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTask.java
@@ -16,6 +16,8 @@
 
 import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.entities.Project;
+import com.google.gerrit.server.git.DelegateRepository;
+import com.google.gerrit.server.git.DelegateRepositoryUnwrapper;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
@@ -57,7 +59,13 @@
           repository.getIdentifier(), projectName);
       // TODO Loop through all the collectors
       Project project = Project.builder(projectNameKey).build();
-      GitStats gitStats = new GitStats((FileRepository) repository, project);
+
+      Repository unwrappedRepo =
+          repository instanceof DelegateRepository
+              ? DelegateRepositoryUnwrapper.unwrap((DelegateRepository) repository)
+              : repository;
+
+      GitStats gitStats = new GitStats((FileRepository) unwrappedRepo, project);
       Map<String, Long> newMetrics = gitStats.get();
       logger.atInfo().log(
           "Here all the metrics for %s - %s", project.getName(), getStringFromMap(newMetrics));