From 5c78e445609eaf1c6bb00b0c863623ea497237a0 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 30 Apr 2018 15:12:58 +0200 Subject: [PATCH] SONAR-10252 Project page of long living branches displays the link to the correct Quality Gate --- .../server/qualitygate/QualityGateFinder.java | 2 +- .../sonar/server/ui/ws/ComponentAction.java | 11 ++++---- .../server/ui/ws/ComponentActionTest.java | 25 +++++++++++++++++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java index d1abced201e..89b7e64783e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java @@ -43,7 +43,7 @@ public class QualityGateFinder { /** * Return effective quality gate of a project. * - * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate ofI the organization + * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate of the organization */ public QualityGateData getQualityGate(DbSession dbSession, OrganizationDto organization, ComponentDto component) { Optional qualityGateId = dbClient.projectQgateAssociationDao().selectQGateIdByComponentId(dbSession, component.getId()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index 6dfb2d4a919..76c55684e0b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -106,7 +106,7 @@ public class ComponentAction implements NavigationWsAction { @Override public void define(NewController context) { - NewAction projectNavigation = context.createAction("component") + NewAction action = context.createAction("component") .setDescription("Get information concerning component navigation for the current user. " + "Requires the 'Browse' permission on the component's project.") .setHandler(this) @@ -116,18 +116,18 @@ public class ComponentAction implements NavigationWsAction { .setChangelog( new Change("6.4", "The 'visibility' field is added")); - projectNavigation.createParam(PARAM_COMPONENT) + action.createParam(PARAM_COMPONENT) .setDescription("A component key.") .setDeprecatedKey("componentKey", "6.4") .setExampleValue(KEY_PROJECT_EXAMPLE_001); - projectNavigation + action .createParam(PARAM_BRANCH) .setDescription("Branch key") .setInternal(true) .setExampleValue(KEY_BRANCH_EXAMPLE_001); - projectNavigation + action .createParam(PARAM_PULL_REQUEST) .setDescription("Pull request id") .setInternal(true) @@ -141,6 +141,7 @@ public class ComponentAction implements NavigationWsAction { String branch = request.param(PARAM_BRANCH); String pullRequest = request.param(PARAM_PULL_REQUEST); ComponentDto component = componentFinder.getByKeyAndOptionalBranchOrPullRequest(session, componentKey, branch, pullRequest); + ComponentDto project = component.getMainBranchProjectUuid() == null ? component : componentFinder.getByUuid(session, component.getMainBranchProjectUuid()); if (!userSession.hasComponentPermission(USER, component) && !userSession.hasComponentPermission(ADMIN, component) && !userSession.isSystemAdministrator()) { @@ -153,7 +154,7 @@ public class ComponentAction implements NavigationWsAction { json.beginObject(); writeComponent(json, session, component, org, analysis.orElse(null)); writeProfiles(json, session, component); - writeQualityGate(json, session, org, component); + writeQualityGate(json, session, org, project); if (userSession.hasComponentPermission(ADMIN, component) || userSession.hasPermission(ADMINISTER_QUALITY_PROFILES, org) || userSession.hasPermission(ADMINISTER_QUALITY_GATES, org)) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java index 91cf01b13a8..ec18a37b36d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java @@ -38,6 +38,7 @@ import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.platform.PluginRepository; import org.sonar.db.DbClient; import org.sonar.db.DbTester; +import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; @@ -253,6 +254,26 @@ public class ComponentActionTest { executeAndVerify(project.getDbKey(), "return_quality_gate.json"); } + @Test + public void quality_gate_for_a_long_living_branch() { + OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org")); + db.qualityGates().createDefaultQualityGate(organization); + ComponentDto project = db.components().insertPrivateProject(organization); + ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.LONG)); + QualityGateDto qualityGateDto = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way")); + db.qualityGates().associateProjectToQualityGate(project, qualityGateDto); + userSession.addProjectPermission(UserRole.USER, project); + init(); + + String json = ws.newRequest() + .setParam("componentKey", longLivingBranch.getKey()) + .setParam("branch", longLivingBranch.getBranch()) + .execute() + .getInput(); + + verify(json, "return_quality_gate.json"); + } + @Test public void return_default_quality_gate() { OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org")); @@ -587,8 +608,8 @@ public class ComponentActionTest { return ws.newRequest().setParam("componentKey", componentKey).execute().getInput(); } - private void verify(String json, String expectedJson) { - assertJson(json).isSimilarTo(getClass().getResource(ComponentActionTest.class.getSimpleName() + "/" + expectedJson)); + private void verify(String json, String jsonFile) { + assertJson(json).isSimilarTo(getClass().getResource(ComponentActionTest.class.getSimpleName() + "/" + jsonFile)); } private void executeAndVerify(String componentKey, String expectedJson) { -- 2.39.5