Added a function to list all repositories.

This commit is contained in:
Andrew Lalis 2018-09-24 12:14:33 +02:00
parent 83fd710e5c
commit 4a4628e6e6
3 changed files with 71 additions and 0 deletions

View File

@ -41,6 +41,7 @@ public class Main {
executor.registerCommand("delete_repos", new DeleteRepos()); executor.registerCommand("delete_repos", new DeleteRepos());
executor.registerCommand("delegate_student_teams", new DelegateStudentTeams(app)); executor.registerCommand("delegate_student_teams", new DelegateStudentTeams(app));
executor.registerCommand("setup_student_repos", new SetupStudentRepos(app)); executor.registerCommand("setup_student_repos", new SetupStudentRepos(app));
executor.registerCommand("list_repos", new ListRepos());
logger.info("GithubManager for Github Repositories in Educational Organizations.\n" + logger.info("GithubManager for Github Repositories in Educational Organizations.\n" +
"© Andrew Lalis (2018), All rights reserved.\n" + "© Andrew Lalis (2018), All rights reserved.\n" +

View File

@ -51,6 +51,28 @@ public class GithubManager {
} }
} }
/**
* Returns a list of repositories with the given substring.
* @param substring A string which all repositories should contain.
* @return A List of repositories whose names contain the given substring.
*/
public List<GHRepository> listReposWithPrefix(String substring) {
List<GHRepository> repos = new ArrayList<>();
try {
List<GHRepository> allRepos = this.organization.listRepositories().asList();
for (GHRepository repo : allRepos) {
if (repo.getName().contains(substring)) {
repos.add(repo);
}
}
} catch (Exception e) {
logger.severe("IOException while listing repositories in the organization.");
e.printStackTrace();
}
return repos;
}
/** /**
* Gets a repository by name. * Gets a repository by name.
* @param name The name of the repository. * @param name The name of the repository.

View File

@ -0,0 +1,48 @@
package nl.andrewlalis.ui.control.command.executables;
import nl.andrewlalis.git_api.GithubManager;
import org.kohsuke.github.GHRepository;
import java.util.List;
import java.util.logging.Logger;
/**
* This executable shows a list of repositories with a given substring.
*/
public class ListRepos extends GithubExecutable {
/**
* The logger for outputting debug info.
*/
private static final Logger logger = Logger.getLogger(ListRepos.class.getName());
static {
logger.setParent(Logger.getGlobal());
}
@Override
protected boolean executeWithManager(GithubManager manager, String[] args) {
if (args.length < 1) {
return false;
}
List<GHRepository> repos = manager.listReposWithPrefix(args[0]);
logger.info(outputRepoList(repos));
return true;
}
/**
* Prints a nicely formatted list of repositories to a string.
* @param repos The list of repositories.
* @return A string representation of the list of repos.
*/
private static String outputRepoList(List<GHRepository> repos) {
StringBuilder sb = new StringBuilder();
sb.append("List of ").append(repos.size()).append(" repositories:\n");
for (GHRepository repo : repos) {
sb.append('\t').append(repo.getName()).append('\n');
}
return sb.toString();
}
}