]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2907 Fix loading of settings from database, when used property 'sonar.branch'
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 17 Jan 2012 15:21:43 +0000 (19:21 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 18 Jan 2012 11:28:20 +0000 (15:28 +0400)
sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java
sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java

index 292f8ce155c80a4152328a48552c6e6a293a7c67..dc605219b8833d07e7689ae2b718462b8ffbac2b 100644 (file)
@@ -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();
   }
index 1e45502a78b3a4d5260d4fecd06ea59c5035eb64..31c668a8cd45ac3ddd9fba44025a23356f9de608 100644 (file)
@@ -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<Property> props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey());
+    List<Property> props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey(), branch);
     for (Property dbProperty : props) {
       setProperty(dbProperty.getKey(), dbProperty.getValue());
     }
index 71b59356fd66e848bb0ee6c3caf18bddceffcaec..91c03732bb250338a00fca499cb310e878700026 100644 (file)
@@ -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<Property> getProjectProperties(DatabaseSessionFactory dbFactory, String moduleKey) {
+  public static List<Property> 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")