diff options
author | John Crygier <john.crygier@aon.com> | 2012-05-03 11:48:16 -0500 |
---|---|---|
committer | John Crygier <john.crygier@aon.com> | 2012-05-03 11:48:16 -0500 |
commit | cca55e4722fa1ceba8a0933dda974162635f3955 (patch) | |
tree | d72d85b27e591632790e9dd42628354c1147b463 | |
parent | e0ae994e31c3a93a13c7b0141fe37dd1aac9b228 (diff) | |
download | gitblit-cca55e4722fa1ceba8a0933dda974162635f3955.tar.gz gitblit-cca55e4722fa1ceba8a0933dda974162635f3955.zip |
Ability to get / set "custom" properties within a RepositoryModel. This makes getting specialized settings in hooks much easier.
-rw-r--r-- | src/com/gitblit/models/RepositoryModel.java | 43 | ||||
-rw-r--r-- | tests/com/gitblit/tests/GitBlitSuite.java | 2 | ||||
-rw-r--r-- | tests/com/gitblit/tests/RepositoryModelTest.java | 87 |
3 files changed, 131 insertions, 1 deletions
diff --git a/src/com/gitblit/models/RepositoryModel.java b/src/com/gitblit/models/RepositoryModel.java index 324f7d47..fd35f36f 100644 --- a/src/com/gitblit/models/RepositoryModel.java +++ b/src/com/gitblit/models/RepositoryModel.java @@ -20,9 +20,16 @@ import java.util.ArrayList; import java.util.Date;
import java.util.List;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.FederationStrategy;
+import com.gitblit.GitBlit;
import com.gitblit.utils.ArrayUtils;
+import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.StringUtils;
/**
@@ -35,6 +42,11 @@ import com.gitblit.utils.StringUtils; public class RepositoryModel implements Serializable, Comparable<RepositoryModel> {
private static final long serialVersionUID = 1L;
+
+ public static String CUSTOM_DEFINED_PROP_SECTION = "gitblit";
+ public static String CUSTOM_DEFINED_PROP_SUBSECTION = "customDefinedProperties";
+
+ private final Logger logger = LoggerFactory.getLogger(RepositoryModel.class);
// field names are reflectively mapped in EditRepository page
public String name;
@@ -91,6 +103,37 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel }
return localBranches;
}
+
+ public String getCustomProperty(String propertyKey) {
+ try {
+ Repository r = GitBlit.self().getRepository(name);
+ StoredConfig config = JGitUtils.readConfig(r);
+
+ return config.getString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey);
+ } catch (Exception e) {
+ logger.error("Error getting Custom Property", e);
+
+ return null;
+ }
+ }
+
+ public String setCustomProperty(String propertyKey, String propertyValue) {
+ try {
+ Repository r = GitBlit.self().getRepository(name);
+ StoredConfig config = JGitUtils.readConfig(r);
+
+ String oldValue = config.getString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey);
+
+ config.setString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey, propertyValue);
+ config.save();
+
+ return oldValue;
+ } catch (Exception e) {
+ logger.error("Error getting Custom Property", e);
+
+ return null;
+ }
+ }
@Override
public String toString() {
diff --git a/tests/com/gitblit/tests/GitBlitSuite.java b/tests/com/gitblit/tests/GitBlitSuite.java index cab5b639..0cb63ec7 100644 --- a/tests/com/gitblit/tests/GitBlitSuite.java +++ b/tests/com/gitblit/tests/GitBlitSuite.java @@ -53,7 +53,7 @@ import com.gitblit.utils.JGitUtils; 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");
diff --git a/tests/com/gitblit/tests/RepositoryModelTest.java b/tests/com/gitblit/tests/RepositoryModelTest.java new file mode 100644 index 00000000..00bf0d0a --- /dev/null +++ b/tests/com/gitblit/tests/RepositoryModelTest.java @@ -0,0 +1,87 @@ +package com.gitblit.tests; + +import static org.junit.Assert.*; + +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.StoredConfig; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.gitblit.GitBlit; +import com.gitblit.models.RepositoryModel; +import com.gitblit.utils.JGitUtils; + +public class RepositoryModelTest { + + private static String oldSection; + private static String oldSubSection; + private static boolean wasStarted = false; + + @BeforeClass + public static void startGitBlit() throws Exception { + wasStarted = GitBlitSuite.startGitblit() == false; + + oldSection = RepositoryModel.CUSTOM_DEFINED_PROP_SECTION; + oldSubSection = RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION; + + RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = "RepositoryModelTest"; + RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = "RepositoryModelTestSubSection"; + } + + @AfterClass + public static void stopGitBlit() throws Exception { + if (wasStarted == false) + GitBlitSuite.stopGitblit(); + + RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = oldSection; + RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = oldSubSection; + } + + @Before + public void initializeConfiguration() throws Exception{ + Repository r = GitBlitSuite.getHelloworldRepository(); + StoredConfig config = JGitUtils.readConfig(r); + + config.unsetSection(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION); + config.setString(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION, "commitMessageRegEx", "\\d"); + config.setString(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION, "anotherProperty", "Hello"); + + config.save(); + } + + @After + public void teardownConfiguration() throws Exception { + Repository r = GitBlitSuite.getHelloworldRepository(); + StoredConfig config = JGitUtils.readConfig(r); + + config.unsetSection(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION); + config.save(); + } + + @Test + public void testGetCustomProperty() throws Exception { + RepositoryModel model = GitBlit.self().getRepositoryModel( + GitBlitSuite.getHelloworldRepository().getDirectory().getName()); + + assertEquals("\\d", model.getCustomProperty("commitMessageRegEx")); + assertEquals("Hello", model.getCustomProperty("anotherProperty")); + } + + @Test + public void testSetCustomProperty() throws Exception { + RepositoryModel model = GitBlit.self().getRepositoryModel( + GitBlitSuite.getHelloworldRepository().getDirectory().getName()); + + assertEquals("\\d", model.getCustomProperty("commitMessageRegEx")); + assertEquals("Hello", model.getCustomProperty("anotherProperty")); + + assertEquals("Hello", model.setCustomProperty("anotherProperty", "GoodBye")); + + assertEquals("\\d", model.getCustomProperty("commitMessageRegEx")); + assertEquals("GoodBye", model.getCustomProperty("anotherProperty")); + } + +} |