From 2b8cc73a602be586c15754f7d87242dbaa5ccfd7 Mon Sep 17 00:00:00 2001 From: Michal Duda Date: Fri, 1 Feb 2019 12:51:32 +0100 Subject: SONARCLOUD-336: Automatically guess the projectKey/organization --- .../scanner/analysis/DefaultAnalysisMode.java | 4 +- .../scanner/bootstrap/GlobalAnalysisMode.java | 2 +- .../bootstrap/GlobalConfigurationProvider.java | 3 +- .../sonar/scanner/bootstrap/GlobalContainer.java | 12 +- .../bootstrap/GlobalServerSettingsProvider.java | 4 +- .../bootstrap/GlobalTempFolderProvider.java | 4 +- .../bootstrap/ProcessedScannerProperties.java | 82 ++++++++++++++ .../scanner/bootstrap/RawScannerProperties.java | 66 +++++++++++ .../sonar/scanner/bootstrap/ScannerProperties.java | 93 --------------- .../scanner/bootstrap/ScannerWsClientProvider.java | 4 +- .../repository/ProjectRepositoriesProvider.java | 4 +- .../repository/QualityProfilesProvider.java | 6 +- .../settings/AbstractSettingsLoader.java | 111 ++++++++++++++++++ .../settings/DefaultGlobalSettingsLoader.java | 35 ++++++ .../settings/DefaultProjectSettingsLoader.java | 38 +++++++ .../repository/settings/DefaultSettingsLoader.java | 125 --------------------- .../repository/settings/GlobalSettingsLoader.java | 26 +++++ .../repository/settings/ProjectSettingsLoader.java | 26 +++++ .../repository/settings/SettingsLoader.java | 28 ----- .../EmptyExternalProjectKeyAndOrganization.java | 38 +++++++ .../scan/ExternalProjectKeyAndOrganization.java | 30 +++++ .../ExternalProjectKeyAndOrganizationLoader.java | 28 +++++ .../ExternalProjectKeyAndOrganizationProvider.java | 53 +++++++++ .../sonar/scanner/scan/ProjectReactorBuilder.java | 6 +- .../sonar/scanner/scan/ProjectScanContainer.java | 10 +- .../scan/ProjectServerSettingsProvider.java | 4 +- 26 files changed, 568 insertions(+), 274 deletions(-) create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ProcessedScannerProperties.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/RawScannerProperties.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerProperties.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/AbstractSettingsLoader.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultGlobalSettingsLoader.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultProjectSettingsLoader.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/GlobalSettingsLoader.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/ProjectSettingsLoader.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/SettingsLoader.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/EmptyExternalProjectKeyAndOrganization.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganization.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationLoader.java create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ExternalProjectKeyAndOrganizationProvider.java (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner') 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 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 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 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 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/RawScannerProperties.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/RawScannerProperties.java new file mode 100644 index 00000000000..343e08c3d50 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/RawScannerProperties.java @@ -0,0 +1,66 @@ +/* + * 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.concurrent.Immutable; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Encryption; + +/** + * Properties that are coming from scanner. + */ +@Immutable +public class RawScannerProperties { + + private final Map properties; + private final Encryption encryption; + + public RawScannerProperties(Map properties) { + encryption = new Encryption(properties.get(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); + Map decryptedProps = new HashMap<>(properties.size()); + for (Map.Entry entry : properties.entrySet()) { + String value = entry.getValue(); + if (value != null && encryption.isEncrypted(value)) { + try { + value = encryption.decrypt(value); + } catch (Exception e) { + throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); + } + } + decryptedProps.put(entry.getKey(), value); + } + this.properties = decryptedProps; + } + + public Encryption getEncryption() { + return encryption; + } + + public Map 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/bootstrap/ScannerProperties.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerProperties.java deleted file mode 100644 index a351765cfb6..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerProperties.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.api.config.Encryption; - -import static org.apache.commons.lang.StringUtils.trimToNull; - -/** - * Properties that are coming from scanner. - */ -@Immutable -public class ScannerProperties implements ProjectKey { - - private final Map properties; - private final Encryption encryption; - - public ScannerProperties(Map properties) { - encryption = new Encryption(properties.get(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); - Map decryptedProps = new HashMap<>(properties.size()); - for (Map.Entry entry : properties.entrySet()) { - String value = entry.getValue(); - if (value != null && encryption.isEncrypted(value)) { - try { - value = encryption.decrypt(value); - } catch (Exception e) { - throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); - } - } - decryptedProps.put(entry.getKey(), value); - } - this.properties = decryptedProps; - } - - public Encryption getEncryption() { - return encryption; - } - - public Map 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/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 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/AbstractSettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/AbstractSettingsLoader.java new file mode 100644 index 00000000000..797acbfcdb9 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/AbstractSettingsLoader.java @@ -0,0 +1,111 @@ +/* + * 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 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; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +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.ScannerWsClient; +import org.sonar.scanner.util.ScannerUtils; +import org.sonarqube.ws.Settings; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpException; + +public abstract class AbstractSettingsLoader { + + private static final Logger LOG = Loggers.get(AbstractSettingsLoader.class); + private final ScannerWsClient wsClient; + + public AbstractSettingsLoader(final ScannerWsClient wsClient) { + this.wsClient = wsClient; + } + + Map 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(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()) { + Settings.ValuesWsResponse values = Settings.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("Unable to load settings", e); + } + } + + @VisibleForTesting + static Map toMap(List settingsList) { + Map result = new LinkedHashMap<>(); + for (Settings.Setting s : settingsList) { + if (!s.getInherited()) { + switch (s.getValueOneOfCase()) { + case VALUE: + result.put(s.getKey(), s.getValue()); + break; + case VALUES: + result.put(s.getKey(), s.getValues().getValuesList().stream().map(StringEscapeUtils::escapeCsv).collect(Collectors.joining(","))); + break; + case FIELDVALUES: + convertPropertySetToProps(result, s); + break; + default: + throw new IllegalStateException("Unknown property value for " + s.getKey()); + } + } + } + return result; + } + + private static void convertPropertySetToProps(Map result, Settings.Setting s) { + List ids = new ArrayList<>(); + int id = 1; + for (Settings.FieldValues.Value v : s.getFieldValues().getFieldValuesList()) { + for (Map.Entry entry : v.getValueMap().entrySet()) { + result.put(s.getKey() + "." + id + "." + entry.getKey(), entry.getValue()); + } + ids.add(String.valueOf(id)); + id++; + } + 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 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 loadProjectSettings() { + return load(scannerProperties.getKeyWithBranch()); + } +} 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 deleted file mode 100644 index e3f820b0cbb..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/DefaultSettingsLoader.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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 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; -import java.util.stream.Collectors; -import javax.annotation.Nullable; -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.client.GetRequest; -import org.sonarqube.ws.client.HttpException; - -public class DefaultSettingsLoader implements SettingsLoader { - - private ScannerWsClient wsClient; - private final ScannerProperties scannerProperties; - private static final Logger LOG = Loggers.get(DefaultSettingsLoader.class); - - public DefaultSettingsLoader(ScannerWsClient wsClient, ScannerProperties scannerProperties) { - this.wsClient = wsClient; - this.scannerProperties = scannerProperties; - } - - @Override - public Map loadGlobalSettings() { - return load(null); - } - - @Override - public Map loadProjectSettings() { - return load(scannerProperties.getKeyWithBranch()); - } - - private Map 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"); - } else { - profiler.startInfo("Load global settings"); - } - try (InputStream is = wsClient.call(new GetRequest(url)).contentStream()) { - 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("Unable to load settings", e); - } - } - - @VisibleForTesting - static Map toMap(List settingsList) { - Map result = new LinkedHashMap<>(); - for (Setting s : settingsList) { - if (!s.getInherited()) { - switch (s.getValueOneOfCase()) { - case VALUE: - result.put(s.getKey(), s.getValue()); - break; - case VALUES: - result.put(s.getKey(), s.getValues().getValuesList().stream().map(StringEscapeUtils::escapeCsv).collect(Collectors.joining(","))); - break; - case FIELDVALUES: - convertPropertySetToProps(result, s); - break; - default: - throw new IllegalStateException("Unknow property value for " + s.getKey()); - } - } - } - return result; - } - - private static void convertPropertySetToProps(Map result, Setting s) { - List ids = new ArrayList<>(); - int id = 1; - for (Value v : s.getFieldValues().getFieldValuesList()) { - for (Map.Entry entry : v.getValue().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(","))); - } -} 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 loadGlobalSettings(); +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/ProjectSettingsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/ProjectSettingsLoader.java new file mode 100644 index 00000000000..9904a2d6b13 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/ProjectSettingsLoader.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 ProjectSettingsLoader { + Map loadProjectSettings(); +} 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/SettingsLoader.java deleted file mode 100644 index b7942a6cf8d..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/SettingsLoader.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 SettingsLoader { - Map loadGlobalSettings(); - - Map 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 getProjectKey() { + return empty(); + } + + @Override + public Optional 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 getProjectKey(); + + Optional 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 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 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 serverSideSettings = loader.loadProjectSettings(); if (StringUtils.isNotBlank(serverSideSettings.get(CoreProperties.MODULE_LEVEL_ARCHIVED_SETTINGS))) { -- cgit v1.2.3