From 97d3afed7f416bc3c0740b05974949ea38520c7e Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 28 Dec 2011 20:09:46 -0500 Subject: [PATCH] Manager support for hook script inheritance --- src/com/gitblit/RpcServlet.java | 6 +- src/com/gitblit/client/EditTeamDialog.java | 41 +++++++ src/com/gitblit/client/GitblitClient.java | 113 +++++++++++++++++- src/com/gitblit/client/RepositoriesPanel.java | 10 +- src/com/gitblit/client/TeamsPanel.java | 6 + 5 files changed, 165 insertions(+), 11 deletions(-) diff --git a/src/com/gitblit/RpcServlet.java b/src/com/gitblit/RpcServlet.java index c33bd8a2..2d513887 100644 --- a/src/com/gitblit/RpcServlet.java +++ b/src/com/gitblit/RpcServlet.java @@ -276,7 +276,7 @@ public class RpcServlet extends JsonServlet { } } else if (RpcRequest.LIST_SETTINGS.equals(reqType)) { // return the server's settings - ServerSettings settings = GitBlit.self().getSettingsModel(); + ServerSettings settings = GitBlit.self().getSettingsModel(); if (allowAdmin) { // return all settings result = settings; @@ -294,12 +294,12 @@ public class RpcServlet extends JsonServlet { keys.add(Keys.federation.sets); } // build the settings - ServerSettings managementSettings = new ServerSettings(); + ServerSettings managementSettings = new ServerSettings(); for (String key : keys) { managementSettings.add(settings.get(key)); } if (allowManagement) { - settings.pushScripts = settings.pushScripts; + managementSettings.pushScripts = settings.pushScripts; } result = managementSettings; } diff --git a/src/com/gitblit/client/EditTeamDialog.java b/src/com/gitblit/client/EditTeamDialog.java index a04ed2cf..49669e2c 100644 --- a/src/com/gitblit/client/EditTeamDialog.java +++ b/src/com/gitblit/client/EditTeamDialog.java @@ -71,6 +71,10 @@ public class EditTeamDialog extends JDialog { private JPalette userPalette; + private JPalette preReceivePalette; + + private JPalette postReceivePalette; + private Set teamnames; public EditTeamDialog(int protocolVersion, ServerSettings settings) { @@ -141,10 +145,24 @@ public class EditTeamDialog extends JDialog { }; usersPanel.add(userPalette, BorderLayout.CENTER); + preReceivePalette = new JPalette(true); + JPanel preReceivePanel = new JPanel(new BorderLayout(5, 5)); + preReceivePanel.add( + newFieldPanel(Translation.get("gb.preReceiveScripts"), preReceivePalette), + BorderLayout.CENTER); + + postReceivePalette = new JPalette(true); + JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5)); + postReceivePanel.add( + newFieldPanel(Translation.get("gb.postReceiveScripts"), postReceivePalette), + BorderLayout.CENTER); + JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP); panel.addTab(Translation.get("gb.general"), fieldsPanelTop); panel.addTab(Translation.get("gb.teamMembers"), usersPanel); panel.addTab(Translation.get("gb.restrictedRepositories"), repositoriesPanel); + panel.addTab(Translation.get("gb.preReceiveScripts"), preReceivePanel); + panel.addTab(Translation.get("gb.postReceiveScripts"), postReceivePanel); JButton createButton = new JButton(Translation.get("gb.save")); createButton.addActionListener(new ActionListener() { @@ -241,6 +259,13 @@ public class EditTeamDialog extends JDialog { team.users.clear(); team.users.addAll(userPalette.getSelections()); + + team.preReceiveScripts.clear(); + team.preReceiveScripts.addAll(preReceivePalette.getSelections()); + + team.postReceiveScripts.clear(); + team.postReceiveScripts.addAll(postReceivePalette.getSelections()); + return true; } @@ -278,6 +303,22 @@ public class EditTeamDialog extends JDialog { userPalette.setObjects(users, selected); } + public void setPreReceiveScripts(List unused, List selected) { + Collections.sort(unused); + if (selected != null) { + Collections.sort(selected); + } + preReceivePalette.setObjects(unused, selected); + } + + public void setPostReceiveScripts(List unused, List selected) { + Collections.sort(unused); + if (selected != null) { + Collections.sort(selected); + } + postReceivePalette.setObjects(unused, selected); + } + public TeamModel getTeam() { if (canceled) { return null; diff --git a/src/com/gitblit/client/GitblitClient.java b/src/com/gitblit/client/GitblitClient.java index f9af8dfe..d37ee911 100644 --- a/src/com/gitblit/client/GitblitClient.java +++ b/src/com/gitblit/client/GitblitClient.java @@ -21,9 +21,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeSet; import com.gitblit.Constants; import com.gitblit.GitBlitException.ForbiddenException; @@ -183,11 +185,105 @@ public class GitblitClient implements Serializable { } } - public List getAvailableScripts() { - if (settings.pushScripts == null) { - return new ArrayList(); + /** + * Returns the list of pre-receive scripts the repository inherited from the + * global settings and team affiliations. + * + * @param repository + * if null only the globally specified scripts are returned + * @return a list of scripts + */ + public List getPreReceiveScriptsInherited(RepositoryModel repository) { + Set scripts = new LinkedHashSet(); + // Globals + for (String script : settings.get(Keys.groovy.preReceiveScripts).getStrings()) { + if (script.endsWith(".groovy")) { + scripts.add(script.substring(0, script.lastIndexOf('.'))); + } else { + scripts.add(script); + } + } + + // Team Scripts + if (repository != null) { + for (String teamname : getPermittedTeamnames(repository)) { + TeamModel team = getTeamModel(teamname); + scripts.addAll(team.preReceiveScripts); + } + } + return new ArrayList(scripts); + } + + /** + * Returns the list of all available Groovy pre-receive push hook scripts + * that are not already inherited by the repository. Script files must have + * .groovy extension + * + * @param repository + * optional parameter + * @return list of available hook scripts + */ + public List getPreReceiveScriptsUnused(RepositoryModel repository) { + Set inherited = new TreeSet(getPreReceiveScriptsInherited(repository)); + + // create list of available scripts by excluding inherited scripts + List scripts = new ArrayList(); + for (String script : settings.pushScripts) { + if (!inherited.contains(script)) { + scripts.add(script); + } + } + return scripts; + } + + /** + * Returns the list of post-receive scripts the repository inherited from + * the global settings and team affiliations. + * + * @param repository + * if null only the globally specified scripts are returned + * @return a list of scripts + */ + public List getPostReceiveScriptsInherited(RepositoryModel repository) { + Set scripts = new LinkedHashSet(); + // Global Scripts + for (String script : settings.get(Keys.groovy.postReceiveScripts).getStrings()) { + if (script.endsWith(".groovy")) { + scripts.add(script.substring(0, script.lastIndexOf('.'))); + } else { + scripts.add(script); + } } - return settings.pushScripts; + // Team Scripts + if (repository != null) { + for (String teamname : getPermittedTeamnames(repository)) { + TeamModel team = getTeamModel(teamname); + scripts.addAll(team.postReceiveScripts); + } + } + return new ArrayList(scripts); + } + + /** + * Returns the list of unused Groovy post-receive push hook scripts that are + * not already inherited by the repository. Script files must have .groovy + * extension + * + * @param repository + * optional parameter + * @return list of available hook scripts + */ + public List getPostReceiveScriptsUnused(RepositoryModel repository) { + Set inherited = new TreeSet(getPostReceiveScriptsInherited(repository)); + + // create list of available scripts by excluding inherited scripts + List scripts = new ArrayList(); + for (String script : settings.pushScripts) { + if (!inherited.contains(script)) { + scripts.add(script); + } + } + return scripts; } public ServerSettings getSettings() { @@ -393,6 +489,15 @@ public class GitblitClient implements Serializable { } return teamnames; } + + public TeamModel getTeamModel(String name) { + for (TeamModel team : allTeams) { + if (team.name.equalsIgnoreCase(name)) { + return team; + } + } + return null; + } public List getFederationSets() { return settings.get(Keys.federation.sets).getStrings(); diff --git a/src/com/gitblit/client/RepositoriesPanel.java b/src/com/gitblit/client/RepositoriesPanel.java index 2994b763..50284be1 100644 --- a/src/com/gitblit/client/RepositoriesPanel.java +++ b/src/com/gitblit/client/RepositoriesPanel.java @@ -357,8 +357,8 @@ public abstract class RepositoriesPanel extends JPanel { dialog.setTeams(gitblit.getTeamnames(), null); dialog.setRepositories(gitblit.getRepositories()); dialog.setFederationSets(gitblit.getFederationSets(), null); - dialog.setPreReceiveScripts(gitblit.getAvailableScripts(), null); - dialog.setPostReceiveScripts(gitblit.getAvailableScripts(), null); + dialog.setPreReceiveScripts(gitblit.getPreReceiveScriptsUnused(null), null); + dialog.setPostReceiveScripts(gitblit.getPostReceiveScriptsUnused(null), null); dialog.setVisible(true); final RepositoryModel newRepository = dialog.getRepository(); final List permittedUsers = dialog.getPermittedUsers(); @@ -417,8 +417,10 @@ public abstract class RepositoriesPanel extends JPanel { dialog.setTeams(gitblit.getTeamnames(), gitblit.getPermittedTeamnames(repository)); dialog.setRepositories(gitblit.getRepositories()); dialog.setFederationSets(gitblit.getFederationSets(), repository.federationSets); - dialog.setPreReceiveScripts(gitblit.getAvailableScripts(), repository.preReceiveScripts); - dialog.setPostReceiveScripts(gitblit.getAvailableScripts(), repository.postReceiveScripts); + dialog.setPreReceiveScripts(gitblit.getPreReceiveScriptsUnused(repository), + repository.preReceiveScripts); + dialog.setPostReceiveScripts(gitblit.getPostReceiveScriptsUnused(repository), + repository.postReceiveScripts); dialog.setVisible(true); final RepositoryModel revisedRepository = dialog.getRepository(); final List permittedUsers = dialog.getPermittedUsers(); diff --git a/src/com/gitblit/client/TeamsPanel.java b/src/com/gitblit/client/TeamsPanel.java index 2d1d914a..b2a18731 100644 --- a/src/com/gitblit/client/TeamsPanel.java +++ b/src/com/gitblit/client/TeamsPanel.java @@ -257,6 +257,8 @@ public abstract class TeamsPanel extends JPanel { dialog.setTeams(gitblit.getTeams()); dialog.setRepositories(gitblit.getRepositories(), null); dialog.setUsers(gitblit.getUsernames(), null); + dialog.setPreReceiveScripts(gitblit.getPreReceiveScriptsUnused(null), null); + dialog.setPostReceiveScripts(gitblit.getPostReceiveScriptsUnused(null), null); dialog.setVisible(true); final TeamModel newTeam = dialog.getTeam(); if (newTeam == null) { @@ -304,6 +306,10 @@ public abstract class TeamsPanel extends JPanel { dialog.setRepositories(gitblit.getRepositories(), new ArrayList(team.repositories)); dialog.setUsers(gitblit.getUsernames(), team.users == null ? null : new ArrayList( team.users)); + dialog.setPreReceiveScripts(gitblit.getPreReceiveScriptsUnused(null), + team.preReceiveScripts); + dialog.setPostReceiveScripts(gitblit.getPostReceiveScriptsUnused(null), + team.postReceiveScripts); dialog.setVisible(true); final TeamModel revisedTeam = dialog.getTeam(); if (revisedTeam == null) { -- 2.39.5