Browse Source

SONAR-11151 Only cache new lines from the report

tags/7.5
Duarte Meneses 5 years ago
parent
commit
411fd8ef50

+ 13
- 9
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepository.java View File

@@ -38,7 +38,7 @@ public class NewLinesRepository {
private final AnalysisMetadataHolder analysisMetadataHolder;
private final ScmInfoRepository scmInfoRepository;
private final PeriodHolder periodHolder;
private final Map<Component, Optional<Set<Integer>>> changedLinesCache = new HashMap<>();
private final Map<Component, Optional<Set<Integer>>> reportChangedLinesCache = new HashMap<>();

public NewLinesRepository(BatchReportReader reportReader, AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder, ScmInfoRepository scmInfoRepository) {
this.reportReader = reportReader;
@@ -48,14 +48,10 @@ public class NewLinesRepository {
}

public boolean newLinesAvailable() {
return periodHolder.hasPeriod();
return isPullRequestOrShortLivedBranch() || periodHolder.hasPeriod();
}

public Optional<Set<Integer>> getNewLines(Component component) {
return changedLinesCache.computeIfAbsent(component, this::computeNewLines);
}

private Optional<Set<Integer>> computeNewLines(Component component) {
Optional<Set<Integer>> reportChangedLines = getChangedLinesFromReport(component);
if (reportChangedLines.isPresent()) {
return reportChangedLines;
@@ -98,11 +94,19 @@ public class NewLinesRepository {
}

private Optional<Set<Integer>> getChangedLinesFromReport(Component component) {
if (analysisMetadataHolder.isPullRequest() || analysisMetadataHolder.isShortLivingBranch()) {
return reportReader.readComponentChangedLines(component.getReportAttributes().getRef())
.map(c -> new HashSet<>(c.getLineList()));
if (isPullRequestOrShortLivedBranch()) {
return reportChangedLinesCache.computeIfAbsent(component, this::readFromReport);
}

return Optional.empty();
}

private boolean isPullRequestOrShortLivedBranch() {
return analysisMetadataHolder.isPullRequest() || analysisMetadataHolder.isShortLivingBranch();
}

private Optional<Set<Integer>> readFromReport(Component component) {
return reportReader.readComponentChangedLines(component.getReportAttributes().getRef())
.map(c -> new HashSet<>(c.getLineList()));
}
}

+ 1
- 2
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepositoryTest.java View File

@@ -56,7 +56,6 @@ public class NewLinesRepositoryTest {

@Test
public void load_new_lines_from_report_if_available_and_pullrequest() {
periodHolder.setPeriod(new Period("", null, Long.MAX_VALUE, ""));
setPullRequest();
createChangedLinesInReport(1, 2, 5);

@@ -80,7 +79,7 @@ public class NewLinesRepositoryTest {
}

@Test
public void return_empty_if_no_period() {
public void return_empty_if_no_period_and_not_pullrequest() {
periodHolder.setPeriod(null);

// even though we have lines in the report and scm data, nothing should be returned since we have no period

Loading…
Cancel
Save