Skip to content

Commit

Permalink
SONAR-6621 Drop support for property sonar.qualitygate to override Qu…
Browse files Browse the repository at this point in the history
…ality Gate
  • Loading branch information
henryju committed Oct 6, 2015
1 parent 41c4f76 commit 7aaa46f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 65 deletions.
Expand Up @@ -20,27 +20,27 @@
package org.sonar.batch.bootstrap; package org.sonar.batch.bootstrap;


import java.util.Map; import java.util.Map;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Loggers;


import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;


public class DroppedPropertyChecker { public class DroppedPropertyChecker {
private final Settings settings;
private final Logger logger; private static final Logger LOG = Loggers.get(DroppedPropertyChecker.class);

private final Map<String, String> settings;
private final Map<String, String> properties; private final Map<String, String> properties;


public DroppedPropertyChecker(Settings settings, Map<String, String> properties) { public DroppedPropertyChecker(Map<String, String> properties, Map<String, String> droppedPropertiesAndMsg) {
this.settings = requireNonNull(settings); this.settings = requireNonNull(properties);
this.logger = Loggers.get(settings.getClass()); this.properties = requireNonNull(droppedPropertiesAndMsg);
this.properties = requireNonNull(properties);
} }


public void checkDroppedProperties() { public void checkDroppedProperties() {
for (Map.Entry<String, String> entry : properties.entrySet()) { for (Map.Entry<String, String> entry : properties.entrySet()) {
if (settings.hasKey(entry.getKey())) { if (settings.containsKey(entry.getKey())) {
logger.warn("Property '{}' is not supported any more. {}", entry.getKey(), entry.getValue()); LOG.warn("Property '{}' is not supported any more. {}", entry.getKey(), entry.getValue());
} }
} }
} }
Expand Down
Expand Up @@ -56,7 +56,7 @@ public GlobalSettings(GlobalProperties bootstrapProps, PropertyDefinitions prope
this.bootstrapProps = bootstrapProps; this.bootstrapProps = bootstrapProps;
this.globalReferentials = globalReferentials; this.globalReferentials = globalReferentials;
init(); init();
new DroppedPropertyChecker(this, DROPPED_PROPERTIES).checkDroppedProperties(); new DroppedPropertyChecker(this.getProperties(), DROPPED_PROPERTIES).checkDroppedProperties();
} }


private void init() { private void init() {
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.sonar.batch.scan; package org.sonar.batch.scan;


import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler; import org.sonar.api.utils.log.Profiler;
import org.sonar.batch.analysis.AnalysisProperties; import org.sonar.batch.analysis.AnalysisProperties;
import org.sonar.batch.bootstrap.DroppedPropertyChecker;
import org.sonar.batch.util.BatchUtils; import org.sonar.batch.util.BatchUtils;


/** /**
Expand All @@ -58,6 +60,13 @@ public class ProjectReactorBuilder {


private static final String INVALID_VALUE_OF_X_FOR_Y = "Invalid value of {0} for {1}"; private static final String INVALID_VALUE_OF_X_FOR_Y = "Invalid value of {0} for {1}";


/**
* A map of dropped properties as key and specific message to display for that property
* (what will happen, what should the user do, ...) as a value
*/
private static final Map<String, String> DROPPED_PROPERTIES = ImmutableMap.of(
"sonar.qualitygate", "It will be ignored.");

private static final Logger LOG = Loggers.get(ProjectReactorBuilder.class); private static final Logger LOG = Loggers.get(ProjectReactorBuilder.class);


/** /**
Expand Down Expand Up @@ -96,7 +105,7 @@ public class ProjectReactorBuilder {
/** /**
* Array of all mandatory properties required for a child project before its properties get merged with its parent ones. * Array of all mandatory properties required for a child project before its properties get merged with its parent ones.
*/ */
protected static final String[] MANDATORY_PROPERTIES_FOR_CHILD = {MODULE_KEY_PROPERTY, CoreProperties.PROJECT_NAME_PROPERTY}; private static final String[] MANDATORY_PROPERTIES_FOR_CHILD = {MODULE_KEY_PROPERTY, CoreProperties.PROJECT_NAME_PROPERTY};


/** /**
* Properties that must not be passed from the parent project to its children. * Properties that must not be passed from the parent project to its children.
Expand All @@ -106,25 +115,26 @@ public class ProjectReactorBuilder {


private static final String NON_ASSOCIATED_PROJECT_KEY = "project"; private static final String NON_ASSOCIATED_PROJECT_KEY = "project";


private final AnalysisProperties taskProps; private final AnalysisProperties analysisProps;
private final AnalysisMode analysisMode; private final AnalysisMode analysisMode;
private File rootProjectWorkDir; private File rootProjectWorkDir;


public ProjectReactorBuilder(AnalysisProperties props, AnalysisMode analysisMode) { public ProjectReactorBuilder(AnalysisProperties props, AnalysisMode analysisMode) {
this.taskProps = props; this.analysisProps = props;
this.analysisMode = analysisMode; this.analysisMode = analysisMode;
} }


public ProjectReactor execute() { public ProjectReactor execute() {
Profiler profiler = Profiler.create(LOG).startInfo("Process project properties"); Profiler profiler = Profiler.create(LOG).startInfo("Process project properties");
new DroppedPropertyChecker(analysisProps.properties(), DROPPED_PROPERTIES).checkDroppedProperties();
Map<String, Map<String, String>> propertiesByModuleIdPath = new HashMap<>(); Map<String, Map<String, String>> propertiesByModuleIdPath = new HashMap<>();
extractPropertiesByModule(propertiesByModuleIdPath, "", "", taskProps.properties()); extractPropertiesByModule(propertiesByModuleIdPath, "", "", analysisProps.properties());
ProjectDefinition rootProject = defineRootProject(propertiesByModuleIdPath.get(""), null); ProjectDefinition rootProject = defineRootProject(propertiesByModuleIdPath.get(""), null);
rootProjectWorkDir = rootProject.getWorkDir(); rootProjectWorkDir = rootProject.getWorkDir();
defineChildren(rootProject, propertiesByModuleIdPath, ""); defineChildren(rootProject, propertiesByModuleIdPath, "");
cleanAndCheckProjectDefinitions(rootProject); cleanAndCheckProjectDefinitions(rootProject);
// Since task properties are now empty we should add root module properties // Since task properties are now empty we should add root module properties
taskProps.properties().putAll(propertiesByModuleIdPath.get("")); analysisProps.properties().putAll(propertiesByModuleIdPath.get(""));
profiler.stopDebug(); profiler.stopDebug();
return new ProjectReactor(rootProject); return new ProjectReactor(rootProject);
} }
Expand Down Expand Up @@ -403,7 +413,7 @@ protected static void cleanAndCheckAggregatorProjectProperties(ProjectDefinition
@VisibleForTesting @VisibleForTesting
protected static void mergeParentProperties(Map<String, String> childProps, Map<String, String> parentProps) { protected static void mergeParentProperties(Map<String, String> childProps, Map<String, String> parentProps) {
for (Map.Entry<String, String> entry : parentProps.entrySet()) { for (Map.Entry<String, String> entry : parentProps.entrySet()) {
String key = (String) entry.getKey(); String key = entry.getKey();
if ((!childProps.containsKey(key) || childProps.get(key).equals(entry.getValue())) if ((!childProps.containsKey(key) || childProps.get(key).equals(entry.getValue()))
&& !NON_HERITED_PROPERTIES_FOR_CHILD.contains(key)) { && !NON_HERITED_PROPERTIES_FOR_CHILD.contains(key)) {
childProps.put(entry.getKey(), entry.getValue()); childProps.put(entry.getKey(), entry.getValue());
Expand Down
Expand Up @@ -19,31 +19,17 @@
*/ */
package org.sonar.batch.scan; package org.sonar.batch.scan;


import org.sonar.batch.repository.ProjectRepositories;

import org.sonar.batch.analysis.DefaultAnalysisMode;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

import org.sonar.api.CoreProperties; import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings; import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException; import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.DroppedPropertyChecker; import org.sonar.batch.analysis.DefaultAnalysisMode;
import org.sonar.batch.bootstrap.GlobalSettings; import org.sonar.batch.bootstrap.GlobalSettings;
import org.sonar.batch.repository.ProjectRepositories;


public class ProjectSettings extends Settings { public class ProjectSettings extends Settings {


/**
* A map of dropped properties as key and specific message to display for that property
* (what will happen, what should the user do, ...) as a value
*/
private static final Map<String, String> DROPPED_PROPERTIES = ImmutableMap.of(
"sonar.qualitygate", "It will be ignored."
);

private final GlobalSettings globalSettings; private final GlobalSettings globalSettings;
private final ProjectRepositories projectRepositories; private final ProjectRepositories projectRepositories;
private final DefaultAnalysisMode mode; private final DefaultAnalysisMode mode;
Expand All @@ -56,7 +42,6 @@ public ProjectSettings(ProjectReactor reactor, GlobalSettings globalSettings, Pr
this.globalSettings = globalSettings; this.globalSettings = globalSettings;
this.projectRepositories = projectRepositories; this.projectRepositories = projectRepositories;
init(reactor); init(reactor);
new DroppedPropertyChecker(this, DROPPED_PROPERTIES).checkDroppedProperties();
} }


private void init(ProjectReactor reactor) { private void init(ProjectReactor reactor) {
Expand Down
Expand Up @@ -22,7 +22,6 @@
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel; import org.sonar.api.utils.log.LoggerLevel;


Expand All @@ -38,27 +37,21 @@ public class DroppedPropertyCheckerTest {


@Test @Test
public void no_log_if_no_dropped_property() { public void no_log_if_no_dropped_property() {
Settings settings = new Settings(); new DroppedPropertyChecker(ImmutableMap.of(DROPPED_PROPERTY_1, SOME_VALUE), ImmutableMap.<String, String>of()).checkDroppedProperties();
settings.setProperty(DROPPED_PROPERTY_1, SOME_VALUE);

new DroppedPropertyChecker(settings, ImmutableMap.<String, String>of()).checkDroppedProperties();


assertThat(logTester.logs()).isEmpty(); assertThat(logTester.logs()).isEmpty();
} }


@Test @Test
public void no_log_if_settings_does_not_contain_any_dropped_property() { public void no_log_if_settings_does_not_contain_any_dropped_property() {
new DroppedPropertyChecker(new Settings(), ImmutableMap.of(DROPPED_PROPERTY_1, DROPPED_PROPERTY_MSG_1)).checkDroppedProperties(); new DroppedPropertyChecker(ImmutableMap.<String, String>of(), ImmutableMap.of(DROPPED_PROPERTY_1, DROPPED_PROPERTY_MSG_1)).checkDroppedProperties();


assertThat(logTester.logs()).isEmpty(); assertThat(logTester.logs()).isEmpty();
} }


@Test @Test
public void warn_log_if_settings_contains_any_dropped_property() { public void warn_log_if_settings_contains_any_dropped_property() {
Settings settings = new Settings(); new DroppedPropertyChecker(ImmutableMap.of(DROPPED_PROPERTY_1, SOME_VALUE), ImmutableMap.of(DROPPED_PROPERTY_1, DROPPED_PROPERTY_MSG_1)).checkDroppedProperties();
settings.setProperty(DROPPED_PROPERTY_1, SOME_VALUE);

new DroppedPropertyChecker(settings, ImmutableMap.of(DROPPED_PROPERTY_1, DROPPED_PROPERTY_MSG_1)).checkDroppedProperties();


assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Property '" + DROPPED_PROPERTY_1 + "' is not supported any more. " + DROPPED_PROPERTY_MSG_1); assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Property '" + DROPPED_PROPERTY_1 + "' is not supported any more. " + DROPPED_PROPERTY_MSG_1);
Expand Down
Expand Up @@ -35,6 +35,8 @@
import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.batch.analysis.AnalysisProperties; import org.sonar.batch.analysis.AnalysisProperties;
import org.sonar.test.TestUtils; import org.sonar.test.TestUtils;


Expand All @@ -47,6 +49,9 @@ public class ProjectReactorBuilderTest {
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();


@Rule
public LogTester logTester = new LogTester();

private AnalysisMode mode; private AnalysisMode mode;


@Before @Before
Expand Down Expand Up @@ -688,6 +693,16 @@ public void doNotMixPropertiesWhenModuleKeyIsPrefixOfAnother() throws IOExceptio
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_com.foo.project.module1.feature")); .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_com.foo.project.module1.feature"));
} }


@Test
public void should_log_a_warning_when_a_dropped_property_is_present() {
Map<String, String> props = loadProps("simple-project");
props.put("sonar.qualitygate", "somevalue");
AnalysisProperties bootstrapProps = new AnalysisProperties(props, null);
new ProjectReactorBuilder(bootstrapProps, mode).execute();

assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Property 'sonar.qualitygate' is not supported any more. It will be ignored.");
}

private Map<String, String> loadPropsFromFile(String filePath) throws IOException { private Map<String, String> loadPropsFromFile(String filePath) throws IOException {
Properties props = new Properties(); Properties props = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath))) { try (FileInputStream fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath))) {
Expand Down
Expand Up @@ -19,18 +19,10 @@
*/ */
package org.sonar.batch.scan; package org.sonar.batch.scan;


import org.sonar.batch.repository.FileData;

import com.google.common.collect.HashBasedTable; import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.ImmutableTable; import com.google.common.collect.ImmutableTable;
import org.sonar.batch.repository.ProjectRepositories; import com.google.common.collect.Table;
import org.sonar.batch.analysis.DefaultAnalysisMode;
import org.sonar.batch.bootstrap.GlobalMode;
import com.google.common.collect.ImmutableMap;

import java.util.Collections; import java.util.Collections;

import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
Expand All @@ -41,10 +33,14 @@
import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.utils.MessageException; import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel; import org.sonar.batch.analysis.DefaultAnalysisMode;
import org.sonar.batch.bootstrap.GlobalMode;
import org.sonar.batch.bootstrap.GlobalProperties; import org.sonar.batch.bootstrap.GlobalProperties;
import org.sonar.batch.bootstrap.GlobalSettings; import org.sonar.batch.bootstrap.GlobalSettings;
import org.sonar.batch.protocol.input.GlobalRepositories; import org.sonar.batch.protocol.input.GlobalRepositories;
import org.sonar.batch.repository.FileData;
import org.sonar.batch.repository.ProjectRepositories;

import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -138,19 +134,9 @@ public void should_fail_when_accessing_secured_properties_in_issues_mode() {
assertThat(batchSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2"); assertThat(batchSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2");
thrown.expect(MessageException.class); thrown.expect(MessageException.class);
thrown thrown
.expectMessage("Access to the secured property 'sonar.foo.secured' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); .expectMessage(
"Access to the secured property 'sonar.foo.secured' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode.");
batchSettings.getString("sonar.foo.secured"); batchSettings.getString("sonar.foo.secured");
} }


@Test
public void should_log_a_warning_when_a_dropper_property_is_present() {
GlobalSettings settings = new GlobalSettings(new GlobalProperties(ImmutableMap.of("sonar.qualitygate", "somevalue")), new PropertyDefinitions(), new GlobalRepositories(),
globalMode);
projectRef = new ProjectRepositories(emptySettings, emptyFileData, null);
new ProjectSettings(new ProjectReactor(project), settings, new PropertyDefinitions(), projectRef, mode);

assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Property 'sonar.qualitygate' is not supported any more. It will be ignored.");

}

} }

0 comments on commit 7aaa46f

Please sign in to comment.