aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersion.java26
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersionTest.java51
2 files changed, 74 insertions, 3 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersion.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersion.java
index 98a8fb0083c..43e6d230723 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersion.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersion.java
@@ -21,7 +21,10 @@ package org.sonar.ce.task.projectanalysis.source;
import java.util.HashMap;
import java.util.Map;
+import javax.annotation.CheckForNull;
+import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.component.Component;
+import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.source.LineHashVersion;
@@ -29,9 +32,13 @@ import org.sonar.db.source.LineHashVersion;
public class DbLineHashVersion {
private final Map<Component, LineHashVersion> lineHashVersionPerComponent = new HashMap<>();
private final DbClient dbClient;
+ private final AnalysisMetadataHolder analysisMetadataHolder;
+ private final ReferenceBranchComponentUuids referenceBranchComponentUuids;
- public DbLineHashVersion(DbClient dbClient) {
+ public DbLineHashVersion(DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder, ReferenceBranchComponentUuids referenceBranchComponentUuids) {
this.dbClient = dbClient;
+ this.analysisMetadataHolder = analysisMetadataHolder;
+ this.referenceBranchComponentUuids = referenceBranchComponentUuids;
}
/**
@@ -43,9 +50,24 @@ public class DbLineHashVersion {
return lineHashVersionPerComponent.computeIfAbsent(component, this::compute) == LineHashVersion.WITH_SIGNIFICANT_CODE;
}
+ @CheckForNull
private LineHashVersion compute(Component component) {
try (DbSession session = dbClient.openSession(false)) {
- return dbClient.fileSourceDao().selectLineHashesVersion(session, component.getUuid());
+ String referenceComponentUuid = getReferenceComponentUuid(component);
+ if (referenceComponentUuid != null) {
+ return dbClient.fileSourceDao().selectLineHashesVersion(session, referenceComponentUuid);
+ } else {
+ return null;
+ }
+ }
+ }
+
+ @CheckForNull
+ private String getReferenceComponentUuid(Component component) {
+ if (analysisMetadataHolder.isPullRequest()) {
+ return referenceBranchComponentUuids.getComponentUuid(component.getDbKey());
+ } else {
+ return component.getUuid();
}
}
}
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersionTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersionTest.java
index 89d1c949491..66a50b5fe11 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersionTest.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/DbLineHashVersionTest.java
@@ -21,7 +21,10 @@ package org.sonar.ce.task.projectanalysis.source;
import org.junit.Rule;
import org.junit.Test;
+import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
+import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.ce.task.projectanalysis.component.Component;
+import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
import org.sonar.ce.task.projectanalysis.component.ReportComponent;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
@@ -29,12 +32,17 @@ import org.sonar.db.component.ComponentTesting;
import org.sonar.db.source.LineHashVersion;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class DbLineHashVersionTest {
@Rule
public DbTester db = DbTester.create();
- private DbLineHashVersion underTest = new DbLineHashVersion(db.getDbClient());
+ private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
+ private ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
+ private DbLineHashVersion underTest = new DbLineHashVersion(db.getDbClient(), analysisMetadataHolder, referenceBranchComponentUuids);
@Test
public void hasLineHashWithSignificantCode_should_return_true() {
@@ -53,6 +61,47 @@ public class DbLineHashVersionTest {
}
@Test
+ public void hasLineHashWithSignificantCode_should_return_false_if_pr_reference_doesnt_have_file() {
+ when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
+ Component component = ReportComponent.builder(Component.Type.FILE, 1).setKey("key").setUuid("123").build();
+ assertThat(underTest.hasLineHashesWithSignificantCode(component)).isFalse();
+
+ verify(analysisMetadataHolder).isPullRequest();
+ verify(referenceBranchComponentUuids).getComponentUuid(component.getDbKey());
+ }
+
+ @Test
+ public void hasLineHashWithSignificantCode_should_return_false_if_pr_reference_has_file_but_it_is_not_in_db() {
+ ComponentDto project = db.components().insertPublicProject();
+ ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
+
+ when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
+ when(referenceBranchComponentUuids.getComponentUuid("key")).thenReturn(file.uuid());
+
+ Component component = ReportComponent.builder(Component.Type.FILE, 1).setKey("key").setUuid("123").build();
+ assertThat(underTest.hasLineHashesWithSignificantCode(component)).isFalse();
+
+ verify(analysisMetadataHolder).isPullRequest();
+ verify(referenceBranchComponentUuids).getComponentUuid(component.getDbKey());
+ }
+
+ @Test
+ public void hasLineHashWithSignificantCode_should_return_true_if_pr_reference_has_file_and_it_is_in_db() {
+ ComponentDto project = db.components().insertPublicProject();
+ ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
+ db.fileSources().insertFileSource(file, dto -> dto.setLineHashesVersion(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue()));
+
+ when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
+ when(referenceBranchComponentUuids.getComponentUuid("key")).thenReturn(file.uuid());
+
+ Component component = ReportComponent.builder(Component.Type.FILE, 1).setKey("key").setUuid("123").build();
+ assertThat(underTest.hasLineHashesWithSignificantCode(component)).isTrue();
+
+ verify(analysisMetadataHolder).isPullRequest();
+ verify(referenceBranchComponentUuids).getComponentUuid(component.getDbKey());
+ }
+
+ @Test
public void should_cache_line_hash_version_from_db() {
ComponentDto project = db.components().insertPublicProject();
ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));