diff options
author | Michal Duda <michal.duda@sonarsource.com> | 2019-02-01 12:51:32 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-02-18 20:20:55 +0100 |
commit | 2b8cc73a602be586c15754f7d87242dbaa5ccfd7 (patch) | |
tree | 9e0a974b045fa96f97fed0ea2620ddccf4545743 /sonar-scanner-engine/src/main/java/org/sonar/scanner | |
parent | e89a888d560520c69f01e45fc5c9649e76fba30d (diff) | |
download | sonarqube-2b8cc73a602be586c15754f7d87242dbaa5ccfd7.tar.gz sonarqube-2b8cc73a602be586c15754f7d87242dbaa5ccfd7.zip |
SONARCLOUD-336: Automatically guess the projectKey/organization
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner')
23 files changed, 384 insertions, 90 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java index b97e63b9ee1..abfc2727df2 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/analysis/DefaultAnalysisMode.java @@ -25,7 +25,7 @@ import org.sonar.api.batch.AnalysisMode; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; -import org.sonar.scanner.bootstrap.ScannerProperties; +import org.sonar.scanner.bootstrap.ProcessedScannerProperties; @Immutable public class DefaultAnalysisMode implements AnalysisMode { @@ -37,7 +37,7 @@ public class DefaultAnalysisMode implements AnalysisMode { private boolean scanAllFiles; - public DefaultAnalysisMode(ScannerProperties props, GlobalAnalysisMode analysisMode) { + public DefaultAnalysisMode(ProcessedScannerProperties props, GlobalAnalysisMode analysisMode) { this.analysisMode = analysisMode; this.analysisProps = props.properties(); load(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java index 421dfdeb73f..26aced1fb60 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalAnalysisMode.java @@ -36,7 +36,7 @@ public class GlobalAnalysisMode { protected boolean issues; protected boolean mediumTestMode; - public GlobalAnalysisMode(ScannerProperties props) { + public GlobalAnalysisMode(RawScannerProperties props) { String mode = props.property(CoreProperties.ANALYSIS_MODE); validate(mode); issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java index be54e94b258..ceb0fc91db6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalConfigurationProvider.java @@ -28,7 +28,8 @@ public class GlobalConfigurationProvider extends ProviderAdapter { private GlobalConfiguration globalConfig; - public GlobalConfiguration provide(GlobalServerSettings globalServerSettings, ScannerProperties scannerProps, PropertyDefinitions propertyDefinitions) { + public GlobalConfiguration provide(GlobalServerSettings globalServerSettings, RawScannerProperties scannerProps, + PropertyDefinitions propertyDefinitions) { if (globalConfig == null) { Map<String, String> mergedSettings = new LinkedHashMap<>(); mergedSettings.putAll(globalServerSettings.properties()); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java index a8202e1fc48..4211ab99cfe 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java @@ -49,8 +49,8 @@ import org.sonar.scanner.platform.DefaultServer; import org.sonar.scanner.repository.DefaultMetricsRepositoryLoader; import org.sonar.scanner.repository.MetricsRepositoryLoader; import org.sonar.scanner.repository.MetricsRepositoryProvider; -import org.sonar.scanner.repository.settings.DefaultSettingsLoader; -import org.sonar.scanner.repository.settings.SettingsLoader; +import org.sonar.scanner.repository.settings.DefaultGlobalSettingsLoader; +import org.sonar.scanner.repository.settings.GlobalSettingsLoader; import org.sonar.scanner.scan.ProjectScanContainer; import org.sonar.scanner.storage.StoragesManager; @@ -71,9 +71,9 @@ public class GlobalContainer extends ComponentContainer { @Override protected void doBeforeStart() { - ScannerProperties scannerProps = new ScannerProperties(scannerProperties); - GlobalAnalysisMode globalMode = new GlobalAnalysisMode(scannerProps); - add(scannerProps); + RawScannerProperties rawScannerProperties = new RawScannerProperties(scannerProperties); + GlobalAnalysisMode globalMode = new GlobalAnalysisMode(rawScannerProperties); + add(rawScannerProperties); add(globalMode); addBootstrapComponents(); } @@ -106,7 +106,7 @@ public class GlobalContainer extends ComponentContainer { UuidFactoryImpl.INSTANCE); addIfMissing(ScannerPluginInstaller.class, PluginInstaller.class); add(CoreExtensionRepositoryImpl.class, CoreExtensionsLoader.class, ScannerCoreExtensionsInstaller.class); - addIfMissing(DefaultSettingsLoader.class, SettingsLoader.class); + addIfMissing(DefaultGlobalSettingsLoader.class, GlobalSettingsLoader.class); addIfMissing(DefaultMetricsRepositoryLoader.class, MetricsRepositoryLoader.class); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalServerSettingsProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalServerSettingsProvider.java index 9f2ff35d9d0..47ab119164f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalServerSettingsProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalServerSettingsProvider.java @@ -25,7 +25,7 @@ import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.CoreProperties; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.scanner.repository.settings.SettingsLoader; +import org.sonar.scanner.repository.settings.GlobalSettingsLoader; public class GlobalServerSettingsProvider extends ProviderAdapter { @@ -33,7 +33,7 @@ public class GlobalServerSettingsProvider extends ProviderAdapter { private GlobalServerSettings singleton; - public GlobalServerSettings provide(SettingsLoader loader) { + public GlobalServerSettings provide(GlobalSettingsLoader loader) { if (singleton == null) { Map<String, String> serverSideSettings = loader.loadGlobalSettings(); singleton = new GlobalServerSettings(serverSideSettings); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalTempFolderProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalTempFolderProvider.java index 20b5e2014a1..ad49d737425 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalTempFolderProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalTempFolderProvider.java @@ -56,7 +56,7 @@ public class GlobalTempFolderProvider extends ProviderAdapter implements Compone this.system = system; } - public TempFolder provide(ScannerProperties scannerProps) { + public TempFolder provide(RawScannerProperties scannerProps) { if (tempFolder == null) { String workingPathName = StringUtils.defaultIfBlank(scannerProps.property(CoreProperties.GLOBAL_WORKING_DIRECTORY), CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE); @@ -95,7 +95,7 @@ public class GlobalTempFolderProvider extends ProviderAdapter implements Compone } } - private Path findSonarHome(ScannerProperties props) { + private Path findSonarHome(RawScannerProperties props) { String home = props.property("sonar.userHome"); if (home != null) { return Paths.get(home).toAbsolutePath(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ProcessedScannerProperties.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ProcessedScannerProperties.java new file mode 100644 index 00000000000..1c9b77ba22e --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ProcessedScannerProperties.java @@ -0,0 +1,82 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.bootstrap; + +import com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.concurrent.Immutable; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.bootstrap.ProjectKey; +import org.sonar.scanner.scan.ExternalProjectKeyAndOrganization; + +import static org.apache.commons.lang.StringUtils.trimToNull; + +/** + * Properties that are coming from scanner. + */ +@Immutable +public class ProcessedScannerProperties implements ProjectKey { + + private final Map<String, String> properties; + + public ProcessedScannerProperties(RawScannerProperties rawScannerProperties, + ExternalProjectKeyAndOrganization externalProjectKeyAndOrganization) { + this.properties = new HashMap<>(); + this.properties.putAll(rawScannerProperties.properties()); + + externalProjectKeyAndOrganization.getProjectKey() + .ifPresent(projectKey -> properties.put(CoreProperties.PROJECT_KEY_PROPERTY, projectKey)); + externalProjectKeyAndOrganization.getOrganization() + .ifPresent(organization -> properties.put(org.sonar.core.config.ScannerProperties.ORGANIZATION, organization)); + } + + public Map<String, String> properties() { + return ImmutableMap.copyOf(properties); + } + + public String property(String key) { + return properties.get(key); + } + + @Override + public String get() { + return getKeyWithBranch(); + } + + private String getKey() { + return properties.get(CoreProperties.PROJECT_KEY_PROPERTY); + } + + public String getKeyWithBranch() { + String branch = getBranch(); + String projectKey = getKey(); + if (branch == null) { + return projectKey; + } + return String.format("%s:%s", projectKey, branch); + } + + @CheckForNull + private String getBranch() { + return trimToNull(properties.get(CoreProperties.PROJECT_BRANCH_PROPERTY)); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerProperties.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/RawScannerProperties.java index a351765cfb6..343e08c3d50 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerProperties.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/RawScannerProperties.java @@ -22,24 +22,20 @@ package org.sonar.scanner.bootstrap; import com.google.common.collect.ImmutableMap; import java.util.HashMap; import java.util.Map; -import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectKey; import org.sonar.api.config.Encryption; -import static org.apache.commons.lang.StringUtils.trimToNull; - /** * Properties that are coming from scanner. */ @Immutable -public class ScannerProperties implements ProjectKey { +public class RawScannerProperties { private final Map<String, String> properties; private final Encryption encryption; - public ScannerProperties(Map<String, String> properties) { + public RawScannerProperties(Map<String, String> properties) { encryption = new Encryption(properties.get(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); Map<String, String> decryptedProps = new HashMap<>(properties.size()); for (Map.Entry<String, String> entry : properties.entrySet()) { @@ -67,27 +63,4 @@ public class ScannerProperties implements ProjectKey { public String property(String key) { return properties.get(key); } - - @Override - public String get() { - return getKeyWithBranch(); - } - - private String getKey() { - return properties.get(CoreProperties.PROJECT_KEY_PROPERTY); - } - - public String getKeyWithBranch() { - String branch = getBranch(); - String projectKey = getKey(); - if (branch == null) { - return projectKey; - } - return String.format("%s:%s", projectKey, branch); - } - - @CheckForNull - private String getBranch() { - return trimToNull(properties.get(CoreProperties.PROJECT_BRANCH_PROPERTY)); - } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java index 810a2c37a41..7f619482ab1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java @@ -38,8 +38,8 @@ public class ScannerWsClientProvider extends ProviderAdapter { private ScannerWsClient wsClient; - public synchronized ScannerWsClient provide(final ScannerProperties scannerProps, final EnvironmentInformation env, - GlobalAnalysisMode globalMode, System2 system) { + public synchronized ScannerWsClient provide(final RawScannerProperties scannerProps, + final EnvironmentInformation env, GlobalAnalysisMode globalMode, System2 system) { if (wsClient == null) { String url = defaultIfBlank(scannerProps.property("sonar.host.url"), CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE); HttpConnector.Builder connectorBuilder = HttpConnector.newBuilder(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java index 30eabd15947..b6fae284f48 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java @@ -24,7 +24,7 @@ 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.GlobalAnalysisMode; -import org.sonar.scanner.bootstrap.ScannerProperties; +import org.sonar.scanner.bootstrap.ProcessedScannerProperties; import org.sonar.scanner.scan.branch.BranchConfiguration; public class ProjectRepositoriesProvider extends ProviderAdapter { @@ -32,7 +32,7 @@ public class ProjectRepositoriesProvider extends ProviderAdapter { private static final String LOG_MSG = "Load project repositories"; private ProjectRepositories project = null; - public ProjectRepositories provide(ProjectRepositoriesLoader loader, ScannerProperties scannerProperties, GlobalAnalysisMode mode, BranchConfiguration branchConfig) { + public ProjectRepositories provide(ProjectRepositoriesLoader loader, ProcessedScannerProperties scannerProperties, GlobalAnalysisMode mode, BranchConfiguration branchConfig) { if (project == null) { boolean isIssuesMode = mode.isIssues(); Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java index 60b52f440ee..7ed441cfa08 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfilesProvider.java @@ -25,7 +25,7 @@ import org.picocontainer.injectors.ProviderAdapter; 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.ScannerProperties; +import org.sonar.scanner.bootstrap.ProcessedScannerProperties; import org.sonar.scanner.rule.QualityProfiles; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; @@ -34,7 +34,7 @@ public class QualityProfilesProvider extends ProviderAdapter { private static final String LOG_MSG = "Load quality profiles"; private QualityProfiles profiles = null; - public QualityProfiles provide(QualityProfileLoader loader, ProjectRepositories projectRepositories, ScannerProperties props) { + public QualityProfiles provide(QualityProfileLoader loader, ProjectRepositories projectRepositories, ProcessedScannerProperties props) { if (this.profiles == null) { List<QualityProfile> profileList; Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); @@ -51,7 +51,7 @@ public class QualityProfilesProvider extends ProviderAdapter { } @CheckForNull - private static String getSonarProfile(ScannerProperties props) { + private static String getSonarProfile(ProcessedScannerProperties props) { String profile = props.property(QualityProfiles.SONAR_PROFILE_PROP); if (profile != null) { LOG.warn("Ability to set quality profile from command line using '" + QualityProfiles.SONAR_PROFILE_PROP 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/AbstractSettingsLoader.java index e3f820b0cbb..797acbfcdb9 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/AbstractSettingsLoader.java @@ -34,47 +34,32 @@ import org.apache.commons.lang.StringEscapeUtils; 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.ScannerProperties; import org.sonar.scanner.bootstrap.ScannerWsClient; import org.sonar.scanner.util.ScannerUtils; -import org.sonarqube.ws.Settings.FieldValues.Value; -import org.sonarqube.ws.Settings.Setting; -import org.sonarqube.ws.Settings.ValuesWsResponse; +import org.sonarqube.ws.Settings; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; -public class DefaultSettingsLoader implements SettingsLoader { +public abstract class AbstractSettingsLoader { - private ScannerWsClient wsClient; - private final ScannerProperties scannerProperties; - private static final Logger LOG = Loggers.get(DefaultSettingsLoader.class); + private static final Logger LOG = Loggers.get(AbstractSettingsLoader.class); + private final ScannerWsClient wsClient; - public DefaultSettingsLoader(ScannerWsClient wsClient, ScannerProperties scannerProperties) { + public AbstractSettingsLoader(final ScannerWsClient wsClient) { this.wsClient = wsClient; - this.scannerProperties = scannerProperties; } - @Override - public Map<String, String> loadGlobalSettings() { - return load(null); - } - - @Override - public Map<String, String> loadProjectSettings() { - return load(scannerProperties.getKeyWithBranch()); - } - - private Map<String, String> load(@Nullable String componentKey) { + Map<String, String> load(@Nullable String componentKey) { String url = "api/settings/values.protobuf"; Profiler profiler = Profiler.create(LOG); if (componentKey != null) { url += "?component=" + ScannerUtils.encodeForUrl(componentKey); - profiler.startInfo("Load project settings"); + profiler.startInfo(String.format("Load project settings for component key: '%s'", componentKey)); } else { profiler.startInfo("Load global settings"); } try (InputStream is = wsClient.call(new GetRequest(url)).contentStream()) { - ValuesWsResponse values = ValuesWsResponse.parseFrom(is); + Settings.ValuesWsResponse values = Settings.ValuesWsResponse.parseFrom(is); profiler.stopInfo(); return toMap(values.getSettingsList()); } catch (HttpException e) { @@ -88,9 +73,9 @@ public class DefaultSettingsLoader implements SettingsLoader { } @VisibleForTesting - static Map<String, String> toMap(List<Setting> settingsList) { + static Map<String, String> toMap(List<Settings.Setting> settingsList) { Map<String, String> result = new LinkedHashMap<>(); - for (Setting s : settingsList) { + for (Settings.Setting s : settingsList) { if (!s.getInherited()) { switch (s.getValueOneOfCase()) { case VALUE: @@ -103,23 +88,24 @@ public class DefaultSettingsLoader implements SettingsLoader { convertPropertySetToProps(result, s); break; default: - throw new IllegalStateException("Unknow property value for " + s.getKey()); + throw new IllegalStateException("Unknown property value for " + s.getKey()); } } } return result; } - private static void convertPropertySetToProps(Map<String, String> result, Setting s) { + private static void convertPropertySetToProps(Map<String, String> result, Settings.Setting s) { List<String> ids = new ArrayList<>(); int id = 1; - for (Value v : s.getFieldValues().getFieldValuesList()) { - for (Map.Entry<String, String> entry : v.getValue().entrySet()) { + for (Settings.FieldValues.Value v : s.getFieldValues().getFieldValuesList()) { + for (Map.Entry<String, String> entry : v.getValueMap().entrySet()) { result.put(s.getKey() + "." + id + "." + entry.getKey(), entry.getValue()); } ids.add(String.valueOf(id)); id++; } - result.put(s.getKey(), ids.stream().collect(Collectors.joining(","))); + result.put(s.getKey(), String.join(",", ids)); } + } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoader.java new file mode 100644 index 00000000000..cd616563730 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoader.java @@ -0,0 +1,35 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.repository.settings; + +import java.util.Map; +import org.sonar.scanner.bootstrap.ScannerWsClient; + +public class DefaultGlobalSettingsLoader extends AbstractSettingsLoader implements GlobalSettingsLoader { + + public DefaultGlobalSettingsLoader(final ScannerWsClient wsClient) { + super(wsClient); + } + + @Override + public Map<String, String> loadGlobalSettings() { + return load(null); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoader.java new file mode 100644 index 00000000000..4274772cc8c --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoader.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.repository.settings; + +import java.util.Map; +import org.sonar.scanner.bootstrap.ProcessedScannerProperties; +import org.sonar.scanner.bootstrap.ScannerWsClient; + +public class DefaultProjectSettingsLoader extends AbstractSettingsLoader implements ProjectSettingsLoader { + private final ProcessedScannerProperties scannerProperties; + + public DefaultProjectSettingsLoader(final ScannerWsClient wsClient, final ProcessedScannerProperties scannerProperties) { + super(wsClient); + this.scannerProperties = scannerProperties; + } + + @Override + public Map<String, String> loadProjectSettings() { + return load(scannerProperties.getKeyWithBranch()); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/GlobalSettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/GlobalSettingsLoader.java new file mode 100644 index 00000000000..4ff0a80e5e2 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/GlobalSettingsLoader.java @@ -0,0 +1,26 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.repository.settings; + +import java.util.Map; + +public interface GlobalSettingsLoader { + Map<String, String> loadGlobalSettings(); +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/SettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/ProjectSettingsLoader.java index b7942a6cf8d..9904a2d6b13 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/SettingsLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/ProjectSettingsLoader.java @@ -21,8 +21,6 @@ package org.sonar.scanner.repository.settings; import java.util.Map; -public interface SettingsLoader { - Map<String, String> loadGlobalSettings(); - +public interface ProjectSettingsLoader { Map<String, String> loadProjectSettings(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/EmptyExternalProjectKeyAndOrganization.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/EmptyExternalProjectKeyAndOrganization.java new file mode 100644 index 00000000000..f6db0415e8f --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/EmptyExternalProjectKeyAndOrganization.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.Optional; +import javax.annotation.concurrent.Immutable; + +import static java.util.Optional.empty; + +@Immutable +public class EmptyExternalProjectKeyAndOrganization implements ExternalProjectKeyAndOrganization { + @Override + public Optional<String> getProjectKey() { + return empty(); + } + + @Override + public Optional<String> getOrganization() { + return empty(); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganization.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganization.java new file mode 100644 index 00000000000..e744d94cf99 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganization.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.Optional; +import javax.annotation.concurrent.Immutable; + +@Immutable +public interface ExternalProjectKeyAndOrganization { + Optional<String> getProjectKey(); + + Optional<String> getOrganization(); +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationLoader.java new file mode 100644 index 00000000000..8aa92b3220c --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationLoader.java @@ -0,0 +1,28 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.Optional; +import org.sonar.api.scanner.ScannerSide; + +@ScannerSide +public interface ExternalProjectKeyAndOrganizationLoader { + Optional<ExternalProjectKeyAndOrganization> load(); +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationProvider.java new file mode 100644 index 00000000000..c7d04564c89 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationProvider.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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 org.picocontainer.annotations.Nullable; +import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.scanner.bootstrap.RawScannerProperties; + +import static org.apache.commons.lang.StringUtils.equalsIgnoreCase; +import static org.sonar.core.config.ScannerProperties.DISABLE_PROJECT_AND_ORG_AUTODETECTION; + +public class ExternalProjectKeyAndOrganizationProvider extends ProviderAdapter { + private static final Logger LOG = Loggers.get(ExternalProjectKeyAndOrganizationProvider.class); + private ExternalProjectKeyAndOrganization properties = null; + + public ExternalProjectKeyAndOrganization provide(RawScannerProperties rawScannerProperties, + @Nullable @javax.annotation.Nullable ExternalProjectKeyAndOrganizationLoader loader) { + if (properties == null) { + boolean disableProjectKeyAndOrgAutodetection = equalsIgnoreCase( + rawScannerProperties.property(DISABLE_PROJECT_AND_ORG_AUTODETECTION), "true"); + if (disableProjectKeyAndOrgAutodetection) { + LOG.info("Skipping project and organization key auto-detection."); + } + + if (loader != null && !disableProjectKeyAndOrgAutodetection) { + properties = loader.load().orElse(new EmptyExternalProjectKeyAndOrganization()); + } else { + properties = new EmptyExternalProjectKeyAndOrganization(); + } + } + + return properties; + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java index 7dceaf350a0..5f709ee61c7 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java @@ -45,7 +45,7 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; import org.sonar.core.config.IssueExclusionProperties; -import org.sonar.scanner.bootstrap.ScannerProperties; +import org.sonar.scanner.bootstrap.ProcessedScannerProperties; import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer; import org.sonar.scanner.issue.ignore.pattern.IssueInclusionPatternInitializer; import org.sonar.scanner.util.ScannerUtils; @@ -110,12 +110,12 @@ public class ProjectReactorBuilder { private static final List<String> NON_HERITED_PROPERTIES_FOR_CHILD = Stream.concat(Stream.of(PROPERTY_PROJECT_BASEDIR, CoreProperties.WORKING_DIRECTORY, PROPERTY_MODULES, CoreProperties.PROJECT_DESCRIPTION_PROPERTY), UNSUPPORTED_PROPS_FOR_MODULES.stream()).collect(toList()); - private final ScannerProperties scannerProps; + private final ProcessedScannerProperties scannerProps; private final AnalysisWarnings analysisWarnings; private File rootProjectWorkDir; private boolean warnExclusionsAlreadyLogged; - public ProjectReactorBuilder(ScannerProperties props, AnalysisWarnings analysisWarnings) { + public ProjectReactorBuilder(ProcessedScannerProperties props, AnalysisWarnings analysisWarnings) { this.scannerProps = props; this.analysisWarnings = analysisWarnings; } 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 09900d136c1..540b88c4cac 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 @@ -45,6 +45,7 @@ import org.sonar.scanner.bootstrap.ExtensionMatcher; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.MetricProvider; import org.sonar.scanner.bootstrap.PostJobExtensionDictionnary; +import org.sonar.scanner.bootstrap.ProcessedScannerProperties; import org.sonar.scanner.cpd.CpdExecutor; import org.sonar.scanner.cpd.CpdSettings; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; @@ -91,6 +92,8 @@ import org.sonar.scanner.repository.QualityProfileLoader; import org.sonar.scanner.repository.QualityProfilesProvider; import org.sonar.scanner.repository.ServerIssuesLoader; import org.sonar.scanner.repository.language.DefaultLanguagesRepository; +import org.sonar.scanner.repository.settings.DefaultProjectSettingsLoader; +import org.sonar.scanner.repository.settings.ProjectSettingsLoader; import org.sonar.scanner.rule.ActiveRulesLoader; import org.sonar.scanner.rule.ActiveRulesProvider; import org.sonar.scanner.rule.DefaultActiveRulesLoader; @@ -142,8 +145,8 @@ public class ProjectScanContainer extends ComponentContainer { @Override protected void doBeforeStart() { - addScannerComponents(); addScannerExtensions(); + addScannerComponents(); ProjectLock lock = getComponentByType(ProjectLock.class); lock.tryLock(); getComponentByType(WorkDirectoriesInitializer.class).execute(); @@ -158,8 +161,10 @@ public class ProjectScanContainer extends ComponentContainer { private void addScannerComponents() { add( - ProjectReactorBuilder.class, + new ExternalProjectKeyAndOrganizationProvider(), + ProcessedScannerProperties.class, ScanProperties.class, + ProjectReactorBuilder.class, WorkDirectoriesInitializer.class, new MutableProjectReactorProvider(), ProjectBuildersExecutor.class, @@ -281,6 +286,7 @@ public class ProjectScanContainer extends ComponentContainer { AnalysisObservers.class); + addIfMissing(DefaultProjectSettingsLoader.class, ProjectSettingsLoader.class); addIfMissing(DefaultRulesLoader.class, RulesLoader.class); addIfMissing(DefaultActiveRulesLoader.class, ActiveRulesLoader.class); addIfMissing(DefaultQualityProfileLoader.class, QualityProfileLoader.class); 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 index a22d79cf7b9..0c4f84f2a09 100644 --- 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 @@ -26,7 +26,7 @@ import org.sonar.api.CoreProperties; import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.scanner.repository.settings.SettingsLoader; +import org.sonar.scanner.repository.settings.ProjectSettingsLoader; public class ProjectServerSettingsProvider extends ProviderAdapter { @@ -39,7 +39,7 @@ public class ProjectServerSettingsProvider extends ProviderAdapter { private ProjectServerSettings singleton = null; - public ProjectServerSettings provide(SettingsLoader loader, AnalysisWarnings analysisWarnings) { + public ProjectServerSettings provide(ProjectSettingsLoader loader, AnalysisWarnings analysisWarnings) { if (singleton == null) { Map<String, String> serverSideSettings = loader.loadProjectSettings(); if (StringUtils.isNotBlank(serverSideSettings.get(CoreProperties.MODULE_LEVEL_ARCHIVED_SETTINGS))) { |