*/
package org.sonar.batch.bootstrap;
-import com.google.common.collect.Maps;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONValue;
import java.util.Map;
public class BatchSettings extends Settings {
+ public static final String BATCH_BOOTSTRAP_PROPERTIES_URL = "/batch_bootstrap/properties";
private Configuration deprecatedConfiguration;
private boolean dryRun;
- // Keep module settings for initialization of ProjectSettings
- // module key -> <key,val>
- private Map<String, Map<String, String>> moduleProperties = Maps.newHashMap();
-
private final BootstrapSettings bootstrapSettings;
private final ServerClient client;
private Map<String, String> savedProperties;
public BatchSettings(BootstrapSettings bootstrapSettings, PropertyDefinitions propertyDefinitions,
- ServerClient client, Configuration deprecatedConfiguration) {
+ ServerClient client, Configuration deprecatedConfiguration) {
super(propertyDefinitions);
getEncryption().setPathToSecretKey(bootstrapSettings.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH));
this.bootstrapSettings = bootstrapSettings;
if (StringUtils.isNotBlank(branch)) {
projectKey = String.format("%s:%s", projectKey, branch);
}
- downloadSettings(client, projectKey);
+ downloadSettings(projectKey);
} else {
LoggerFactory.getLogger(BatchSettings.class).info("Load batch settings");
- downloadSettings(client, null);
+ downloadSettings(null);
}
addProperties(bootstrapSettings.properties());
this.setProperties(savedProperties);
}
- private void downloadSettings(ServerClient client, @Nullable String projectKey) {
+ private void downloadSettings(@Nullable String projectKey) {
String url;
if (StringUtils.isNotBlank(projectKey)) {
- url = "/batch_bootstrap/properties?project=" + projectKey + "&dryRun=" + dryRun;
+ url = BATCH_BOOTSTRAP_PROPERTIES_URL + "?project=" + projectKey + "&dryRun=" + dryRun;
} else {
- url = "/batch_bootstrap/properties?dryRun=" + dryRun;
+ url = BATCH_BOOTSTRAP_PROPERTIES_URL + "?dryRun=" + dryRun;
}
String jsonText = client.request(url);
List<Map<String, String>> json = (List<Map<String, String>>) JSONValue.parse(jsonText);
for (Map<String, String> jsonProperty : json) {
String key = jsonProperty.get("k");
String value = jsonProperty.get("v");
- String moduleKey = jsonProperty.get("p");
- if (moduleKey == null || projectKey == null || moduleKey.equals(projectKey)) {
- setProperty(key, value);
- }
- if (moduleKey != null) {
- Map<String, String> map = moduleProperties.get(moduleKey);
- if (map == null) {
- map = Maps.newHashMap();
- moduleProperties.put(moduleKey, map);
- }
- map.put(key, value);
- }
+ setProperty(key, value);
}
}
- public Map<String, String> getModuleProperties(String projectKey) {
- return moduleProperties.get(projectKey);
- }
-
@Override
protected void doOnSetProperty(String key, @Nullable String value) {
deprecatedConfiguration.setProperty(key, value);
import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
+import org.json.simple.JSONValue;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.bootstrap.BatchSettings;
+import org.sonar.batch.bootstrap.ServerClient;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
+import static org.sonar.batch.bootstrap.BatchSettings.BATCH_BOOTSTRAP_PROPERTIES_URL;
+
/**
* @since 2.12
*/
private final Configuration deprecatedCommonsConf;
private boolean dryRun;
+ private final ServerClient client;
- public ModuleSettings(BatchSettings batchSettings, ProjectDefinition project, Configuration deprecatedCommonsConf) {
+ public ModuleSettings(BatchSettings batchSettings, ProjectDefinition project, Configuration deprecatedCommonsConf, ServerClient client) {
super(batchSettings.getDefinitions());
+ this.client = client;
getEncryption().setPathToSecretKey(batchSettings.getString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH));
this.dryRun = "true".equals(batchSettings.getString(CoreProperties.DRY_RUN));
projectKey = String.format("%s:%s", projectKey, branch);
}
addProperties(batchSettings.getProperties());
- Map<String, String> moduleProps = batchSettings.getModuleProperties(projectKey);
- if (moduleProps != null) {
- for (Map.Entry<String, String> entry : moduleProps.entrySet()) {
- setProperty(entry.getKey(), entry.getValue());
- }
+ downloadSettings(projectKey);
+ }
+
+ private void downloadSettings(String moduleKey) {
+ String url = BATCH_BOOTSTRAP_PROPERTIES_URL + "?project=" + moduleKey + "&dryRun=" + dryRun;
+ String jsonText = client.request(url);
+ List<Map<String, String>> json = (List<Map<String, String>>) JSONValue.parse(jsonText);
+ for (Map<String, String> jsonProperty : json) {
+ String key = jsonProperty.get("k");
+ String value = jsonProperty.get("v");
+ setProperty(key, value);
}
}
import org.sonar.api.utils.SonarException;
import java.util.Collections;
-import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
assertThat(batchSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2");
thrown.expect(SonarException.class);
thrown
- .expectMessage("Access to the secured property 'sonar.foo.secured' is not possible in local (dry run) SonarQube analysis. The SonarQube plugin which requires this property must be deactivated in dry run mode.");
+ .expectMessage("Access to the secured property 'sonar.foo.secured' is not possible in local (dry run) SonarQube analysis. The SonarQube plugin which requires this property must be deactivated in dry run mode.");
batchSettings.getString("sonar.foo.secured");
}
- @Test
- public void should_keep_module_settings_for_later() {
- when(client.request("/batch_bootstrap/properties?dryRun=false")).thenReturn(JSON_RESPONSE);
- when(client.request("/batch_bootstrap/properties?project=struts&dryRun=false")).thenReturn(REACTOR_JSON_RESPONSE);
-
- BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf);
- batchSettings.init(new ProjectReactor(project));
-
- Map<String, String> moduleSettings = batchSettings.getModuleProperties("struts-core");
-
- assertThat(moduleSettings).hasSize(1);
- assertThat(moduleSettings.get("sonar.java.coveragePlugin")).isEqualTo("cobertura");
- }
-
@Test
public void system_props_should_override_build_props() {
when(client.request("/batch_bootstrap/properties?dryRun=false")).thenReturn(JSON_RESPONSE);
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.bootstrap.BatchSettings;
+import org.sonar.batch.bootstrap.ServerClient;
import java.util.List;
@Rule
public ExpectedException thrown = ExpectedException.none();
+ ServerClient client = mock(ServerClient.class);
+
@Test
public void testOrderedProjects() {
ProjectDefinition grandParent = ProjectDefinition.create();
BatchSettings batchSettings = mock(BatchSettings.class);
when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions());
when(batchSettings.getProperties()).thenReturn(ImmutableMap.of(
- "overridding", "batch",
- "on-batch", "true"
- ));
- when(batchSettings.getModuleProperties("struts-core")).thenReturn(ImmutableMap.of(
- "on-module", "true",
- "overridding", "module"
- ));
+ "overridding", "batch",
+ "on-batch", "true"
+ ));
+ when(client.request("/batch_bootstrap/properties?project=struts-core&dryRun=false"))
+ .thenReturn("[{\"k\":\"on-module\",\"v\":\"true\"}," +
+ "{\"k\":\"overridding\",\"v\":\"module\"}]");
ProjectDefinition module = ProjectDefinition.create().setKey("struts-core");
Configuration deprecatedConf = new PropertiesConfiguration();
- ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, deprecatedConf);
+ ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, deprecatedConf, client);
assertThat(moduleSettings.getString("overridding")).isEqualTo("module");
assertThat(moduleSettings.getString("on-batch")).isEqualTo("true");
BatchSettings batchSettings = mock(BatchSettings.class);
when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions());
when(batchSettings.getProperties()).thenReturn(ImmutableMap.of(
- "sonar.foo.secured", "bar"
- ));
- when(batchSettings.getModuleProperties("struts-core")).thenReturn(ImmutableMap.of(
- "sonar.foo.license.secured", "bar2"
- ));
+ "sonar.foo.secured", "bar"
+ ));
+ when(client.request("/batch_bootstrap/properties?project=struts-core&dryRun=false"))
+ .thenReturn("[{\"k\":\"sonar.foo.license.secured\",\"v\":\"bar2\"}]");
ProjectDefinition module = ProjectDefinition.create().setKey("struts-core");
Configuration deprecatedConf = new PropertiesConfiguration();
- ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, deprecatedConf);
+ ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, deprecatedConf, client);
assertThat(moduleSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2");
assertThat(moduleSettings.getString("sonar.foo.secured")).isEqualTo("bar");
when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions());
when(batchSettings.getString(CoreProperties.DRY_RUN)).thenReturn("true");
when(batchSettings.getProperties()).thenReturn(ImmutableMap.of(
- "sonar.foo.secured", "bar"
- ));
- when(batchSettings.getModuleProperties("struts-core")).thenReturn(ImmutableMap.of(
- "sonar.foo.license.secured", "bar2"
- ));
+ "sonar.foo.secured", "bar"
+ ));
+ when(client.request("/batch_bootstrap/properties?project=struts-core&dryRun=true"))
+ .thenReturn("[{\"k\":\"sonar.foo.license.secured\",\"v\":\"bar2\"}]");
ProjectDefinition module = ProjectDefinition.create().setKey("struts-core");
Configuration deprecatedConf = new PropertiesConfiguration();
- ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, deprecatedConf);
+ ModuleSettings moduleSettings = new ModuleSettings(batchSettings, module, deprecatedConf, client);
assertThat(moduleSettings.getString("sonar.foo.license.secured")).isEqualTo("bar2");
thrown.expect(SonarException.class);
thrown
- .expectMessage("Access to the secured property 'sonar.foo.secured' is not possible in local (dry run) SonarQube analysis. The SonarQube plugin which requires this property must be deactivated in dry run mode.");
+ .expectMessage("Access to the secured property 'sonar.foo.secured' is not possible in local (dry run) SonarQube analysis. The SonarQube plugin which requires this property must be deactivated in dry run mode.");
moduleSettings.getString("sonar.foo.secured");
}
}