Refactoring adds SshUtil class Move common parts of few commands to the static utility class. Change-Id: Id003117db6462f88944438daf6c6ee2c34a13664
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 8144f18..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); - } - } - - 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/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/RepoStats.java b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java index e6573c9..c67c643 100644 --- a/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java +++ b/src/main/java/com/ericsson/gerrit/plugins/gcconductor/command/RepoStats.java
@@ -17,10 +17,8 @@ import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; 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; @@ -33,7 +31,6 @@ 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.Constants; import org.eclipse.jgit.lib.RepositoryCache; import org.eclipse.jgit.lib.RepositoryCache.FileKey; import org.eclipse.jgit.util.FS; @@ -61,7 +58,7 @@ repositoryPath = repositoryPath.toRealPath(); } if (!FileKey.isGitRepository(repositoryPath.toFile(), FS.DETECTED)) { - repositoryPath = resolvePath(); + repositoryPath = GcUtils.resolvePath(gitRepositoryManager, projectCache, repository); } stdout.println(getRepoStatistics(repositoryPath.toString())); @@ -70,27 +67,6 @@ } } - private Path resolvePath() throws UnloggedFailure { - if (!(gitRepositoryManager instanceof LocalDiskRepositoryManager)) { - throw die("Unable to resolve path to " + repository); - } - String projectName = AddToQueue.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 RepoStatistics getRepoStatistics(String repositoryPath) throws UnloggedFailure { try (FileRepository repository = (FileRepository)