From 1269984e8e09338c057d068d715ade7df5a0c354 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 14 Mar 2022 11:27:18 -0500 Subject: SONAR-16115 Store plugin's scanner cache in SonarQube --- .../sonar/scanner/protocol/output/FileStructure.java | 4 ++++ .../scanner/protocol/output/ScannerReportReader.java | 17 +++++++++++++++++ .../scanner/protocol/output/ScannerReportWriter.java | 7 +++++++ .../src/main/protobuf/scanner_internal.proto | 8 ++++++++ .../protocol/output/ScannerReportReaderTest.java | 20 ++++++++++++++++++++ 5 files changed, 56 insertions(+) create mode 100644 sonar-scanner-protocol/src/main/protobuf/scanner_internal.proto (limited to 'sonar-scanner-protocol') diff --git a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java index a7bafb3e307..041bf6cbeff 100644 --- a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java +++ b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/FileStructure.java @@ -66,6 +66,10 @@ public class FileStructure { return new File(dir, "metadata.pb"); } + public File pluginCache() { + return new File(dir, "plugin-cache.pb"); + } + public File analysisLog() { return new File(dir, "analysis.log"); } diff --git a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java index 8ebb7b623f1..aaa0190a802 100644 --- a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java +++ b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportReader.java @@ -19,7 +19,11 @@ */ package org.sonar.scanner.protocol.output; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; import javax.annotation.CheckForNull; import org.sonar.core.util.CloseableIterator; import org.sonar.core.util.Protobuf; @@ -75,6 +79,19 @@ public class ScannerReportReader { return null; } + @CheckForNull + public InputStream getPluginCache() { + File file = fileStructure.pluginCache(); + if (fileExists(file)) { + try { + return new BufferedInputStream(new FileInputStream(fileStructure.pluginCache())); + } catch (FileNotFoundException e) { + throw new IllegalStateException("Unable to open file " + fileStructure.pluginCache(), e); + } + } + return null; + } + public ScannerReport.Component readComponent(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, componentRef); if (!fileExists(file)) { diff --git a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java index 3ae3fa517db..88f16d14d19 100644 --- a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java +++ b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/output/ScannerReportWriter.java @@ -26,6 +26,7 @@ import java.io.OutputStream; import javax.annotation.concurrent.Immutable; import org.sonar.core.util.ContextException; import org.sonar.core.util.Protobuf; +import org.sonar.scanner.protocol.internal.ScannerInternal; @Immutable public class ScannerReportWriter { @@ -94,6 +95,12 @@ public class ScannerReportWriter { return file; } + public File writePluginCache(ScannerInternal.PluginCacheMsg cacheMsg) { + File file = fileStructure.pluginCache(); + Protobuf.writeGzip(cacheMsg, file); + return file; + } + public void appendComponentExternalIssue(int componentRef, ScannerReport.ExternalIssue issue) { File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { diff --git a/sonar-scanner-protocol/src/main/protobuf/scanner_internal.proto b/sonar-scanner-protocol/src/main/protobuf/scanner_internal.proto new file mode 100644 index 00000000000..b0d38058c4c --- /dev/null +++ b/sonar-scanner-protocol/src/main/protobuf/scanner_internal.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +option java_package = "org.sonar.scanner.protocol.internal"; +option optimize_for = SPEED; + +message PluginCacheMsg { + map map = 1; +} diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java index 88ef3b84a52..58333d108cf 100644 --- a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java +++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java @@ -20,9 +20,13 @@ package org.sonar.scanner.protocol.output; import com.google.common.collect.Lists; +import com.google.protobuf.ByteString; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.util.AbstractMap; +import java.util.zip.GZIPInputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.junit.Before; @@ -30,9 +34,12 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.core.util.CloseableIterator; +import org.sonar.core.util.Protobuf; +import org.sonar.scanner.protocol.internal.ScannerInternal.PluginCacheMsg; import org.sonar.scanner.protocol.output.ScannerReport.Measure.StringValue; import org.sonar.scanner.protocol.output.ScannerReport.SyntaxHighlightingRule.HighlightingType; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -204,6 +211,19 @@ public class ScannerReportReaderTest { assertThat(sut.readCpdTextBlocks(1)).toIterable().hasSize(1); } + @Test + public void read_plugin_cache() throws IOException { + ScannerReportWriter writer = new ScannerReportWriter(dir); + writer.writePluginCache(PluginCacheMsg.newBuilder() + .putMap("key", ByteString.copyFrom("data", UTF_8)) + .build()); + + ScannerReportReader reader = new ScannerReportReader(dir); + + PluginCacheMsg cache = Protobuf.read(new GZIPInputStream(reader.getPluginCache()), PluginCacheMsg.parser()); + assertThat(cache.getMapMap()).containsOnly(new AbstractMap.SimpleEntry<>("key", ByteString.copyFrom("data", UTF_8))); + } + @Test public void empty_list_if_no_duplication_block_found() { assertThat(underTest.readComponentDuplications(UNKNOWN_COMPONENT_REF)).toIterable().isEmpty(); -- cgit v1.2.3