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"
*/
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;
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) {
*/
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;
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);
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);
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
.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();
}
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");
- }
}