From 39c96105a7f039155d4a508122ad6a1ba8a79b54 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 22 Aug 2017 17:10:04 +0200 Subject: [PATCH] SONAR-9616 mergeBranch returns default main branch when empty --- .../projectbranch/ws/BranchDtoToWsBranch.java | 81 +++++++++++++++++++ .../server/projectbranch/ws/BranchesWs.java | 2 + .../server/projectbranch/ws/ListAction.java | 51 +++--------- .../server/projectbranch/ws/ShowAction.java | 59 +++----------- .../projectbranch/ws/ListActionTest.java | 69 +++++++++++----- .../projectbranch/ws/ShowActionTest.java | 29 +++++-- 6 files changed, 178 insertions(+), 113 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchDtoToWsBranch.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchDtoToWsBranch.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchDtoToWsBranch.java new file mode 100644 index 00000000000..fa0813568ef --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchDtoToWsBranch.java @@ -0,0 +1,81 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.projectbranch.ws; + +import java.util.Map; +import javax.annotation.Nullable; +import org.sonar.db.component.BranchDto; +import org.sonar.db.measure.MeasureDto; +import org.sonarqube.ws.Common; +import org.sonarqube.ws.WsBranches; + +import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; +import static org.sonar.api.measures.CoreMetrics.BUGS_KEY; +import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY; +import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY; +import static org.sonar.core.util.Protobuf.setNullable; +import static org.sonar.db.component.BranchType.LONG; +import static org.sonar.db.component.BranchType.SHORT; +import static org.sonar.server.projectbranch.ws.BranchesWs.DEFAULT_MAIN_BRANCH_NAME; + +public class BranchDtoToWsBranch { + + private BranchDtoToWsBranch() { + // static methods only + } + + public static WsBranches.Branch.Builder toBranchBuilder(BranchDto branch, @Nullable BranchDto mergeBranch, Map measuresByMetricKey) { + WsBranches.Branch.Builder builder = WsBranches.Branch.newBuilder(); + String branchKey = branch.getKey(); + String effectiveBranchKey = branchKey == null && branch.isMain() ? DEFAULT_MAIN_BRANCH_NAME : branchKey; + setNullable(effectiveBranchKey, builder::setName); + builder.setIsMain(branch.isMain()); + builder.setType(Common.BranchType.valueOf(branch.getBranchType().name())); + if (mergeBranch != null) { + String mergeBranchKey = mergeBranch.getKey(); + if (mergeBranchKey == null && branch.getBranchType().equals(SHORT)) { + builder.setMergeBranch(DEFAULT_MAIN_BRANCH_NAME); + } else { + setNullable(mergeBranchKey, builder::setMergeBranch); + } + } + + if (branch.getBranchType().equals(LONG)) { + WsBranches.Branch.Status.Builder statusBuilder = WsBranches.Branch.Status.newBuilder(); + MeasureDto measure = measuresByMetricKey.get(ALERT_STATUS_KEY); + setNullable(measure, m -> builder.setStatus(statusBuilder.setQualityGateStatus(m.getData()))); + } + + if (branch.getBranchType().equals(SHORT)) { + WsBranches.Branch.Status.Builder statusBuilder = WsBranches.Branch.Status.newBuilder(); + MeasureDto bugs = measuresByMetricKey.get(BUGS_KEY); + setNullable(bugs, m -> builder.setStatus(statusBuilder.setBugs(m.getValue().intValue()))); + + MeasureDto vulnerabilities = measuresByMetricKey.get(VULNERABILITIES_KEY); + setNullable(vulnerabilities, m -> builder.setStatus(statusBuilder.setVulnerabilities(m.getValue().intValue()))); + + MeasureDto codeSmells = measuresByMetricKey.get(CODE_SMELLS_KEY); + setNullable(codeSmells, m -> builder.setStatus(statusBuilder.setCodeSmells(m.getValue().intValue()))); + builder.setStatus(statusBuilder); + } + return builder; + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java index ceb76423220..0fc3ce92a8b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java @@ -26,6 +26,8 @@ import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters. public class BranchesWs implements WebService { + public static final String DEFAULT_MAIN_BRANCH_NAME = "master"; + private final BranchWsAction[] actions; public BranchesWs(BranchWsAction... actions) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java index 38b902660a2..1291dba44b9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.function.Function; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -40,7 +41,6 @@ import org.sonar.server.component.ComponentFinder; import org.sonar.server.user.UserSession; import org.sonar.server.ws.WsUtils; import org.sonarqube.ws.WsBranches; -import org.sonarqube.ws.WsBranches.Branch.Status; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; @@ -50,14 +50,11 @@ import static org.sonar.api.measures.CoreMetrics.BUGS_KEY; import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY; import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY; import static org.sonar.api.resources.Qualifiers.PROJECT; -import static org.sonar.core.util.Protobuf.setNullable; import static org.sonar.core.util.stream.MoreCollectors.index; import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; -import static org.sonar.db.component.BranchType.LONG; -import static org.sonar.db.component.BranchType.SHORT; +import static org.sonar.server.projectbranch.ws.BranchDtoToWsBranch.toBranchBuilder; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; -import static org.sonarqube.ws.Common.BranchType; import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST; import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT; @@ -100,7 +97,6 @@ public class ListAction implements BranchWsAction { List metrics = dbClient.metricDao().selectByKeys(dbSession, asList(ALERT_STATUS_KEY, BUGS_KEY, VULNERABILITIES_KEY, CODE_SMELLS_KEY)); Map metricsById = metrics.stream().collect(uniqueIndex(MetricDto::getId)); - Map metricIdsByKey = metrics.stream().collect(uniqueIndex(MetricDto::getKey, MetricDto::getId)); Collection branches = dbClient.branchDao().selectByComponent(dbSession, project); Map mergeBranchesByUuid = dbClient.branchDao() @@ -113,48 +109,23 @@ public class ListAction implements BranchWsAction { WsBranches.ListWsResponse.Builder protobufResponse = WsBranches.ListWsResponse.newBuilder(); branches.stream() .filter(b -> b.getKeeType().equals(BranchKeyType.BRANCH)) - .forEach(b -> addToProtobuf(protobufResponse, b, mergeBranchesByUuid, metricIdsByKey, measuresByComponentUuids)); + .forEach(b -> addBranch(protobufResponse, b, mergeBranchesByUuid, metricsById, measuresByComponentUuids.get(b.getUuid()))); WsUtils.writeProtobuf(protobufResponse.build(), request, response); } } - private static void addToProtobuf(WsBranches.ListWsResponse.Builder response, BranchDto branch, Map mergeBranchesByUuid, - Map metricIdsByKey, Multimap measuresByComponentUuids) { - WsBranches.Branch.Builder builder = response.addBranchesBuilder(); - setNullable(branch.getKey(), builder::setName); - builder.setIsMain(branch.isMain()); - builder.setType(BranchType.valueOf(branch.getBranchType().name())); + private static void addBranch(WsBranches.ListWsResponse.Builder response, BranchDto branch, Map mergeBranchesByUuid, + Map metricsById, Collection measures) { + + BranchDto mergeBranch = null; String mergeBranchUuid = branch.getMergeBranchUuid(); if (mergeBranchUuid != null) { - BranchDto mergeBranch = mergeBranchesByUuid.get(mergeBranchUuid); + mergeBranch = mergeBranchesByUuid.get(mergeBranchUuid); checkState(mergeBranch != null, "Component uuid '%s' cannot be found", mergeBranch); - setNullable(mergeBranch.getKey(), builder::setMergeBranch); - } - - Collection componentMeasures = measuresByComponentUuids.get(branch.getUuid()); - if (branch.getBranchType().equals(LONG)) { - Status.Builder statusBuilder = Status.newBuilder(); - int qualityGateStatusMetricId = metricIdsByKey.get(ALERT_STATUS_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == qualityGateStatusMetricId).findAny() - .ifPresent(measure -> builder.setStatus(statusBuilder.setQualityGateStatus(measure.getData()))); - } - - if (branch.getBranchType().equals(SHORT)) { - Status.Builder statusBuilder = Status.newBuilder(); - int bugsMetricId = metricIdsByKey.get(BUGS_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == bugsMetricId).findAny() - .ifPresent(measure -> statusBuilder.setBugs(measure.getValue().intValue())); - - int vulnerabilitiesMetricId = metricIdsByKey.get(VULNERABILITIES_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == vulnerabilitiesMetricId).findAny() - .ifPresent(measure -> statusBuilder.setVulnerabilities(measure.getValue().intValue())); - - int codeSmellMetricId = metricIdsByKey.get(CODE_SMELLS_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == codeSmellMetricId).findAny() - .ifPresent(measure -> statusBuilder.setCodeSmells(measure.getValue().intValue())); - builder.setStatus(statusBuilder); } - builder.build(); + response.addBranches( + toBranchBuilder(branch, mergeBranch, + measures.stream().collect(uniqueIndex(m -> metricsById.get(m.getMetricId()).getKey(), Function.identity())))); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java index 1f3e1bffa52..041611379c8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java @@ -19,13 +19,13 @@ */ package org.sonar.server.projectbranch.ws; -import com.google.common.collect.Multimap; import com.google.common.io.Resources; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Function; import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -40,9 +40,7 @@ import org.sonar.db.metric.MetricDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.computation.task.projectanalysis.analysis.Branch; import org.sonar.server.user.UserSession; -import org.sonar.server.ws.WsUtils; import org.sonarqube.ws.WsBranches; -import org.sonarqube.ws.WsBranches.Branch.Status; import org.sonarqube.ws.WsBranches.ShowWsResponse; import static com.google.common.base.Preconditions.checkState; @@ -51,14 +49,12 @@ import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; import static org.sonar.api.measures.CoreMetrics.BUGS_KEY; import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY; import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY; -import static org.sonar.core.util.Protobuf.setNullable; import static org.sonar.core.util.stream.MoreCollectors.index; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; -import static org.sonar.db.component.BranchType.LONG; -import static org.sonar.db.component.BranchType.SHORT; +import static org.sonar.server.projectbranch.ws.BranchDtoToWsBranch.toBranchBuilder; import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_FILE_EXAMPLE_001; -import static org.sonarqube.ws.Common.BranchType; +import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_SHOW; import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH; import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_COMPONENT; @@ -105,7 +101,7 @@ public class ShowAction implements BranchWsAction { @Override public void handle(Request request, Response response) throws Exception { if (!isBranchFeatureSupported) { - WsUtils.writeProtobuf(WsBranches.Branch.newBuilder().build(), request, response); + writeProtobuf(WsBranches.Branch.newBuilder().build(), request, response); return; } String projectKey = request.mandatoryParam(PARAM_COMPONENT); @@ -117,17 +113,20 @@ public class ShowAction implements BranchWsAction { List metrics = dbClient.metricDao().selectByKeys(dbSession, asList(ALERT_STATUS_KEY, BUGS_KEY, VULNERABILITIES_KEY, CODE_SMELLS_KEY)); Map metricsById = metrics.stream().collect(uniqueIndex(MetricDto::getId)); - Map metricIdsByKey = metrics.stream().collect(uniqueIndex(MetricDto::getKey, MetricDto::getId)); BranchDto branch = getBranch(dbSession, component.projectUuid()); String mergeBranchUuid = branch.getMergeBranchUuid(); BranchDto mergeBranch = mergeBranchUuid == null ? null : getBranch(dbSession, mergeBranchUuid); - Multimap measuresByComponentUuids = dbClient.measureDao() + Collection measures = dbClient.measureDao() .selectByComponentsAndMetrics(dbSession, Collections.singletonList(branch.getUuid()), metricsById.keySet()) - .stream().collect(index(MeasureDto::getComponentUuid)); + .stream().collect(index(MeasureDto::getComponentUuid)) + .get(branch.getUuid()); - WsUtils.writeProtobuf(buildResponse(branch, mergeBranch, metricIdsByKey, measuresByComponentUuids), request, response); + writeProtobuf(ShowWsResponse.newBuilder() + .setBranch( + toBranchBuilder(branch, mergeBranch, measures.stream().collect(uniqueIndex(m -> metricsById.get(m.getMetricId()).getKey(), Function.identity())))) + .build(), request, response); } } @@ -144,40 +143,4 @@ public class ShowAction implements BranchWsAction { return branch.get(); } - private static ShowWsResponse buildResponse(BranchDto branch, @Nullable BranchDto mergeBranch, - Map metricIdsByKey, Multimap measuresByComponentUuids) { - WsBranches.Branch.Builder builder = WsBranches.Branch.newBuilder(); - setNullable(branch.getKey(), builder::setName); - builder.setIsMain(branch.isMain()); - builder.setType(BranchType.valueOf(branch.getBranchType().name())); - if (mergeBranch != null) { - setNullable(mergeBranch.getKey(), builder::setMergeBranch); - } - - Status.Builder statusBuilder = Status.newBuilder(); - Collection componentMeasures = measuresByComponentUuids.get(branch.getUuid()); - if (branch.getBranchType().equals(LONG)) { - int qualityGateStatusMetricId = metricIdsByKey.get(ALERT_STATUS_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == qualityGateStatusMetricId).findAny() - .ifPresent(measure -> statusBuilder.setQualityGateStatus(measure.getData())); - } - - if (branch.getBranchType().equals(SHORT)) { - int bugsMetricId = metricIdsByKey.get(BUGS_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == bugsMetricId).findAny() - .ifPresent(measure -> statusBuilder.setBugs(measure.getValue().intValue())); - - int vulnerabilitiesMetricId = metricIdsByKey.get(VULNERABILITIES_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == vulnerabilitiesMetricId).findAny() - .ifPresent(measure -> statusBuilder.setVulnerabilities(measure.getValue().intValue())); - - int codeSmellMetricId = metricIdsByKey.get(CODE_SMELLS_KEY); - componentMeasures.stream().filter(m -> m.getMetricId() == codeSmellMetricId).findAny() - .ifPresent(measure -> statusBuilder.setCodeSmells(measure.getValue().intValue())); - } - - builder.setStatus(statusBuilder); - return ShowWsResponse.newBuilder().setBranch(builder).build(); - } - } diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java index 5193cd898d9..7aea4ab3e05 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java @@ -74,7 +74,7 @@ public class ListActionTest { private MetricDto vulnerabilities; private MetricDto codeSmells; - public WsActionTester tester = new WsActionTester(new ListAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), resourceTypes))); + public WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), resourceTypes))); @Before public void setUp() throws Exception { @@ -86,7 +86,7 @@ public class ListActionTest { @Test public void test_definition() { - WebService.Action definition = tester.getDef(); + WebService.Action definition = ws.getDef(); assertThat(definition.key()).isEqualTo("list"); assertThat(definition.isPost()).isFalse(); assertThat(definition.isInternal()).isTrue(); @@ -99,7 +99,7 @@ public class ListActionTest { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The 'project' parameter is missing"); - tester.newRequest().execute(); + ws.newRequest().execute(); } @Test @@ -111,7 +111,7 @@ public class ListActionTest { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Invalid project key"); - tester.newRequest() + ws.newRequest() .setParam("project", file.getDbKey()) .execute(); } @@ -121,7 +121,7 @@ public class ListActionTest { expectedException.expect(NotFoundException.class); expectedException.expectMessage("Component key 'foo' not found"); - tester.newRequest() + ws.newRequest() .setParam("project", "foo") .execute(); } @@ -131,13 +131,28 @@ public class ListActionTest { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(UserRole.USER, project); - ListWsResponse response = tester.newRequest() + ListWsResponse response = ws.newRequest() .setParam("project", project.getDbKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getBranchesList()) - .extracting(Branch::hasName, Branch::getIsMain, Branch::getType) - .containsExactlyInAnyOrder(tuple(false, true, Common.BranchType.LONG)); + .extracting(Branch::getName, Branch::getIsMain, Branch::getType) + .containsExactlyInAnyOrder(tuple("master", true, Common.BranchType.LONG)); + } + + @Test + public void main_branch_with_specified_name() { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = db.components().insertMainBranch(organization, "head"); + userSession.logIn().addProjectPermission(UserRole.USER, project); + + ListWsResponse response = ws.newRequest() + .setParam("project", project.getDbKey()) + .executeProtobuf(ListWsResponse.class); + + assertThat(response.getBranchesList()) + .extracting(Branch::getName, Branch::getIsMain, Branch::getType) + .containsExactlyInAnyOrder(tuple("head", true, Common.BranchType.LONG)); } @Test @@ -145,7 +160,7 @@ public class ListActionTest { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn().addProjectPermission(UserRole.USER, project); - String json = tester.newRequest() + String json = ws.newRequest() .setParam("project", project.getDbKey()) .setMediaType(MediaTypes.JSON) .execute() @@ -160,14 +175,14 @@ public class ListActionTest { db.components().insertProjectBranch(project, b -> b.setKey("feature/foo")); userSession.logIn().addProjectPermission(UserRole.USER, project); - ListWsResponse response = tester.newRequest() + ListWsResponse response = ws.newRequest() .setParam("project", project.getDbKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getBranchesList()) .extracting(Branch::getName, Branch::getType) .containsExactlyInAnyOrder( - tuple("", Common.BranchType.LONG), + tuple("master", Common.BranchType.LONG), tuple("feature/foo", Common.BranchType.LONG), tuple("feature/bar", Common.BranchType.LONG)); } @@ -183,17 +198,33 @@ public class ListActionTest { ComponentDto shortLivingBranchOnMaster = db.components().insertProjectBranch(project, b -> b.setKey("short_on_master").setBranchType(BranchType.SHORT).setMergeBranchUuid(project.uuid())); - ListWsResponse response = tester.newRequest() + ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); assertThat(response.getBranchesList()) .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) .containsExactlyInAnyOrder( - tuple("", Common.BranchType.LONG, ""), + tuple("master", Common.BranchType.LONG, ""), tuple(longLivingBranch.getBranch(), Common.BranchType.LONG, ""), tuple(shortLivingBranch.getBranch(), Common.BranchType.SHORT, longLivingBranch.getBranch()), - tuple(shortLivingBranchOnMaster.getBranch(), Common.BranchType.SHORT, "")); + tuple(shortLivingBranchOnMaster.getBranch(), Common.BranchType.SHORT, "master")); + } + + @Test + public void mergeBranch_is_using_default_main_name_when_main_branch_has_no_name() { + ComponentDto project = db.components().insertMainBranch(); + userSession.logIn().addProjectPermission(UserRole.USER, project); + ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, + b -> b.setKey("short").setBranchType(BranchType.SHORT).setMergeBranchUuid(project.uuid())); + + WsBranches.ShowWsResponse response = ws.newRequest() + .setParam("project", shortLivingBranch.getKey()) + .executeProtobuf(WsBranches.ShowWsResponse.class); + + assertThat(response.getBranch()) + .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) + .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), Common.BranchType.SHORT, "master"); } @Test @@ -204,7 +235,7 @@ public class ListActionTest { SnapshotDto branchAnalysis = db.components().insertSnapshot(branch); db.measures().insertMeasure(branch, branchAnalysis, qualityGateStatus, m -> m.setData("OK")); - ListWsResponse response = tester.newRequest() + ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); @@ -225,7 +256,7 @@ public class ListActionTest { db.measures().insertMeasure(shortLivingBranch, branchAnalysis, vulnerabilities, m -> m.setValue(2d)); db.measures().insertMeasure(shortLivingBranch, branchAnalysis, codeSmells, m -> m.setValue(3d)); - ListWsResponse response = tester.newRequest() + ListWsResponse response = ws.newRequest() .setParam("project", project.getKey()) .executeProtobuf(ListWsResponse.class); @@ -247,7 +278,7 @@ public class ListActionTest { expectedException.expect(NotFoundException.class); expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey())); - tester.newRequest() + ws.newRequest() .setParam("project", branch.getDbKey()) .execute(); } @@ -259,12 +290,12 @@ public class ListActionTest { db.components().insertProjectBranch(project, b -> b.setKey("feature/foo").setBranchType(BranchType.SHORT).setMergeBranchUuid(longLivingBranch.uuid())); userSession.logIn().addProjectPermission(UserRole.USER, project); - String json = tester.newRequest() + String json = ws.newRequest() .setParam("project", project.getDbKey()) .execute() .getInput(); - assertJson(json).isSimilarTo(tester.getDef().responseExampleAsString()); + assertJson(json).isSimilarTo(ws.getDef().responseExampleAsString()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java index 388377741ea..05c6acc3dbd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java @@ -101,8 +101,8 @@ public class ShowActionTest { .executeProtobuf(ShowWsResponse.class); assertThat(response.getBranch()) - .extracting(Branch::hasName, Branch::getType, Branch::getIsMain, Branch::hasMergeBranch) - .containsExactlyInAnyOrder(false, Common.BranchType.LONG, true, false); + .extracting(Branch::getName, Branch::getType, Branch::getIsMain, Branch::hasMergeBranch) + .containsExactlyInAnyOrder("master", Common.BranchType.LONG, true, false); } @Test @@ -134,8 +134,8 @@ public class ShowActionTest { .executeProtobuf(ShowWsResponse.class); assertThat(response.getBranch()) - .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) - .containsExactlyInAnyOrder(longLivingBranch.getBranch(), Common.BranchType.LONG, ""); + .extracting(Branch::getName, Branch::getType, Branch::hasMergeBranch) + .containsExactlyInAnyOrder(longLivingBranch.getBranch(), Common.BranchType.LONG, false); } @Test @@ -157,6 +157,23 @@ public class ShowActionTest { .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), Common.BranchType.SHORT, longLivingBranch.getBranch()); } + @Test + public void mergeBranch_is_using_default_main_name_when_main_branch_has_no_name() { + ComponentDto project = db.components().insertMainBranch(); + userSession.logIn().addProjectPermission(UserRole.USER, project); + ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, + b -> b.setKey("short").setBranchType(BranchType.SHORT).setMergeBranchUuid(project.uuid())); + + ShowWsResponse response = ws.newRequest() + .setParam("component", shortLivingBranch.getKey()) + .setParam("branch", shortLivingBranch.getBranch()) + .executeProtobuf(ShowWsResponse.class); + + assertThat(response.getBranch()) + .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) + .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), Common.BranchType.SHORT, "master"); + } + @Test public void quality_gate_status_on_long_living_branch() { ComponentDto project = db.components().insertMainBranch(); @@ -211,8 +228,8 @@ public class ShowActionTest { .executeProtobuf(ShowWsResponse.class); assertThat(response.getBranch()) - .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch) - .containsExactlyInAnyOrder(file.getBranch(), Common.BranchType.LONG, ""); + .extracting(Branch::getName, Branch::getType, Branch::hasMergeBranch) + .containsExactlyInAnyOrder(file.getBranch(), Common.BranchType.LONG, false); } @Test -- 2.39.5