]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12721 add pullRequest and branch to response api/hotspots/show
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 13 Dec 2019 10:46:33 +0000 (11:46 +0100)
committerSonarTech <sonartech@sonarsource.com>
Mon, 13 Jan 2020 19:46:29 +0000 (20:46 +0100)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java
sonar-ws/src/main/protobuf/ws-hotspots.proto

index 0ac88897685dbc5c35a4f9bac9343ed5d5eb7f59..2ce7e3c3036048d90a32c9a8ab63fb454ead1373 100644 (file)
@@ -40,6 +40,8 @@ public class HotspotWsResponseFormatter {
       .setQualifier(component.qualifier())
       .setName(component.name())
       .setLongName(component.longName());
+    ofNullable(component.getBranch()).ifPresent(builder::setBranch);
+    ofNullable(component.getPullRequest()).ifPresent(builder::setPullRequest);
     ofNullable(component.path()).ifPresent(builder::setPath);
     return builder.build();
   }
index 1ae8411818a40994a07442970ec814a29babebeb..2317cee6b050665835513db42d66844fd9db1f6f 100644 (file)
@@ -45,6 +45,7 @@ import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.issue.IssueDto;
 import org.sonar.db.issue.IssueTesting;
@@ -241,8 +242,8 @@ public class ShowActionTest {
       .executeProtobuf(Hotspots.ShowWsResponse.class);
 
     assertThat(response.getKey()).isEqualTo(hotspot.getKey());
-    verifyComponent(response.getComponent(), file);
-    verifyComponent(response.getProject(), project);
+    verifyComponent(response.getComponent(), file, null, null);
+    verifyComponent(response.getProject(), project, null, null);
     verifyRule(response.getRule(), rule);
     assertThat(response.hasTextRange()).isFalse();
   }
@@ -511,8 +512,47 @@ public class ShowActionTest {
     Hotspots.ShowWsResponse response = newRequest(hotspot)
       .executeProtobuf(Hotspots.ShowWsResponse.class);
 
-    verifyComponent(response.getProject(), project);
-    verifyComponent(response.getComponent(), project);
+    verifyComponent(response.getProject(), project, null, null);
+    verifyComponent(response.getComponent(), project, null, null);
+  }
+
+  @Test
+  public void returns_branch_but_no_pullRequest_on_component_and_project_on_non_main_branch() {
+    ComponentDto project = dbTester.components().insertPublicProject();
+    ComponentDto branch = dbTester.components().insertProjectBranch(project);
+    ComponentDto file = dbTester.components().insertComponent(newFileDto(branch));
+    userSessionRule.registerComponents(project);
+    RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
+    IssueDto hotspot = dbTester.issues().insertIssue(newHotspot(branch, file, rule)
+      .setLocations(DbIssues.Locations.newBuilder()
+        .setTextRange(DbCommons.TextRange.newBuilder().build())
+        .build()));
+
+    Hotspots.ShowWsResponse response = newRequest(hotspot)
+      .executeProtobuf(Hotspots.ShowWsResponse.class);
+
+    verifyComponent(response.getProject(), branch, branch.getBranch(), null);
+    verifyComponent(response.getComponent(), file, branch.getBranch(), null);
+  }
+
+  @Test
+  public void returns_pullRequest_but_no_branch_on_component_and_project_on_pullRequest() {
+    ComponentDto project = dbTester.components().insertPublicProject();
+    ComponentDto pullRequest = dbTester.components().insertProjectBranch(project,
+      t -> t.setBranchType(BranchType.PULL_REQUEST));
+    ComponentDto file = dbTester.components().insertComponent(newFileDto(pullRequest));
+    userSessionRule.registerComponents(project);
+    RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
+    IssueDto hotspot = dbTester.issues().insertIssue(newHotspot(pullRequest, file, rule)
+      .setLocations(DbIssues.Locations.newBuilder()
+        .setTextRange(DbCommons.TextRange.newBuilder().build())
+        .build()));
+
+    Hotspots.ShowWsResponse response = newRequest(hotspot)
+      .executeProtobuf(Hotspots.ShowWsResponse.class);
+
+    verifyComponent(response.getProject(), pullRequest, null, pullRequest.getPullRequest());
+    verifyComponent(response.getComponent(), file, null, pullRequest.getPullRequest());
   }
 
   @Test
@@ -552,7 +592,7 @@ public class ShowActionTest {
     assertThat(wsRule.getVulnerabilityProbability()).isEqualTo(SQCategory.OTHERS.getVulnerability().name());
   }
 
-  private static void verifyComponent(Hotspots.Component wsComponent, ComponentDto dto) {
+  private static void verifyComponent(Hotspots.Component wsComponent, ComponentDto dto, @Nullable String branch, @Nullable String pullRequest) {
     assertThat(wsComponent.getKey()).isEqualTo(dto.getKey());
     if (dto.path() == null) {
       assertThat(wsComponent.hasPath()).isFalse();
@@ -562,6 +602,16 @@ public class ShowActionTest {
     assertThat(wsComponent.getQualifier()).isEqualTo(dto.qualifier());
     assertThat(wsComponent.getName()).isEqualTo(dto.name());
     assertThat(wsComponent.getLongName()).isEqualTo(dto.longName());
+    if (branch == null) {
+      assertThat(wsComponent.hasBranch()).isFalse();
+    } else {
+      assertThat(wsComponent.getBranch()).isEqualTo(branch);
+    }
+    if (pullRequest == null) {
+      assertThat(wsComponent.hasPullRequest()).isFalse();
+    } else {
+      assertThat(wsComponent.getPullRequest()).isEqualTo(pullRequest);
+    }
   }
 
   private static IssueDto newHotspot(ComponentDto project, ComponentDto file, RuleDefinitionDto rule) {
index 4d4f4efce302118c8e127e4900d83f5788ce8b42..1606042472faa412cb266cc382ee155d7970255f 100644 (file)
@@ -74,6 +74,8 @@ message Component {
   optional string name = 4;
   optional string longName = 5;
   optional string path = 6;
+  optional string branch = 7;
+  optional string pullRequest = 8;
 }
 
 message Rule {