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/src/main/java/org/sonar | |
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/src/main/java/org/sonar')
4 files changed, 23 insertions, 10 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(); |