From: Sébastien Lesaint Date: Fri, 13 Dec 2019 10:46:33 +0000 (+0100) Subject: SONAR-12721 add pullRequest and branch to response api/hotspots/show X-Git-Tag: 8.2.0.32929~191 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3353f49ff648be72119b47d7a77884b5dc2ffbee;p=sonarqube.git SONAR-12721 add pullRequest and branch to response api/hotspots/show --- diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java index 0ac88897685..2ce7e3c3036 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java @@ -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(); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java index 1ae8411818a..2317cee6b05 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java @@ -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) { diff --git a/sonar-ws/src/main/protobuf/ws-hotspots.proto b/sonar-ws/src/main/protobuf/ws-hotspots.proto index 4d4f4efce30..1606042472f 100644 --- a/sonar-ws/src/main/protobuf/ws-hotspots.proto +++ b/sonar-ws/src/main/protobuf/ws-hotspots.proto @@ -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 {