From 8bbfab3f37bb1ec42bd28fe30de697b0b5f3530a Mon Sep 17 00:00:00 2001 From: Ignas Mikalajunas Date: Sat, 10 Aug 2024 02:50:20 +0300 Subject: [PATCH] Added list_repositories subcommand --- .../lt/pow/nukagit/cli/ListRepositories.java | 20 +++++++++++++++++++ .../main/java/lt/pow/nukagit/cli/Main.java | 1 + .../lt/pow/nukagit/db/DatabaseModule.java | 2 ++ .../lt/pow/nukagit/db/dao/NukagitDfsDao.java | 4 ++++ .../pow/nukagit/db/entities/Repository.java | 8 ++++++++ .../nukagit/dfs/DfsRepositoryResolver.java | 5 ++++- .../nukagit/db/dao/NukagitDfsDaoTest.groovy | 15 ++++++++++++++ 7 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 cli/src/main/java/lt/pow/nukagit/cli/ListRepositories.java create mode 100644 src/main/java/lt/pow/nukagit/db/entities/Repository.java diff --git a/cli/src/main/java/lt/pow/nukagit/cli/ListRepositories.java b/cli/src/main/java/lt/pow/nukagit/cli/ListRepositories.java new file mode 100644 index 0000000..aa1a8a2 --- /dev/null +++ b/cli/src/main/java/lt/pow/nukagit/cli/ListRepositories.java @@ -0,0 +1,20 @@ +package lt.pow.nukagit.cli; + +import lt.pow.nukagit.proto.Repositories; +import picocli.CommandLine; + +@CommandLine.Command(name = "list_repositories", description = "List repositories") +public class ListRepositories implements Runnable { + @CommandLine.ParentCommand + private Main parent; + @Override + public void run() { + System.out.println("Repositories:"); + var repositories = parent.repositoriesGrpcClient() + .listRepositories(Repositories.ListRepositoriesRequest.newBuilder().build()) + .getRepositoriesList(); + for (var repository : repositories) { + System.out.println(repository.getName()); + } + } +} diff --git a/cli/src/main/java/lt/pow/nukagit/cli/Main.java b/cli/src/main/java/lt/pow/nukagit/cli/Main.java index 7731d16..8bcd33e 100644 --- a/cli/src/main/java/lt/pow/nukagit/cli/Main.java +++ b/cli/src/main/java/lt/pow/nukagit/cli/Main.java @@ -10,6 +10,7 @@ subcommands = { AddUser.class, CreateRepository.class, + ListRepositories.class, }) public class Main implements Runnable { @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Display this help message") diff --git a/src/main/java/lt/pow/nukagit/db/DatabaseModule.java b/src/main/java/lt/pow/nukagit/db/DatabaseModule.java index 4381de0..86fa9db 100644 --- a/src/main/java/lt/pow/nukagit/db/DatabaseModule.java +++ b/src/main/java/lt/pow/nukagit/db/DatabaseModule.java @@ -11,6 +11,7 @@ import lt.pow.nukagit.db.dao.UsersDao; import lt.pow.nukagit.db.entities.Pack; import lt.pow.nukagit.db.entities.PublicKeyData; +import lt.pow.nukagit.db.entities.Repository; import lt.pow.nukagit.db.entities.UserPublicKey; import org.github.gestalt.config.Gestalt; import org.github.gestalt.config.exceptions.GestaltException; @@ -48,6 +49,7 @@ static Jdbi jdbi(DataSource dataSource) { jdbi.registerColumnMapper(new BigIntegerColumnMapper()); JdbiImmutables jdbiImmutables = jdbi.getConfig(JdbiImmutables.class); jdbiImmutables.registerImmutable(Pack.class); + jdbiImmutables.registerImmutable(Repository.class); jdbiImmutables.registerImmutable(UserPublicKey.class); jdbiImmutables.registerModifiable(UserPublicKey.class); jdbiImmutables.registerImmutable(PublicKeyData.class); diff --git a/src/main/java/lt/pow/nukagit/db/dao/NukagitDfsDao.java b/src/main/java/lt/pow/nukagit/db/dao/NukagitDfsDao.java index 91d7491..8302146 100644 --- a/src/main/java/lt/pow/nukagit/db/dao/NukagitDfsDao.java +++ b/src/main/java/lt/pow/nukagit/db/dao/NukagitDfsDao.java @@ -1,6 +1,7 @@ package lt.pow.nukagit.db.dao; import lt.pow.nukagit.db.entities.Pack; +import lt.pow.nukagit.db.entities.Repository; import org.jdbi.v3.core.statement.UnableToExecuteStatementException; import org.jdbi.v3.sqlobject.customizer.Bind; import org.jdbi.v3.sqlobject.customizer.BindMethods; @@ -19,6 +20,9 @@ public interface NukagitDfsDao { + "ON DUPLICATE KEY UPDATE name = name") void upsertRepository(@Bind("name") String name); + @SqlQuery("SELECT * FROM repositories WHERE not_archived = true ORDER BY name") + List listRepositories(); + @SqlQuery("SELECT id FROM repositories WHERE name = :name AND not_archived = true") UUID getRepositoryIdByName(@Bind("name") String name); diff --git a/src/main/java/lt/pow/nukagit/db/entities/Repository.java b/src/main/java/lt/pow/nukagit/db/entities/Repository.java new file mode 100644 index 0000000..e666427 --- /dev/null +++ b/src/main/java/lt/pow/nukagit/db/entities/Repository.java @@ -0,0 +1,8 @@ +package lt.pow.nukagit.db.entities; + +import org.immutables.value.Value; + +@Value.Immutable +public interface Repository { + String name(); +} diff --git a/src/main/java/lt/pow/nukagit/dfs/DfsRepositoryResolver.java b/src/main/java/lt/pow/nukagit/dfs/DfsRepositoryResolver.java index 358d8d7..d5e3875 100644 --- a/src/main/java/lt/pow/nukagit/dfs/DfsRepositoryResolver.java +++ b/src/main/java/lt/pow/nukagit/dfs/DfsRepositoryResolver.java @@ -13,6 +13,7 @@ import javax.inject.Inject; import javax.inject.Singleton; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -56,6 +57,8 @@ public synchronized Repository resolveDfsRepository(String username, String[] ar } public synchronized List listRepositories() { - return List.copyOf(repositoryCache.keySet()); + ArrayList repositories = new ArrayList<>(repositoryCache.keySet()); + dfsDao.listRepositories().forEach(repository -> repositories.add(repository.name())); + return repositories; } } diff --git a/src/test/groovy/lt/pow/nukagit/db/dao/NukagitDfsDaoTest.groovy b/src/test/groovy/lt/pow/nukagit/db/dao/NukagitDfsDaoTest.groovy index 6243ae9..986af9b 100644 --- a/src/test/groovy/lt/pow/nukagit/db/dao/NukagitDfsDaoTest.groovy +++ b/src/test/groovy/lt/pow/nukagit/db/dao/NukagitDfsDaoTest.groovy @@ -46,6 +46,21 @@ class NukagitDfsDaoTest extends DatabaseTestBase { repoId1 != repoId2 } + def "we can list non archived repositories"() { + given: + def repoName1 = random.nextObject(String.class) + def repoName2 = random.nextObject(String.class) + dao.upsertRepository(repoName1) + dao.upsertRepository(repoName2) + def repoId = dao.upsertRepositoryAndGetId(random.nextObject(String.class)) + dao.archiveRepository(repoId) + when: + def repositoryNames = dao.listRepositories() .stream().map(it -> it.name()).toList() + then: + repositoryNames.size() == 2 + repositoryNames == [repoName1, repoName2].sort { it } + } + def "we can commit an empty set of packs"() { given: def repoId = dao.upsertRepositoryAndGetId(random.nextObject(String.class))