aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2018-06-21 10:19:35 +0200
committersonartech <sonartech@sonarsource.com>2018-07-04 09:31:05 +0200
commit10594fcb0ba85b4c78431d9e2f854d82baf87a54 (patch)
treeb016bf58096bb9d4b1141b47929591c8854a1ca7 /sonar-scanner-engine
parentbcb9937aa3d92eb934c089d59b0ddcb6ca28f864 (diff)
downloadsonarqube-10594fcb0ba85b4c78431d9e2f854d82baf87a54.tar.gz
sonarqube-10594fcb0ba85b4c78431d9e2f854d82baf87a54.zip
SONAR-10880 No hotspots on PR and short living branches
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/DefaultActiveRulesLoader.java26
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java46
2 files changed, 62 insertions, 10 deletions
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<LoadedActiveRule> 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<LoadedActiveRule> 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";
}