From 7c1cdc8b2f3686626de0f1a523965f5555f469d7 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 10 May 2012 17:38:25 -0400 Subject: [PATCH] Revisions to custom fields --- docs/01_setup.mkd | 10 ++++ groovy/blockpush.groovy | 3 + groovy/jenkins.groovy | 3 + groovy/localclone.groovy | 3 + groovy/protect-refs.groovy | 3 + groovy/sendmail.groovy | 3 + src/com/gitblit/GitBlit.java | 14 ++++- src/com/gitblit/IStoredSettings.java | 18 ++++++ .../gitblit/wicket/GitBlitWebApp.properties | 2 +- .../wicket/GitBlitWebApp_ja.properties | 2 + .../wicket/pages/EditRepositoryPage.html | 6 +- .../wicket/pages/EditRepositoryPage.java | 55 +++++++++---------- 12 files changed, 89 insertions(+), 33 deletions(-) diff --git a/docs/01_setup.mkd b/docs/01_setup.mkd index 68c10255..c8cd09db 100644 --- a/docs/01_setup.mkd +++ b/docs/01_setup.mkd @@ -398,6 +398,16 @@ Some sample scripts are included in the GO and WAR distributions to show you how Hook contributions and improvements are welcome. +### Custom Fields + +*SINCE 1.0.0* + +Gitblit allows custom repository string fields to be defined in `gitblit.properties` or `web.xml`. Entry textfields are automatically created for these fields in the Edit Repository page of Gitblit and the Edit Repository dialog of the Gitblit Manager. These fields are accessible from your Groovy hook scripts as + + repository.customFields.myField + +This feature allows you to customize the behavior of your hook scripts without hard-coding values in the hook scripts themselves. + ### Pre-Receive Pre-Receive scripts execute after the pushed objects have all been written to the Git repository but before the refs have been updated to point to these new objects. diff --git a/groovy/blockpush.groovy b/groovy/blockpush.groovy index 186b2427..d0e2c2e0 100644 --- a/groovy/blockpush.groovy +++ b/groovy/blockpush.groovy @@ -58,6 +58,9 @@ import com.gitblit.utils.ClientLogger * url Base url for Gitblit String * logger Logs messages to Gitblit org.slf4j.Logger * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger + * + * Accessing Gitblit Custom Fields: + * def myCustomField = repository.customFields.myCustomField * */ diff --git a/groovy/jenkins.groovy b/groovy/jenkins.groovy index 41083d24..73360581 100644 --- a/groovy/jenkins.groovy +++ b/groovy/jenkins.groovy @@ -53,6 +53,9 @@ import org.slf4j.Logger * url Base url for Gitblit String * logger Logs messages to Gitblit org.slf4j.Logger * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger + * + * Accessing Gitblit Custom Fields: + * def myCustomField = repository.customFields.myCustomField * */ // Indicate we have started the script diff --git a/groovy/localclone.groovy b/groovy/localclone.groovy index e1d44938..11868d71 100644 --- a/groovy/localclone.groovy +++ b/groovy/localclone.groovy @@ -63,6 +63,9 @@ import org.slf4j.Logger * url Base url for Gitblit String * logger Logs messages to Gitblit org.slf4j.Logger * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger + * + * Accessing Gitblit Custom Fields: + * def myCustomField = repository.customFields.myCustomField * */ diff --git a/groovy/protect-refs.groovy b/groovy/protect-refs.groovy index 48ac60f1..f730bcb2 100644 --- a/groovy/protect-refs.groovy +++ b/groovy/protect-refs.groovy @@ -64,6 +64,9 @@ import org.slf4j.Logger * url Base url for Gitblit String * logger Logs messages to Gitblit org.slf4j.Logger * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger + * + * Accessing Gitblit Custom Fields: + * def myCustomField = repository.customFields.myCustomField * */ diff --git a/groovy/sendmail.groovy b/groovy/sendmail.groovy index 888f6643..8d223ef5 100644 --- a/groovy/sendmail.groovy +++ b/groovy/sendmail.groovy @@ -59,6 +59,9 @@ import org.slf4j.Logger * url Base url for Gitblit String * logger Logs messages to Gitblit org.slf4j.Logger * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger + * + * Accessing Gitblit Custom Fields: + * def myCustomField = repository.customFields.myCustomField * */ diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java index 57421a31..a86cfd7f 100644 --- a/src/com/gitblit/GitBlit.java +++ b/src/com/gitblit/GitBlit.java @@ -30,6 +30,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -254,6 +255,17 @@ public class GitBlit implements ServletContextListener { return self().settings.getStrings(key); } + /** + * Returns a map of space-separated key-value pairs from the specified key. + * + * @see IStoredSettings.getStrings(String key) + * @param name + * @return map of string, string + */ + public static Map getMap(String key) { + return self().settings.getMap(key); + } + /** * Returns the list of keys whose name starts with the specified prefix. If * the prefix is null or empty, all key names are returned. @@ -859,7 +871,7 @@ public class GitBlit implements ServletContextListener { Constants.CONFIG_GITBLIT, null, "indexBranch"))); // Custom defined properties - model.customFields = new HashMap(); + model.customFields = new LinkedHashMap(); for (String aProperty : config.getNames(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS)) { model.customFields.put(aProperty, config.getString(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS, aProperty)); } diff --git a/src/com/gitblit/IStoredSettings.java b/src/com/gitblit/IStoredSettings.java index 2f45f09d..e0600919 100644 --- a/src/com/gitblit/IStoredSettings.java +++ b/src/com/gitblit/IStoredSettings.java @@ -16,6 +16,7 @@ package com.gitblit; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Properties; @@ -203,6 +204,23 @@ public abstract class IStoredSettings { } return strings; } + + /** + * Returns a map of strings from the specified key. + * + * @param name + * @return map of string, string + */ + public Map getMap(String name) { + Map map = new LinkedHashMap(); + for (String string : getStrings(name)) { + String[] kvp = string.split("=", 2); + String key = kvp[0]; + String value = kvp[1]; + map.put(key, value); + } + return map; + } /** * Override the specified key with the specified value. diff --git a/src/com/gitblit/wicket/GitBlitWebApp.properties b/src/com/gitblit/wicket/GitBlitWebApp.properties index dfbdd4bf..83b716af 100644 --- a/src/com/gitblit/wicket/GitBlitWebApp.properties +++ b/src/com/gitblit/wicket/GitBlitWebApp.properties @@ -203,7 +203,7 @@ 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.customFieldsDescription = custom fields available to Groovy hooks gb.accessPermissions = access permissions gb.filters = filters gb.generalDescription = common settings diff --git a/src/com/gitblit/wicket/GitBlitWebApp_ja.properties b/src/com/gitblit/wicket/GitBlitWebApp_ja.properties index 7e50bd8d..d73b37ea 100755 --- a/src/com/gitblit/wicket/GitBlitWebApp_ja.properties +++ b/src/com/gitblit/wicket/GitBlitWebApp_ja.properties @@ -202,6 +202,8 @@ gb.mailingLists = \u30e1\u30fc\u30ea\u30f3\u30b0\u30ea\u30b9\u30c8 gb.preReceiveScripts = pre-receive \u30b9\u30af\u30ea\u30d7\u30c8 gb.postReceiveScripts = post-receive \u30b9\u30af\u30ea\u30d7\u30c8 gb.hookScripts = \u30d5\u30c3\u30af\u30b9\u30af\u30ea\u30d7\u30c8 +gb.customFields = custom fields +gb.customFieldsDescription = custom fields available to Groovy hooks gb.accessPermissions = \u30a2\u30af\u30bb\u30b9\u6a29\u9650 gb.filters = \u30d5\u30a3\u30eb\u30bf\u30fc gb.generalDescription = \u4e00\u822c\u7684\u306a\u8a2d\u5b9a diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.html b/src/com/gitblit/wicket/pages/EditRepositoryPage.html index 07c55226..a419698d 100644 --- a/src/com/gitblit/wicket/pages/EditRepositoryPage.html +++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.html @@ -36,11 +36,11 @@

 

-
+

 

- +
-
 
+
 
diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java index f3e2c386..e057e2a2 100644 --- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -16,15 +16,14 @@ 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.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import org.apache.wicket.PageParameters; @@ -39,7 +38,6 @@ 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; @@ -157,22 +155,24 @@ public class EditRepositoryPage extends RootSubPage { .self().getPostReceiveScriptsUnused(repositoryModel)), new StringChoiceRenderer(), 12, true); - // Dynamic Custom Defined Properties Properties - final List> definedProperties = new ArrayList>(); - List customFields = GitBlit.getStrings(Keys.repository.customFields); - for (String customFieldDef : customFields) { - String[] customFieldProperty = customFieldDef.split("="); - definedProperties.add(new AbstractMap.SimpleEntry(customFieldProperty[0], customFieldProperty[1])); - } - - final ListView> customFieldsListView = new ListView>("customFieldsListView", definedProperties) { + // custom fields + final Map customFieldsMap = GitBlit.getMap(Keys.groovy.customFields); + List customKeys = new ArrayList(customFieldsMap.keySet()); + final ListView customFieldsListView = new ListView("customFieldsListView", customKeys) { + + private static final long serialVersionUID = 1L; + @Override - protected void populateItem(ListItem> item) { - String value = repositoryModel.customFields.get(item.getModelObject().getKey()); + protected void populateItem(ListItem item) { + String key = item.getModelObject(); + item.add(new Label("customFieldLabel", customFieldsMap.get(key))); - 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("customFieldValue", new Model(value))); + String value = ""; + if (repositoryModel.customFields != null && repositoryModel.customFields.containsKey(key)) { + value = repositoryModel.customFields.get(key); + } + TextField field = new TextField("customFieldValue", new Model(value)); + item.add(field); } }; customFieldsListView.setReuseItems(true); @@ -277,11 +277,14 @@ public class EditRepositoryPage extends RootSubPage { } repositoryModel.postReceiveScripts = postReceiveScripts; - // Loop over each of the user defined properties + // custom fields + repositoryModel.customFields = new LinkedHashMap(); for (int i = 0; i < customFieldsListView.size(); i++) { - ListItem> item = (ListItem>) customFieldsListView.get(i); - String key = item.get(0).getId(); // Item 0 is our 'fake' label - String value = ((TextField)item.get(2)).getValue(); // Item 2 is out text box + ListItem child = (ListItem) customFieldsListView.get(i); + String key = child.getModelObject(); + + TextField field = (TextField) child.get("customFieldValue"); + String value = field.getValue(); repositoryModel.customFields.put(key, value); } @@ -371,13 +374,9 @@ public class EditRepositoryPage extends RootSubPage { 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); + WebMarkupContainer customFieldsSection = new WebMarkupContainer("customFieldsSection"); + customFieldsSection.add(customFieldsListView); + form.add(customFieldsSection.setVisible(!GitBlit.getString(Keys.groovy.customFields, "").isEmpty())); form.add(new Button("save")); Button cancel = new Button("cancel") { -- 2.39.5