diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2022-10-13 16:23:01 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-10-14 20:03:03 +0000 |
commit | cf8ab09b8d5fe9e72be77ef610d911b40f177214 (patch) | |
tree | 63b1383d356d44f13aab3f2fce69d3a51e38be66 /sonar-scanner-engine/src | |
parent | fdb78927f43c324750c185f105ade4d151fa9193 (diff) | |
download | sonarqube-cf8ab09b8d5fe9e72be77ef610d911b40f177214.tar.gz sonarqube-cf8ab09b8d5fe9e72be77ef610d911b40f177214.zip |
SONAR-16633 splitting plugins into two categories in the scanner context
Diffstat (limited to 'sonar-scanner-engine/src')
11 files changed, 78 insertions, 16 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPlugin.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPlugin.java index 9de35691fb6..bc9696365e9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPlugin.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPlugin.java @@ -20,6 +20,7 @@ package org.sonar.scanner.bootstrap; import org.sonar.core.platform.PluginInfo; +import org.sonar.core.plugin.PluginType; import org.sonar.updatecenter.common.Version; public class ScannerPlugin { @@ -27,10 +28,12 @@ public class ScannerPlugin { private final String key; private final long updatedAt; private final PluginInfo info; + private final PluginType type; - public ScannerPlugin(String key, long updatedAt, PluginInfo info) { + public ScannerPlugin(String key, long updatedAt, PluginType type, PluginInfo info) { this.key = key; this.updatedAt = updatedAt; + this.type = type; this.info = info; } @@ -54,4 +57,7 @@ public class ScannerPlugin { return updatedAt; } + public PluginType getType() { + return type; + } } 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 83821718d88..bdad9644779 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 @@ -32,6 +32,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.platform.PluginInfo; +import org.sonar.core.plugin.PluginType; import org.sonarqube.ws.client.GetRequest; import static java.lang.String.format; @@ -75,12 +76,12 @@ public class ScannerPluginInstaller implements PluginInstaller { private Loaded loadPlugins(Map<String, ScannerPlugin> result) { for (InstalledPlugin plugin : listInstalledPlugins()) { Optional<File> jarFile = pluginFiles.get(plugin); - if (!jarFile.isPresent()) { + 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, info)); + result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, PluginType.valueOf(plugin.type), info)); } return new Loaded(true, null); } @@ -122,6 +123,7 @@ public class ScannerPluginInstaller implements PluginInstaller { String key; String hash; long updatedAt; + String type; public InstalledPlugin() { // http://stackoverflow.com/a/18645370/229031 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 fcf68cc3a04..a123741bbb0 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 @@ -33,6 +33,7 @@ import org.sonar.core.platform.PluginClassLoader; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginJarExploder; import org.sonar.core.platform.PluginRepository; +import org.sonar.core.plugin.PluginType; import static java.util.stream.Collectors.toList; import static org.sonar.api.utils.Preconditions.checkState; @@ -68,7 +69,7 @@ public class ScannerPluginRepository implements PluginRepository, Startable { for (Object[] localPlugin : installer.installLocals()) { String pluginKey = (String) localPlugin[0]; PluginInfo pluginInfo = new PluginInfo(pluginKey); - pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginInfo.getKey(), (long) localPlugin[2], pluginInfo)); + pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginInfo.getKey(), (long) localPlugin[2], PluginType.BUNDLED, pluginInfo)); pluginInstancesByKeys.put(pluginKey, (Plugin) localPlugin[1]); } @@ -115,6 +116,14 @@ public class ScannerPluginRepository implements PluginRepository, Startable { return pluginsByKeys.values().stream().map(ScannerPlugin::getInfo).collect(toList()); } + public Collection<PluginInfo> getExternalPluginsInfos() { + return pluginsByKeys.values().stream().filter(p -> p.getType() == PluginType.EXTERNAL).map(ScannerPlugin::getInfo).collect(toList()); + } + + public Collection<PluginInfo> getBundledPluginsInfos() { + return pluginsByKeys.values().stream().filter(p -> p.getType() == PluginType.BUNDLED).map(ScannerPlugin::getInfo).collect(toList()); + } + @Override public PluginInfo getPluginInfo(String key) { ScannerPlugin info = pluginsByKeys.get(key); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java index 5800b912a7b..fcdd2af7be6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java @@ -27,6 +27,7 @@ import java.util.Map; import javax.annotation.Priority; import org.sonar.api.Plugin; import org.sonar.core.platform.PluginInfo; +import org.sonar.core.plugin.PluginType; import org.sonar.scanner.bootstrap.PluginInstaller; import org.sonar.scanner.bootstrap.ScannerPlugin; @@ -37,7 +38,7 @@ public class FakePluginInstaller implements PluginInstaller { private final List<Object[]> mediumTestPlugins = new ArrayList<>(); public FakePluginInstaller add(String pluginKey, File jarFile, long lastUpdatedAt) { - pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginKey, lastUpdatedAt, PluginInfo.create(jarFile))); + pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginKey, lastUpdatedAt, PluginType.BUNDLED, PluginInfo.create(jarFile))); return this; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java index ce0c1b5a166..31eabd7a9fb 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisContextReportPublisher.java @@ -71,6 +71,7 @@ public class AnalysisContextReportPublisher { File analysisLog = writer.getFileStructure().analysisLog(); try (BufferedWriter fileWriter = Files.newBufferedWriter(analysisLog.toPath(), StandardCharsets.UTF_8)) { writePlugins(fileWriter); + writeBundledAnalyzers(fileWriter); writeGlobalSettings(fileWriter); writeProjectSettings(fileWriter); writeModulesSettings(fileWriter); @@ -80,8 +81,15 @@ public class AnalysisContextReportPublisher { } private void writePlugins(BufferedWriter fileWriter) throws IOException { - fileWriter.write("SonarQube plugins:\n"); - for (PluginInfo p : pluginRepo.getPluginInfos()) { + fileWriter.write("Plugins:\n"); + for (PluginInfo p : pluginRepo.getExternalPluginsInfos()) { + fileWriter.append(String.format(" - %s %s (%s)", p.getName(), p.getVersion(), p.getKey())).append('\n'); + } + } + + private void writeBundledAnalyzers(BufferedWriter fileWriter) throws IOException { + fileWriter.write("Bundled analyzers:\n"); + for (PluginInfo p : pluginRepo.getBundledPluginsInfos()) { fileWriter.append(String.format(" - %s %s (%s)", p.getName(), p.getVersion(), p.getKey())).append('\n'); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java index bc6222af6a1..29e3046f26e 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java @@ -28,6 +28,7 @@ import org.sonar.core.platform.ExplodedPlugin; import org.sonar.core.platform.PluginClassLoader; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginJarExploder; +import org.sonar.core.plugin.PluginType; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -47,7 +48,7 @@ public class ScannerPluginRepositoryTest { @Test public void install_and_load_plugins() { PluginInfo info = new PluginInfo("java"); - ImmutableMap<String, ScannerPlugin> plugins = ImmutableMap.of("java", new ScannerPlugin("java", 1L, info)); + ImmutableMap<String, ScannerPlugin> plugins = ImmutableMap.of("java", new ScannerPlugin("java", 1L, PluginType.EXTERNAL, info)); Plugin instance = mock(Plugin.class); when(loader.load(anyMap())).thenReturn(ImmutableMap.of("java", instance)); when(installer.installRemotes()).thenReturn(plugins); @@ -59,6 +60,8 @@ public class ScannerPluginRepositoryTest { assertThat(underTest.getPluginInfo("java")).isSameAs(info); assertThat(underTest.getPluginInstance("java")).isSameAs(instance); assertThat(underTest.getPluginInstances()).containsOnly(instance); + assertThat(underTest.getBundledPluginsInfos()).isEmpty(); + assertThat(underTest.getExternalPluginsInfos()).isEqualTo(underTest.getPluginInfos()); underTest.stop(); verify(loader).unload(anyCollection()); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java index 54561735cee..a0d39fd35dd 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java @@ -21,6 +21,7 @@ package org.sonar.scanner.report; import com.google.common.collect.ImmutableMap; import java.io.File; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; @@ -53,8 +54,6 @@ import static org.mockito.Mockito.when; public class AnalysisContextReportPublisherTest { - private static final String BIZ = "BIZ"; - private static final String FOO = "FOO"; private static final String SONAR_SKIP = "sonar.skip"; private static final String COM_FOO = "com.foo"; @@ -64,7 +63,7 @@ public class AnalysisContextReportPublisherTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - private ScannerPluginRepository pluginRepo = mock(ScannerPluginRepository.class); + private final ScannerPluginRepository pluginRepo = mock(ScannerPluginRepository.class); private AnalysisContextReportPublisher publisher; private System2 system2; private GlobalServerSettings globalServerSettings; @@ -86,7 +85,7 @@ public class AnalysisContextReportPublisherTest { @Test public void shouldOnlyDumpPluginsByDefault() throws Exception { - when(pluginRepo.getPluginInfos()).thenReturn(singletonList(new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0")))); + when(pluginRepo.getExternalPluginsInfos()).thenReturn(singletonList(new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0")))); ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() @@ -138,7 +137,8 @@ public class AnalysisContextReportPublisherTest { List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); assertThat(lines).containsExactly( - "SonarQube plugins:", + "Plugins:", + "Bundled analyzers:", "Global server settings:", "Project server settings:", " - com.foo=bar", @@ -238,7 +238,8 @@ public class AnalysisContextReportPublisherTest { List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); assertThat(lines).containsExactly( - "SonarQube plugins:", + "Plugins:", + "Bundled analyzers:", "Global server settings:", "Project server settings:", "Project scanner properties:", @@ -248,4 +249,29 @@ public class AnalysisContextReportPublisherTest { " - sonar.projectKey=foo" ); } + + @Test + public void init_splitsPluginsByTypeInTheFile() throws IOException { + ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); + DefaultInputModule parent = new DefaultInputModule(ProjectDefinition.create() + .setBaseDir(temp.newFolder()) + .setWorkDir(temp.newFolder()) + .setProperty("sonar.projectKey", "parent") + .setProperty(SONAR_SKIP, "true")); + when(hierarchy.root()).thenReturn(parent); + + when(pluginRepo.getExternalPluginsInfos()).thenReturn(List.of(new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0")))); + when(pluginRepo.getBundledPluginsInfos()).thenReturn(List.of(new PluginInfo("java").setName("Java").setVersion(Version.create("9.7")))); + + publisher.init(writer); + + List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); + + System.out.println(lines); + + assertThat(lines).contains("Plugins:", + " - Xoo 1.0 (xoo)", + "Bundled analyzers:", + " - Java 9.7 (java)"); + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index f10faf46a4b..fd677d2aaf9 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -41,6 +41,7 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.scm.ScmProvider; +import org.sonar.core.plugin.PluginType; import org.sonar.scanner.ProjectInfo; import org.sonar.scanner.bootstrap.ScannerPlugin; import org.sonar.scanner.bootstrap.ScannerPluginRepository; @@ -123,8 +124,8 @@ public class MetadataPublisherTest { Date date = new Date(); when(qProfiles.findAll()).thenReturn(Collections.singletonList(new QProfile("q1", "Q1", "java", date))); when(pluginRepository.getPluginsByKey()).thenReturn(ImmutableMap.of( - "java", new ScannerPlugin("java", 12345L, null), - "php", new ScannerPlugin("php", 45678L, null))); + "java", new ScannerPlugin("java", 12345L, PluginType.BUNDLED, null), + "php", new ScannerPlugin("php", 45678L, PluginType.BUNDLED, null))); File outputDir = temp.newFolder(); ScannerReportWriter writer = new ScannerReportWriter(outputDir); when(referenceBranchSupplier.getFromProperties()).thenReturn("newCodeReference"); diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/blue-installed.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/blue-installed.json index c33470d53c5..656149c3d19 100644 --- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/blue-installed.json +++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/blue-installed.json @@ -3,11 +3,13 @@ { "key": "scmgit", "hash": "abc", + "type": "BUNDLED", "updatedAt": 100 }, { "key": "java", "hash": "def", + "type": "EXTERNAL", "updatedAt": 200 } ] diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/green-installed.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/green-installed.json index 1379b68eca3..caf8abc663f 100644 --- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/green-installed.json +++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/green-installed.json @@ -3,11 +3,13 @@ { "key": "java", "hash": "def", + "type": "BUNDLED", "updatedAt": 200 }, { "key": "cobol", "hash": "ghi", + "type": "EXTERNAL", "updatedAt": 300 } ] diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json index c33470d53c5..656149c3d19 100644 --- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json +++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json @@ -3,11 +3,13 @@ { "key": "scmgit", "hash": "abc", + "type": "BUNDLED", "updatedAt": 100 }, { "key": "java", "hash": "def", + "type": "EXTERNAL", "updatedAt": 200 } ] |