John Crygier
2012-05-04 4a5a55229bf066bf933dde6cb6f51a4378d67fb7
Change techique for custom properties to fall in line with reading / writing of all other properties
4 files modified
101 ■■■■■ changed files
src/com/gitblit/Constants.java 4 ●●●● patch | view | raw | blame | history
src/com/gitblit/GitBlit.java 11 ●●●●● patch | view | raw | blame | history
src/com/gitblit/models/RepositoryModel.java 45 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/RepositoryModelTest.java 41 ●●●●● patch | view | raw | blame | history
src/com/gitblit/Constants.java
@@ -72,6 +72,10 @@
    
    public static final String DEFAULT_BRANCH = "default";
    
    public static String CUSTOM_DEFINED_PROP_SECTION = "gitblit";
    public static String CUSTOM_DEFINED_PROP_SUBSECTION = "customDefinedProperties";
    public static String getGitBlitVersion() {
        return NAME + " v" + VERSION;
    }
src/com/gitblit/GitBlit.java
@@ -857,6 +857,12 @@
                    "gitblit", null, "mailingList")));
            model.indexedBranches = new ArrayList<String>(Arrays.asList(config.getStringList(
                    "gitblit", null, "indexBranch")));
            // Custom defined properties
            model.userDefinedProperties = new HashMap<String, String>();
            for (String aProperty : config.getNames(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION)) {
                model.userDefinedProperties.put(aProperty, config.getString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, aProperty));
            }
        }
        model.HEAD = JGitUtils.getHEADRef(r);
        model.availableRefs = JGitUtils.getAvailableHeadTargets(r);
@@ -1104,6 +1110,11 @@
        updateList(config, "mailingList", repository.mailingLists);
        updateList(config, "indexBranch", repository.indexedBranches);
        // User Defined Properties
        for (Entry<String, String> singleProperty : repository.userDefinedProperties.entrySet()) {
            config.setString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, singleProperty.getKey(), singleProperty.getValue());
        }
        try {
            config.save();
        } catch (IOException e) {
src/com/gitblit/models/RepositoryModel.java
@@ -19,17 +19,11 @@
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 java.util.Map;
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;
/**
@@ -42,11 +36,6 @@
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;
@@ -75,6 +64,7 @@
    public List<String> preReceiveScripts;
    public List<String> postReceiveScripts;
    public List<String> mailingLists;
    public Map<String, String> userDefinedProperties;
    private String displayName;
    
    public RepositoryModel() {
@@ -102,37 +92,6 @@
            }
        }
        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
tests/com/gitblit/tests/RepositoryModelTest.java
@@ -1,6 +1,6 @@
package com.gitblit.tests;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
@@ -10,6 +10,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
import com.gitblit.Constants;
import com.gitblit.GitBlit;
import com.gitblit.models.RepositoryModel;
import com.gitblit.utils.JGitUtils;
@@ -24,11 +25,11 @@
    public static void startGitBlit() throws Exception {
        wasStarted = GitBlitSuite.startGitblit() == false;
        
        oldSection = RepositoryModel.CUSTOM_DEFINED_PROP_SECTION;
        oldSubSection = RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION;
        oldSection = Constants.CUSTOM_DEFINED_PROP_SECTION;
        oldSubSection = Constants.CUSTOM_DEFINED_PROP_SUBSECTION;
        
        RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = "RepositoryModelTest";
        RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = "RepositoryModelTestSubSection";
        Constants.CUSTOM_DEFINED_PROP_SECTION = "RepositoryModelTest";
        Constants.CUSTOM_DEFINED_PROP_SUBSECTION = "RepositoryModelTestSubSection";
    }
    
    @AfterClass
@@ -36,8 +37,8 @@
        if (wasStarted == false)
            GitBlitSuite.stopGitblit();
        
        RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = oldSection;
        RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = oldSubSection;
        Constants.CUSTOM_DEFINED_PROP_SECTION = oldSection;
        Constants.CUSTOM_DEFINED_PROP_SUBSECTION = oldSubSection;
    }
    
    @Before
@@ -45,9 +46,9 @@
        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.unsetSection(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION);
        config.setString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, "commitMessageRegEx", "\\d");
        config.setString(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION, "anotherProperty", "Hello");
        
        config.save();
    }
@@ -57,7 +58,7 @@
        Repository r = GitBlitSuite.getHelloworldRepository();
        StoredConfig config = JGitUtils.readConfig(r);
        
        config.unsetSection(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION);
        config.unsetSection(Constants.CUSTOM_DEFINED_PROP_SECTION, Constants.CUSTOM_DEFINED_PROP_SUBSECTION);
        config.save();
    }
@@ -66,8 +67,8 @@
        RepositoryModel model = GitBlit.self().getRepositoryModel(
                GitBlitSuite.getHelloworldRepository().getDirectory().getName());
        
        assertEquals("\\d", model.getCustomProperty("commitMessageRegEx"));
        assertEquals("Hello", model.getCustomProperty("anotherProperty"));
        assertEquals("\\d", model.userDefinedProperties.get("commitMessageRegEx"));
        assertEquals("Hello", model.userDefinedProperties.get("anotherProperty"));
    }
    
    @Test
@@ -75,13 +76,17 @@
        RepositoryModel model = GitBlit.self().getRepositoryModel(
                GitBlitSuite.getHelloworldRepository().getDirectory().getName());
        
        assertEquals("\\d", model.getCustomProperty("commitMessageRegEx"));
        assertEquals("Hello", model.getCustomProperty("anotherProperty"));
        assertEquals("\\d", model.userDefinedProperties.get("commitMessageRegEx"));
        assertEquals("Hello", model.userDefinedProperties.get("anotherProperty"));
        
        assertEquals("Hello", model.setCustomProperty("anotherProperty", "GoodBye"));
        assertEquals("Hello", model.userDefinedProperties.put("anotherProperty", "GoodBye"));
        GitBlit.self().updateRepositoryModel(model.name, model, false);
        
        assertEquals("\\d", model.getCustomProperty("commitMessageRegEx"));
        assertEquals("GoodBye", model.getCustomProperty("anotherProperty"));
        model = GitBlit.self().getRepositoryModel(
                GitBlitSuite.getHelloworldRepository().getDirectory().getName());
        assertEquals("\\d", model.userDefinedProperties.get("commitMessageRegEx"));
        assertEquals("GoodBye", model.userDefinedProperties.get("anotherProperty"));
    }
}