From: Julien HENRY Date: Thu, 21 Jun 2018 08:19:35 +0000 (+0200) Subject: SONAR-10880 No hotspots on PR and short living branches X-Git-Tag: 7.5~881 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=10594fcb0ba85b4c78431d9e2f854d82baf87a54;p=sonarqube.git SONAR-10880 No hotspots on PR and short living branches --- diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitor.java index af09f83a34c..84bd6d90c4d 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitor.java @@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.issue; import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.sonar.api.rules.RuleType; import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; import org.sonar.ce.task.projectanalysis.component.Component; import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit; @@ -76,7 +77,9 @@ public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter { newIssues.forEach(issue -> { issueLifecycle.initNewOpenIssue(issue); - list.add(issue); + if (analysisMetadataHolder.isLongLivingBranch() || issue.type() != RuleType.SECURITY_HOTSPOT) { + list.add(issue); + } }); if (list.isEmpty()) { @@ -106,7 +109,9 @@ public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter { DefaultIssue raw = entry.getKey(); DefaultIssue base = entry.getValue(); issueLifecycle.mergeExistingOpenIssue(raw, base); - process(component, raw, cacheAppender); + if (analysisMetadataHolder.isLongLivingBranch() || raw.type() != RuleType.SECURITY_HOTSPOT) { + process(component, raw, cacheAppender); + } } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/DefaultActiveRulesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/DefaultActiveRulesLoader.java index 098451022fe..3eaed17e31b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/DefaultActiveRulesLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/DefaultActiveRulesLoader.java @@ -21,13 +21,17 @@ package org.sonar.scanner.rule; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.io.IOUtils; import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.RuleType; import org.sonar.scanner.bootstrap.ScannerWsClient; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.util.ScannerUtils; import org.sonarqube.ws.Rules; import org.sonarqube.ws.Rules.Active; @@ -42,11 +46,23 @@ import static org.sonar.api.utils.DateUtils.parseDateTime; public class DefaultActiveRulesLoader implements ActiveRulesLoader { private static final String RULES_SEARCH_URL = "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives,createdAt&activation=true"; + private static final String RULES_SEARCH_NO_HOTSPOT_URL; + + static { + // need to use static initializer because of https://bugs.openjdk.java.net/browse/JDK-8077605 + RULES_SEARCH_NO_HOTSPOT_URL = RULES_SEARCH_URL + "&types=" + + Arrays.stream(RuleType.values()) + .filter(t -> t != RuleType.SECURITY_HOTSPOT) + .map(Enum::name) + .collect(Collectors.joining(",")); + } private final ScannerWsClient wsClient; + private final BranchConfiguration branchConfiguration; - public DefaultActiveRulesLoader(ScannerWsClient wsClient) { + public DefaultActiveRulesLoader(ScannerWsClient wsClient, BranchConfiguration branchConfiguration) { this.wsClient = wsClient; + this.branchConfiguration = branchConfiguration; } @Override @@ -72,9 +88,13 @@ public class DefaultActiveRulesLoader implements ActiveRulesLoader { return ruleList; } - private static String getUrl(String qualityProfileKey, int page, int pageSize) { + private String getUrl(String qualityProfileKey, int page, int pageSize) { StringBuilder builder = new StringBuilder(1024); - builder.append(RULES_SEARCH_URL); + if (branchConfiguration.isShortOrPullRequest()) { + builder.append(RULES_SEARCH_NO_HOTSPOT_URL); + } else { + builder.append(RULES_SEARCH_URL); + } builder.append("&qprofile=").append(ScannerUtils.encodeForUrl(qualityProfileKey)); builder.append("&p=").append(page); builder.append("&ps=").append(pageSize); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java index 92af68671cd..21e0a884165 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java @@ -31,6 +31,7 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; import org.sonar.scanner.WsTestUtil; import org.sonar.scanner.bootstrap.ScannerWsClient; +import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonarqube.ws.Rules; import org.sonarqube.ws.Rules.Active; import org.sonarqube.ws.Rules.ActiveList; @@ -42,6 +43,7 @@ import org.sonarqube.ws.Rules.SearchResponse.Builder; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; public class DefaultActiveRulesLoaderTest { @@ -54,19 +56,22 @@ public class DefaultActiveRulesLoaderTest { private DefaultActiveRulesLoader loader; private ScannerWsClient wsClient; + private BranchConfiguration branchConfig; @Before public void setUp() { wsClient = mock(ScannerWsClient.class); - loader = new DefaultActiveRulesLoader(wsClient); + branchConfig = mock(BranchConfiguration.class); + when(branchConfig.isShortOrPullRequest()).thenReturn(false); + loader = new DefaultActiveRulesLoader(wsClient, branchConfig); } @Test public void feed_real_response_encode_qp() throws IOException { int total = PAGE_SIZE_1 + PAGE_SIZE_2; - WsTestUtil.mockStream(wsClient, urlOfPage(1), responseOfSize(PAGE_SIZE_1, total)); - WsTestUtil.mockStream(wsClient, urlOfPage(2), responseOfSize(PAGE_SIZE_2, total)); + WsTestUtil.mockStream(wsClient, urlOfPage(1, false), responseOfSize(PAGE_SIZE_1, total)); + WsTestUtil.mockStream(wsClient, urlOfPage(2, false), responseOfSize(PAGE_SIZE_2, total)); Collection activeRules = loader.load("c+-test_c+-values-17445"); assertThat(activeRules).hasSize(total); @@ -80,14 +85,41 @@ public class DefaultActiveRulesLoaderTest { .extracting(LoadedActiveRule::getSeverity) .containsExactly(SEVERITY_VALUE); - WsTestUtil.verifyCall(wsClient, urlOfPage(1)); - WsTestUtil.verifyCall(wsClient, urlOfPage(2)); + WsTestUtil.verifyCall(wsClient, urlOfPage(1, false)); + WsTestUtil.verifyCall(wsClient, urlOfPage(2, false)); verifyNoMoreInteractions(wsClient); } - private String urlOfPage(int page) { - return "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives,createdAt&activation=true&qprofile=c%2B-test_c%2B-values-17445&p=" + page + @Test + public void no_hotspots_on_pr_or_short_branches() throws IOException { + when(branchConfig.isShortOrPullRequest()).thenReturn(true); + int total = PAGE_SIZE_1 + PAGE_SIZE_2; + + WsTestUtil.mockStream(wsClient, urlOfPage(1, true), responseOfSize(PAGE_SIZE_1, total)); + WsTestUtil.mockStream(wsClient, urlOfPage(2, true), responseOfSize(PAGE_SIZE_2, total)); + + Collection activeRules = loader.load("c+-test_c+-values-17445"); + assertThat(activeRules).hasSize(total); + assertThat(activeRules) + .filteredOn(r -> r.getRuleKey().equals(EXAMPLE_KEY)) + .extracting(LoadedActiveRule::getParams) + .extracting(p -> p.get(FORMAT_KEY)) + .containsExactly(FORMAT_VALUE); + assertThat(activeRules) + .filteredOn(r -> r.getRuleKey().equals(EXAMPLE_KEY)) + .extracting(LoadedActiveRule::getSeverity) + .containsExactly(SEVERITY_VALUE); + + WsTestUtil.verifyCall(wsClient, urlOfPage(1, true)); + WsTestUtil.verifyCall(wsClient, urlOfPage(2, true)); + + verifyNoMoreInteractions(wsClient); + } + + private String urlOfPage(int page, boolean noHotspots) { + return "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives,createdAt&activation=true" + + (noHotspots ? "&types=CODE_SMELL,BUG,VULNERABILITY" : "") + "&qprofile=c%2B-test_c%2B-values-17445&p=" + page + "&ps=500"; }