diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-03-24 15:56:09 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-25 20:02:53 +0000 |
commit | a2b4550cb7891479d4e0af5ad48423914362d34f (patch) | |
tree | 469021e0dc88af8da62b2fcc08b618064a893342 /sonar-scanner-engine | |
parent | 12f329db53ca150f774cee9143fea19102356fb9 (diff) | |
download | sonarqube-a2b4550cb7891479d4e0af5ad48423914362d34f.tar.gz sonarqube-a2b4550cb7891479d4e0af5ad48423914362d34f.zip |
SONAR-16187 Analysis cache gets cache from different branch when needed
Diffstat (limited to 'sonar-scanner-engine')
8 files changed, 56 insertions, 30 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java index 035d3cf0010..4832bd774b0 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java @@ -55,10 +55,8 @@ public class AnalysisCacheLoader { public Optional<AnalysisCacheMsg> load() { String url = URL + "?project=" + project.key(); - if (branchConfiguration.branchType() == BranchType.BRANCH && branchConfiguration.branchName() != null) { - url = url + "&branch=" + branchConfiguration.branchName(); - } else if (branchConfiguration.isPullRequest()) { - url = url + "&pullRequest=" + branchConfiguration.pullRequestKey(); + if (branchConfiguration.referenceBranchName() != null) { + url = url + "&branch=" + branchConfiguration.referenceBranchName(); } GetRequest request = new GetRequest(url).setHeader(ACCEPT_ENCODING, "gzip"); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheProvider.java index b340261f569..6424d67f422 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheProvider.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.util.Map; import org.jetbrains.annotations.Nullable; import org.sonar.api.batch.sensor.cache.ReadCache; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.springframework.context.annotation.Bean; import static java.util.Collections.emptyMap; @@ -38,9 +39,9 @@ public class AnalysisCacheProvider { } @Bean("WriteCache") - public ScannerWriteCache provideWriter(AnalysisCacheEnabled analysisCacheEnabled, ReadCache readCache) { + public ScannerWriteCache provideWriter(AnalysisCacheEnabled analysisCacheEnabled, ReadCache readCache, BranchConfiguration branchConfiguration) { if (analysisCacheEnabled.isEnabled()) { - return new WriteCacheImpl(readCache); + return new WriteCacheImpl(readCache, branchConfiguration); } return new NoOpWriteCache(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/WriteCacheImpl.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/WriteCacheImpl.java index adeb6216d23..748bfdacc5b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/WriteCacheImpl.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/WriteCacheImpl.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; import org.sonar.api.batch.sensor.cache.ReadCache; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static java.util.Collections.unmodifiableMap; import static org.sonar.api.utils.Preconditions.checkArgument; @@ -32,16 +33,21 @@ import static org.sonar.api.utils.Preconditions.checkNotNull; public class WriteCacheImpl implements ScannerWriteCache { private final ReadCache readCache; + private final BranchConfiguration branchConfiguration; private final Map<String, byte[]> cache = new HashMap<>(); - public WriteCacheImpl(ReadCache readCache) { + public WriteCacheImpl(ReadCache readCache, BranchConfiguration branchConfiguration) { this.readCache = readCache; + this.branchConfiguration = branchConfiguration; } @Override public void write(String key, InputStream data) { checkNotNull(data); checkKey(key); + if (branchConfiguration.isPullRequest()) { + return; + } try { byte[] arr = data.readAllBytes(); cache.put(key, arr); @@ -54,6 +60,9 @@ public class WriteCacheImpl implements ScannerWriteCache { public void write(String key, byte[] data) { checkNotNull(data); checkKey(key); + if (branchConfiguration.isPullRequest()) { + return; + } cache.put(key, Arrays.copyOf(data, data.length)); } @@ -61,7 +70,9 @@ public class WriteCacheImpl implements ScannerWriteCache { public void copyFromPrevious(String key) { checkArgument(readCache.contains(key), "Previous cache doesn't contain key '%s'", key); checkKey(key); - + if (branchConfiguration.isPullRequest()) { + return; + } try { cache.put(key, readCache.read(key).readAllBytes()); } catch (IOException e) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisCachePublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisCachePublisher.java index 6daae79356d..fd637fc606f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisCachePublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/AnalysisCachePublisher.java @@ -26,19 +26,22 @@ import org.sonar.scanner.cache.ScannerWriteCache; import org.sonar.scanner.protocol.internal.ScannerInternal; import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg; import org.sonar.scanner.protocol.output.ScannerReportWriter; +import org.sonar.scanner.scan.branch.BranchConfiguration; public class AnalysisCachePublisher implements ReportPublisherStep { private final AnalysisCacheEnabled analysisCacheEnabled; + private final BranchConfiguration branchConfiguration; private final ScannerWriteCache cache; - public AnalysisCachePublisher(AnalysisCacheEnabled analysisCacheEnabled, ScannerWriteCache cache) { + public AnalysisCachePublisher(AnalysisCacheEnabled analysisCacheEnabled, BranchConfiguration branchConfiguration, ScannerWriteCache cache) { this.analysisCacheEnabled = analysisCacheEnabled; + this.branchConfiguration = branchConfiguration; this.cache = cache; } @Override public void publish(ScannerReportWriter writer) { - if (!analysisCacheEnabled.isEnabled() || cache.getCache().isEmpty()) { + if (!analysisCacheEnabled.isEnabled() || branchConfiguration.isPullRequest() || cache.getCache().isEmpty()) { return; } AnalysisCacheMsg.Builder analysisCacheMsg = ScannerInternal.AnalysisCacheMsg.newBuilder(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java index 1c1a04c7e08..52eb5b484df 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java @@ -74,8 +74,7 @@ public class AnalysisCacheLoaderTest { @Test public void loads_content_for_branch() throws IOException { - when(branchConfiguration.branchType()).thenReturn(BranchType.BRANCH); - when(branchConfiguration.branchName()).thenReturn("name"); + when(branchConfiguration.referenceBranchName()).thenReturn("name"); setResponse(MSG); AnalysisCacheMsg msg = loader.load().get(); @@ -85,16 +84,6 @@ public class AnalysisCacheLoaderTest { } @Test - public void loads_content_for_pr() throws IOException { - when(branchConfiguration.isPullRequest()).thenReturn(true); - when(branchConfiguration.pullRequestKey()).thenReturn("key"); - setResponse(MSG); - AnalysisCacheMsg msg = loader.load().get(); - assertThat(msg).isEqualTo(MSG); - assertRequestPath("api/analysis_cache/get?project=myproject&pullRequest=key"); - } - - @Test public void loads_compressed_content() throws IOException { setCompressedResponse(MSG); AnalysisCacheMsg msg = loader.load().get(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheProviderTest.java index 5435e815c5f..840ca3fd585 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheProviderTest.java @@ -21,6 +21,7 @@ package org.sonar.scanner.cache; import org.junit.Test; import org.sonar.api.batch.sensor.cache.ReadCache; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -29,10 +30,11 @@ import static org.mockito.Mockito.when; public class AnalysisCacheProviderTest { - AnalysisCacheEnabled analysisCacheEnabled = mock(AnalysisCacheEnabled.class); - AnalysisCacheMemoryStorage storage = mock(AnalysisCacheMemoryStorage.class); - ReadCache readCache = mock(ReadCache.class); - AnalysisCacheProvider cacheProvider = new AnalysisCacheProvider(); + private final AnalysisCacheEnabled analysisCacheEnabled = mock(AnalysisCacheEnabled.class); + private final AnalysisCacheMemoryStorage storage = mock(AnalysisCacheMemoryStorage.class); + private final ReadCache readCache = mock(ReadCache.class); + private final AnalysisCacheProvider cacheProvider = new AnalysisCacheProvider(); + private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class); @Test public void provide_noop_reader_cache_when_disable() { @@ -44,7 +46,7 @@ public class AnalysisCacheProviderTest { @Test public void provide_noop_writer_cache_when_disable() { when(analysisCacheEnabled.isEnabled()).thenReturn(false); - var cache = cacheProvider.provideWriter(analysisCacheEnabled, readCache); + var cache = cacheProvider.provideWriter(analysisCacheEnabled, readCache, branchConfiguration); assertThat(cache).isInstanceOf(AnalysisCacheProvider.NoOpWriteCache.class); } @@ -59,7 +61,7 @@ public class AnalysisCacheProviderTest { @Test public void provide_real_writer_cache_when_enable() { when(analysisCacheEnabled.isEnabled()).thenReturn(true); - var cache = cacheProvider.provideWriter(analysisCacheEnabled, readCache); + var cache = cacheProvider.provideWriter(analysisCacheEnabled, readCache, branchConfiguration); assertThat(cache).isInstanceOf(WriteCacheImpl.class); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/WriteCacheImplTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/WriteCacheImplTest.java index 653ded84d72..da86bf8c8bb 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/WriteCacheImplTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/WriteCacheImplTest.java @@ -22,6 +22,7 @@ package org.sonar.scanner.cache; import java.io.ByteArrayInputStream; import java.io.InputStream; import org.junit.Test; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -31,7 +32,8 @@ import static org.mockito.Mockito.when; public class WriteCacheImplTest { private final ReadCacheImpl readCache = mock(ReadCacheImpl.class); - private final WriteCacheImpl writeCache = new WriteCacheImpl(readCache); + private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class); + private final WriteCacheImpl writeCache = new WriteCacheImpl(readCache, branchConfiguration); @Test public void write_bytes_adds_entries() { @@ -44,6 +46,15 @@ public class WriteCacheImplTest { } @Test + public void dont_write_if_its_pull_request() { + byte[] b1 = new byte[] {1, 2, 3}; + when(branchConfiguration.isPullRequest()).thenReturn(true); + writeCache.write("key1", b1); + writeCache.write("key2", new ByteArrayInputStream(b1)); + assertThat(writeCache.getCache()).isEmpty(); + } + + @Test public void write_inputStream_adds_entries() { byte[] b1 = new byte[] {1, 2, 3}; byte[] b2 = new byte[] {3, 4}; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisCachePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisCachePublisherTest.java index c79939c16af..31e16737c83 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisCachePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisCachePublisherTest.java @@ -29,6 +29,7 @@ import org.junit.rules.TemporaryFolder; import org.sonar.scanner.cache.AnalysisCacheEnabled; import org.sonar.scanner.cache.ScannerWriteCache; import org.sonar.scanner.protocol.output.ScannerReportWriter; +import org.sonar.scanner.scan.branch.BranchConfiguration; import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.assertThat; @@ -44,7 +45,8 @@ public class AnalysisCachePublisherTest { private final ScannerWriteCache writeCache = mock(ScannerWriteCache.class); private final AnalysisCacheEnabled analysisCacheEnabled = mock(AnalysisCacheEnabled.class); - private final AnalysisCachePublisher publisher = new AnalysisCachePublisher(analysisCacheEnabled, writeCache); + private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class); + private final AnalysisCachePublisher publisher = new AnalysisCachePublisher(analysisCacheEnabled, branchConfiguration, writeCache); private ScannerReportWriter scannerReportWriter; @@ -62,6 +64,15 @@ public class AnalysisCachePublisherTest { } @Test + public void publish_does_nothing_if_pull_request() { + when(analysisCacheEnabled.isEnabled()).thenReturn(true); + when(branchConfiguration.isPullRequest()).thenReturn(true); + publisher.publish(scannerReportWriter); + verifyNoInteractions(writeCache); + assertThat(scannerReportWriter.getFileStructure().root()).isEmptyDirectory(); + } + + @Test public void publish_cache() { when(writeCache.getCache()).thenReturn(Map.of("key1", "value1".getBytes(StandardCharsets.UTF_8))); when(analysisCacheEnabled.isEnabled()).thenReturn(true); |