From a8e0f0de6ada2d99d4f7499396145208e017b575 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 14 Dec 2017 14:10:39 +0100 Subject: [PATCH] SONAR-10134 Search return only projects from quality gate organization --- .../ProjectQgateAssociationQuery.java | 6 +++++ .../ProjectQgateAssociationMapper.xml | 1 + .../ProjectQgateAssociationDaoTest.java | 20 ++++++++++++++++ .../qualitygate/ws/SearchActionTest.java | 24 ++++++++++++++++++- 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java index f57e20bd308..fffedf2582b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java @@ -40,6 +40,7 @@ public class ProjectQgateAssociationQuery { public static final Set AVAILABLE_MEMBERSHIP = ImmutableSet.of(ANY, IN, OUT); private final String gateId; + private final String organizationUuid; private final String membership; private final String projectSearch; @@ -55,6 +56,7 @@ public class ProjectQgateAssociationQuery { private ProjectQgateAssociationQuery(Builder builder) { this.gateId = Long.toString(builder.qualityGate.getId()); + this.organizationUuid = builder.qualityGate.getOrganizationUuid(); this.membership = builder.membership; this.projectSearch = builder.projectSearch; if (this.projectSearch == null) { @@ -71,6 +73,10 @@ public class ProjectQgateAssociationQuery { return gateId; } + public String organizationUuid() { + return organizationUuid; + } + @CheckForNull public String membership() { return membership; diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml index 14c8cbd1b58..a2a05d73d07 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml @@ -12,6 +12,7 @@ and proj.enabled = ${_true} and proj.main_branch_project_uuid is null and proj.copy_component_uuid is null + and proj.organization_uuid=#{query.organizationUuid, jdbcType=VARCHAR} and prop.text_value IS NOT NULL diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java index 3de2b4f21e6..ed9fa8fc254 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java @@ -130,6 +130,26 @@ public class ProjectQgateAssociationDaoTest { .containsExactly(project1.getId(), project3.getId(), project2.getId()); } + @Test + public void return_only_projects_from_organization() { + OrganizationDto organization = db.organizations().insert(); + OrganizationDto otherOrganization = db.organizations().insert(); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization); + ComponentDto project = db.components().insertPrivateProject(organization); + ComponentDto otherProject = db.components().insertPrivateProject(otherOrganization); + db.qualityGates().associateProjectToQualityGate(project, qualityGate); + db.qualityGates().associateProjectToQualityGate(otherProject, otherQualityGate); + + List result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder() + .qualityGate(qualityGate) + .build()); + + assertThat(result) + .extracting(ProjectQgateAssociationDto::getId) + .containsExactlyInAnyOrder(project.getId()); + } + @Test public void select_qgate_id_is_absent() { ComponentDto project = db.components().insertPrivateProject(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java index 4658e9edc59..7856dede61b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java @@ -316,7 +316,7 @@ public class SearchActionTest { OrganizationDto organization = db.organizations().insert(); QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); for (int i = 0; i < 20; i++) { - ComponentDto project = db.components().insertPublicProject(); + ComponentDto project = db.components().insertPublicProject(organization); db.qualityGates().associateProjectToQualityGate(project, qualityGate); } @@ -333,6 +333,28 @@ public class SearchActionTest { assertThat(response.getResultsCount()).isEqualTo(10); } + @Test + public void return_only_projects_from_organization() { + OrganizationDto organization = db.organizations().insert(); + OrganizationDto otherOrganization = db.organizations().insert(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); + QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization); + ComponentDto project = db.components().insertPublicProject(organization); + ComponentDto otherProject = db.components().insertPublicProject(otherOrganization); + db.qualityGates().associateProjectToQualityGate(project, qualityGate); + db.qualityGates().associateProjectToQualityGate(otherProject, otherQualityGate); + + SearchResponse response = ws.newRequest() + .setParam(PARAM_GATE_ID, valueOf(qualityGate.getId())) + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .setParam(PARAM_SELECTED, ALL.value()) + .executeProtobuf(SearchResponse.class); + + assertThat(response.getResultsList()) + .extracting(Result::getName) + .containsExactlyInAnyOrder(project.name()); + } + @Test public void fail_on_unknown_quality_gate() { OrganizationDto organization = db.organizations().insert(); -- 2.39.5