Skip to content

Commit

Permalink
Merge pull request #16 from jcrygier/custom_properties
Browse files Browse the repository at this point in the history
Custom Defined Repository Properties (Issue #92)
  • Loading branch information
gitblit committed May 9, 2012
2 parents b94965e + 022ebb4 commit 5f22725
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 6 deletions.
10 changes: 10 additions & 0 deletions distrib/gitblit.properties
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,13 @@ server.storePassword = gitblit
# SINCE 0.5.0
# RESTART REQUIRED
server.shutdownPort = 8081

# Custom Defined Properties for Repositories
# Space delimited (use quotes if labels have spaces) list of custom properties
# to show up on the "Edit Repository" page, with labels. Thes custom properties will
# then be available for hooks.
#
# E.g. "commit-msg-regex=Commit Message Regualar Expression" another-property=Another
#
# SINCE 1.0.0
repository.customFields =
4 changes: 4 additions & 0 deletions src/com/gitblit/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public class Constants {

public static final String DEFAULT_BRANCH = "default";

public static String CUSTOM_FIELDS_PROP_SECTION = "gitblit";

public static String CUSTOM_FIELDS_PROP_SUBSECTION = "customFields";

public static String getGitBlitVersion() {
return NAME + " v" + VERSION;
}
Expand Down
11 changes: 11 additions & 0 deletions src/com/gitblit/GitBlit.java
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,12 @@ public RepositoryModel getRepositoryModel(String repositoryName) {
"gitblit", null, "mailingList")));
model.indexedBranches = new ArrayList<String>(Arrays.asList(config.getStringList(
"gitblit", null, "indexBranch")));

// Custom defined properties
model.customFields = new HashMap<String, String>();
for (String aProperty : config.getNames(Constants.CUSTOM_FIELDS_PROP_SECTION, Constants.CUSTOM_FIELDS_PROP_SUBSECTION)) {
model.customFields.put(aProperty, config.getString(Constants.CUSTOM_FIELDS_PROP_SECTION, Constants.CUSTOM_FIELDS_PROP_SUBSECTION, aProperty));
}
}
model.HEAD = JGitUtils.getHEADRef(r);
model.availableRefs = JGitUtils.getAvailableHeadTargets(r);
Expand Down Expand Up @@ -1103,6 +1109,11 @@ public void updateConfiguration(Repository r, RepositoryModel repository) {
updateList(config, "postReceiveScript", repository.postReceiveScripts);
updateList(config, "mailingList", repository.mailingLists);
updateList(config, "indexBranch", repository.indexedBranches);

// User Defined Properties
for (Entry<String, String> singleProperty : repository.customFields.entrySet()) {
config.setString(Constants.CUSTOM_FIELDS_PROP_SECTION, Constants.CUSTOM_FIELDS_PROP_SUBSECTION, singleProperty.getKey(), singleProperty.getValue());
}

try {
config.save();
Expand Down
53 changes: 51 additions & 2 deletions src/com/gitblit/client/EditRepositoryDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
Expand All @@ -44,10 +47,12 @@
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListCellRenderer;
import javax.swing.ScrollPaneConstants;

import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.FederationStrategy;
Expand Down Expand Up @@ -117,6 +122,8 @@ public class EditRepositoryDialog extends JDialog {
private JLabel postReceiveInherited;

private Set<String> repositoryNames;

private JPanel customFieldsPanel;

public EditRepositoryDialog(int protocolVersion) {
this(protocolVersion, new RepositoryModel());
Expand Down Expand Up @@ -277,6 +284,12 @@ private void initialize(int protocolVersion, RepositoryModel anRepository) {
JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5));
postReceivePanel.add(postReceivePalette, BorderLayout.CENTER);
postReceivePanel.add(postReceiveInherited, BorderLayout.WEST);

customFieldsPanel = new JPanel();
customFieldsPanel.setLayout(new BoxLayout(customFieldsPanel, BoxLayout.Y_AXIS));
JScrollPane customFieldsScrollPane = new JScrollPane(customFieldsPanel);
customFieldsScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
customFieldsScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP);
panel.addTab(Translation.get("gb.general"), fieldsPanel);
Expand All @@ -290,6 +303,9 @@ private void initialize(int protocolVersion, RepositoryModel anRepository) {
}
panel.addTab(Translation.get("gb.preReceiveScripts"), preReceivePanel);
panel.addTab(Translation.get("gb.postReceiveScripts"), postReceivePanel);

panel.addTab(Translation.get("gb.customFields"), customFieldsScrollPane);


JButton createButton = new JButton(Translation.get("gb.save"));
createButton.addActionListener(new ActionListener() {
Expand Down Expand Up @@ -331,11 +347,15 @@ public Insets getInsets() {
pack();
nameField.requestFocus();
}

private JPanel newFieldPanel(String label, JComponent comp) {
return newFieldPanel(label, 150, comp);
}

private JPanel newFieldPanel(String label, int labelSize, JComponent comp) {
JLabel fieldLabel = new JLabel(label);
fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
fieldLabel.setPreferredSize(new Dimension(150, 20));
fieldLabel.setPreferredSize(new Dimension(labelSize, 20));
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
panel.add(fieldLabel);
panel.add(comp);
Expand Down Expand Up @@ -448,6 +468,15 @@ private boolean validateFields() {
repository.indexedBranches = indexedBranchesPalette.getSelections();
repository.preReceiveScripts = preReceivePalette.getSelections();
repository.postReceiveScripts = postReceivePalette.getSelections();

// Custom Fields
repository.customFields = new HashMap<String, String>();

for (Component aCustomFieldPanel : customFieldsPanel.getComponents()) {
JTextField textField = (JTextField) ((JPanel)aCustomFieldPanel).getComponent(1);
repository.customFields.put(textField.getName(), textField.getText());
}

return true;
}

Expand Down Expand Up @@ -525,6 +554,26 @@ public List<String> getPermittedUsers() {
public List<String> getPermittedTeams() {
return teamsPalette.getSelections();
}

public void setCustomFields(RepositoryModel repository, List<String> customFields) {
customFieldsPanel.removeAll();

for (String customFieldDef : customFields) {
String[] customFieldProperty = customFieldDef.split("=");
String fieldName = customFieldProperty[0];
String fieldLabel = customFieldProperty[1];

JTextField textField = new JTextField(repository.customFields.get(fieldName), 50);
textField.setName(fieldName);

customFieldsPanel.add(newFieldPanel(fieldLabel, 250, textField));
}

if (customFields.size() < 14) {
customFieldsPanel.add(Box.createVerticalGlue());
customFieldsPanel.add(Box.createRigidArea(new Dimension(300, 300 - (customFields.size() * 22))));
}
}

/**
* ListCellRenderer to display descriptive text about the access
Expand Down
2 changes: 2 additions & 0 deletions src/com/gitblit/client/RepositoriesPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import com.gitblit.Constants;
import com.gitblit.Constants.RpcRequest;
import com.gitblit.Keys;
import com.gitblit.models.FeedModel;
import com.gitblit.models.RepositoryModel;
import com.gitblit.utils.StringUtils;
Expand Down Expand Up @@ -430,6 +431,7 @@ protected void editRepository(final RepositoryModel repository) {
gitblit.getPreReceiveScriptsInherited(repository), repository.preReceiveScripts);
dialog.setPostReceiveScripts(gitblit.getPostReceiveScriptsUnused(repository),
gitblit.getPostReceiveScriptsInherited(repository), repository.postReceiveScripts);
dialog.setCustomFields(repository, gitblit.getSettings().get(Keys.repository.customFields).getStrings());
dialog.setVisible(true);
final RepositoryModel revisedRepository = dialog.getRepository();
final List<String> permittedUsers = dialog.getPermittedUsers();
Expand Down
2 changes: 2 additions & 0 deletions src/com/gitblit/models/RepositoryModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.FederationStrategy;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel
public List<String> preReceiveScripts;
public List<String> postReceiveScripts;
public List<String> mailingLists;
public Map<String, String> customFields;
private String displayName;

public RepositoryModel() {
Expand Down
2 changes: 2 additions & 0 deletions src/com/gitblit/wicket/GitBlitWebApp.properties
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ gb.mailingLists = mailing lists
gb.preReceiveScripts = pre-receive scripts
gb.postReceiveScripts = post-receive scripts
gb.hookScripts = hook scripts
gb.customFields = custom fields
gb.customFieldsDescription = custom fields available to groovy hooks
gb.accessPermissions = access permissions
gb.filters = filters
gb.generalDescription = common settings
Expand Down
6 changes: 5 additions & 1 deletion src/com/gitblit/wicket/pages/EditRepositoryPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
<tr><td colspan="2"><h3><wicket:message key="gb.hookScripts"></wicket:message> &nbsp;<small><wicket:message key="gb.hookScriptsDescription"></wicket:message></small></h3></td></tr>
<tr><th style="vertical-align: top;"><wicket:message key="gb.preReceiveScripts"></wicket:message><p></p><span wicket:id="inheritedPreReceive"></span></th><td style="padding:2px;"><span wicket:id="preReceiveScripts"></span></td></tr>
<tr><th style="vertical-align: top;"><wicket:message key="gb.postReceiveScripts"></wicket:message><p></p><span wicket:id="inheritedPostReceive"></span></th><td style="padding:2px;"><span wicket:id="postReceiveScripts"></span></td></tr>
<tr><td colspan='2'><div class="form-actions"><input class="btn btn-primary" type="submit" value="Save" wicket:message="value:gb.save" wicket:id="save" tabindex="16" /> &nbsp; <input class="btn" type="submit" value="Cancel" wicket:message="value:gb.cancel" wicket:id="cancel" tabindex="17" /></div></td></tr>
<div wicket:id="customFiledsSection">
<tr><td colspan="2"><h3><wicket:message key="gb.customFields"></wicket:message> &nbsp;<small><wicket:message key="gb.customFieldsDescription"></wicket:message></small></h3></td></tr>
<tr wicket:id="customFieldsListView"><th style="vertical-align: top;"><span wicket:id="customFieldLabel"></span></th><td class="edit"><input class="span8" type="text" wicket:id="customFieldValue" size="30" tabindex="16" /></td></tr>
</div>
<tr><td colspan='2'><div class="form-actions"><input class="btn btn-primary" type="submit" value="Save" wicket:message="value:gb.save" wicket:id="save" tabindex="17" /> &nbsp; <input class="btn" type="submit" value="Cancel" wicket:message="value:gb.cancel" wicket:id="cancel" tabindex="18" /></div></td></tr>
</tbody>
</table>
</form>
Expand Down
48 changes: 46 additions & 2 deletions src/com/gitblit/wicket/pages/EditRepositoryPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,40 @@
package com.gitblit.wicket.pages;

import java.text.MessageFormat;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListItemModel;
import org.apache.wicket.markup.html.list.ListView;
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;

import com.gitblit.Constants;
import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.FederationStrategy;
import com.gitblit.Constants;
import com.gitblit.GitBlit;
import com.gitblit.GitBlitException;
import com.gitblit.Keys;
Expand Down Expand Up @@ -149,6 +156,26 @@ protected void setupPage(final RepositoryModel repositoryModel) {
new ListModel<String>(postReceiveScripts), new CollectionModel<String>(GitBlit
.self().getPostReceiveScriptsUnused(repositoryModel)),
new StringChoiceRenderer(), 12, true);

// Dynamic Custom Defined Properties Properties
final List<Entry<String, String>> definedProperties = new ArrayList<Entry<String, String>>();
List<String> customFields = GitBlit.getStrings(Keys.repository.customFields);
for (String customFieldDef : customFields) {
String[] customFieldProperty = customFieldDef.split("=");
definedProperties.add(new AbstractMap.SimpleEntry<String, String>(customFieldProperty[0], customFieldProperty[1]));
}

final ListView<Entry<String, String>> customFieldsListView = new ListView<Entry<String, String>>("customFieldsListView", definedProperties) {
@Override
protected void populateItem(ListItem<Entry<String, String>> item) {
String value = repositoryModel.customFields.get(item.getModelObject().getKey());

item.add(new Label(item.getModelObject().getKey(), item.getModelObject().getValue())); // Used to get the key later
item.add(new Label("customFieldLabel", item.getModelObject().getValue()));
item.add(new TextField<String>("customFieldValue", new Model<String>(value)));
}
};
customFieldsListView.setReuseItems(true);

CompoundPropertyModel<RepositoryModel> model = new CompoundPropertyModel<RepositoryModel>(
repositoryModel);
Expand Down Expand Up @@ -249,7 +276,16 @@ protected void onSubmit() {
postReceiveScripts.add(post.next());
}
repositoryModel.postReceiveScripts = postReceiveScripts;


// Loop over each of the user defined properties
for (int i = 0; i < customFieldsListView.size(); i++) {
ListItem<ListItemModel<String>> item = (ListItem<ListItemModel<String>>) customFieldsListView.get(i);
String key = item.get(0).getId(); // Item 0 is our 'fake' label
String value = ((TextField<String>)item.get(2)).getValue(); // Item 2 is out text box

repositoryModel.customFields.put(key, value);
}

// save the repository
GitBlit.self().updateRepositoryModel(oldName, repositoryModel, isCreate);

Expand Down Expand Up @@ -334,6 +370,14 @@ protected void onSubmit() {
form.add(postReceivePalette);
form.add(new BulletListPanel("inheritedPostReceive", "inherited", GitBlit.self()
.getPostReceiveScriptsInherited(repositoryModel)));

WebMarkupContainer customFiledsSection = new WebMarkupContainer("customFiledsSection") {
public boolean isVisible() {
return GitBlit.getString(Keys.repository.customFields, "").isEmpty() == false;
};
};
customFiledsSection.add(customFieldsListView);
form.add(customFiledsSection);

form.add(new Button("save"));
Button cancel = new Button("cancel") {
Expand Down
2 changes: 1 addition & 1 deletion tests/com/gitblit/tests/GitBlitSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,
DiffUtilsTest.class, MetricUtilsTest.class, TicgitUtilsTest.class,
GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class,
GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class })
GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class, RepositoryModelTest.class })
public class GitBlitSuite {

public static final File REPOSITORIES = new File("git");
Expand Down
Loading

0 comments on commit 5f22725

Please sign in to comment.