From cf0426185f6b1048e83656e9a43caebfc783058f Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Tue, 17 Jan 2012 19:21:43 +0400 Subject: [PATCH] SONAR-2907 Fix loading of settings from database, when used property 'sonar.branch' --- .../batch/config/BatchSettingsEnhancer.java | 4 ++-- .../org/sonar/batch/config/ProjectSettings.java | 16 ++++++++++++---- .../sonar/core/config/ConfigurationUtils.java | 11 +++++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java b/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java index 292f8ce155c..dc605219b88 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java @@ -19,6 +19,7 @@ */ package org.sonar.batch.config; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.database.configuration.Property; import org.sonar.core.config.ConfigurationUtils; @@ -42,8 +43,7 @@ public final class BatchSettingsEnhancer { } public void start() { - String projectKey = reactor.getRoot().getKey(); - setIfNotDefined(ConfigurationUtils.getProjectProperties(dbFactory, projectKey)); + setIfNotDefined(ConfigurationUtils.getProjectProperties(dbFactory, reactor.getRoot().getKey(), settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY))); setIfNotDefined(ConfigurationUtils.getGeneralProperties(dbFactory)); settings.updateDeprecatedCommonsConfiguration(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java b/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java index 1e45502a78b..31c668a8cd4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java @@ -21,6 +21,7 @@ package org.sonar.batch.config; import com.google.common.collect.Lists; import org.apache.commons.configuration.Configuration; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; @@ -51,9 +52,16 @@ public class ProjectSettings extends Settings { public ProjectSettings load() { clear(); + // hack to obtain "sonar.branch" before loading settings from database + loadBuildProperties(); + addEnvironmentVariables(); + addSystemProperties(); + String branch = getString(CoreProperties.PROJECT_BRANCH_PROPERTY); + clear(); + // order is important -> bottom-up. The last one overrides all the others. loadDatabaseGlobalSettings(); - loadDatabaseProjectSettings(projectDefinition); + loadDatabaseProjectSettings(projectDefinition, branch); loadBuildProperties(); addEnvironmentVariables(); addSystemProperties(); @@ -70,11 +78,11 @@ public class ProjectSettings extends Settings { } } - private void loadDatabaseProjectSettings(ProjectDefinition projectDef) { + private void loadDatabaseProjectSettings(ProjectDefinition projectDef, String branch) { if (projectDef.getParent() != null) { - loadDatabaseProjectSettings(projectDef.getParent()); + loadDatabaseProjectSettings(projectDef.getParent(), branch); } - List props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey()); + List props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey(), branch); for (Property dbProperty : props) { setProperty(dbProperty.getKey(), dbProperty.getValue()); } diff --git a/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java b/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java index 71b59356fd6..91c03732bb2 100644 --- a/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java @@ -22,6 +22,7 @@ package org.sonar.core.config; import org.apache.commons.configuration.Configuration; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.text.StrSubstitutor; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.configuration.Property; @@ -84,9 +85,15 @@ public final class ConfigurationUtils { return result; } - public static List getProjectProperties(DatabaseSessionFactory dbFactory, String moduleKey) { + public static List getProjectProperties(DatabaseSessionFactory dbFactory, String moduleKey, String branch) { + final String completeKey; + if (StringUtils.isNotBlank(branch)) { + completeKey = String.format("%s:%s", moduleKey, branch); + } else { + completeKey = moduleKey; + } DatabaseSession session = prepareDbSession(dbFactory); - ResourceModel resource = session.getSingleResult(ResourceModel.class, "key", moduleKey); + ResourceModel resource = session.getSingleResult(ResourceModel.class, "key", completeKey); if (resource != null) { return session .createQuery("from " + Property.class.getSimpleName() + " p where p.resourceId=:resourceId and p.userId is null") -- 2.39.5