diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-11-19 11:08:59 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-01-16 09:43:02 +0100 |
commit | e4c0b858c7e6807801c6481d995ca11682c58fad (patch) | |
tree | 612e7224f9bd131076005d56964a9fc5be0d72c3 /sonar-scanner-engine/src/main/java/org | |
parent | b6f878d8d48c55c03bcbf2ba8010526f1fa12f49 (diff) | |
download | sonarqube-e4c0b858c7e6807801c6481d995ca11682c58fad.tar.gz sonarqube-e4c0b858c7e6807801c6481d995ca11682c58fad.zip |
SONAR-11488 Stop loading module level settings in scanner
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org')
15 files changed, 170 insertions, 143 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java index eb5c23b442a..a9fa6e899b7 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfiguration.java @@ -21,8 +21,6 @@ package org.sonar.scanner.bootstrap; import java.util.Map; import javax.annotation.concurrent.Immutable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.config.Encryption; import org.sonar.api.config.PropertyDefinitions; import org.sonar.scanner.config.DefaultConfiguration; @@ -30,8 +28,6 @@ import org.sonar.scanner.config.DefaultConfiguration; @Immutable public class GlobalConfiguration extends DefaultConfiguration { - private static final Logger LOG = LoggerFactory.getLogger(GlobalConfiguration.class); - public GlobalConfiguration(PropertyDefinitions propertyDefinitions, Encryption encryption, GlobalAnalysisMode mode, Map<String, String> settings) { super(propertyDefinitions, encryption, mode, settings); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java index a6def248ce2..e04de9c05d9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java @@ -101,8 +101,10 @@ public class ScannerWsClient { "Please provide the values of the properties %s and %s.", CoreProperties.LOGIN, CoreProperties.PASSWORD)); } - if (code == HTTP_FORBIDDEN || code == HTTP_BAD_REQUEST) { - // SONAR-4397 Details are in response content + if (code == HTTP_FORBIDDEN) { + throw MessageException.of("You're not authorized to run analysis. Please contact the project administrator."); + } + if (code == HTTP_BAD_REQUEST) { String jsonMsg = tryParseAsJsonError(response.content()); if (jsonMsg != null) { throw MessageException.of(jsonMsg); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java index 22977f997a5..c0d091e824b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java @@ -24,7 +24,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.StandardOpenOption; +import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -43,7 +43,10 @@ import org.sonar.core.platform.PluginInfo; import org.sonar.scanner.bootstrap.GlobalServerSettings; import org.sonar.scanner.bootstrap.ScannerPluginRepository; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.repository.ProjectRepositories; +import org.sonar.scanner.scan.ProjectServerSettings; +import org.sonar.scanner.scan.filesystem.InputComponentStore; + +import static java.util.stream.Collectors.toList; @ScannerSide public class AnalysisContextReportPublisher { @@ -56,22 +59,24 @@ public class AnalysisContextReportPublisher { private static final String SONAR_PROP_PREFIX = "sonar."; private static final int MAX_WIDTH = 1000; private final ScannerPluginRepository pluginRepo; + private final ProjectServerSettings projectServerSettings; private final AnalysisMode mode; private final System2 system; - private final ProjectRepositories projectRepos; private final GlobalServerSettings globalServerSettings; private final InputModuleHierarchy hierarchy; + private final InputComponentStore store; private ScannerReportWriter writer; - public AnalysisContextReportPublisher(AnalysisMode mode, ScannerPluginRepository pluginRepo, System2 system, - ProjectRepositories projectRepos, GlobalServerSettings globalServerSettings, InputModuleHierarchy hierarchy) { + public AnalysisContextReportPublisher(ProjectServerSettings projectServerSettings, AnalysisMode mode, ScannerPluginRepository pluginRepo, System2 system, + GlobalServerSettings globalServerSettings, InputModuleHierarchy hierarchy, InputComponentStore store) { + this.projectServerSettings = projectServerSettings; this.mode = mode; this.pluginRepo = pluginRepo; this.system = system; - this.projectRepos = projectRepos; this.globalServerSettings = globalServerSettings; this.hierarchy = hierarchy; + this.store = store; } public void init(ScannerReportWriter writer) { @@ -87,6 +92,8 @@ public class AnalysisContextReportPublisher { } writePlugins(fileWriter); writeGlobalSettings(fileWriter); + writeProjectSettings(fileWriter); + writeModulesSettings(fileWriter); } catch (IOException e) { throw new IllegalStateException("Unable to write analysis log", e); } @@ -125,30 +132,40 @@ public class AnalysisContextReportPublisher { } private void writeGlobalSettings(BufferedWriter fileWriter) throws IOException { - fileWriter.append("Global properties:\n"); + fileWriter.append("Global server settings:\n"); Map<String, String> props = globalServerSettings.properties(); for (String prop : new TreeSet<>(props.keySet())) { dumpPropIfNotSensitive(fileWriter, prop, props.get(prop)); } } - public void dumpModuleSettings(DefaultInputModule module) { - if (mode.isIssues()) { - return; + private void writeProjectSettings(BufferedWriter fileWriter) throws IOException { + fileWriter.append("Project server settings:\n"); + Map<String, String> props = projectServerSettings.properties(); + for (String prop : new TreeSet<>(props.keySet())) { + dumpPropIfNotSensitive(fileWriter, prop, props.get(prop)); } + fileWriter.append("Project scanner properties:\n"); + writeScannerProps(fileWriter, hierarchy.root().properties()); + } - File analysisLog = writer.getFileStructure().analysisLog(); - try (BufferedWriter fileWriter = Files.newBufferedWriter(analysisLog.toPath(), StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.APPEND)) { + private void writeModulesSettings(BufferedWriter fileWriter) throws IOException { + for (DefaultInputModule module : store.allModules()) { + if (module.equals(hierarchy.root())) { + continue; + } Map<String, String> moduleSpecificProps = collectModuleSpecificProps(module); - fileWriter.append(String.format("Settings for module: %s", module.key())).append('\n'); - for (String prop : new TreeSet<>(moduleSpecificProps.keySet())) { - if (isSystemProp(prop) || isEnvVariable(prop) || !isSqProp(prop)) { - continue; - } - dumpPropIfNotSensitive(fileWriter, prop, moduleSpecificProps.get(prop)); + fileWriter.append(String.format("Scanner properties of module: %s", module.key())).append('\n'); + writeScannerProps(fileWriter, moduleSpecificProps); + } + } + + private void writeScannerProps(BufferedWriter fileWriter, Map<String, String> props) throws IOException { + for (Map.Entry<String, String> prop : props.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).collect(toList())) { + if (isSystemProp(prop.getKey()) || isEnvVariable(prop.getKey()) || !isSqProp(prop.getKey())) { + continue; } - } catch (IOException e) { - throw new IllegalStateException("Unable to write analysis log", e); + dumpPropIfNotSensitive(fileWriter, prop.getKey(), prop.getValue()); } } @@ -161,9 +178,6 @@ public class AnalysisContextReportPublisher { */ private Map<String, String> collectModuleSpecificProps(DefaultInputModule module) { Map<String, String> moduleSpecificProps = new HashMap<>(); - if (projectRepos.moduleExists(module.getKeyWithBranch())) { - moduleSpecificProps.putAll(projectRepos.settings(module.getKeyWithBranch())); - } AbstractProjectOrModule parent = hierarchy.parent(module); if (parent == null) { moduleSpecificProps.putAll(module.properties()); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java index 6a60632e65e..043264de8b3 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java @@ -37,7 +37,6 @@ import org.sonar.scanner.util.ScannerUtils; import org.sonarqube.ws.Batch; import org.sonarqube.ws.Batch.WsProjectResponse; import org.sonarqube.ws.Batch.WsProjectResponse.FileDataByPath; -import org.sonarqube.ws.Batch.WsProjectResponse.Settings; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.WsResponse; @@ -100,14 +99,6 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad WsProjectResponse response = WsProjectResponse.parseFrom(is); Table<String, String, FileData> fileDataTable = HashBasedTable.create(); - Table<String, String, String> settings = HashBasedTable.create(); - - Map<String, Settings> settingsByModule = response.getSettingsByModule(); - for (Map.Entry<String, Settings> e1 : settingsByModule.entrySet()) { - for (Map.Entry<String, String> e2 : e1.getValue().getSettings().entrySet()) { - settings.put(e1.getKey(), e2.getKey(), e2.getValue()); - } - } Map<String, FileDataByPath> fileDataByModuleAndPath = response.getFileDataByModuleAndPath(); for (Map.Entry<String, FileDataByPath> e1 : fileDataByModuleAndPath.entrySet()) { @@ -117,7 +108,7 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad } } - return new ProjectRepositories(settings, fileDataTable, new Date(response.getLastAnalysisDate())); + return new ProjectRepositories(fileDataTable, new Date(response.getLastAnalysisDate())); } catch (IOException e) { throw new IllegalStateException("Couldn't load project repository for " + projectKey, e); } finally { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java index a5fb6901603..bed6c1c2c34 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java @@ -29,21 +29,18 @@ import javax.annotation.concurrent.Immutable; @Immutable public class ProjectRepositories { - private final ImmutableTable<String, String, String> settingsByModule; private final ImmutableTable<String, String, FileData> fileDataByModuleAndPath; private final Date lastAnalysisDate; private final boolean exists; public ProjectRepositories() { this.exists = false; - this.settingsByModule = new ImmutableTable.Builder<String, String, String>().build(); this.fileDataByModuleAndPath = new ImmutableTable.Builder<String, String, FileData>().build(); this.lastAnalysisDate = null; } - public ProjectRepositories(Table<String, String, String> settingsByModule, Table<String, String, FileData> fileDataByModuleAndPath, - @Nullable Date lastAnalysisDate) { - this.settingsByModule = ImmutableTable.copyOf(settingsByModule); + public ProjectRepositories(Table<String, String, FileData> fileDataByModuleAndPath, + @Nullable Date lastAnalysisDate) { this.fileDataByModuleAndPath = ImmutableTable.copyOf(fileDataByModuleAndPath); this.lastAnalysisDate = lastAnalysisDate; this.exists = true; @@ -61,14 +58,6 @@ public class ProjectRepositories { return fileDataByModuleAndPath; } - public boolean moduleExists(String moduleKey) { - return settingsByModule.containsRow(moduleKey); - } - - public Map<String, String> settings(String moduleKey) { - return settingsByModule.row(moduleKey); - } - @CheckForNull public FileData fileData(String projectKeyWithBranch, String path) { return fileDataByModuleAndPath.get(projectKeyWithBranch, path); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java index 8c91389eb45..504c61f5c0b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java @@ -22,7 +22,9 @@ package org.sonar.scanner.repository.settings; import com.google.common.annotations.VisibleForTesting; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -38,6 +40,7 @@ import org.sonarqube.ws.Settings.FieldValues.Value; import org.sonarqube.ws.Settings.Setting; import org.sonarqube.ws.Settings.ValuesWsResponse; import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpException; public class DefaultSettingsLoader implements SettingsLoader { @@ -62,8 +65,13 @@ public class DefaultSettingsLoader implements SettingsLoader { ValuesWsResponse values = ValuesWsResponse.parseFrom(is); profiler.stopInfo(); return toMap(values.getSettingsList()); + } catch (HttpException e) { + if (e.code() == HttpURLConnection.HTTP_NOT_FOUND) { + return Collections.emptyMap(); + } + throw e; } catch (IOException e) { - throw new IllegalStateException("Failed to load server settings", e); + throw new IllegalStateException("Unable to load settings", e); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java index 7f03d2cca65..556d09b9184 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleConfigurationProvider.java @@ -20,7 +20,6 @@ package org.sonar.scanner.scan; import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -30,39 +29,24 @@ import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.report.AnalysisContextReportPublisher; -import org.sonar.scanner.repository.ProjectRepositories; public class ModuleConfigurationProvider extends ProviderAdapter { private ModuleConfiguration moduleConfiguration; - public ModuleConfiguration provide(GlobalConfiguration globalConfig, DefaultInputModule module, ProjectRepositories projectRepos, + public ModuleConfiguration provide(GlobalConfiguration globalConfig, DefaultInputModule module, ProjectServerSettings projectServerSettings, GlobalAnalysisMode analysisMode, AnalysisContextReportPublisher contextReportPublisher) { if (moduleConfiguration == null) { Map<String, String> settings = new LinkedHashMap<>(); - settings.putAll(addServerSidePropertiesIfModuleExists(projectRepos, module.definition())); + settings.putAll(projectServerSettings.properties()); addScannerSideProperties(settings, module.definition()); - contextReportPublisher.dumpModuleSettings(module); moduleConfiguration = new ModuleConfiguration(globalConfig.getDefinitions(), globalConfig.getEncryption(), analysisMode, settings); } return moduleConfiguration; } - private static Map<String, String> addServerSidePropertiesIfModuleExists(ProjectRepositories projectRepos, ProjectDefinition def) { - if (projectRepos.moduleExists(def.getKeyWithBranch())) { - return projectRepos.settings(def.getKeyWithBranch()); - } else { - // Module doesn't exist on server. Try to add parent server settings as inheritance. - ProjectDefinition parentDef = def.getParent(); - if (parentDef != null) { - return addServerSidePropertiesIfModuleExists(projectRepos, parentDef); - } - return Collections.emptyMap(); - } - } - private static void addScannerSideProperties(Map<String, String> settings, ProjectDefinition project) { List<ProjectDefinition> orderedProjects = getTopDownParentProjects(project); for (ProjectDefinition p : orderedProjects) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java index 11853632ef2..e8b8ceb7864 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableModuleSettings.java @@ -20,15 +20,9 @@ package org.sonar.scanner.scan; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Optional; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; -import org.sonar.scanner.bootstrap.GlobalConfiguration; -import org.sonar.scanner.repository.ProjectRepositories; import static java.util.Objects.requireNonNull; @@ -38,49 +32,15 @@ import static java.util.Objects.requireNonNull; @Deprecated public class MutableModuleSettings extends Settings { - private final ProjectRepositories projectRepos; - private final AnalysisMode analysisMode; private final Map<String, String> properties = new HashMap<>(); - public MutableModuleSettings(GlobalConfiguration globalConfig, ProjectDefinition moduleDefinition, ProjectRepositories projectSettingsRepo, - AnalysisMode analysisMode) { - super(globalConfig.getDefinitions(), globalConfig.getEncryption()); - this.projectRepos = projectSettingsRepo; - this.analysisMode = analysisMode; - - init(moduleDefinition, globalConfig); - } - - private MutableModuleSettings init(ProjectDefinition moduleDefinition, GlobalConfiguration globalConfig) { - addProjectProperties(moduleDefinition, globalConfig); - addBuildProperties(moduleDefinition); - return this; - } - - private void addProjectProperties(ProjectDefinition def, GlobalConfiguration globalConfig) { - addProperties(globalConfig.getProperties()); - do { - if (projectRepos.moduleExists(def.getKeyWithBranch())) { - addProperties(projectRepos.settings(def.getKeyWithBranch())); - break; - } - def = def.getParent(); - } while (def != null); - } - - private void addBuildProperties(ProjectDefinition project) { - List<ProjectDefinition> orderedProjects = ModuleConfigurationProvider.getTopDownParentProjects(project); - for (ProjectDefinition p : orderedProjects) { - addProperties(p.properties()); - } + public MutableModuleSettings(ModuleConfiguration config) { + super(config.getDefinitions(), config.getEncryption()); + addProperties(config.getProperties()); } @Override protected Optional<String> get(String key) { - if (analysisMode.isIssues() && key.endsWith(".secured") && !key.contains(".license")) { - throw MessageException.of("Access to the secured property '" + key - + "' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); - } return Optional.ofNullable(properties.get(key)); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java index 4d8d3f7a43d..a85e5720303 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/MutableProjectSettings.java @@ -22,12 +22,8 @@ package org.sonar.scanner.scan; import java.util.HashMap; import java.util.Map; import java.util.Optional; -import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; -import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.GlobalConfiguration; -import org.sonar.scanner.repository.ProjectRepositories; import static java.util.Objects.requireNonNull; @@ -37,23 +33,19 @@ import static java.util.Objects.requireNonNull; @Deprecated public class MutableProjectSettings extends Settings { - private final GlobalAnalysisMode mode; private final Map<String, String> properties = new HashMap<>(); - public MutableProjectSettings(ProjectReactor reactor, GlobalConfiguration globalConfig, ProjectRepositories projectRepositories, GlobalAnalysisMode mode) { + public MutableProjectSettings(GlobalConfiguration globalConfig) { super(globalConfig.getDefinitions(), globalConfig.getEncryption()); - this.mode = mode; addProperties(globalConfig.getProperties()); - addProperties(projectRepositories.settings(reactor.getRoot().getKeyWithBranch())); - addProperties(reactor.getRoot().properties()); + } + + public void complete(ProjectConfiguration projectConfig) { + addProperties(projectConfig.getProperties()); } @Override protected Optional<String> get(String key) { - if (mode.isIssues() && key.endsWith(".secured") && !key.contains(".license")) { - throw MessageException.of("Access to the secured property '" + key - + "' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); - } return Optional.ofNullable(properties.get(key)); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java index 9db726d7e75..8c774902e88 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectConfigurationProvider.java @@ -22,26 +22,26 @@ package org.sonar.scanner.scan; import java.util.LinkedHashMap; import java.util.Map; import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.GlobalConfiguration; import org.sonar.scanner.bootstrap.GlobalServerSettings; -import org.sonar.scanner.repository.ProjectRepositories; public class ProjectConfigurationProvider extends ProviderAdapter { private ProjectConfiguration projectConfig; - public ProjectConfiguration provide(GlobalServerSettings globalServerSettings, AbstractProjectOrModule project, - GlobalConfiguration globalConfig, ProjectRepositories projectRepositories, GlobalAnalysisMode mode) { + public ProjectConfiguration provide(DefaultInputProject project, GlobalConfiguration globalConfig, GlobalServerSettings globalServerSettings, ProjectServerSettings projectServerSettings, + GlobalAnalysisMode mode, MutableProjectSettings projectSettings) { if (projectConfig == null) { Map<String, String> settings = new LinkedHashMap<>(); settings.putAll(globalServerSettings.properties()); - settings.putAll(projectRepositories.settings(project.getKeyWithBranch())); + settings.putAll(projectServerSettings.properties()); settings.putAll(project.properties()); projectConfig = new ProjectConfiguration(globalConfig.getDefinitions(), globalConfig.getEncryption(), mode, settings); + projectSettings.complete(projectConfig); } return projectConfig; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 7e595179de1..0079aedb077 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -150,6 +150,7 @@ public class ProjectScanContainer extends ComponentContainer { new ProjectPullRequestsProvider(), DefaultAnalysisMode.class, new ProjectRepositoriesProvider(), + new ProjectServerSettingsProvider(), // temp new AnalysisTempFolderProvider(), diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettings.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettings.java new file mode 100644 index 00000000000..c1e096283ca --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettings.java @@ -0,0 +1,46 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import javax.annotation.concurrent.Immutable; + +/** + * Project settings coming from the server. Does not contain global settings. + */ +@Immutable +public class ProjectServerSettings { + + private final Map<String, String> properties; + + public ProjectServerSettings(Map<String, String> properties) { + this.properties = properties; + } + + public Map<String, String> properties() { + return ImmutableMap.copyOf(properties); + } + + public String property(String key) { + return properties.get(key); + } + +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java new file mode 100644 index 00000000000..2a85f416445 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectServerSettingsProvider.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan; + +import java.util.Map; +import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.scanner.bootstrap.ScannerProperties; +import org.sonar.scanner.repository.settings.SettingsLoader; + +public class ProjectServerSettingsProvider extends ProviderAdapter { + + private ProjectServerSettings singleton; + + public ProjectServerSettings provide(SettingsLoader loader, ScannerProperties scannerProperties) { + if (singleton == null) { + Map<String, String> serverSideSettings = loader.load(scannerProperties.getKeyWithBranch()); + singleton = new ProjectServerSettings(serverSideSettings); + } + return singleton; + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java index a1c78fc65dd..ab450cbedef 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java @@ -22,7 +22,6 @@ package org.sonar.scanner.scan.branch; import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; - import org.picocontainer.annotations.Nullable; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.batch.bootstrap.ProjectDefinition; @@ -31,7 +30,8 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; import org.sonar.scanner.bootstrap.GlobalConfiguration; -import org.sonar.scanner.repository.settings.SettingsLoader; +import org.sonar.scanner.bootstrap.GlobalServerSettings; +import org.sonar.scanner.scan.ProjectServerSettings; public class BranchConfigurationProvider extends ProviderAdapter { @@ -41,13 +41,14 @@ public class BranchConfigurationProvider extends ProviderAdapter { private BranchConfiguration branchConfiguration = null; public BranchConfiguration provide(@Nullable BranchConfigurationLoader loader, GlobalConfiguration globalConfiguration, ProjectReactor reactor, - SettingsLoader settingsLoader, ProjectBranches branches, ProjectPullRequests pullRequests) { + GlobalServerSettings globalServerSettings, ProjectServerSettings projectServerSettings, + ProjectBranches branches, ProjectPullRequests pullRequests) { if (branchConfiguration == null) { if (loader == null) { branchConfiguration = new DefaultBranchConfiguration(); } else { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - Supplier<Map<String, String>> settingsSupplier = createSettingsSupplier(globalConfiguration, reactor.getRoot(), settingsLoader); + Supplier<Map<String, String>> settingsSupplier = createSettingsSupplier(reactor.getRoot(), globalServerSettings, projectServerSettings); branchConfiguration = loader.load(globalConfiguration.getProperties(), settingsSupplier, branches, pullRequests); profiler.stopInfo(); } @@ -55,13 +56,14 @@ public class BranchConfigurationProvider extends ProviderAdapter { return branchConfiguration; } - private static Supplier<Map<String, String>> createSettingsSupplier(GlobalConfiguration globalConfiguration, ProjectDefinition root, SettingsLoader settingsLoader) { + private static Supplier<Map<String, String>> createSettingsSupplier(ProjectDefinition root, + GlobalServerSettings globalServerSettings, ProjectServerSettings projectServerSettings) { // we can't get ProjectConfiguration because it creates a circular dependency. // We create our own settings which will only be loaded if needed. return () -> { Map<String, String> settings = new HashMap<>(); - settings.putAll(globalConfiguration.getProperties()); - settings.putAll(settingsLoader.load(root.getKeyWithBranch())); + settings.putAll(globalServerSettings.properties()); + settings.putAll(projectServerSettings.properties()); settings.putAll(root.properties()); return settings; }; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java index 8d1071b172c..a0f7e952db9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java @@ -34,8 +34,8 @@ import java.util.stream.Stream; import javax.annotation.CheckForNull; import org.sonar.api.batch.fs.InputComponent; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.FileExtensionPredicate; import org.sonar.api.scanner.fs.InputProject; import org.sonar.scanner.scan.branch.BranchConfiguration; @@ -51,7 +51,7 @@ public class InputComponentStore { private final Map<String, InputFile> globalInputFileCache = new HashMap<>(); private final Table<String, String, InputFile> inputFileByModuleCache = TreeBasedTable.create(); // indexed by key with branch - private final Map<String, AbstractProjectOrModule> inputModuleCache = new HashMap<>(); + private final Map<String, DefaultInputModule> inputModuleCache = new HashMap<>(); private final Map<String, InputComponent> inputComponents = new HashMap<>(); private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create(); private final SetMultimap<String, InputFile> filesByExtensionCache = LinkedHashMultimap.create(); @@ -125,11 +125,11 @@ public class InputComponentStore { } @CheckForNull - public AbstractProjectOrModule getModule(String moduleKeyWithBranch) { + public DefaultInputModule getModule(String moduleKeyWithBranch) { return inputModuleCache.get(moduleKeyWithBranch); } - public void put(AbstractProjectOrModule inputModule) { + public void put(DefaultInputModule inputModule) { String key = inputModule.key(); String keyWithBranch = inputModule.getKeyWithBranch(); Preconditions.checkNotNull(inputModule); @@ -154,4 +154,8 @@ public class InputComponentStore { public SortedSet<String> getLanguages(String moduleKey) { return languagesCache.getOrDefault(moduleKey, Collections.emptySortedSet()); } + + public Collection<DefaultInputModule> allModules() { + return inputModuleCache.values(); + } } |