]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17321 expose ruleKey in hotspot.search endpoint
authorBenjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com>
Mon, 26 Sep 2022 07:04:16 +0000 (09:04 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 26 Sep 2022 20:03:17 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java
server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/search-example.json
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java
sonar-ws/src/main/protobuf/ws-hotspots.proto

index 6abb2b5bc4392c7e079afe3ad7018b2936925047..6ee7ced5fc8c8728abe5a684af2de63c25362a46 100644 (file)
@@ -200,7 +200,8 @@ public class SearchAction implements HotspotsWsAction {
       .setInternal(true)
       .setChangelog(
         new Change("9.6", "Added parameters 'pciDss-3.2' and 'pciDss-4.0"),
-        new Change("9.7", "Hotspot flows in the response may contain a description and a type"));
+        new Change("9.7", "Hotspot flows in the response may contain a description and a type"),
+        new Change("9.7", "Hotspot in the response contain the corresponding ruleKey"));
 
     action.addPagingParams(100);
     action.createParam(PARAM_PROJECT_KEY)
@@ -582,7 +583,8 @@ public class SearchAction implements HotspotsWsAction {
         .setComponent(hotspot.getComponentKey())
         .setProject(hotspot.getProjectKey())
         .setSecurityCategory(sqCategory.getKey())
-        .setVulnerabilityProbability(sqCategory.getVulnerability().name());
+        .setVulnerabilityProbability(sqCategory.getVulnerability().name())
+        .setRuleKey(hotspot.getRuleKey().toString());
       ofNullable(hotspot.getStatus()).ifPresent(builder::setStatus);
       ofNullable(hotspot.getResolution()).ifPresent(builder::setResolution);
       ofNullable(hotspot.getLine()).ifPresent(builder::setLine);
index acab5d5bd5059dd54dfdb4d4644d6f25e90d5721..f23e1a62e077fc90ef8f2d5468fec2342f98745e 100644 (file)
@@ -17,7 +17,9 @@
       "assignee": "assignee-uuid",
       "author": "joe",
       "creationDate": "2020-01-02T15:43:10+0100",
-      "updateDate": "2020-01-02T15:43:10+0100"
+      "updateDate": "2020-01-02T15:43:10+0100",
+      "flows": [],
+      "ruleKey": "repository-0:rule-0"
     },
     {
       "key": "hotspot-1",
@@ -31,7 +33,9 @@
       "assignee": "assignee-uuid",
       "author": "joe",
       "creationDate": "2020-01-02T15:43:10+0100",
-      "updateDate": "2020-01-02T15:43:10+0100"
+      "updateDate": "2020-01-02T15:43:10+0100",
+      "flows": [],
+      "ruleKey": "repository-1:rule-1"
     },
     {
       "key": "hotspot-2",
@@ -45,7 +49,9 @@
       "assignee": "assignee-uuid",
       "author": "joe",
       "creationDate": "2020-01-02T15:43:10+0100",
-      "updateDate": "2020-01-02T15:43:10+0100"
+      "updateDate": "2020-01-02T15:43:10+0100",
+      "flows": [],
+      "ruleKey": "repository-2:rule-2"
     }
   ],
   "components": [
@@ -63,4 +69,4 @@
       "longName": "test-project"
     }
   ]
-}
+}
\ No newline at end of file
index 20c49231e418d23c04e71f0c0c9466419c01bf7c..858f38546604c8fc26ab40963a827d9306159794 100644 (file)
@@ -41,6 +41,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.sonar.api.impl.utils.TestSystem2;
 import org.sonar.api.issue.Issue;
+import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rules.RuleType;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
@@ -1841,7 +1842,8 @@ public class SearchActionTest {
 
     IssueDto[] hotspots = IntStream.range(0, 3)
       .mapToObj(i -> {
-        RuleDto rule = newRule(SECURITY_HOTSPOT)
+        RuleKey ruleKey = RuleKey.of("repository-"+i,"rule-"+i);
+        RuleDto rule = newRule(SECURITY_HOTSPOT,ruleKey)
           .setSecurityStandards(Sets.newHashSet(SQCategory.WEAK_CRYPTOGRAPHY.getKey()));
         return insertHotspot(rule, project, fileWithHotspot, issueDto -> issueDto.setKee("hotspot-" + i)
           .setAssigneeUuid("assignee-uuid")
@@ -1860,6 +1862,24 @@ public class SearchActionTest {
       .assertJson(actionTester.getDef().responseExampleAsString());
   }
 
+  @Test
+  public void returns_hotspots_with_ruleKey() {
+    ComponentDto project = dbTester.components().insertPublicProject();
+    userSessionRule.registerComponents(project);
+    indexPermissions();
+    ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
+    RuleDto rule1 = newRule(SECURITY_HOTSPOT);
+    insertHotspot(project, file, rule1);
+    indexIssues();
+
+    SearchWsResponse response = newRequest(project)
+      .executeProtobuf(SearchWsResponse.class);
+
+    assertThat(response.getHotspotsList())
+      .extracting(SearchWsResponse.Hotspot::getRuleKey)
+      .containsExactly(rule1.getKey().toString());
+  }
+
   private IssueDto insertHotspot(ComponentDto project, ComponentDto file, RuleDto rule) {
     return insertHotspot(rule, project, file, t -> {
     });
@@ -1960,6 +1980,13 @@ public class SearchActionTest {
     });
   }
 
+  private RuleDto newRule(RuleType ruleType, RuleKey ruleKey){
+    RuleDto ruleDto = RuleTesting.newRule(ruleKey)
+      .setType(ruleType);
+    dbTester.rules().insert(ruleDto);
+    return ruleDto;
+  }
+
   private RuleDto newRule(RuleType ruleType, Consumer<RuleDto> populate) {
     RuleDto ruleDto = RuleTesting.newRule()
       .setType(ruleType);
index 41b89d19269b432fb4ba5e3c274824495951a3c0..d725326a1e03d62041648305f00f562c19ec94ac 100644 (file)
@@ -48,6 +48,7 @@ message SearchWsResponse {
     optional string updateDate = 13;
     optional sonarqube.ws.commons.TextRange textRange = 14;
     repeated sonarqube.ws.commons.Flow flows = 15;
+    optional string ruleKey = 16;
   }
 }