From cca55e4722fa1ceba8a0933dda974162635f3955 Mon Sep 17 00:00:00 2001
From: John Crygier <john.crygier@aon.com>
Date: Thu, 03 May 2012 12:48:16 -0400
Subject: [PATCH] Ability to get / set "custom" properties within a RepositoryModel.  This makes getting specialized settings in hooks much easier.

---
 tests/com/gitblit/tests/RepositoryModelTest.java |   87 +++++++++++++++++++++++++++++
 src/com/gitblit/models/RepositoryModel.java      |   43 ++++++++++++++
 tests/com/gitblit/tests/GitBlitSuite.java        |    2 
 3 files changed, 131 insertions(+), 1 deletions(-)

diff --git a/src/com/gitblit/models/RepositoryModel.java b/src/com/gitblit/models/RepositoryModel.java
index 324f7d4..fd35f36 100644
--- a/src/com/gitblit/models/RepositoryModel.java
+++ b/src/com/gitblit/models/RepositoryModel.java
@@ -20,9 +20,16 @@
 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 @@
 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 @@
 		}
 		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 cab5b63..0cb63ec 100644
--- a/tests/com/gitblit/tests/GitBlitSuite.java
+++ b/tests/com/gitblit/tests/GitBlitSuite.java
@@ -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");
diff --git a/tests/com/gitblit/tests/RepositoryModelTest.java b/tests/com/gitblit/tests/RepositoryModelTest.java
new file mode 100644
index 0000000..00bf0d0
--- /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"));
+	}
+
+}

--
Gitblit v1.9.1