);
CREATE UNIQUE INDEX "UNIQ_ORG_QUALITY_GATES" ON "ORG_QUALITY_GATES" ("ORGANIZATION_UUID","QUALITY_GATE_UUID");
+CREATE TABLE "PROJECT_QGATES" (
+"PROJECT_UUID" VARCHAR(40) NOT NULL,
+"QUALITY_GATE_UUID" VARCHAR(40) NOT NULL,
+
+CONSTRAINT "PK_PROJECT_QGATES" PRIMARY KEY ("PROJECT_UUID")
+);
+CREATE UNIQUE INDEX "UNIQ_PROJECT_QGATES" ON "PROJECT_QGATES" ("PROJECT_UUID", "QUALITY_GATE_UUID");
+
CREATE TABLE "PROPERTIES" (
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"PROP_KEY" VARCHAR(512) NOT NULL,
return id == null ? Optional.empty() : Optional.of(Long.valueOf(id));
}
+ /**
+ * @return quality gate uuid if a specific Quality Gate has been defined for the given component uuid. <br>
+ * Returns <code>{@link Optional#empty()}</code> otherwise (ex: default quality gate applies)
+ */
+ public Optional<String> selectQGateUuidByComponentUuid(DbSession dbSession, String componentUuid) {
+ String uuid = mapper(dbSession).selectQGateUuidByComponentUuid(componentUuid);
+ return Optional.ofNullable(uuid);
+ }
+
private static ProjectQgateAssociationMapper mapper(DbSession session) {
return session.getMapper(ProjectQgateAssociationMapper.class);
}
+ public void deleteByProjectUuid(DbSession dbSession, String projectUuid) {
+ mapper(dbSession).deleteByProjectUuid(projectUuid);
+ }
+
+ public void deleteByQGateUuid(DbSession dbSession, String qGateUuid) {
+ mapper(dbSession).deleteByQGateUuid(qGateUuid);
+ }
+
+ public void insertProjectQGateAssociation(DbSession dbSession, String projectUuid, String qGateUuid) {
+ mapper(dbSession).insertProjectQGateAssociation(projectUuid, qGateUuid);
+ }
+
+ public void updateProjectQGateAssociation(DbSession dbSession, String projectUuid, String qGateUuid) {
+ mapper(dbSession).updateProjectQGateAssociation(projectUuid, qGateUuid);
+ }
}
@CheckForNull
String selectQGateIdByComponentId(long componentId);
+
+ @CheckForNull
+ String selectQGateUuidByComponentUuid(String componentUuid);
+
+ void deleteByProjectUuid(String projectUuid);
+
+ void insertProjectQGateAssociation(@Param("projectUuid") String projectUuid, @Param("qGateUuid") String qGateUuid);
+
+ void deleteByQGateUuid(String qGateUuid);
+
+ void updateProjectQGateAssociation(@Param("projectUuid") String projectUuid, @Param("qGateUuid") String qGateUuid);
+
}
private static QualityGateMapper mapper(DbSession session) {
return session.getMapper(QualityGateMapper.class);
}
+
+ public QualityGateDto selectByProjectUuid(DbSession dbSession, String uuid) {
+ return mapper(dbSession).selectByProjectUuid(uuid);
+ }
}
void update(QualityGateDto qGate);
void ensureOneBuiltInQualityGate(String builtInQualityName);
+
+ QualityGateDto selectByProjectUuid(@Param("projectUuid") String projectUuid);
}
</where>
</select>
+ <select id="selectQGateUuidByComponentUuid" parameterType="String" resultType="string">
+ SELECT quality_gate_uuid
+ FROM project_qgates
+ <where>
+ AND project_uuid=#{componentUuid}
+ </where>
+ </select>
+
+ <delete id="deleteByProjectUuid" parameterType="String">
+ DELETE
+ FROM project_qgates
+ WHERE
+ project_uuid=#{uuid,jdbcType=VARCHAR}
+ </delete>
+
+ <delete id="deleteByQGateUuid" parameterType="String">
+ DELETE
+ FROM project_qgates
+ WHERE
+ quality_gate_uuid=#{uuid,jdbcType=VARCHAR}
+ </delete>
+
+ <insert id="insertProjectQGateAssociation" parameterType="map">
+ INSERT into project_qgates
+ (
+ project_uuid,
+ quality_gate_uuid
+ )
+ VALUES (
+ #{projectUuid,jdbcType=VARCHAR},
+ #{qGateUuid,jdbcType=VARCHAR}
+ )
+ </insert>
+
+ <update id="updateProjectQGateAssociation" parameterType="map">
+ UPDATE project_qgates
+ SET
+ quality_gate_uuid=#{qGateUuid,jdbcType=VARCHAR}
+ WHERE
+ project_uuid = #{projectUuid,jdbcType=VARCHAR}
+ </update>
+
</mapper>
qg.id = #{id, jdbcType=BIGINT}
</select>
+ <select id="selectByProjectUuid" parameterType="Map" resultType="org.sonar.db.qualitygate.QualityGateDto">
+ SELECT
+ <include refid="gateColumns"/>
+ FROM
+ quality_gates qg
+ INNER JOIN
+ project_qgates pqg ON pqg.quality_gate_uuid = qg.uuid AND pqg.project_uuid = #{projectUuid, jdbcType=VARCHAR}
+ </select>
+
<select id="selectById" parameterType="long" resultType="QualityGate">
select
<include refid="gateColumns"/>
.qualityGate(qualityGate)
.membership(ProjectQgateAssociationQuery.IN)
.build()))
- .extracting(ProjectQgateAssociationDto::getId, ProjectQgateAssociationDto::getName, ProjectQgateAssociationDto::getGateId)
- .containsExactlyInAnyOrder(
- tuple(project1.getId(), project1.name(), qualityGate.getId().toString()),
- tuple(project2.getId(), project2.name(), qualityGate.getId().toString()));
+ .extracting(ProjectQgateAssociationDto::getId, ProjectQgateAssociationDto::getName, ProjectQgateAssociationDto::getGateId)
+ .containsExactlyInAnyOrder(
+ tuple(project1.getId(), project1.name(), qualityGate.getId().toString()),
+ tuple(project2.getId(), project2.name(), qualityGate.getId().toString()));
assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
.qualityGate(qualityGate)
.membership(ProjectQgateAssociationQuery.OUT)
.build()))
- .extracting(ProjectQgateAssociationDto::getId, ProjectQgateAssociationDto::getName, ProjectQgateAssociationDto::getGateId)
- .containsExactlyInAnyOrder(tuple(project3.getId(), project3.name(), null));
+ .extracting(ProjectQgateAssociationDto::getId, ProjectQgateAssociationDto::getName, ProjectQgateAssociationDto::getGateId)
+ .containsExactlyInAnyOrder(tuple(project3.getId(), project3.name(), null));
}
@Test
.qualityGate(qualityGate)
.projectSearch("one")
.build()))
- .extracting(ProjectQgateAssociationDto::getId)
- .containsExactlyInAnyOrder(project1.getId());
+ .extracting(ProjectQgateAssociationDto::getId)
+ .containsExactlyInAnyOrder(project1.getId());
assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
.qualityGate(qualityGate)
.projectSearch("project")
.build()))
- .extracting(ProjectQgateAssociationDto::getId)
- .containsExactlyInAnyOrder(project1.getId(), project2.getId(), project3.getId());
+ .extracting(ProjectQgateAssociationDto::getId)
+ .containsExactlyInAnyOrder(project1.getId(), project2.getId(), project3.getId());
}
@Test
assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
.qualityGate(qualityGate)
.build()))
- .extracting(ProjectQgateAssociationDto::getId)
- .containsExactly(project1.getId(), project3.getId(), project2.getId());
+ .extracting(ProjectQgateAssociationDto::getId)
+ .containsExactly(project1.getId(), project3.getId(), project2.getId());
}
@Test
assertThat(result).contains(qualityGate1.getId());
}
+ @Test
+ public void select_qgate_uuid_by_component_uuid() {
+ OrganizationDto organization = db.organizations().insert();
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ ComponentDto project = db.components().insertPrivateProject(organization);
+
+ db.qualityGates().associateProjectToQualityGate(project, qualityGate);
+
+ Optional<String> qGateUuid = underTest.selectQGateUuidByComponentUuid(dbSession, project.uuid());
+
+ assertThat(qGateUuid).contains(qualityGate.getUuid());
+ }
+
+
+ @Test
+ public void delete_by_project_uuid() {
+ OrganizationDto organization = db.organizations().insert();
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ ComponentDto project = db.components().insertPrivateProject(organization);
+
+ db.qualityGates().associateProjectToQualityGate(project, qualityGate);
+
+ underTest.deleteByProjectUuid(dbSession, project.uuid());
+
+ Optional<String> deletedQualityGate = db.qualityGates().selectQGateUuidByComponentUuid(project.uuid());
+
+ assertThat(deletedQualityGate).isEmpty();
+ }
+
+ @Test
+ public void delete_by_qgate_uuid() {
+ OrganizationDto organization = db.organizations().insert();
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ ComponentDto project = db.components().insertPrivateProject(organization);
+
+ db.qualityGates().associateProjectToQualityGate(project, qualityGate);
+
+ underTest.deleteByQGateUuid(dbSession, qualityGate.getUuid());
+
+ Optional<String> deletedQualityGate = db.qualityGates().selectQGateUuidByComponentUuid(project.uuid());
+
+ assertThat(deletedQualityGate).isEmpty();
+ }
+
+ @Test
+ public void update_project_qgate_association() {
+ OrganizationDto organization = db.organizations().insert();
+ QGateWithOrgDto firstQualityGate = db.qualityGates().insertQualityGate(organization);
+ QGateWithOrgDto secondQualityGate = db.qualityGates().insertQualityGate(organization);
+ ComponentDto project = db.components().insertPrivateProject(organization);
+
+ db.qualityGates().associateProjectToQualityGate(project, firstQualityGate);
+
+ underTest.updateProjectQGateAssociation(dbSession, project.uuid(), secondQualityGate.getUuid());
+
+ Optional<String> updatedQualityGateUuid = db.qualityGates().selectQGateUuidByComponentUuid(project.uuid());
+
+ assertThat(updatedQualityGateUuid).contains(secondQualityGate.getUuid());
+ }
}
import org.sonar.core.util.Uuids;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
import static java.lang.String.format;
assertThat(underTest.selectById(dbSession, -1L)).isNull();
}
+ @Test
+ public void select_by_uuid() {
+ QGateWithOrgDto dto = qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("QG Name").setBuiltIn(false));
+ QualityGateDto qualityGateToAssociate = underTest.selectById(dbSession, dto.getId());
+ ComponentDto project = db.components().insertPrivateProject();
+
+ qualityGateDbTester.associateProjectToQualityGate(project, qualityGateToAssociate);
+
+ QualityGateDto qualityGateFromSelect = underTest.selectByProjectUuid(dbSession, project.uuid());
+
+ assertThat(qualityGateFromSelect.getUuid()).isEqualTo(qualityGateToAssociate.getUuid());
+ }
+
@Test
public void select_by_organization_and_uuid() {
OrganizationDto organization = db.organizations().insert();
import java.util.Arrays;
import java.util.Date;
+import java.util.Optional;
import java.util.function.Consumer;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
.setKey("sonar.qualitygate")
.setResourceId(component.getId())
.setValue(String.valueOf(qualityGate.getId())));
+
+ dbClient.projectQgateAssociationDao().insertProjectQGateAssociation(dbSession, component.uuid(), qualityGate.getUuid());
+
db.commit();
}
db.commit();
return condition;
}
+
+ public Optional<String> selectQGateUuidByComponentUuid(String uuid) {
+ return dbClient.projectQgateAssociationDao().selectQGateUuidByComponentUuid(dbSession, uuid);
+ }
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.platform.db.migration.version.v80;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.server.platform.db.migration.SupportsBlueGreen;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
+import org.sonar.server.platform.db.migration.sql.CreateTableBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+@SupportsBlueGreen
+public class CreateProjectQualityGatesTable extends DdlChange {
+
+ private static final String TABLE_NAME = "project_qgates";
+
+ private static final VarcharColumnDef PROJECT_UUID_COLUMN = newVarcharColumnDefBuilder()
+ .setColumnName("project_uuid")
+ .setIsNullable(false)
+ .setLimit(UUID_SIZE)
+ .build();
+ private static final VarcharColumnDef QUALITY_GATE_UUID_COLUMN = newVarcharColumnDefBuilder()
+ .setColumnName("quality_gate_uuid")
+ .setIsNullable(false)
+ .setLimit(UUID_SIZE)
+ .build();
+
+ public CreateProjectQualityGatesTable(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ if (tableExists()) {
+ return;
+ }
+ context.execute(new CreateTableBuilder(getDialect(), TABLE_NAME)
+ .addPkColumn(PROJECT_UUID_COLUMN)
+ .addColumn(QUALITY_GATE_UUID_COLUMN)
+ .build());
+
+ context.execute(new CreateIndexBuilder()
+ .setTable(TABLE_NAME)
+ .setName("uniq_project_qgates")
+ .setUnique(true)
+ .addColumn(PROJECT_UUID_COLUMN)
+ .addColumn(QUALITY_GATE_UUID_COLUMN)
+ .build());
+ }
+
+ private boolean tableExists() throws SQLException {
+ try (Connection connection = getDatabase().getDataSource().getConnection()) {
+ return DatabaseUtils.tableExists(TABLE_NAME, connection);
+ }
+ }
+}
@Override
public void addSteps(MigrationStepRegistry registry) {
registry
- .add(3000, "Set Organizations#guarded column nullable", MakeOrganizationsGuardedNullable.class);
+ .add(3000, "Set Organizations#guarded column nullable", MakeOrganizationsGuardedNullable.class)
+ .add(3001, "Create ProjectQualityGates table", CreateProjectQualityGatesTable.class);
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.platform.db.migration.version.v80;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static java.sql.Types.VARCHAR;
+
+public class CreateProjectQualityGatesTableTest {
+ private static final String TABLE_NAME = "project_qgates";
+
+ @Rule
+ public CoreDbTester dbTester = CoreDbTester.createEmpty();
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private CreateProjectQualityGatesTable underTest = new CreateProjectQualityGatesTable(dbTester.database());
+
+ @Test
+ public void table_has_been_created() throws SQLException {
+ underTest.execute();
+
+ dbTester.assertTableExists(TABLE_NAME);
+ dbTester.assertPrimaryKey(TABLE_NAME, "pk_project_qgates", "project_uuid");
+ dbTester.assertUniqueIndex(TABLE_NAME, "uniq_project_qgates", "project_uuid", "quality_gate_uuid");
+
+ dbTester.assertColumnDefinition(TABLE_NAME, "project_uuid", VARCHAR, 40, false);
+ dbTester.assertColumnDefinition(TABLE_NAME, "quality_gate_uuid", VARCHAR, 40, false);
+
+ //script should not fail if executed twice
+ underTest.execute();
+ }
+}
import org.junit.Test;
import org.sonar.server.platform.db.migration.version.DbVersion;
-import org.sonar.server.platform.db.migration.version.v79.DbVersion79;
import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount;
import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber;
@Test
public void verify_migration_count() {
- verifyMigrationCount(underTest, 1);
+ verifyMigrationCount(underTest, 2);
}
}
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
import static com.google.common.base.Preconditions.checkArgument;
-import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY;
import static org.sonar.server.util.Validation.IS_ALREADY_USED_MESSAGE;
public class QualityGateUpdater {
dbClient.qualityGateDao().update(qualityGateDto, dbSession);
}
- public void dissociateProject(DbSession dbSession, ComponentDto project) {
- dbClient.propertiesDao().deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession);
- }
-
- public void associateProject(DbSession dbSession, ComponentDto project, QualityGateDto qualityGate) {
- dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
- .setKey(SONAR_QUALITYGATE_PROPERTY)
- .setResourceId(project.getId())
- .setValue(String.valueOf(qualityGate.getId())));
- }
-
private void checkQualityGateDoesNotAlreadyExist(DbSession dbSession, OrganizationDto organizationDto, String name) {
QualityGateDto existingQgate = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organizationDto, name);
checkArgument(existingQgate == null, IS_ALREADY_USED_MESSAGE, "Name");
private void dissociateProject(DbSession dbSession, OrganizationDto organization, ComponentDto project) {
wsSupport.checkCanAdminProject(organization, project);
dbClient.propertiesDao().deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession);
+ dbClient.projectQgateAssociationDao().deleteByProjectUuid(dbSession, project.uuid());
dbSession.commit();
}
try {
long dbId = Long.parseLong(projectId);
- return Optional.ofNullable(dbClient.componentDao().selectById(dbSession, dbId).orElse(null));
+ return dbClient.componentDao().selectById(dbSession, dbId);
} catch (NumberFormatException e) {
return Optional.empty();
}
wsSupport.checkCanEdit(qualityGate);
dbClient.propertiesDao().deleteByKeyAndValue(dbSession, SONAR_QUALITYGATE_PROPERTY, String.valueOf(qualityGate.getId()));
+ dbClient.projectQgateAssociationDao().deleteByQGateUuid(dbSession, qualityGate.getUuid());
dbClient.qualityGateDao().delete(qualityGate, dbSession);
dbSession.commit();
response.noContent();
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QGateWithOrgDto;
+import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.component.ComponentFinder.ParamNames;
.setResourceId(project.getId())
.setValue(String.valueOf(qualityGate.getId())));
+ QualityGateDto currentQualityGate = dbClient.qualityGateDao().selectByProjectUuid(dbSession, project.uuid());
+ if (currentQualityGate == null) {
+ // project uses the default profile
+ dbClient.projectQgateAssociationDao()
+ .insertProjectQGateAssociation(dbSession, project.uuid(), qualityGate.getUuid());
+ } else if (!qualityGate.getUuid().equals(currentQualityGate.getUuid())) {
+ dbClient.projectQgateAssociationDao()
+ .updateProjectQGateAssociation(dbSession, project.uuid(), qualityGate.getUuid());
+ }
+
dbSession.commit();
}
response.noContent();
try {
long dbId = Long.parseLong(projectId);
- return Optional.ofNullable(dbClient.componentDao().selectById(dbSession, dbId).orElse(null));
+ return dbClient.componentDao().selectById(dbSession, dbId);
} catch (NumberFormatException e) {
return Optional.empty();
}
*/
package org.sonar.server.qualitygate.ws;
+import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
.setParam("organization", organization.getKey())
.execute();
- assertDeselected(project.getId());
+ assertDeselected(project);
}
@Test
.setParam("organization", organization.getKey())
.execute();
- assertDeselected(project.getId());
+ assertDeselected(project);
}
@Test
.setParam("organization", organization.getKey())
.execute();
- assertDeselected(project.getId());
+ assertDeselected(project);
}
@Test
.setParam("organization", organization.getKey())
.execute();
- assertDeselected(project.getId());
+ assertDeselected(project);
}
@Test
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
ComponentDto project = db.components().insertPrivateProject(organization);
- String gateId = valueOf(qualityGate.getId());
associateProjectToQualityGate(project, qualityGate);
// Another project
ComponentDto anotherProject = db.components().insertPrivateProject(organization);
.setParam("organization", organization.getKey())
.execute();
- assertDeselected(project.getId());
- assertSelected(gateId, anotherProject.getId());
+ assertDeselected(project);
+ assertSelected(qualityGate, anotherProject);
}
@Test
.setParam("projectKey", project.getKey())
.execute();
- assertDeselected(project.getId());
+ assertDeselected(project);
}
@Test
.setResourceId(project.getId())
.setValue(qualityGate.getId().toString())
.setKey(SONAR_QUALITYGATE_PROPERTY));
+ db.qualityGates().associateProjectToQualityGate(project, qualityGate);
db.commit();
}
- private void assertDeselected(long projectId) {
- assertThat(dbClient.propertiesDao().selectProjectProperty(projectId, SONAR_QUALITYGATE_PROPERTY)).isNull();
+ private void assertDeselected(ComponentDto project) {
+ Optional<String> qGateUuid = db.qualityGates().selectQGateUuidByComponentUuid(project.uuid());
+ assertThat(qGateUuid)
+ .isNotNull()
+ .isEmpty();
+
+ assertThat(dbClient.propertiesDao().selectProjectProperty(project.getId(), SONAR_QUALITYGATE_PROPERTY)).isNull();
}
- private void assertSelected(String qGateId, long projectId) {
- assertThat(dbClient.propertiesDao().selectProjectProperty(projectId, SONAR_QUALITYGATE_PROPERTY).getValue()).isEqualTo(qGateId);
+ private void assertSelected(QGateWithOrgDto qualityGate, ComponentDto project) {
+ Optional<String> qGateUuid = db.qualityGates().selectQGateUuidByComponentUuid(project.uuid());
+ assertThat(qGateUuid)
+ .isNotNull()
+ .isNotEmpty()
+ .hasValue(qualityGate.getUuid());
+ String qGateId = dbClient.propertiesDao().selectProjectProperty(project.getId(), SONAR_QUALITYGATE_PROPERTY).getValue();
+ assertThat(qGateId).isEqualTo(String.valueOf(qualityGate.getId()));
}
}
.isEmpty();
assertThat(db.getDbClient().propertiesDao().selectProjectProperties(prj2.getDbKey()))
.isEmpty();
+
+ assertThat(db.getDbClient().projectQgateAssociationDao().selectQGateUuidByComponentUuid(dbSession, prj1.uuid()))
+ .isEmpty();
+ assertThat(db.getDbClient().projectQgateAssociationDao().selectQGateUuidByComponentUuid(dbSession, prj2.uuid()))
+ .isEmpty();
}
@Test
*/
package org.sonar.server.qualitygate.ws;
+import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
assertSelected(qualityGate, project);
}
+ @Test
+ public void change_quality_gate_for_project() {
+ OrganizationDto organization = db.organizations().insert();
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
+ QGateWithOrgDto initialQualityGate = db.qualityGates().insertQualityGate(organization);
+ QGateWithOrgDto secondQualityGate = db.qualityGates().insertQualityGate(organization);
+ ComponentDto project = db.components().insertPrivateProject(organization);
+
+ ws.newRequest()
+ .setParam("gateId", initialQualityGate.getId().toString())
+ .setParam("projectKey", project.getKey())
+ .setParam("organization", organization.getKey())
+ .execute();
+
+ ws.newRequest()
+ .setParam("gateId", secondQualityGate.getId().toString())
+ .setParam("projectKey", project.getKey())
+ .setParam("organization", organization.getKey())
+ .execute();
+
+ assertSelected(secondQualityGate, project);
+ }
+
+ @Test
+ public void select_same_quality_gate_for_project_twice() {
+ OrganizationDto organization = db.organizations().insert();
+ userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
+ QGateWithOrgDto initialQualityGate = db.qualityGates().insertQualityGate(organization);
+ ComponentDto project = db.components().insertPrivateProject(organization);
+
+ ws.newRequest()
+ .setParam("gateId", initialQualityGate.getId().toString())
+ .setParam("projectKey", project.getKey())
+ .setParam("organization", organization.getKey())
+ .execute();
+
+ ws.newRequest()
+ .setParam("gateId", initialQualityGate.getId().toString())
+ .setParam("projectKey", project.getKey())
+ .setParam("organization", organization.getKey())
+ .execute();
+
+ assertSelected(initialQualityGate, project);
+ }
+
@Test
public void project_admin() {
OrganizationDto organization = db.organizations().insert();
expectedException.expect(NotFoundException.class);
ws.newRequest()
- .setParam("gateId", String.valueOf("1"))
+ .setParam("gateId", "1")
.setParam("projectKey", project.getKey())
.setParam("organization", organization.getKey())
.execute();
private void assertSelected(QualityGateDto qualityGate, ComponentDto project) {
assertThat(dbClient.propertiesDao().selectProjectProperty(project.getId(), SONAR_QUALITYGATE_PROPERTY).getValue()).isEqualTo(qualityGate.getId().toString());
+ Optional<String> qGateUuid = db.qualityGates().selectQGateUuidByComponentUuid(project.uuid());
+ assertThat(qGateUuid)
+ .isNotNull()
+ .isNotEmpty()
+ .hasValue(qualityGate.getUuid());
}
}