]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10147 Remove 'sonar.qualitygate' setting usage to store default quality gate
authorEric Hartmann <hartmann.eric@gmail.com>
Thu, 7 Dec 2017 15:55:49 +0000 (16:55 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-db-dao/src/test/resources/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest/shared.xml
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java

index 830b74c8798d6d0d4f999548b44dad1f36444924..081048e62f71b9ef46194873c406ceadccae1624 100644 (file)
             id="8"
             private="[false]"/>
 
-  <properties id="1"
-              prop_key="sonar.qualitygate"
-              resource_id="[null]"
-              is_empty="[false]"
-              text_value="43"
-              created_at="1555000"/>
   <properties id="2"
               prop_key="sonar.qualitygate"
               resource_id="1"
index d4306095d5603e9c90752d494ce4a52bc37fc87b..32744f1de84cb23e4ca50180662faf24ccc3c8fd 100644 (file)
  */
 package org.sonar.server.qualitygate;
 
-import javax.annotation.Nullable;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 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.util.Validation.IS_ALREADY_USED_MESSAGE;
-import static org.sonar.server.ws.WsUtils.checkFound;
 
 public class QualityGateUpdater {
 
-  public static final String SONAR_QUALITYGATE_PROPERTY = "sonar.qualitygate";
-
   private final DbClient dbClient;
   private final UuidFactory uuidFactory;
 
@@ -73,15 +68,9 @@ public class QualityGateUpdater {
     checkQualityGateDoesNotAlreadyExist(dbSession, organizationDto, name);
   }
 
-  public void setDefault(DbSession dbSession, @Nullable QualityGateDto qualityGateDto) {
-    if (qualityGateDto == null) {
-      dbClient.propertiesDao().deleteGlobalProperty(SONAR_QUALITYGATE_PROPERTY, dbSession);
-    } else {
-      long qualityGateId = qualityGateDto.getId();
-      checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId), "There is no quality gate with id=" + qualityGateId);
-      dbClient.propertiesDao().saveProperty(dbSession,
-        new PropertyDto().setKey(SONAR_QUALITYGATE_PROPERTY).setValue(qualityGateDto.getId().toString()));
-    }
+  public void setDefault(DbSession dbSession, OrganizationDto organizationDto, QualityGateDto qualityGateDto) {
+    organizationDto.setDefaultQualityGateUuid(qualityGateDto.getUuid());
+    dbClient.qualityGateDao().update(qualityGateDto, dbSession);
   }
 
   private void checkQualityGateDoesNotAlreadyExist(DbSession dbSession, OrganizationDto organizationDto, String name) {
index 59cd8971aa5e4254767369131b25c63d4abdc0dd..0df8928ded6ba8d30aacba33be89cac9053ef1db 100644 (file)
  */
 package org.sonar.server.qualitygate;
 
-import javax.annotation.Nullable;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.permission.OrganizationPermission;
 import org.sonar.db.property.PropertiesDao;
-import org.sonar.db.property.PropertyDto;
 import org.sonar.db.qualitygate.QualityGateDao;
 import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.exceptions.NotFoundException;
@@ -57,33 +55,6 @@ public class QualityGates {
     this.organizationProvider = organizationProvider;
   }
 
-  /**
-   * Use {@link QualityGateUpdater#setDefault(DbSession, QualityGateDto)}
-   * @deprecated
-   */
-  @Deprecated
-  public void setDefault(DbSession dbSession, @Nullable Long idToUseAsDefault) {
-    checkIsQualityGateAdministrator();
-    if (idToUseAsDefault == null) {
-      propertiesDao.deleteGlobalProperty(SONAR_QUALITYGATE_PROPERTY, dbSession);
-    } else {
-      QualityGateDto newDefault = getNonNullQgate(dbSession, idToUseAsDefault);
-      propertiesDao.saveProperty(dbSession, new PropertyDto().setKey(SONAR_QUALITYGATE_PROPERTY).setValue(newDefault.getId().toString()));
-    }
-  }
-
-  /**
-   * Use {@link QualityGateUpdater#setDefault(DbSession, QualityGateDto)}
-   * @deprecated
-   */
-  @Deprecated
-  public void setDefault(@Nullable Long idToUseAsDefault) {
-    try (DbSession dbSession = dbClient.openSession(false)) {
-      setDefault(dbSession, idToUseAsDefault);
-      dbSession.commit();
-    }
-  }
-
   public void dissociateProject(DbSession dbSession, ComponentDto project) {
     checkProjectAdmin(project);
     propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession);
index ea7a71ce3bb6148d59aa518babe61cdc9ef1497e..97d2bdb33475d89b6a89ccae425c2789bf66fcfb 100644 (file)
@@ -98,12 +98,6 @@ public class RegisterQualityGates implements Startable {
         builtin = createQualityGate(dbSession, BUILTIN_QUALITY_GATE_NAME);
       }
 
-      // Set builtin as default if there is no default
-      if (!qualityGateFinder.getDefault(dbSession).isPresent()) {
-        LOGGER.info("Built-in quality gate [{}] has been set as default", BUILTIN_QUALITY_GATE_NAME);
-        qualityGateUpdater.setDefault(dbSession, builtin);
-      }
-
       // Set builtin if missing
       if (!builtin.isBuiltIn()) {
         builtin.setBuiltIn(true);
index 823d7ced164fb58faaa15e8460bb7f04e200ccfd..54f1f9d0cbe7392c3fec0629816b6dbb0d5a03e0 100644 (file)
@@ -22,14 +22,36 @@ package org.sonar.server.qualitygate.ws;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.server.qualitygate.QualityGates;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.qualitygate.QualityGateFinder;
+import org.sonar.server.qualitygate.QualityGateUpdater;
+import org.sonar.server.user.UserSession;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
+import static org.sonar.server.qualitygate.ws.QualityGatesWs.parseId;
+import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.Qualitygates.QualityGate.newBuilder;
 
 public class SetAsDefaultAction implements QualityGatesWsAction {
 
-  private final QualityGates qualityGates;
+  private final DbClient dbClient;
+  private final UserSession userSession;
+  private final QualityGateUpdater qualityGateUpdater;
+  private final QualityGateFinder qualityGateFinder;
+  private final QualityGatesWsSupport wsSupport;
 
-  public SetAsDefaultAction(QualityGates qualityGates) {
-    this.qualityGates = qualityGates;
+  public SetAsDefaultAction(DbClient dbClient, UserSession userSession, QualityGateUpdater qualityGateUpdater,
+    QualityGateFinder qualityGateFinder, QualityGatesWsSupport qualityGatesWsSupport) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.qualityGateUpdater = qualityGateUpdater;
+    this.qualityGateFinder = qualityGateFinder;
+    this.wsSupport = qualityGatesWsSupport;
   }
 
   @Override
@@ -45,11 +67,23 @@ public class SetAsDefaultAction implements QualityGatesWsAction {
       .setDescription("ID of the quality gate to set as default")
       .setRequired(true)
       .setExampleValue("1");
+
+    wsSupport.createOrganizationParam(action);
   }
 
   @Override
   public void handle(Request request, Response response) {
-    qualityGates.setDefault(QualityGatesWs.parseId(request, QualityGatesWsParameters.PARAM_ID));
+    Long id = parseId(request, PARAM_ID);
+
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
+      userSession.checkPermission(ADMINISTER_QUALITY_GATES, organization);
+      QualityGateDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id);
+      organization.setDefaultQualityGateUuid(qualityGate.getUuid());
+      dbClient.organizationDao().update(dbSession, organization);
+      dbSession.commit();
+    }
+
     response.noContent();
   }
 
index ce5f18f106151fa0ba59434e42694854766f0b09..03e795f5ecb1e8411338b89004b28b411585a1c5 100644 (file)
@@ -83,20 +83,4 @@ public class QualityGatesTest {
 
     userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid());
   }
-
-  @Test
-  public void should_select_default_qgate() {
-    long defaultId = QUALITY_GATE_ID;
-    String defaultName = "Default Name";
-    when(dao.selectById(dbSession, defaultId)).thenReturn(new QualityGateDto().setId(defaultId).setName(defaultName));
-
-    underTest.setDefault(defaultId);
-
-    verify(dao).selectById(dbSession, defaultId);
-    ArgumentCaptor<PropertyDto> propertyCaptor = ArgumentCaptor.forClass(PropertyDto.class);
-    verify(propertiesDao).saveProperty(any(DbSession.class), propertyCaptor.capture());
-
-    assertThat(propertyCaptor.getValue().getKey()).isEqualTo("sonar.qualitygate");
-    assertThat(propertyCaptor.getValue().getValue()).isEqualTo("42");
-  }
 }