From 147ad4c393cf43774f83b7dba4b80b375bbe3c4e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 24 Jan 2018 13:37:58 +0100 Subject: [PATCH] SONAR-10315 do not fail if a plugin has been uninstalled between analysis and CE processing --- .../step/LoadReportAnalysisMetadataHolderStep.java | 6 ++---- .../server/plugins/ServerPluginJarExploder.java | 2 -- .../LoadReportAnalysisMetadataHolderStepTest.java | 14 +++++++++----- .../org/sonar/core/platform/PluginRepository.java | 4 ++++ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java index 1010269638c..5fe57312e81 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java @@ -29,7 +29,6 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.MessageException; import org.sonar.ce.queue.CeTask; import org.sonar.core.component.ComponentKeys; -import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; @@ -129,13 +128,12 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep { @CheckForNull private String getBasePluginKey(Plugin p) { - PluginInfo pluginInfo = pluginRepository.getPluginInfo(p.getKey()); - if (pluginInfo == null) { + if (!pluginRepository.hasPlugin(p.getKey())) { // May happen if plugin was uninstalled between start of scanner analysis and now. // But it doesn't matter since all active rules are removed anyway, so no issues will be reported return null; } - return pluginInfo.getBasePlugin(); + return pluginRepository.getPluginInfo(p.getKey()).getBasePlugin(); } /** diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java index dc2aeb6e76e..178a36334e3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java @@ -21,7 +21,6 @@ package org.sonar.server.plugins; import java.io.File; import org.apache.commons.io.FileUtils; -import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.ZipUtils; import org.sonar.core.platform.ExplodedPlugin; @@ -32,7 +31,6 @@ import org.sonar.server.platform.ServerFileSystem; import static org.apache.commons.io.FileUtils.forceMkdir; @ServerSide -@ComputeEngineSide public class ServerPluginJarExploder extends PluginJarExploder { private final ServerFileSystem fs; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java index ba8bf417e1e..f47b84d1dbe 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java @@ -302,20 +302,24 @@ public class LoadReportAnalysisMetadataHolderStepTest { } @Test - public void execute_read_plugins_from_report() { + public void execute_reads_plugins_from_report() { ScannerReport.Metadata.Builder metadataBuilder = newBatchReportBuilder(); metadataBuilder.getMutablePluginsByKey().put("java", ScannerReport.Metadata.Plugin.newBuilder().setKey("java").setUpdatedAt(12345L).build()); metadataBuilder.getMutablePluginsByKey().put("php", ScannerReport.Metadata.Plugin.newBuilder().setKey("php").setUpdatedAt(678910L).build()); metadataBuilder.getMutablePluginsByKey().put("customjava", ScannerReport.Metadata.Plugin.newBuilder().setKey("customjava").setUpdatedAt(111111L).build()); - when(pluginRepository.getPluginInfo("customjava")).thenReturn(new PluginInfo("customjava").setBasePlugin("java")); - reportReader.setMetadata(metadataBuilder.build()); + when(pluginRepository.hasPlugin("java")).thenReturn(true); + when(pluginRepository.getPluginInfo("java")).thenReturn(new PluginInfo("java")); + when(pluginRepository.hasPlugin("customjava")).thenReturn(true); + when(pluginRepository.getPluginInfo("customjava")).thenReturn(new PluginInfo("customjava").setBasePlugin("java")); + // php plugin has been uninstalled between runs of scanner and compute engine + when(pluginRepository.hasPlugin("php")).thenReturn(false); + underTest.execute(); - assertThat(analysisMetadataHolder.getScannerPluginsByKey()).containsOnlyKeys("java", "php", "customjava"); assertThat(analysisMetadataHolder.getScannerPluginsByKey().values()).extracting(ScannerPlugin::getKey, ScannerPlugin::getBasePluginKey, ScannerPlugin::getUpdatedAt) - .containsOnly( + .containsExactlyInAnyOrder( tuple("java", null, 12345L), tuple("customjava", "java", 111111L), tuple("php", null, 678910L)); diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java b/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java index e2f053cd744..42c44f8f284 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/PluginRepository.java @@ -35,6 +35,10 @@ public interface PluginRepository { Collection getPluginInfos(); + /** + * Never returns {@code null}, a runtime exception is thrown + * if no plugins have the specified key. + */ PluginInfo getPluginInfo(String key); /** -- 2.39.5