Skip to content

Commit

Permalink
Added list_repositories subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignas committed Aug 9, 2024
1 parent 9f6f13c commit 8bbfab3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cli/src/main/java/lt/pow/nukagit/cli/ListRepositories.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
1 change: 1 addition & 0 deletions cli/src/main/java/lt/pow/nukagit/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/lt/pow/nukagit/db/DatabaseModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/lt/pow/nukagit/db/dao/NukagitDfsDao.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Repository> listRepositories();

@SqlQuery("SELECT id FROM repositories WHERE name = :name AND not_archived = true")
UUID getRepositoryIdByName(@Bind("name") String name);

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/lt/pow/nukagit/db/entities/Repository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package lt.pow.nukagit.db.entities;

import org.immutables.value.Value;

@Value.Immutable
public interface Repository {
String name();
}
5 changes: 4 additions & 1 deletion src/main/java/lt/pow/nukagit/dfs/DfsRepositoryResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,6 +57,8 @@ public synchronized Repository resolveDfsRepository(String username, String[] ar
}

public synchronized List<String> listRepositories() {
return List.copyOf(repositoryCache.keySet());
ArrayList<String> repositories = new ArrayList<>(repositoryCache.keySet());
dfsDao.listRepositories().forEach(repository -> repositories.add(repository.name()));
return repositories;
}
}
15 changes: 15 additions & 0 deletions src/test/groovy/lt/pow/nukagit/db/dao/NukagitDfsDaoTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 8bbfab3

Please sign in to comment.