]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10880 No hotspots on PR and short living branches
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 21 Jun 2018 08:19:35 +0000 (10:19 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 4 Jul 2018 07:31:05 +0000 (09:31 +0200)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitor.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/DefaultActiveRulesLoader.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java

index af09f83a34c056f2785effba9a55218725183ddd..84bd6d90c4d27d76498c6730d8952c7d8ea39f1a 100644 (file)
@@ -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);
+      }
     }
   }
 
index 098451022fe365e82dacfd3a3af983ad818ce0c2..3eaed17e31bb2b522f239498d57dfd38f72fbcb9 100644 (file)
@@ -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);
index 92af68671cde074b49d455bece6cff247249b24c..21e0a884165f23695b98323b1f7966b90226f237 100644 (file)
@@ -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";
   }