Add command remove-from-queue

The command allows admins to remove a repo from the queue in case that
is not picked up by the executor. Administrators also have an option
--force to remove repo in case of executor crash.

Change-Id: I43f96776ce94a5c84132c8787222b63982101d3c
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/SshModule.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/SshModule.java
index 9435cfc..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
@@ -25,5 +25,6 @@
     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 83f6e26..99e9225 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -32,7 +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 
+* _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)