diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2023-12-06 16:58:03 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-01-04 20:02:48 +0000 |
commit | 7013e543f07fca1831fd1efee29997981b6ec19b (patch) | |
tree | 658cc82afdefacdfa1e1454cc78fd9c3dbf5d652 /sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap | |
parent | 8fd8c030e7dda6eb03c83eb4e59474f5e2d4e401 (diff) | |
download | sonarqube-7013e543f07fca1831fd1efee29997981b6ec19b.tar.gz sonarqube-7013e543f07fca1831fd1efee29997981b6ec19b.zip |
SONAR-21195 Enhance scanner engine to download only required plugins
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap')
6 files changed, 495 insertions, 31 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java index 5df9f8481fc..d9561394360 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ExtensionInstaller.java @@ -19,6 +19,7 @@ */ package org.sonar.scanner.bootstrap; +import java.util.Collection; import javax.annotation.Nullable; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; @@ -47,7 +48,13 @@ public class ExtensionInstaller { } // plugin extensions - for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { + installExtensionsForPlugins(container, matcher, pluginRepository.getPluginInfos()); + + return this; + } + + public void installExtensionsForPlugins(ExtensionContainer container, ExtensionMatcher matcher, Collection<PluginInfo> pluginInfos) { + for (PluginInfo pluginInfo : pluginInfos) { Plugin plugin = pluginRepository.getPluginInstance(pluginInfo.getKey()); Plugin.Context context = new PluginContextImpl.Builder() .setSonarRuntime(sonarRuntime) @@ -59,8 +66,6 @@ public class ExtensionInstaller { doInstall(container, matcher, pluginInfo, extension); } } - - return this; } private static void doInstall(ExtensionContainer container, ExtensionMatcher matcher, @Nullable PluginInfo pluginInfo, Object extension) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/PluginInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/PluginInstaller.java index 9b2c10a25f9..0e117e75136 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/PluginInstaller.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/PluginInstaller.java @@ -21,15 +21,28 @@ package org.sonar.scanner.bootstrap; import java.util.List; import java.util.Map; +import java.util.Set; public interface PluginInstaller { /** - * Gets the list of plugins installed on server and downloads them if not + * Loads/downloads all plugins that are installed on the server. + * @return information about all installed plugins, grouped by key + */ + Map<String, ScannerPlugin> installAllPlugins(); + + /** + * Gets the list of plugins that are not required for any specific languages and downloads them if not * already in local cache. * @return information about all installed plugins, grouped by key */ - Map<String, ScannerPlugin> installRemotes(); + Map<String, ScannerPlugin> installRequiredPlugins(); + + /** + * Loads/downloads plugins that are required for the given languageKeys. + * @return information about any plugins installed by this call, grouped by key + */ + Map<String, ScannerPlugin> installPluginsForLanguages(Set<String> languageKeys); /** * Used only by medium tests. diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java index 1b22fa6397a..d2453c3b6ec 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java @@ -22,11 +22,14 @@ package org.sonar.scanner.bootstrap; import com.google.gson.Gson; import java.io.File; import java.io.Reader; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; import javax.annotation.Nullable; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; @@ -48,21 +51,58 @@ public class ScannerPluginInstaller implements PluginInstaller { private final PluginFiles pluginFiles; private final DefaultScannerWsClient wsClient; + private List<InstalledPlugin> availablePlugins; + public ScannerPluginInstaller(PluginFiles pluginFiles, DefaultScannerWsClient wsClient) { this.pluginFiles = pluginFiles; this.wsClient = wsClient; } @Override - public Map<String, ScannerPlugin> installRemotes() { + public Map<String, ScannerPlugin> installAllPlugins() { + LOG.info("Loading all plugins"); + return installPlugins(p -> true).installedPluginsByKey; + } + + @Override + public Map<String, ScannerPlugin> installRequiredPlugins() { + LOG.info("Loading required plugins"); + InstallResult result = installPlugins(p -> p.getRequiredForLanguages() == null || p.getRequiredForLanguages().isEmpty()); + + LOG.debug("Plugins not loaded because they are optional: {}", result.skippedPlugins); + + return result.installedPluginsByKey; + } + + @Override + public Map<String, ScannerPlugin> installPluginsForLanguages(Set<String> languageKeys) { + LOG.info("Loading plugins for detected languages"); + LOG.debug("Detected languages: {}", languageKeys); + InstallResult result = installPlugins( + p -> p.getRequiredForLanguages() != null && !Collections.disjoint(p.getRequiredForLanguages(), languageKeys) + ); + + List<InstalledPlugin> skippedLanguagePlugins = result.skippedPlugins.stream() + .filter(p -> p.getRequiredForLanguages() != null && !p.getRequiredForLanguages().isEmpty()).toList(); + LOG.debug("Optional language-specific plugins not loaded: {}", skippedLanguagePlugins); + + return result.installedPluginsByKey; + } + + private InstallResult installPlugins(Predicate<InstalledPlugin> pluginFilter) { + if (this.availablePlugins == null) { + this.availablePlugins = listInstalledPlugins(); + } + Profiler profiler = Profiler.create(LOG).startInfo("Load/download plugins"); try { - Map<String, ScannerPlugin> result = new HashMap<>(); - Loaded loaded = loadPlugins(result); + InstallResult result = new InstallResult(); + Loaded loaded = loadPlugins(result, pluginFilter); if (!loaded.ok) { // retry once, a plugin may have been uninstalled during downloads - result.clear(); - loaded = loadPlugins(result); + this.availablePlugins = listInstalledPlugins(); + result.installedPluginsByKey.clear(); + loaded = loadPlugins(result, pluginFilter); if (!loaded.ok) { throw new IllegalStateException(format("Fail to download plugin [%s]. Not found.", loaded.notFoundPlugin)); } @@ -73,16 +113,23 @@ public class ScannerPluginInstaller implements PluginInstaller { } } - private Loaded loadPlugins(Map<String, ScannerPlugin> result) { - for (InstalledPlugin plugin : listInstalledPlugins()) { + private Loaded loadPlugins(InstallResult result, Predicate<InstalledPlugin> pluginFilter) { + List<InstalledPlugin> pluginsToInstall = availablePlugins.stream() + .filter(pluginFilter).toList(); + + for (InstalledPlugin plugin : pluginsToInstall) { Optional<File> jarFile = pluginFiles.get(plugin); if (jarFile.isEmpty()) { return new Loaded(false, plugin.key); } PluginInfo info = PluginInfo.create(jarFile.get()); - result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, PluginType.valueOf(plugin.type), info)); + result.installedPluginsByKey.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, PluginType.valueOf(plugin.type), info)); } + + result.skippedPlugins = availablePlugins.stream() + .filter(Predicate.not(pluginFilter)).toList(); + return new Loaded(true, null); } @@ -97,7 +144,7 @@ public class ScannerPluginInstaller implements PluginInstaller { /** * Gets information about the plugins installed on server (filename, checksum) */ - private InstalledPlugin[] listInstalledPlugins() { + private List<InstalledPlugin> listInstalledPlugins() { Profiler profiler = Profiler.create(LOG).startInfo("Load plugins index"); GetRequest getRequest = new GetRequest(PLUGINS_WS_URL); InstalledPlugins installedPlugins; @@ -111,8 +158,13 @@ public class ScannerPluginInstaller implements PluginInstaller { return installedPlugins.plugins; } + private static class InstallResult { + Map<String, ScannerPlugin> installedPluginsByKey = new HashMap<>(); + List<InstalledPlugin> skippedPlugins = new ArrayList<>(); + } + private static class InstalledPlugins { - InstalledPlugin[] plugins; + List<InstalledPlugin> plugins; public InstalledPlugins() { // http://stackoverflow.com/a/18645370/229031 @@ -124,10 +176,21 @@ public class ScannerPluginInstaller implements PluginInstaller { String hash; long updatedAt; String type; + private Set<String> requiredForLanguages; public InstalledPlugin() { // http://stackoverflow.com/a/18645370/229031 } + + public Set<String> getRequiredForLanguages() { + return requiredForLanguages; + } + + @Override + public String toString() { + return key; + } + } private static class Loaded { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginRepository.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginRepository.java index 1f8c747678b..4d1894b59f8 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginRepository.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginRepository.java @@ -20,14 +20,16 @@ package org.sonar.scanner.bootstrap; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.stream.Collectors; +import java.util.Set; import javax.annotation.CheckForNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.Plugin; import org.sonar.api.Startable; +import org.sonar.api.config.Configuration; import org.sonar.core.platform.ExplodedPlugin; import org.sonar.core.platform.PluginClassLoader; import org.sonar.core.platform.PluginInfo; @@ -35,6 +37,7 @@ import org.sonar.core.platform.PluginJarExploder; import org.sonar.core.platform.PluginRepository; import org.sonar.core.plugin.PluginType; +import static java.util.stream.Collectors.toMap; import static org.sonar.api.utils.Preconditions.checkState; /** @@ -47,22 +50,33 @@ public class ScannerPluginRepository implements PluginRepository, Startable { private final PluginJarExploder pluginJarExploder; private final PluginClassLoader loader; + private final Configuration properties; + private Map<String, Plugin> pluginInstancesByKeys; private Map<String, ScannerPlugin> pluginsByKeys; private Map<ClassLoader, String> keysByClassLoader; + private boolean shouldLoadAllPluginsOnStart; - public ScannerPluginRepository(PluginInstaller installer, PluginJarExploder pluginJarExploder, PluginClassLoader loader) { + public ScannerPluginRepository(PluginInstaller installer, PluginJarExploder pluginJarExploder, PluginClassLoader loader, Configuration properties) { this.installer = installer; this.pluginJarExploder = pluginJarExploder; this.loader = loader; + this.properties = properties; } @Override public void start() { - pluginsByKeys = new HashMap<>(installer.installRemotes()); - Map<String, ExplodedPlugin> explodedPLuginsByKey = pluginsByKeys.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, e -> pluginJarExploder.explode(e.getValue().getInfo()))); - pluginInstancesByKeys = new HashMap<>(loader.load(explodedPLuginsByKey)); + shouldLoadAllPluginsOnStart = properties.getBoolean("sonar.plugins.loadAll").orElse(false); + if (shouldLoadAllPluginsOnStart) { + LOG.warn("sonar.plugins.loadAll is true, so ALL available plugins will be downloaded"); + pluginsByKeys = new HashMap<>(installer.installAllPlugins()); + } else { + pluginsByKeys = new HashMap<>(installer.installRequiredPlugins()); + } + + Map<String, ExplodedPlugin> explodedPluginsByKey = pluginsByKeys.entrySet().stream() + .collect(toMap(Map.Entry::getKey, e -> pluginJarExploder.explode(e.getValue().getInfo()))); + pluginInstancesByKeys = new HashMap<>(loader.load(explodedPluginsByKey)); // this part is only used by medium tests for (Object[] localPlugin : installer.installLocals()) { @@ -77,7 +91,29 @@ public class ScannerPluginRepository implements PluginRepository, Startable { keysByClassLoader.put(e.getValue().getClass().getClassLoader(), e.getKey()); } - logPlugins(); + logPlugins(pluginsByKeys.values()); + } + + public Collection<PluginInfo> installPluginsForLanguages(Set<String> languageKeys) { + if (shouldLoadAllPluginsOnStart) { + return Collections.emptySet(); + } + + var languagePluginsByKeys = new HashMap<>(installer.installPluginsForLanguages(languageKeys)); + + pluginsByKeys.putAll(languagePluginsByKeys); + + Map<String, ExplodedPlugin> explodedPluginsByKey = languagePluginsByKeys.entrySet().stream() + .collect(toMap(Map.Entry::getKey, e -> pluginJarExploder.explode(e.getValue().getInfo()))); + pluginInstancesByKeys.putAll(new HashMap<>(loader.load(explodedPluginsByKey))); + + keysByClassLoader = new HashMap<>(); + for (Map.Entry<String, Plugin> e : pluginInstancesByKeys.entrySet()) { + keysByClassLoader.put(e.getValue().getClass().getClassLoader(), e.getKey()); + } + + logPlugins(languagePluginsByKeys.values()); + return languagePluginsByKeys.values().stream().map(ScannerPlugin::getInfo).toList(); } @CheckForNull @@ -85,12 +121,12 @@ public class ScannerPluginRepository implements PluginRepository, Startable { return keysByClassLoader.get(cl); } - private void logPlugins() { - if (pluginsByKeys.isEmpty()) { + private static void logPlugins(Collection<ScannerPlugin> plugins) { + if (plugins.isEmpty()) { LOG.debug("No plugins loaded"); } else { - LOG.debug("Plugins:"); - for (ScannerPlugin p : pluginsByKeys.values()) { + LOG.debug("Plugins loaded:"); + for (ScannerPlugin p : plugins) { LOG.debug(" * {} {} ({})", p.getName(), p.getVersion(), p.getKey()); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringGlobalContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringGlobalContainer.java index f0e1983a8c3..3c3bf87af47 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringGlobalContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringGlobalContainer.java @@ -54,9 +54,8 @@ import org.sonar.scanner.repository.DefaultMetricsRepositoryLoader; import org.sonar.scanner.repository.DefaultNewCodePeriodLoader; import org.sonar.scanner.repository.MetricsRepositoryProvider; import org.sonar.scanner.repository.settings.DefaultGlobalSettingsLoader; -import org.sonar.scanner.scan.SpringProjectScanContainer; -@Priority(3) +@Priority(4) public class SpringGlobalContainer extends SpringComponentContainer { private static final Logger LOG = LoggerFactory.getLogger(SpringGlobalContainer.class); private final Map<String, String> scannerProperties; @@ -120,7 +119,7 @@ public class SpringGlobalContainer extends SpringComponentContainer { @Override protected void doAfterStart() { - installPlugins(); + installRequiredPlugins(); loadCoreExtensions(); long startTime = System.currentTimeMillis(); @@ -136,12 +135,12 @@ public class SpringGlobalContainer extends SpringComponentContainer { throw MessageException.of("The preview mode, along with the 'sonar.analysis.mode' parameter, is no more supported. You should stop using this parameter."); } getComponentByType(RuntimeJavaVersion.class).checkJavaVersion(); - new SpringProjectScanContainer(this).execute(); + new SpringScannerContainer(this).execute(); LOG.info("Analysis total time: {}", formatTime(System.currentTimeMillis() - startTime)); } - private void installPlugins() { + private void installRequiredPlugins() { PluginRepository pluginRepository = getComponentByType(PluginRepository.class); for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { Plugin instance = pluginRepository.getPluginInstance(pluginInfo.getKey()); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java new file mode 100644 index 00000000000..0cba8e33dfc --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java @@ -0,0 +1,348 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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 javax.annotation.Nullable; +import javax.annotation.Priority; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.batch.fs.internal.FileMetadata; +import org.sonar.api.batch.fs.internal.SensorStrategy; +import org.sonar.api.batch.rule.CheckFactory; +import org.sonar.api.batch.sensor.issue.internal.DefaultNoSonarFilter; +import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.api.utils.MessageException; +import org.sonar.core.extension.CoreExtensionsInstaller; +import org.sonar.core.metric.ScannerMetrics; +import org.sonar.core.platform.SpringComponentContainer; +import org.sonar.scanner.DefaultFileLinesContextFactory; +import org.sonar.scanner.ProjectInfo; +import org.sonar.scanner.analysis.AnalysisTempFolderProvider; +import org.sonar.scanner.cache.AnalysisCacheEnabled; +import org.sonar.scanner.cache.AnalysisCacheMemoryStorage; +import org.sonar.scanner.cache.AnalysisCacheProvider; +import org.sonar.scanner.cache.DefaultAnalysisCacheLoader; +import org.sonar.scanner.ci.CiConfigurationProvider; +import org.sonar.scanner.ci.vendors.AppVeyor; +import org.sonar.scanner.ci.vendors.AwsCodeBuild; +import org.sonar.scanner.ci.vendors.AzureDevops; +import org.sonar.scanner.ci.vendors.Bamboo; +import org.sonar.scanner.ci.vendors.BitbucketPipelines; +import org.sonar.scanner.ci.vendors.Bitrise; +import org.sonar.scanner.ci.vendors.Buildkite; +import org.sonar.scanner.ci.vendors.CircleCi; +import org.sonar.scanner.ci.vendors.CirrusCi; +import org.sonar.scanner.ci.vendors.CodeMagic; +import org.sonar.scanner.ci.vendors.DroneCi; +import org.sonar.scanner.ci.vendors.GithubActions; +import org.sonar.scanner.ci.vendors.GitlabCi; +import org.sonar.scanner.ci.vendors.Jenkins; +import org.sonar.scanner.ci.vendors.SemaphoreCi; +import org.sonar.scanner.ci.vendors.TravisCi; +import org.sonar.scanner.cpd.CpdExecutor; +import org.sonar.scanner.cpd.CpdSettings; +import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; +import org.sonar.scanner.issue.IssueFilters; +import org.sonar.scanner.issue.IssuePublisher; +import org.sonar.scanner.issue.ignore.EnforceIssuesFilter; +import org.sonar.scanner.issue.ignore.IgnoreIssuesFilter; +import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer; +import org.sonar.scanner.issue.ignore.pattern.IssueInclusionPatternInitializer; +import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader; +import org.sonar.scanner.report.ActiveRulesPublisher; +import org.sonar.scanner.report.AnalysisCachePublisher; +import org.sonar.scanner.report.AnalysisContextReportPublisher; +import org.sonar.scanner.report.AnalysisWarningsPublisher; +import org.sonar.scanner.report.CeTaskReportDataHolder; +import org.sonar.scanner.report.ChangedLinesPublisher; +import org.sonar.scanner.report.ComponentsPublisher; +import org.sonar.scanner.report.ContextPropertiesPublisher; +import org.sonar.scanner.report.JavaArchitectureInformationProvider; +import org.sonar.scanner.report.MetadataPublisher; +import org.sonar.scanner.report.ReportPublisher; +import org.sonar.scanner.report.ScannerFileStructureProvider; +import org.sonar.scanner.report.SourcePublisher; +import org.sonar.scanner.report.TestExecutionPublisher; +import org.sonar.scanner.repository.ContextPropertiesCache; +import org.sonar.scanner.repository.DefaultProjectRepositoriesLoader; +import org.sonar.scanner.repository.DefaultQualityProfileLoader; +import org.sonar.scanner.repository.ProjectRepositoriesProvider; +import org.sonar.scanner.repository.QualityProfilesProvider; +import org.sonar.scanner.repository.ReferenceBranchSupplier; +import org.sonar.scanner.repository.language.DefaultLanguagesRepository; +import org.sonar.scanner.repository.language.LanguagesRepository; +import org.sonar.scanner.repository.settings.DefaultProjectSettingsLoader; +import org.sonar.scanner.rule.ActiveRulesProvider; +import org.sonar.scanner.rule.DefaultActiveRulesLoader; +import org.sonar.scanner.rule.QProfileVerifier; +import org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator; +import org.sonar.scanner.scan.InputModuleHierarchyProvider; +import org.sonar.scanner.scan.InputProjectProvider; +import org.sonar.scanner.scan.ModuleIndexer; +import org.sonar.scanner.scan.MutableProjectReactorProvider; +import org.sonar.scanner.scan.MutableProjectSettings; +import org.sonar.scanner.scan.ProjectBuildersExecutor; +import org.sonar.scanner.scan.ProjectConfigurationProvider; +import org.sonar.scanner.scan.ProjectLock; +import org.sonar.scanner.scan.ProjectReactorBuilder; +import org.sonar.scanner.scan.ProjectReactorValidator; +import org.sonar.scanner.scan.ProjectServerSettingsProvider; +import org.sonar.scanner.scan.ScanProperties; +import org.sonar.scanner.scan.SonarGlobalPropertiesFilter; +import org.sonar.scanner.scan.SpringProjectScanContainer; +import org.sonar.scanner.scan.WorkDirectoriesInitializer; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchConfigurationProvider; +import org.sonar.scanner.scan.branch.BranchType; +import org.sonar.scanner.scan.branch.ProjectBranchesProvider; +import org.sonar.scanner.scan.filesystem.DefaultProjectFileSystem; +import org.sonar.scanner.scan.filesystem.FileIndexer; +import org.sonar.scanner.scan.filesystem.InputComponentStore; +import org.sonar.scanner.scan.filesystem.LanguageDetection; +import org.sonar.scanner.scan.filesystem.MetadataGenerator; +import org.sonar.scanner.scan.filesystem.ProjectCoverageAndDuplicationExclusions; +import org.sonar.scanner.scan.filesystem.ProjectExclusionFilters; +import org.sonar.scanner.scan.filesystem.ProjectFileIndexer; +import org.sonar.scanner.scan.filesystem.ScannerComponentIdGenerator; +import org.sonar.scanner.scan.filesystem.StatusDetection; +import org.sonar.scanner.scan.measure.DefaultMetricFinder; +import org.sonar.scanner.scm.ScmChangedFilesProvider; +import org.sonar.scanner.scm.ScmConfiguration; +import org.sonar.scanner.scm.ScmPublisher; +import org.sonar.scanner.scm.ScmRevisionImpl; +import org.sonar.scanner.sensor.DefaultSensorStorage; +import org.sonar.scanner.sensor.ExecutingSensorContext; +import org.sonar.scanner.sensor.ProjectSensorContext; +import org.sonar.scanner.sensor.ProjectSensorOptimizer; +import org.sonar.scanner.sensor.UnchangedFilesHandler; +import org.sonar.scm.git.GitScmSupport; +import org.sonar.scm.svn.SvnScmSupport; + +import static org.sonar.api.batch.InstantiationStrategy.PER_BATCH; +import static org.sonar.core.extension.CoreExtensionsInstaller.noExtensionFilter; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isDeprecatedScannerSide; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isInstantiationStrategy; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isScannerSide; + +@Priority(3) +public class SpringScannerContainer extends SpringComponentContainer { + private static final Logger LOG = LoggerFactory.getLogger(SpringScannerContainer.class); + + public SpringScannerContainer(SpringComponentContainer globalContainer) { + super(globalContainer); + } + + @Override + protected void doBeforeStart() { + addScannerExtensions(); + addComponents(); + } + + private void addScannerExtensions() { + getParentComponentByType(CoreExtensionsInstaller.class) + .install(this, noExtensionFilter(), extension -> getScannerProjectExtensionsFilter().accept(extension)); + getParentComponentByType(ExtensionInstaller.class) + .install(this, getScannerProjectExtensionsFilter()); + } + + private void addComponents() { + add( + ScanProperties.class, + ProjectReactorBuilder.class, + WorkDirectoriesInitializer.class, + new MutableProjectReactorProvider(), + ProjectBuildersExecutor.class, + ProjectLock.class, + ProjectReactorValidator.class, + ProjectInfo.class, + new BranchConfigurationProvider(), + new ProjectBranchesProvider(), + ProjectRepositoriesProvider.class, + new ProjectServerSettingsProvider(), + AnalysisCacheEnabled.class, + DeprecatedPropertiesWarningGenerator.class, + + // temp + new AnalysisTempFolderProvider(), + + // file system + ModuleIndexer.class, + InputComponentStore.class, + PathResolver.class, + new InputProjectProvider(), + new InputModuleHierarchyProvider(), + ScannerComponentIdGenerator.class, + new ScmChangedFilesProvider(), + StatusDetection.class, + LanguageDetection.class, + MetadataGenerator.class, + FileMetadata.class, + FileIndexer.class, + ProjectFileIndexer.class, + ProjectExclusionFilters.class, + + // rules + new ActiveRulesProvider(), + new QualityProfilesProvider(), + CheckFactory.class, + QProfileVerifier.class, + + // issues + DefaultNoSonarFilter.class, + IssueFilters.class, + IssuePublisher.class, + + // metrics + DefaultMetricFinder.class, + + // issue exclusions + IssueInclusionPatternInitializer.class, + IssueExclusionPatternInitializer.class, + IssueExclusionsLoader.class, + EnforceIssuesFilter.class, + IgnoreIssuesFilter.class, + + // context + ContextPropertiesCache.class, + + SensorStrategy.class, + + MutableProjectSettings.class, + SonarGlobalPropertiesFilter.class, + ProjectConfigurationProvider.class, + + ProjectCoverageAndDuplicationExclusions.class, + + // Plugin cache + AnalysisCacheProvider.class, + AnalysisCacheMemoryStorage.class, + DefaultAnalysisCacheLoader.class, + + // Report + ReferenceBranchSupplier.class, + ScannerMetrics.class, + JavaArchitectureInformationProvider.class, + ReportPublisher.class, + ScannerFileStructureProvider.class, + AnalysisContextReportPublisher.class, + MetadataPublisher.class, + ActiveRulesPublisher.class, + ComponentsPublisher.class, + ContextPropertiesPublisher.class, + AnalysisCachePublisher.class, + TestExecutionPublisher.class, + SourcePublisher.class, + ChangedLinesPublisher.class, + AnalysisWarningsPublisher.class, + + CeTaskReportDataHolder.class, + + // Cpd + CpdExecutor.class, + CpdSettings.class, + SonarCpdBlockIndex.class, + + // SCM + ScmConfiguration.class, + ScmPublisher.class, + ScmRevisionImpl.class, + + // Sensors + DefaultSensorStorage.class, + DefaultFileLinesContextFactory.class, + ProjectSensorContext.class, + ProjectSensorOptimizer.class, + ExecutingSensorContext.class, + + UnchangedFilesHandler.class, + + // Filesystem + DefaultProjectFileSystem.class, + + // CI + new CiConfigurationProvider(), + AppVeyor.class, + AwsCodeBuild.class, + AzureDevops.class, + Bamboo.class, + BitbucketPipelines.class, + Bitrise.class, + Buildkite.class, + CircleCi.class, + CirrusCi.class, + DroneCi.class, + GithubActions.class, + CodeMagic.class, + GitlabCi.class, + Jenkins.class, + SemaphoreCi.class, + TravisCi.class + ); + + add(GitScmSupport.getObjects()); + add(SvnScmSupport.getObjects()); + + add(DefaultProjectSettingsLoader.class, + DefaultActiveRulesLoader.class, + DefaultQualityProfileLoader.class, + DefaultProjectRepositoriesLoader.class); + + addIfMissing(DefaultLanguagesRepository.class, LanguagesRepository.class); + + } + + static ExtensionMatcher getScannerProjectExtensionsFilter() { + return extension -> { + if (isDeprecatedScannerSide(extension)) { + return isInstantiationStrategy(extension, PER_BATCH); + } + return isScannerSide(extension); + }; + } + + @Override + protected void doAfterStart() { + ScanProperties properties = getComponentByType(ScanProperties.class); + properties.validate(); + + properties.get("sonar.branch").ifPresent(deprecatedBranch -> { + throw MessageException.of("The 'sonar.branch' parameter is no longer supported. You should stop using it. " + + "Branch analysis is available in Developer Edition and above. See https://www.sonarsource.com/plans-and-pricing/developer/ for more information."); + }); + + BranchConfiguration branchConfig = getComponentByType(BranchConfiguration.class); + if (branchConfig.branchType() == BranchType.PULL_REQUEST) { + LOG.info("Pull request {} for merge into {} from {}", branchConfig.pullRequestKey(), pullRequestBaseToDisplayName(branchConfig.targetBranchName()), + branchConfig.branchName()); + } else if (branchConfig.branchName() != null) { + LOG.info("Branch name: {}", branchConfig.branchName()); + } + + getComponentByType(DeprecatedPropertiesWarningGenerator.class).execute(); + + getComponentByType(ProjectFileIndexer.class).index(); + new SpringProjectScanContainer(this).execute(); + } + + private static String pullRequestBaseToDisplayName(@Nullable String pullRequestBase) { + return pullRequestBase != null ? pullRequestBase : "default branch"; + } + +} |