]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10136 Add method to get/set default quality gate on organization
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 7 Dec 2017 15:58:47 +0000 (16:58 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java

index f606589a6247012a42d7649a9b2d90d6400ab837..6735a3e746d1900877b5229077cd90c6e102c122 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.Pagination;
+import org.sonar.db.qualitygate.QGateWithOrgDto;
 import org.sonar.db.user.GroupDto;
 
 import static java.util.Objects.requireNonNull;
@@ -109,6 +110,10 @@ public class OrganizationDao implements Dao {
     getMapper(dbSession).updateDefaultGroupId(uuid, requireNonNull(defaultGroupId, "Default group id cannot be null"), system2.now());
   }
 
+  public void setDefaultQualityGate(DbSession dbSession, OrganizationDto organization, QGateWithOrgDto qualityGate) {
+    getMapper(dbSession).updateDefaultQualityGate(organization.getUuid(), qualityGate.getUuid(), system2.now());
+  }
+
   public boolean getNewProjectPrivate(DbSession dbSession, OrganizationDto organization) {
     return getMapper(dbSession).selectNewProjectPrivateByUuid(organization.getUuid());
   }
index bcaa10f513fee7ee391a71ad8db67b668a5fc058..c28d96fdda72b0586b4213f36d9172089c6c998b 100644 (file)
@@ -65,6 +65,9 @@ public interface OrganizationMapper {
   void updateDefaultGroupId(@Param("organizationUuid") String organizationUuid,
     @Param("defaultGroupId") int defaultGroupId, @Param("now") long now);
 
+  void updateDefaultQualityGate(@Param("organizationUuid") String organizationUuid,
+    @Param("defaultQualityGateUuid") String defaultQualityGateUuid, @Param("now") long now);
+
   void updateNewProjectPrivate(@Param("organizationUuid") String organizationUuid, @Param("newProjectPrivate") boolean newProjectPrivate, @Param("now") long now);
 
   int deleteByUuid(@Param("uuid") String uuid);
index 3d90f2a5f298a58bfbb109df2df3e82fad1b532a..614320715efb952808ad148b7571c915ca72a80a 100644 (file)
@@ -67,6 +67,10 @@ public class QualityGateDao implements Dao {
     return mapper(session).selectByIdAndOrganization(id, organization.getUuid());
   }
 
+  public QGateWithOrgDto selectDefault(DbSession dbSession, OrganizationDto organization) {
+    return mapper(dbSession).selectDefault(organization.getUuid());
+  }
+
   public void delete(QualityGateDto qGate, DbSession session) {
     mapper(session).delete(qGate.getId());
   }
index a6fa14c48eee20224ef1141934ae84eade218db2..533be402260b5a5485b887fc74c038983039ff2e 100644 (file)
@@ -40,6 +40,8 @@ public interface QualityGateMapper {
 
   QGateWithOrgDto selectByIdAndOrganization(@Param("id") long id, @Param("organizationUuid") String organizationUuid);
 
+  QGateWithOrgDto selectDefault(@Param("organizationUuid") String organizationUuid);
+
   QualityGateDto selectBuiltIn();
 
   void delete(long id);
index fcdacde0253a72dd394b2345fc1a3958916ab7ef..41d758d95362dedfb908bce15534b6cb22ffa356 100644 (file)
       uuid = #{organizationUuid, jdbcType=VARCHAR}
   </update>
 
+  <update id="updateDefaultQualityGate">
+    update organizations
+    set
+      default_quality_gate_uuid = #{defaultQualityGateUuid, jdbcType=INTEGER},
+      updated_at = #{now, jdbcType=BIGINT}
+    where
+      uuid = #{organizationUuid, jdbcType=VARCHAR}
+  </update>
+
   <update id="updateNewProjectPrivate">
     update organizations
     set
index 468ecab43e1a16c5b9eb7a6a3c5d36f9c02fb71c..aeddd3947e4a263bc3ca88cca2609fd1c2e35f5a 100644 (file)
       is_built_in = ${_true}
   </select>
 
+  <select id="selectDefault" resultType="org.sonar.db.qualitygate.QGateWithOrgDto">
+    SELECT
+    <include refid="qateWithOrgColumns"/>
+    FROM quality_gates qg
+    INNER JOIN org_quality_gates oqg ON oqg.quality_gate_uuid = qg.uuid
+    INNER JOIN organizations o ON o.default_quality_gate_uuid = qg.uuid AND o.uuid=#{organizationUuid, jdbcType=VARCHAR}
+  </select>
+
   <update id="delete" parameterType="long">
     delete from quality_gates where id=#{id}
   </update>
index e57971cf294b2d4cb5c7b9134448e22a9b6f9992..c6bfeb8e232498dcf1f681544efe373a6e33d693 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.dialect.Dialect;
 import org.sonar.db.dialect.Oracle;
+import org.sonar.db.qualitygate.QGateWithOrgDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.GroupTesting;
 import org.sonar.db.user.UserDto;
@@ -664,6 +665,19 @@ public class OrganizationDaoTest {
     underTest.setDefaultGroupId(dbSession, "uuid", GroupTesting.newGroupDto().setId(null));
   }
 
+  @Test
+  public void setDefaultQualityGate() {
+    when(system2.now()).thenReturn(DATE_3);
+    OrganizationDto organization = dbTester.organizations().insert();
+    QGateWithOrgDto qualityGate = dbTester.qualityGates().insertQualityGate(organization);
+
+    underTest.setDefaultQualityGate(dbSession, organization, qualityGate);
+    dbTester.commit();
+
+    assertThat(dbClient.qualityGateDao().selectDefault(dbSession, organization).getUuid()).isEqualTo(qualityGate.getUuid());
+    verifyOrganizationUpdatedAt(organization.getUuid(), DATE_3);
+  }
+
   @Test
   public void update_fails_with_NPE_if_OrganizationDto_is_null() {
     expectDtoCanNotBeNull();
@@ -1007,7 +1021,8 @@ public class OrganizationDaoTest {
       " guarded as \"guarded\", user_id as \"userId\"," +
       " created_at as \"createdAt\", updated_at as \"updatedAt\"," +
       " default_perm_template_project as \"projectDefaultPermTemplate\"," +
-      " default_perm_template_view as \"viewDefaultPermTemplate\"" +
+      " default_perm_template_view as \"viewDefaultPermTemplate\"," +
+      " default_quality_gate_uuid as \"defaultQualityGateUuid\" " +
       " from organizations");
     assertThat(rows).hasSize(1);
     return rows.get(0);
index e3d73d3e8ac57c45fec001779838d79d79ce3c54..99e93b43054cab510c589b4c7856856e5cc1736a 100644 (file)
@@ -145,6 +145,19 @@ public class QualityGateDaoTest {
     assertThat(underTest.selectByOrganizationAndId(dbSession, organization, 123L)).isNull();
   }
 
+  @Test
+  public void select_default() {
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+    db.getDbClient().organizationDao().setDefaultQualityGate(dbSession, organization, qualityGate);
+    OrganizationDto otherOrganization = db.organizations().insert();
+    QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
+    db.getDbClient().organizationDao().setDefaultQualityGate(dbSession, otherOrganization, otherQualityGate);
+
+    assertThat(underTest.selectDefault(dbSession, organization).getUuid()).isEqualTo(qualityGate.getUuid());
+    assertThat(underTest.selectDefault(dbSession, otherOrganization).getUuid()).isEqualTo(otherQualityGate.getUuid());
+  }
+
   @Test
   public void delete() {
     OrganizationDto organization = db.organizations().insert();