From 0b911946c4d81df6c8f411089f2e3036b4251e2b Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 21 Dec 2011 17:13:00 -0500 Subject: [PATCH] Support mailing lists for Teams --- docs/01_features.mkd | 1 + docs/01_setup.mkd | 51 +++++++++++++++---- docs/04_releases.mkd | 3 +- groovy/sendmail.groovy | 10 ++++ src/com/gitblit/ConfigUserService.java | 13 ++++- src/com/gitblit/FileUserService.java | 9 ++++ src/com/gitblit/GitBlit.java | 3 +- src/com/gitblit/client/EditTeamDialog.java | 30 ++++++++--- src/com/gitblit/models/TeamModel.java | 8 +++ src/com/gitblit/utils/StringUtils.java | 5 +- .../gitblit/wicket/GitBlitWebApp.properties | 3 +- .../wicket/pages/EditRepositoryPage.html | 2 +- .../gitblit/wicket/pages/EditTeamPage.html | 4 +- .../gitblit/wicket/pages/EditTeamPage.java | 26 +++++++++- .../gitblit/wicket/pages/EditUserPage.html | 3 +- 15 files changed, 144 insertions(+), 27 deletions(-) diff --git a/docs/01_features.mkd b/docs/01_features.mkd index 39fbfaac..7d33911b 100644 --- a/docs/01_features.mkd +++ b/docs/01_features.mkd @@ -12,6 +12,7 @@ - Java/Swing Gitblit Manager tool - Gitweb inspired web UI - Groovy pre- and post- push hook scripts, per-repository or globally for all repositories +- Email push notifications *(via sendmail.groovy push script)* - Administrators may create, edit, rename, or delete repositories through the web UI or RPC interface - Administrators may create, edit, rename, or delete users through the web UI or RPC interface - Administrators may create, edit, rename, or delete teams through the web UI or RPC interface diff --git a/docs/01_setup.mkd b/docs/01_setup.mkd index 7ea57565..e400a0b1 100644 --- a/docs/01_setup.mkd +++ b/docs/01_setup.mkd @@ -195,6 +195,7 @@ The `users.conf` file uses a Git-style configuration format: user = murdock user = babaracus repository = topsecret.git + mailingList = list@ateam.org The `users.conf` file allows flexibility for adding new fields to a UserModel object that the original `users.properties` file does not afford without imposing the complexity of relying on an embedded SQL database. @@ -204,7 +205,7 @@ All users are stored in the `users.properties` file or in the file you specified The format of `users.properties` loosely follows Jetty's convention for HashRealms: username=password,role1,role2,role3... - @teamname=!username1,!username2,!username3,repository1,repository2,repository3... + @teamname=&mailinglist,!username1,!username2,!username3,repository1,repository2,repository3... #### Usernames Usernames must be unique and are case-insensitive. @@ -218,7 +219,7 @@ There are two actual *roles* in Gitblit: *#admin*, which grants administrative p ## Authentication and Authorization Customization -### Choice 1: Customize Authentication Only +### Customize Authentication Only This is the simplest choice where you implement custom authentication and delegate all other standard user and team operations to one of Gitblit's user service implementations. This choice insulates your customization from changes in User and Team model classes and additional API that may be added to IUserService. Please subclass [com.gitblit.GitblitUserService](https://github.com/gitblit/gitblit/blob/master/src/com/gitblit/GitblitUserService.java) and override the *setup()* and *authenticate()* methods. @@ -228,7 +229,7 @@ You may use your subclass by specifying its fully qualified classname in the *re Your subclass must be on Gitblit's classpath and must have a public default constructor. -### Choice 2: Customize Everything +### Customize Everything Instead of maintaining a `users.conf` or `users.properties` file, you may want to integrate Gitblit into an existing environment. You may use your own custom *com.gitblit.IUserService* implementation by specifying its fully qualified classname in the *realm.userService* setting. @@ -247,15 +248,16 @@ The Groovy hook mechanism allows for dynamic extension of Gitblit to execute cus ### Rules, Requirements, & Behaviors 1. Your Groovy scripts must be stored in the *groovy.scriptsFolder* as specified in `gitblit.properties` or `web.xml`. 2. All script files must have the *.groovy* extension. Because of this you may omit the extension when specifying the script. -3. Scripts must be explicitly specified to be executed, no scripts are *automatically* executed by name or extension. -4. A script can be specified to run on *all repositories* by adding the script file name to *groovy.preReceiveScripts* or *groovy.postReceiveScripts* in `gitblit.properties` or `web.xml`. Be mindful of access retrictions and global properties like *mail.mailingLists* if specifying *sendmail* to run on all repositories. -5. Scripts may also be specified per-repository in the repository's settings. -6. Globally specified scripts are excluded from the list of available scripts in a repository's settings -7. Globally specified scripts are executed first, in their listed order, followed by per-repository scripts, in their listed order. -8. A script may only be defined once in a pre-receive list and once in a post-receive list. +3. Script filenames must not have spaces! +4. Scripts must be explicitly specified to be executed, no scripts are *automatically* executed by name or extension. +5. A script can be specified to run on *all repositories* by adding the script file name to *groovy.preReceiveScripts* or *groovy.postReceiveScripts* in `gitblit.properties` or `web.xml`. +6. Scripts may also be specified per-repository in the repository's settings. +7. Globally specified scripts are excluded from the list of available scripts in a repository's settings +8. Globally specified scripts are executed first, in their listed order, followed by per-repository scripts, in their listed order. +9. A script may only be defined once in a pre-receive chain and once in a post-receive chain. You may execute the same script on pre-receive and post-receive, just not multiple times within a pre-receive or post-receive event. -9. Gitblit does not differentiate between what can be a pre-receive script and what can be a post-receive script. -10. If a script *returns false* then the pre-receive or post-receive hook chain is aborted and none of the subsequent scripts will execute. +10. Gitblit does not differentiate between what can be a pre-receive script and what can be a post-receive script. +11. If a script *returns false* then the hook chain is aborted and none of the subsequent scripts will execute. Some sample scripts are included in the GO and WAR distributions to show you how you can tap into Gitblit with the provided bound variables. Additional implementation details may be specified in the header comment of these examples. @@ -273,6 +275,33 @@ Post-Receive scripts execute after all refs have been updated. This is the appropriate point to trigger continuous integration builds or send email notifications, etc. +## Push Email Notifications + +Gitblit implements email notifications in *sendmail.groovy* which uses the Groovy Hook Script mechanism. This allows for dynamic customization of the notification process at the installation site and serves as an example push script. + +### Enabling Push Notifications + +In order to send email notifications on a push to Gitblit, this script must be specified somewhere in the *post-receive* script chain. +You may specify *sendmail* in one of two places: + +1. *groovy.postReceiveScripts* in `gitblit.properties` or `web.xml`, globally applied to all repositories +2. post-receive scripts of a Repository definition + +### Destination Addresses + +Gitblit does not currently support individual subscriptions to repositories; i.e. a *user* can not subscribe or unsubscribe from push notifications. + +However, Repository Managers and Administrators can specify subscribed email addresses in one of three places: + +1. *mail.mailingLists* in `gitblit.properties` or `web.xml`, globally applied to all push-notified repositories +2. mailing lists in a Team definition, applied to all repositories that are part of the team definition +3. mailing lists in a Repository definition + +All three sources are checked and merged into a unique list of destination addresses for push notifications. + +**NOTE:** +Care should be taken when devising your notification scheme as it relates to any VIEW restricted repositories you might have. Setting a global mailing list and activating push notifications for a VIEW restricted repository may send unwanted emails. + ## Client Setup and Configuration ### Https with Self-Signed Certificates You must tell Git/JGit not to verify the self-signed certificate in order to perform any remote Git operations. diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd index 292ff987..3e49c188 100644 --- a/docs/04_releases.mkd +++ b/docs/04_releases.mkd @@ -5,11 +5,12 @@ - updated: Gitblit GO is now monolithic like the WAR build. (issue 30) This change helps adoption of GO in environments without an internet connection or with a restricted connection. -- added: Groovy 1.8.4 and sample pre- and post- push Groovy hook scripts. Hook scripts can be set per-repository or globally for all repositories. +- added: Groovy 1.8.4 and a push hook script mechanism. Hook scripts can be set per-repository or globally for all repositories. Unfortunately this adds another 6 MB to the 8MB Gitblit package, but it allows for a *very* powerful, flexible, platform-independent hook script mechanism. **New:** *groovy.scriptsFolder = groovy* **New:** *groovy.preReceiveScripts =* **New:** *groovy.postReceiveScripts =* +- added: *sendmail.groovy* for optional email notifications on push - added: New key for mailing lists. This is used in conjunction with the *sendmail.groovy* hook script. **New:** *mail.mailingLists =* - added: GitblitUserService. This is a wrapper object for the built-in user service implementations. For those wanting to only implement *custom authentication* it is recommended to subclass GitblitUserService and override the appropriate methods. Going forward, this will insulate customized behavior from new IUserService API and changes in model classes. diff --git a/groovy/sendmail.groovy b/groovy/sendmail.groovy index b43f9dea..1020241f 100644 --- a/groovy/sendmail.groovy +++ b/groovy/sendmail.groovy @@ -16,6 +16,7 @@ import com.gitblit.GitBlit import com.gitblit.Keys import com.gitblit.models.RepositoryModel +import com.gitblit.models.TeamModel import com.gitblit.models.UserModel import com.gitblit.utils.JGitUtils import org.eclipse.jgit.lib.Repository @@ -87,6 +88,15 @@ if (mailinglist != null) { // add all mailing lists defined in gitblit.properties or web.xml toAddresses.addAll(gitblit.getStrings(Keys.mail.mailingLists)) +// add all team mailing lists +def teams = gitblit.getRepositoryTeams(repository) +for (team in teams) { + TeamModel model = gitblit.getTeamModel(team) + if (model.mailingLists) { + toAddresses.addAll(model.mailingLists) + } +} + // add all mailing lists for the repository toAddresses.addAll(repository.mailingLists) diff --git a/src/com/gitblit/ConfigUserService.java b/src/com/gitblit/ConfigUserService.java index a0a38e6a..b899d924 100644 --- a/src/com/gitblit/ConfigUserService.java +++ b/src/com/gitblit/ConfigUserService.java @@ -62,6 +62,8 @@ public class ConfigUserService implements IUserService { private static final String REPOSITORY = "repository"; private static final String ROLE = "role"; + + private static final String MAILINGLIST = "mailingList"; private final File realmFile; @@ -303,7 +305,7 @@ public class ConfigUserService implements IUserService { List list = new ArrayList(teams.keySet()); return list; } - + /** * Returns the list of all users who are allowed to bypass the access * restriction placed on the specified repository. @@ -648,6 +650,14 @@ public class ConfigUserService implements IUserService { if (model.users != null) { config.setStringList(TEAM, model.name, USER, new ArrayList(model.users)); } + + // null check on "final" mailing lists because JSON-sourced + // TeamModel + // can have a null users object + if (model.mailingLists != null) { + config.setStringList(TEAM, model.name, MAILINGLIST, new ArrayList( + model.mailingLists)); + } } config.save(); @@ -714,6 +724,7 @@ public class ConfigUserService implements IUserService { team.addRepositories(Arrays.asList(config.getStringList(TEAM, teamname, REPOSITORY))); team.addUsers(Arrays.asList(config.getStringList(TEAM, teamname, USER))); + team.addMailingLists(Arrays.asList(config.getStringList(TEAM, teamname, MAILINGLIST))); teams.put(team.name.toLowerCase(), team); diff --git a/src/com/gitblit/FileUserService.java b/src/com/gitblit/FileUserService.java index 880ca7b8..27892f71 100644 --- a/src/com/gitblit/FileUserService.java +++ b/src/com/gitblit/FileUserService.java @@ -618,15 +618,19 @@ public class FileUserService extends FileSettings implements IUserService { TeamModel team = new TeamModel(username.substring(1)); List repositories = new ArrayList(); List users = new ArrayList(); + List mailingLists = new ArrayList(); for (String role : roles) { if (role.charAt(0) == '!') { users.add(role.substring(1)); + } else if (role.charAt(0) == '&') { + mailingLists.add(role.substring(1)); } else { repositories.add(role); } } team.addRepositories(repositories); team.addUsers(users); + team.addMailingLists(mailingLists); teams.put(team.name.toLowerCase(), team); } else { // user definition @@ -832,6 +836,11 @@ public class FileUserService extends FileSettings implements IUserService { sb.append(user); sb.append(','); } + for (String address : model.mailingLists) { + sb.append('&'); + sb.append(address); + sb.append(','); + } // trim trailing comma sb.setLength(sb.length() - 1); allUsers.remove("@" + teamname); diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java index 835aa132..c818dd50 100644 --- a/src/com/gitblit/GitBlit.java +++ b/src/com/gitblit/GitBlit.java @@ -25,6 +25,7 @@ import java.lang.reflect.Field; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -1505,7 +1506,7 @@ public class GitBlit implements ServletContextListener { * @param message * @param toAddresses */ - public void sendMail(String subject, String message, ArrayList toAddresses) { + public void sendMail(String subject, String message, Collection toAddresses) { this.sendMail(subject, message, toAddresses.toArray(new String[0])); } diff --git a/src/com/gitblit/client/EditTeamDialog.java b/src/com/gitblit/client/EditTeamDialog.java index 4297599c..a04ed2cf 100644 --- a/src/com/gitblit/client/EditTeamDialog.java +++ b/src/com/gitblit/client/EditTeamDialog.java @@ -65,6 +65,8 @@ public class EditTeamDialog extends JDialog { private JTextField teamnameField; + private JTextField mailingListsField; + private JPalette repositoryPalette; private JPalette userPalette; @@ -105,16 +107,20 @@ public class EditTeamDialog extends JDialog { private void initialize(int protocolVersion, TeamModel aTeam) { teamnameField = new JTextField(aTeam.name == null ? "" : aTeam.name, 25); + mailingListsField = new JTextField(aTeam.mailingLists == null ? "" + : StringUtils.flattenStrings(aTeam.mailingLists, " "), 50); + JPanel fieldsPanel = new JPanel(new GridLayout(0, 1)); fieldsPanel.add(newFieldPanel(Translation.get("gb.teamName"), teamnameField)); + fieldsPanel.add(newFieldPanel(Translation.get("gb.mailingLists"), mailingListsField)); final Insets _insets = new Insets(5, 5, 5, 5); repositoryPalette = new JPalette(); userPalette = new JPalette(); - + JPanel fieldsPanelTop = new JPanel(new BorderLayout()); fieldsPanelTop.add(fieldsPanel, BorderLayout.NORTH); - + JPanel repositoriesPanel = new JPanel(new BorderLayout()) { private static final long serialVersionUID = 1L; @@ -140,7 +146,6 @@ public class EditTeamDialog extends JDialog { panel.addTab(Translation.get("gb.teamMembers"), usersPanel); panel.addTab(Translation.get("gb.restrictedRepositories"), repositoriesPanel); - JButton createButton = new JButton(Translation.get("gb.save")); createButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { @@ -162,7 +167,7 @@ public class EditTeamDialog extends JDialog { JPanel controls = new JPanel(); controls.add(cancelButton); controls.add(createButton); - + JPanel centerPanel = new JPanel(new BorderLayout(5, 5)) { private static final long serialVersionUID = 1L; @@ -218,9 +223,22 @@ public class EditTeamDialog extends JDialog { } team.name = tname; + String ml = mailingListsField.getText(); + if (!StringUtils.isEmpty(ml)) { + Set list = new HashSet(); + for (String address : ml.split("(,|\\s)")) { + if (StringUtils.isEmpty(address)) { + continue; + } + list.add(address.toLowerCase()); + } + team.mailingLists.clear(); + team.mailingLists.addAll(list); + } + team.repositories.clear(); team.repositories.addAll(repositoryPalette.getSelections()); - + team.users.clear(); team.users.addAll(userPalette.getSelections()); return true; @@ -251,7 +269,7 @@ public class EditTeamDialog extends JDialog { } repositoryPalette.setObjects(restricted, selected); } - + public void setUsers(List users, List selected) { Collections.sort(users); if (selected != null) { diff --git a/src/com/gitblit/models/TeamModel.java b/src/com/gitblit/models/TeamModel.java index 195b9d5c..3258ef6c 100644 --- a/src/com/gitblit/models/TeamModel.java +++ b/src/com/gitblit/models/TeamModel.java @@ -18,6 +18,7 @@ package com.gitblit.models; import java.io.Serializable; import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -35,6 +36,7 @@ public class TeamModel implements Serializable, Comparable { public String name; public final Set users = new HashSet(); public final Set repositories = new HashSet(); + public final Set mailingLists = new HashSet(); public TeamModel(String name) { this.name = name; @@ -76,6 +78,12 @@ public class TeamModel implements Serializable, Comparable { users.remove(name.toLowerCase()); } + public void addMailingLists(Collection addresses) { + for (String address:addresses) { + mailingLists.add(address.toLowerCase()); + } + } + @Override public String toString() { return name; diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index a51c157f..1e478999 100644 --- a/src/com/gitblit/utils/StringUtils.java +++ b/src/com/gitblit/utils/StringUtils.java @@ -19,6 +19,7 @@ import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -127,7 +128,7 @@ public class StringUtils { * @param values * @return flattened list */ - public static String flattenStrings(List values) { + public static String flattenStrings(Collection values) { return flattenStrings(values, " "); } @@ -139,7 +140,7 @@ public class StringUtils { * @param separator * @return flattened list */ - public static String flattenStrings(List values, String separator) { + public static String flattenStrings(Collection values, String separator) { StringBuilder sb = new StringBuilder(); for (String value : values) { sb.append(value).append(separator); diff --git a/src/com/gitblit/wicket/GitBlitWebApp.properties b/src/com/gitblit/wicket/GitBlitWebApp.properties index c0b0010d..aead2c41 100644 --- a/src/com/gitblit/wicket/GitBlitWebApp.properties +++ b/src/com/gitblit/wicket/GitBlitWebApp.properties @@ -200,4 +200,5 @@ gb.mailingLists = mailing lists gb.mailingListsDescription = used by the sendmail hook gb.preReceiveScripts = pre-receive scripts gb.postReceiveScripts = post-receive scripts -gb.hookScripts = hook scripts \ No newline at end of file +gb.hookScripts = hook scripts +gb.accessPermissions = access permissions \ No newline at end of file diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.html b/src/com/gitblit/wicket/pages/EditRepositoryPage.html index 310f59e5..9381344b 100644 --- a/src/com/gitblit/wicket/pages/EditRepositoryPage.html +++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.html @@ -23,7 +23,7 @@       -

+

-
+   +


diff --git a/src/com/gitblit/wicket/pages/EditTeamPage.java b/src/com/gitblit/wicket/pages/EditTeamPage.java index 47f3568f..c5c240fd 100644 --- a/src/com/gitblit/wicket/pages/EditTeamPage.java +++ b/src/com/gitblit/wicket/pages/EditTeamPage.java @@ -18,8 +18,10 @@ package com.gitblit.wicket.pages; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; import org.apache.wicket.PageParameters; import org.apache.wicket.extensions.markup.html.form.palette.Palette; @@ -28,6 +30,8 @@ import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.model.CompoundPropertyModel; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; import org.apache.wicket.model.util.CollectionModel; import org.apache.wicket.model.util.ListModel; @@ -45,6 +49,8 @@ public class EditTeamPage extends RootSubPage { private final boolean isCreate; + private IModel mailingLists; + public EditTeamPage() { // create constructor super(); @@ -128,6 +134,20 @@ public class EditTeamPage extends RootSubPage { teamModel.users.clear(); teamModel.users.addAll(members); + // set mailing lists + String ml = mailingLists.getObject(); + if (!StringUtils.isEmpty(ml)) { + Set list = new HashSet(); + for (String address : ml.split("(,|\\s)")) { + if (StringUtils.isEmpty(address)) { + continue; + } + list.add(address.toLowerCase()); + } + teamModel.mailingLists.clear(); + teamModel.mailingLists.addAll(list); + } + try { GitBlit.self().updateTeamModel(oldName, teamModel, isCreate); } catch (GitBlitException e) { @@ -149,8 +169,12 @@ public class EditTeamPage extends RootSubPage { // field names reflective match TeamModel fields form.add(new TextField("name")); - form.add(repositories); form.add(users); + mailingLists = new Model(teamModel.mailingLists == null ? "" + : StringUtils.flattenStrings(teamModel.mailingLists, " ")); + form.add(new TextField("mailingLists", mailingLists)); + + form.add(repositories); form.add(new Button("save")); Button cancel = new Button("cancel") { diff --git a/src/com/gitblit/wicket/pages/EditUserPage.html b/src/com/gitblit/wicket/pages/EditUserPage.html index 319a2b65..87263c24 100644 --- a/src/com/gitblit/wicket/pages/EditUserPage.html +++ b/src/com/gitblit/wicket/pages/EditUserPage.html @@ -10,12 +10,13 @@
+ - + -- 2.39.5