diff --git a/src/main/java/nl/andrewlalis/Main.java b/src/main/java/nl/andrewlalis/Main.java index 869813e..cb1d021 100644 --- a/src/main/java/nl/andrewlalis/Main.java +++ b/src/main/java/nl/andrewlalis/Main.java @@ -42,6 +42,7 @@ public class Main { executor.registerCommand("generate_assignments", new GenerateAssignmentsRepo()); executor.registerCommand("define_ta_teams", new DefineTaTeams(app)); executor.registerCommand("list_errors", new ListErrors()); + executor.registerCommand("delete_repos", new DeleteRepos()); 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 48db804..aa4c23f 100644 --- a/src/main/java/nl/andrewlalis/git_api/GithubManager.java +++ b/src/main/java/nl/andrewlalis/git_api/GithubManager.java @@ -168,6 +168,7 @@ public class GithubManager { if (repo.getName().contains(substring)) { try { repo.delete(); + logger.info("Deleted repository: " + repo.getName()); } catch (IOException e) { InitializerApp.organization.addError(new Error(Severity.HIGH, "Could not delete repository: " + repo.getName())); e.printStackTrace(); diff --git a/src/main/java/nl/andrewlalis/ui/control/command/executables/DeleteRepos.java b/src/main/java/nl/andrewlalis/ui/control/command/executables/DeleteRepos.java new file mode 100644 index 0000000..d38b615 --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/command/executables/DeleteRepos.java @@ -0,0 +1,17 @@ +package nl.andrewlalis.ui.control.command.executables; + +import nl.andrewlalis.git_api.GithubManager; + +/** + * Deletes all repositories with a given substring. + */ +public class DeleteRepos extends GithubExecutable { + @Override + protected boolean executeWithManager(GithubManager manager, String[] args) { + if (args.length < 1) { + return false; + } + manager.deleteAllRepositories(args[0]); + return true; + } +} diff --git a/src/main/java/nl/andrewlalis/ui/control/command/executables/ListErrors.java b/src/main/java/nl/andrewlalis/ui/control/command/executables/ListErrors.java index 61dc7bb..92804c0 100644 --- a/src/main/java/nl/andrewlalis/ui/control/command/executables/ListErrors.java +++ b/src/main/java/nl/andrewlalis/ui/control/command/executables/ListErrors.java @@ -22,6 +22,9 @@ public class ListErrors implements Executable { @Override public boolean execute(String[] args) { StringBuilder sb = new StringBuilder("Runtime Errors:\n"); + if (InitializerApp.organization.getErrors().isEmpty()) { + sb.append("None"); + } for (Error error : InitializerApp.organization.getErrors()) { sb.append(error); } diff --git a/src/main/java/nl/andrewlalis/ui/control/listeners/DeleteReposListener.java b/src/main/java/nl/andrewlalis/ui/control/listeners/DeleteReposListener.java new file mode 100644 index 0000000..d615bde --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/listeners/DeleteReposListener.java @@ -0,0 +1,32 @@ +package nl.andrewlalis.ui.control.listeners; + +import nl.andrewlalis.ui.control.command.CommandExecutor; +import nl.andrewlalis.ui.view.InitializerApp; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +/** + * A listener for when the user wants to delete repositories. + */ +public class DeleteReposListener extends ExecutableListener { + + public DeleteReposListener(CommandExecutor executor, InitializerApp app) { + super(executor, app); + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + String input = JOptionPane.showInputDialog(this.app, "Please enter a string which, if a repository contains it, results in deleting the repository.", "Enter Substing", JOptionPane.QUESTION_MESSAGE); + if (input != null) { + int decision = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete all repositories that contain \"" + "\" in their name?", "Delete Repositories", JOptionPane.YES_NO_OPTION); + if (decision == JOptionPane.YES_OPTION) { + this.executor.executeCommand("delete_repos", new String[]{ + app.getOrganizationName(), + app.getAccessToken(), + input + }); + } + } + } +} diff --git a/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java b/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java index de06018..c279862 100644 --- a/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java +++ b/src/main/java/nl/andrewlalis/ui/view/InitializerApp.java @@ -7,6 +7,7 @@ import nl.andrewlalis.ui.control.listeners.*; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; @@ -118,23 +119,22 @@ public class InitializerApp extends JFrame { JPanel commonActionsPanel = new JPanel(); commonActionsPanel.setLayout(new BoxLayout(commonActionsPanel, BoxLayout.PAGE_AXIS)); - JButton archiveAllButton = new JButton("Archive All"); - archiveAllButton.addActionListener(new ArchiveAllListener(this.executor, this)); - commonActionsPanel.add(archiveAllButton); + commonActionsPanel.add(this.generateButtonPanel("Archive All", new ArchiveAllListener(this.executor, this))); + commonActionsPanel.add(this.generateButtonPanel("Read Students File", new ReadStudentsFileListener(this.executor, this))); + commonActionsPanel.add(this.generateButtonPanel("Generate Assignments Repo", new GenerateAssignmentsRepoListener(this.executor, this))); - JButton generateStudentTeamsButton = new JButton("Read teams from file"); - generateStudentTeamsButton.addActionListener(new ReadStudentsFileListener(this.executor, this)); - commonActionsPanel.add(generateStudentTeamsButton); + // TODO: Enable this once the define teams dialog is complete. +// JButton defineTaTeamsButton = new JButton("Define TA Teams"); +// defineTaTeamsButton.addActionListener(new DefineTaTeamsListener(this.executor, this)); +// commonActionsPanel.add(f); - JButton generateAssignmentsRepoButton = new JButton("Generate Assignments Repo"); - generateAssignmentsRepoButton.addActionListener(new GenerateAssignmentsRepoListener(this.executor, this)); - commonActionsPanel.add(generateAssignmentsRepoButton); + commonActionsPanel.add(this.generateButtonPanel("Delete Repos", new DeleteReposListener(this.executor, this))); - JButton defineTaTeamsButton = new JButton("Define TA Teams"); - defineTaTeamsButton.addActionListener(new DefineTaTeamsListener(this.executor, this)); - commonActionsPanel.add(defineTaTeamsButton); + // Extra panel to push buttons to the top. + JPanel buttonAlignmentPanel = new JPanel(new BorderLayout()); + buttonAlignmentPanel.add(commonActionsPanel, BorderLayout.NORTH); - githubManagerPanel.add(commonActionsPanel, BorderLayout.CENTER); + githubManagerPanel.add(buttonAlignmentPanel, BorderLayout.CENTER); return githubManagerPanel; } @@ -190,6 +190,20 @@ public class InitializerApp extends JFrame { return newPanel; } + /** + * Generates a button with an attached action listener. + * @param buttonText The text to display on the button. + * @param listener The listener to attach to the button. + * @return A BorderLayout JPanel which contains the button in the CENTER location. + */ + private JPanel generateButtonPanel(String buttonText, ActionListener listener) { + JPanel panel = new JPanel(new BorderLayout()); + JButton button = new JButton(buttonText); + button.addActionListener(listener); + panel.add(button, BorderLayout.CENTER); + return panel; + } + /** * Gets the organization name entered in the relevant field. * @return The organization name the user has entered.