Merge "Trust user in Gerrit docker setup"
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java
index eadf28c..63a4551 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/AddToQueue.java
@@ -20,10 +20,8 @@
 import com.ericsson.gerrit.plugins.gcconductor.GcQueueException;
 import com.ericsson.gerrit.plugins.gcconductor.Hostname;
 import com.google.gerrit.common.data.GlobalCapability;
-import com.google.gerrit.entities.Project;
 import com.google.gerrit.extensions.annotations.RequiresCapability;
 import com.google.gerrit.server.git.GitRepositoryManager;
-import com.google.gerrit.server.git.LocalDiskRepositoryManager;
 import com.google.gerrit.server.project.ProjectCache;
 import com.google.gerrit.sshd.AdminHighPriorityCommand;
 import com.google.gerrit.sshd.CommandMetaData;
@@ -32,7 +30,6 @@
 import java.io.IOException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
 import org.eclipse.jgit.util.FS;
 import org.kohsuke.args4j.Argument;
@@ -70,7 +67,7 @@
         repositoryPath = repositoryPath.toRealPath();
       }
       if (!FileKey.isGitRepository(repositoryPath.toFile(), FS.DETECTED)) {
-        repositoryPath = resolvePath();
+        repositoryPath = GcUtils.resolvePath(gitRepositoryManager, projectCache, repository);
       }
       repository = repositoryPath.toString();
       queue.add(repository, hostName, aggressive);
@@ -82,39 +79,4 @@
       throw die(e);
     }
   }
-
-  private Path resolvePath() throws UnloggedFailure {
-    if (!(gitRepositoryManager instanceof LocalDiskRepositoryManager)) {
-      throw die("Unable to resolve path to " + repository);
-    }
-    String projectName = extractFrom(repository);
-    Project.NameKey nameKey = Project.nameKey(projectName);
-    if (projectCache.get(nameKey) == null) {
-      throw die(String.format("Repository %s not found", repository));
-    }
-    LocalDiskRepositoryManager localDiskRepositoryManager =
-        (LocalDiskRepositoryManager) gitRepositoryManager;
-    try {
-      return localDiskRepositoryManager
-          .getBasePath(nameKey)
-          .resolve(projectName.concat(Constants.DOT_GIT_EXT))
-          .toRealPath();
-    } catch (IOException e) {
-      throw die(e);
-    }
-  }
-
-  private String extractFrom(String path) {
-    String name = path;
-    if (name.startsWith("/")) {
-      name = name.substring(1);
-    }
-    if (name.endsWith("/")) {
-      name = name.substring(0, name.length() - 1);
-    }
-    if (name.endsWith(Constants.DOT_GIT_EXT)) {
-      name = name.substring(0, name.indexOf(Constants.DOT_GIT_EXT));
-    }
-    return name;
-  }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/GcUtils.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/GcUtils.java
new file mode 100644
index 0000000..b6623a4
--- /dev/null
+++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/GcUtils.java
@@ -0,0 +1,67 @@
+// 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.ericsson.gerrit.plugins.gcconductor.command;
+
+import static com.google.gerrit.pgm.init.api.InitUtil.die;
+
+import com.google.gerrit.entities.Project;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.gerrit.server.git.LocalDiskRepositoryManager;
+import com.google.gerrit.server.project.ProjectCache;
+import com.google.gerrit.sshd.BaseCommand.UnloggedFailure;
+import java.io.IOException;
+import java.nio.file.Path;
+import org.eclipse.jgit.lib.Constants;
+
+public class GcUtils {
+  private GcUtils() {}
+
+  static Path resolvePath(
+      GitRepositoryManager gitRepositoryManager, ProjectCache projectCache, String repository)
+      throws UnloggedFailure {
+    if (!(gitRepositoryManager instanceof LocalDiskRepositoryManager)) {
+      throw die("Unable to resolve path to " + repository);
+    }
+    String projectName = extractFrom(repository);
+    Project.NameKey nameKey = Project.nameKey(projectName);
+    if (projectCache.get(nameKey) == null) {
+      throw die(String.format("Repository %s not found", repository));
+    }
+    LocalDiskRepositoryManager localDiskRepositoryManager =
+        (LocalDiskRepositoryManager) gitRepositoryManager;
+    try {
+      return localDiskRepositoryManager
+          .getBasePath(nameKey)
+          .resolve(projectName.concat(Constants.DOT_GIT_EXT))
+          .toRealPath();
+    } catch (IOException e) {
+      throw die(e.toString());
+    }
+  }
+
+  static String extractFrom(String path) {
+    String name = path;
+    if (name.startsWith("/")) {
+      name = name.substring(1);
+    }
+    if (name.endsWith("/")) {
+      name = name.substring(0, name.length() - 1);
+    }
+    if (name.endsWith(Constants.DOT_GIT_EXT)) {
+      name = name.substring(0, name.indexOf(Constants.DOT_GIT_EXT));
+    }
+    return name;
+  }
+}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RemoveFromQueue.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RemoveFromQueue.java
new file mode 100644
index 0000000..fd4589c
--- /dev/null
+++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RemoveFromQueue.java
@@ -0,0 +1,75 @@
+// 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.ericsson.gerrit.plugins.gcconductor.command;
+
+import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
+
+import com.ericsson.gerrit.plugins.gcconductor.GcQueue;
+import com.ericsson.gerrit.plugins.gcconductor.GcQueueException;
+import com.ericsson.gerrit.plugins.gcconductor.RepositoryInfo;
+import com.google.gerrit.common.data.GlobalCapability;
+import com.google.gerrit.extensions.annotations.RequiresCapability;
+import com.google.gerrit.sshd.AdminHighPriorityCommand;
+import com.google.gerrit.sshd.CommandMetaData;
+import com.google.gerrit.sshd.SshCommand;
+import com.google.inject.Inject;
+import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.Option;
+
+@AdminHighPriorityCommand
+@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
+@CommandMetaData(
+    name = "remove-from-queue",
+    description = "remove repo from the queue",
+    runsAt = MASTER_OR_SLAVE)
+final class RemoveFromQueue extends SshCommand {
+  @Argument(index = 0, required = true, metaVar = "REPOSITORY")
+  private String repository;
+
+  @Option(name = "--force", usage = "remove repository if repository is picked up by GC-Executor")
+  private boolean force;
+
+  @Inject private GcQueue queue;
+
+  @Override
+  protected void run() throws UnloggedFailure {
+    try {
+      if (!queue.contains(repository)) {
+        throw die(String.format("%s is not in the queue", repository));
+      }
+      if (!force && isPickUp()) {
+        throw die(String.format("%s repository already picked up by GC-executor", repository));
+      }
+      queue.remove(repository);
+      stdout.println(
+          String.format(
+              "%s was removed from GC queue, warning: repository can be rescheduled again by evaluation task",
+              repository));
+    } catch (GcQueueException e) {
+      throw die(e);
+    }
+  }
+
+  private boolean isPickUp() throws GcQueueException {
+    for (RepositoryInfo repositoryInfo : queue.list()) {
+      if (repository.equals(repositoryInfo.getPath())
+          && null != repositoryInfo.getExecutor()
+          && !repositoryInfo.getExecutor().isEmpty()) {
+        return true;
+      }
+    }
+    return false;
+  }
+}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java
new file mode 100644
index 0000000..c67c643
--- /dev/null
+++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java
@@ -0,0 +1,79 @@
+// 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.ericsson.gerrit.plugins.gcconductor.command;
+
+import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
+
+import com.google.gerrit.common.data.GlobalCapability;
+import com.google.gerrit.extensions.annotations.RequiresCapability;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.gerrit.server.project.ProjectCache;
+import com.google.gerrit.sshd.AdminHighPriorityCommand;
+import com.google.gerrit.sshd.CommandMetaData;
+import com.google.gerrit.sshd.SshCommand;
+import com.google.inject.Inject;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.eclipse.jgit.internal.storage.file.FileRepository;
+import org.eclipse.jgit.internal.storage.file.GC;
+import org.eclipse.jgit.internal.storage.file.GC.RepoStatistics;
+import org.eclipse.jgit.lib.RepositoryCache;
+import org.eclipse.jgit.lib.RepositoryCache.FileKey;
+import org.eclipse.jgit.util.FS;
+import org.kohsuke.args4j.Argument;
+
+@AdminHighPriorityCommand
+@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
+@CommandMetaData(
+    name = "repo-stats",
+    description = "display repo dirtiness statistics",
+    runsAt = MASTER_OR_SLAVE)
+final class RepoStats extends SshCommand {
+  @Argument(index = 0, required = true, metaVar = "REPOSITORY")
+  private String repository;
+
+  @Inject private GitRepositoryManager gitRepositoryManager;
+
+  @Inject private ProjectCache projectCache;
+
+  @Override
+  protected void run() throws UnloggedFailure {
+    try {
+      Path repositoryPath = Paths.get(repository);
+      if (repositoryPath.toFile().exists()) {
+        repositoryPath = repositoryPath.toRealPath();
+      }
+      if (!FileKey.isGitRepository(repositoryPath.toFile(), FS.DETECTED)) {
+        repositoryPath = GcUtils.resolvePath(gitRepositoryManager, projectCache, repository);
+      }
+
+      stdout.println(getRepoStatistics(repositoryPath.toString()));
+    } catch (IOException e) {
+      throw die(e);
+    }
+  }
+
+  private RepoStatistics getRepoStatistics(String repositoryPath) throws UnloggedFailure {
+    try (FileRepository repository =
+        (FileRepository)
+            RepositoryCache.open(FileKey.exact(new File(repositoryPath), FS.DETECTED))) {
+      return new GC(repository).getStatistics();
+    } catch (IOException e) {
+      throw die(e);
+    }
+  }
+}
diff --git a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java
index 7940bba..ad43f08 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java
@@ -24,5 +24,7 @@
     command(ShowQueue.class);
     command(AddToQueue.class);
     command(BumpToFirst.class);
+    command(RepoStats.class);
+    command(RemoveFromQueue.class);
   }
 }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 870a2ad..99e9225 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -32,6 +32,8 @@
   helpful to be able to change the _set_queued_from_ field, so that the running
   one can pick up repositories that were not initially added to the queue by its
   corresponding Gerrit instance.
+* _repo-stats_ Display a repository dirtiness statistics
+* _remove-from-queue_ Remove repository form GC queue.
 
 [build]: build.html
 [config]: config.html
diff --git a/src/main/resources/Documentation/cmd-remove-from-queue.md b/src/main/resources/Documentation/cmd-remove-from-queue.md
new file mode 100644
index 0000000..688a5f7
--- /dev/null
+++ b/src/main/resources/Documentation/cmd-remove-from-queue.md
@@ -0,0 +1,30 @@
+remove-from-queue
+=====================
+
+NAME
+----
+remove-from-queue - Remove repository form GC queue.
+
+SYNOPSIS
+--------
+>     ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ remove-from-queue <REPOSITORY> [--force]
+
+DESCRIPTION
+-----------
+Remove repository form GC queue. Can be used to clean up the queue in case of gc-executor failure.
+With force option it allowing to remove repository when it is already pickup by gc-executor.
+This doesn't stop executor gc-process. Force option should be use when gc-executor cannot/should not
+continue with gc-execution.
+
+ACCESS
+------
+Any user who has configured an SSH key and has been granted the
+`Administrate Server` global capability.
+
+SCRIPTING
+---------
+This command is intended to be used in a script.
+
+GERRIT
+------
+Part of [Gerrit Code Review](../../../Documentation/index.html)
diff --git a/src/main/resources/Documentation/cmd-repo-stats.md b/src/main/resources/Documentation/cmd-repo-stats.md
new file mode 100644
index 0000000..25434ab
--- /dev/null
+++ b/src/main/resources/Documentation/cmd-repo-stats.md
@@ -0,0 +1,53 @@
+repo-stats
+=====================
+
+NAME
+----
+repo-stats - Display a repository dirtiness statistics
+
+SYNOPSIS
+--------
+>     ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats <REPOSITORY>
+
+DESCRIPTION
+-----------
+Display a repository dirtiness statistics.
+
+An absolute path to the repository (including the .git suffix) or the project
+name are accepted. A symlink pointing to a repository is also admitted.
+
+Displaying statistic can be usefully to determine repo condition. Can be a part of script with 
+adding a repository to the GC queue.
+
+ACCESS
+------
+Any user who has configured an SSH key and has been granted the
+`Administrate Server` global capability.
+
+SCRIPTING
+---------
+This command is intended to be used in a script.
+
+EXAMPLES
+--------
+Absolute path to a repository:
+
+```
+$ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats /repos/my/repo.git
+```
+
+Symlink pointing to a repository:
+
+```
+$ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats /opt/gerrit/repos/my/repo.git
+```
+
+Name of the project:
+
+```
+$ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ repo-stats my/repo
+```
+
+GERRIT
+------
+Part of [Gerrit Code Review](../../../Documentation/index.html)
diff --git a/src/test/docker/etc/gerrit.config b/src/test/docker/etc/gerrit.config
index 88fc81b..d07b58b 100644
--- a/src/test/docker/etc/gerrit.config
+++ b/src/test/docker/etc/gerrit.config
@@ -29,4 +29,3 @@
 	databaseName = gc
 	packed = 40
 	loose = 400
-	expireTimeRecheck = 5m
diff --git a/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CheckProjectStatisticsUpToGc.scala b/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CheckProjectStatisticsUpToGc.scala
index b4b73ec..57fa14b 100644
--- a/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CheckProjectStatisticsUpToGc.scala
+++ b/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CheckProjectStatisticsUpToGc.scala
@@ -37,7 +37,7 @@
   val test: ScenarioBuilder = scenario(uniqueName)
     .feed(data)
     .exec(http(uniqueName).get("${url}")
-      .check(regex("\"number_of_loose_objects\": (\\d+),")
+      .check(regex("\"number_of_loose_objects\":(\\d+),")
         .is("0")))
 
   setUp(
diff --git a/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CreateChangesTriggeringGcWithProject.scala b/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CreateChangesTriggeringGcWithProject.scala
index f0859ba..ead0026 100644
--- a/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CreateChangesTriggeringGcWithProject.scala
+++ b/src/test/scala/com/ericsson/gerrit/plugins/gcconductor/scenarios/CreateChangesTriggeringGcWithProject.scala
@@ -38,7 +38,9 @@
       constantUsersPerSec(createChanges.changesPerSecond) during (createChanges.secondsToChanges seconds),
       nothingFor(createChanges.secondsToNextEvaluation seconds),
       nothingFor(createChanges.secondsForLastEvaluation / 2 seconds),
-      atOnceUsers(createChanges.ChangesForLastEvaluation)
+      atOnceUsers(createChanges.ChangesForLastEvaluation),
+      nothingFor(createChanges.secondsToNextEvaluation seconds),
+      nothingFor(createChanges.secondsForLastEvaluation / 2 seconds)
     ),
     checkStatsUpToGc.test.inject(
       nothingFor(stepWaitTime(checkStatsUpToGc) seconds),