diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-03-14 11:27:18 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-18 20:02:57 +0000 |
commit | 1269984e8e09338c057d068d715ade7df5a0c354 (patch) | |
tree | b483c992fd8c57b79188c936fb9bfb6ff7955ed8 /sonar-scanner-protocol/src | |
parent | e987de516356fe37ee8eb35793d5fd709e2a099b (diff) | |
download | sonarqube-1269984e8e09338c057d068d715ade7df5a0c354.tar.gz sonarqube-1269984e8e09338c057d068d715ade7df5a0c354.zip |
SONAR-16115 Store plugin's scanner cache in SonarQube
Diffstat (limited to 'sonar-scanner-protocol/src')
5 files changed, 56 insertions, 0 deletions
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<string, bytes> 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; @@ -205,6 +212,19 @@ public class ScannerReportReaderTest { } @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(); } |