From 4a4628e6e687bd7fa8d89ea65388de7075c2b331 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Mon, 24 Sep 2018 12:14:33 +0200 Subject: [PATCH] Added a function to list all repositories. --- src/main/java/nl/andrewlalis/Main.java | 1 + .../nl/andrewlalis/git_api/GithubManager.java | 22 +++++++++ .../command/executables/ListRepos.java | 48 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/main/java/nl/andrewlalis/ui/control/command/executables/ListRepos.java diff --git a/src/main/java/nl/andrewlalis/Main.java b/src/main/java/nl/andrewlalis/Main.java index ed72ebc..f0be3c2 100644 --- a/src/main/java/nl/andrewlalis/Main.java +++ b/src/main/java/nl/andrewlalis/Main.java @@ -41,6 +41,7 @@ public class Main { executor.registerCommand("delete_repos", new DeleteRepos()); executor.registerCommand("delegate_student_teams", new DelegateStudentTeams(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" + "© Andrew Lalis (2018), All rights reserved.\n" + diff --git a/src/main/java/nl/andrewlalis/git_api/GithubManager.java b/src/main/java/nl/andrewlalis/git_api/GithubManager.java index 4832a3a..4f8b148 100644 --- a/src/main/java/nl/andrewlalis/git_api/GithubManager.java +++ b/src/main/java/nl/andrewlalis/git_api/GithubManager.java @@ -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 listReposWithPrefix(String substring) { + List repos = new ArrayList<>(); + try { + List 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. * @param name The name of the repository. diff --git a/src/main/java/nl/andrewlalis/ui/control/command/executables/ListRepos.java b/src/main/java/nl/andrewlalis/ui/control/command/executables/ListRepos.java new file mode 100644 index 0000000..34e5874 --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/command/executables/ListRepos.java @@ -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 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 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(); + } +}