}
/**
- * Returns all projects (Scope {@link org.sonar.api.resources.Scopes#PROJECT} and qualifier
- * {@link org.sonar.api.resources.Qualifiers#PROJECT}) which are enabled.
+ * Returns all projects (Scope {@link Scopes#PROJECT} and qualifier
+ * {@link Qualifiers#PROJECT}) which are enabled.
*
* Branches are not returned.
*
org.kee as "key",
org.name as "name",
org.description as "description",
+ org.default_quality_gate_uuid as "defaultQualityGateUuid",
org.url as "url",
org.avatar_url as "avatarUrl",
org.guarded as "guarded",
name = #{organization.name, jdbcType=VARCHAR},
description = #{organization.description, jdbcType=VARCHAR},
url = #{organization.url, jdbcType=VARCHAR},
+ default_quality_gate_uuid = #{organization.defaultQualityGateUuid, jdbcType=VARCHAR},
avatar_url = #{organization.avatarUrl, jdbcType=VARCHAR},
updated_at = #{organization.updatedAt, jdbcType=BIGINT}
where
.setName(randomAlphanumeric(64))
.setDescription(randomAlphanumeric(256))
.setAvatarUrl(randomAlphanumeric(256))
- .setDefaultQualityGateUuid("1") // TODO check that using it directly is ok
+ .setDefaultQualityGateUuid("_NOT_SET_")
.setUrl(randomAlphanumeric(256));
}
}
@Test
public void associate() {
- QualityGateDto qgate = db.qualityGates().insertQualityGate();
+ QualityGateDto qgate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
OrganizationDto org = db.organizations().insert();
underTest.associate(dbSession, Uuids.createFast(), org, qgate);
}
private void insertQualityGates() {
- qualityGateDbTester.insertQualityGate(g -> g.setName("Very strict").setBuiltIn(false));
- qualityGateDbTester.insertQualityGate(g -> g.setName("Balanced").setBuiltIn(false));
- qualityGateDbTester.insertQualityGate(g -> g.setName("Lenient").setBuiltIn(false));
+ qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Very strict").setBuiltIn(false));
+ qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Balanced").setBuiltIn(false));
+ qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Lenient").setBuiltIn(false));
}
}
this.dbSession = db.getSession();
}
- public QualityGateDto insertQualityGate(String name) {
- return insertQualityGate(qualityGate -> qualityGate.setName(name));
- }
-
public QualityGateDto insertBuiltInQualityGate() {
QualityGateDto builtin = dbClient.qualityGateDao().insert(dbSession, new QualityGateDto()
.setName("Sonar way")
return builtin;
}
- @SafeVarargs
- public final QualityGateDto insertQualityGate(Consumer<QualityGateDto>... dtoPopulators) {
- return insertQualityGate(db.getDefaultOrganization(), dtoPopulators);
+ public void setBuiltInAsDefaultOn(OrganizationDto organizationDto) {
+ QualityGateDto builtinQG = dbClient.qualityGateDao().selectBuiltIn(dbSession);
+ dbClient.organizationDao().update(dbSession, organizationDto.setDefaultQualityGateUuid(builtinQG.getUuid()));
+ if (dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organizationDto, builtinQG.getUuid()) == null) {
+ associateQualityGateToOrganization(builtinQG, organizationDto);
+ }
+ dbSession.commit();
}
@SafeVarargs
db.commit();
}
- public QualityGateDto createDefaultQualityGate(String qualityGateName) {
- QualityGateDto defaultQGate = insertQualityGate(qualityGateName);
- setDefaultQualityGate(defaultQGate);
+ public void associateQualityGateToOrganization(QualityGateDto qualityGate, OrganizationDto organization) {
+ dbClient.qualityGateDao().associate(dbSession, Uuids.createFast(), organization, qualityGate);
+ dbSession.commit();
+ }
+
+ @SafeVarargs
+ public final QualityGateDto createDefaultQualityGate(OrganizationDto organization, Consumer<QualityGateDto>... dtoPopulators) {
+ QualityGateDto defaultQGate = insertQualityGate(organization, dtoPopulators);
+ setDefaultQualityGate(organization, defaultQGate);
return defaultQGate;
}
- public void setDefaultQualityGate(QualityGateDto qualityGate) {
- dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
- .setKey("sonar.qualitygate")
- .setValue(String.valueOf(qualityGate.getId())));
- db.commit();
+ public void setDefaultQualityGate(OrganizationDto organization, QualityGateDto qualityGate) {
+ dbClient.organizationDao().update(dbSession, organization.setDefaultQualityGateUuid(qualityGate.getUuid()));
+ dbSession.commit();
}
@SafeVarargs
package org.sonar.server.qualitygate;
import java.util.Optional;
-import org.apache.commons.lang.StringUtils;
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.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import static com.google.common.base.Preconditions.checkState;
-import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
import static org.sonar.server.ws.WsUtils.checkFound;
public class QualityGateFinder {
+ public static final String SONAR_QUALITYGATE_PROPERTY = "sonar.qualitygate";
+
private final DbClient dbClient;
public QualityGateFinder(DbClient dbClient) {
/**
* Return effective quality gate of a project.
*
- * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate.
- * As it's possible to have no default quality gate, this method can return {@link Optional#empty()}
+ * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate ofI the organization
*/
- public Optional<QualityGateData> getQualityGate(DbSession dbSession, long componentId) {
+ public QualityGateData getQualityGate(DbSession dbSession, OrganizationDto organization, long componentId) {
Optional<Long> qualityGateId = dbClient.projectQgateAssociationDao().selectQGateIdByComponentId(dbSession, componentId);
if (qualityGateId.isPresent()) {
QualityGateDto qualityGate = checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId.get()), "No quality gate has been found for id %s", qualityGateId);
- return Optional.of(new QualityGateData(qualityGate, false));
+ return new QualityGateData(qualityGate, false);
} else {
- Optional<QualityGateDto> defaultQualityGate = getDefault(dbSession);
- if (!defaultQualityGate.isPresent()) {
- return Optional.empty();
- }
- return Optional.of(new QualityGateData(defaultQualityGate.get(), true));
+ QualityGateDto defaultQualityGate = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, organization.getDefaultQualityGateUuid());
+ checkState(defaultQualityGate != null, "Unable to find the quality gate [%s] for organization [%s]", organization.getDefaultQualityGateUuid(), organization.getUuid());
+ return new QualityGateData(defaultQualityGate, true);
}
}
"No quality gate has been found for id %s in organization %s", qualityGateId, organization.getName());
}
- // TODO As there is always a default quality gate, this method should not return an optional
- public Optional<QualityGateDto> getDefault(DbSession dbSession) {
- Optional<Long> defaultQualityGateId = getDefaultId(dbSession);
-
- if (!defaultQualityGateId.isPresent()) {
- // For the moment, it's possible to have no default quality gate, but it will change with SONAR-8507
- return Optional.empty();
- } else {
- return Optional.ofNullable(
- dbClient.qualityGateDao().selectById(dbSession, defaultQualityGateId.get()));
- }
+ public QualityGateDto getDefault(DbSession dbSession, OrganizationDto organization) {
+ QGateWithOrgDto qgate = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, organization.getDefaultQualityGateUuid());
+ checkState(qgate != null, "Default quality gate [%s] is missing on organization [%s]", organization.getDefaultQualityGateUuid(), organization.getUuid());
+ return qgate;
}
public QualityGateDto getBuiltInQualityGate(DbSession dbSession) {
return builtIn;
}
- private Optional<Long> getDefaultId(DbSession dbSession) {
- PropertyDto defaultQualityGateId = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY);
-
- if (defaultQualityGateId == null || StringUtils.isBlank(defaultQualityGateId.getValue())) {
- // For the moment, it's possible to have no default quality gate, but it will change with SONAR-8507
- return Optional.empty();
- }
-
- return Optional.of(Long.valueOf(defaultQualityGateId.getValue()));
- }
-
public static class QualityGateData {
private final QualityGateDto qualityGate;
private final boolean isDefault;
@Override
protected void configureModule() {
add(
- QualityGates.class,
QualityGateUpdater.class,
QualityGateConditionsUpdater.class,
QgateProjectFinder.class,
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");
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 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.qualitygate;
-
-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.qualitygate.QualityGateDao;
-import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.user.UserSession;
-
-import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
-
-/**
- * Methods from this class should be moved to {@link QualityGateUpdater} and to classes QualityGateFinder / QualityGateConditionsUpdater / etc.
- * in order to have classes with clearer responsibilities and more easily testable (without having to use too much mocks)
- */
-public class QualityGates {
-
- public static final String SONAR_QUALITYGATE_PROPERTY = "sonar.qualitygate";
-
- private final DbClient dbClient;
- private final QualityGateDao dao;
- private final PropertiesDao propertiesDao;
- private final UserSession userSession;
- private final DefaultOrganizationProvider organizationProvider;
-
- public QualityGates(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider) {
- this.dbClient = dbClient;
- this.dao = dbClient.qualityGateDao();
- this.propertiesDao = dbClient.propertiesDao();
- this.userSession = userSession;
- this.organizationProvider = organizationProvider;
- }
-
- public void dissociateProject(DbSession dbSession, ComponentDto project) {
- checkProjectAdmin(project);
- propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession);
- dbSession.commit();
- }
-
- private QualityGateDto getNonNullQgate(DbSession dbSession, long id) {
- QualityGateDto qGate = dao.selectById(dbSession, id);
- if (qGate == null) {
- throw new NotFoundException("There is no quality gate with id=" + id);
- }
- return qGate;
- }
-
- private void checkIsQualityGateAdministrator() {
- userSession.checkPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid());
- }
-
- private void checkProjectAdmin(ComponentDto project) {
- if (!userSession.hasPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, project.getOrganizationUuid())
- && !userSession.hasComponentPermission(UserRole.ADMIN, project)) {
- throw insufficientPrivilegesException();
- }
- }
-}
private final DbClient dbClient;
private final QualityGateConditionsUpdater qualityGateConditionsUpdater;
- private final QualityGateFinder qualityGateFinder;
- private final QualityGateUpdater qualityGateUpdater;
private final QualityGateDao qualityGateDao;
private final QualityGateConditionDao qualityGateConditionDao;
private final UuidFactory uuidFactory;
private final System2 system2;
- public RegisterQualityGates(DbClient dbClient, QualityGateUpdater qualityGateUpdater,
- QualityGateConditionsUpdater qualityGateConditionsUpdater, QualityGateFinder qualityGateFinder, UuidFactory uuidFactory, System2 system2) {
+ public RegisterQualityGates(DbClient dbClient,
+ QualityGateConditionsUpdater qualityGateConditionsUpdater, UuidFactory uuidFactory, System2 system2) {
this.dbClient = dbClient;
this.qualityGateConditionsUpdater = qualityGateConditionsUpdater;
- this.qualityGateUpdater = qualityGateUpdater;
- this.qualityGateFinder = qualityGateFinder;
this.qualityGateDao = dbClient.qualityGateDao();
this.qualityGateConditionDao = dbClient.gateConditionDao();
this.uuidFactory = uuidFactory;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentFinder;
-import static org.sonar.server.qualitygate.QualityGateUpdater.SONAR_QUALITYGATE_PROPERTY;
+import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
*/
package org.sonar.server.qualitygate.ws;
-import java.util.Optional;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
QGateWithOrgDto qualityGate = finder.getByOrganizationAndId(dbSession, organization, qualityGateId);
- Optional<QualityGateDto> defaultQualityGate = finder.getDefault(dbSession);
- checkArgument(!defaultQualityGate.isPresent() || !defaultQualityGate.get().getId().equals(qualityGate.getId()), "The default quality gate cannot be removed");
+ QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization);
+ checkArgument(!defaultQualityGate.getId().equals(qualityGate.getId()), "The default quality gate cannot be removed");
wsSupport.checkCanEdit(qualityGate);
dbClient.qualityGateDao().delete(qualityGate, dbSession);
*/
package org.sonar.server.qualitygate.ws;
-import java.util.Optional;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
throw insufficientPrivilegesException();
}
- Optional<QualityGateData> data = qualityGateFinder.getQualityGate(dbSession, project.getId());
+ QualityGateData data = qualityGateFinder.getQualityGate(dbSession, organization, project.getId());
writeProtobuf(buildResponse(data), request, response);
}
}
- private static GetByProjectResponse buildResponse(Optional<QualityGateData> data) {
- if (!data.isPresent()) {
- return GetByProjectResponse.getDefaultInstance();
- }
-
- QualityGateDto qualityGate = data.get().getQualityGate();
+ private static GetByProjectResponse buildResponse(QualityGateData data) {
+ QualityGateDto qualityGate = data.getQualityGate();
GetByProjectResponse.Builder response = GetByProjectResponse.newBuilder();
response.getQualityGateBuilder()
.setId(qualityGate.getId())
.setName(qualityGate.getName())
- .setDefault(data.get().isDefault());
+ .setDefault(data.isDefault());
return response.build();
}
import com.google.common.io.Resources;
import java.util.Collection;
-import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
public void handle(Request request, Response response) {
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
- Optional<QualityGateDto> defaultQualityGate = finder.getDefault(dbSession);
+ QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization);
Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization);
- writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate.orElse(null)), request, response);
+ writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate), request, response);
}
}
import org.sonar.server.component.ComponentFinder.ParamNames;
import org.sonar.server.qualitygate.QualityGateFinder;
-import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
+import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_SELECT;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID;
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 DbClient dbClient;
private final UserSession userSession;
- private final QualityGateUpdater qualityGateUpdater;
private final QualityGateFinder qualityGateFinder;
private final QualityGatesWsSupport wsSupport;
- public SetAsDefaultAction(DbClient dbClient, UserSession userSession, QualityGateUpdater qualityGateUpdater,
+ public SetAsDefaultAction(DbClient dbClient, UserSession userSession,
QualityGateFinder qualityGateFinder, QualityGatesWsSupport qualityGatesWsSupport) {
this.dbClient = dbClient;
this.userSession = userSession;
- this.qualityGateUpdater = qualityGateUpdater;
this.qualityGateFinder = qualityGateFinder;
this.wsSupport = qualityGatesWsSupport;
}
import com.google.common.io.Resources;
import java.util.Collection;
import java.util.Map;
-import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
QualityGateDto qualityGate = getByNameOrId(dbSession, organization, name, id);
Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
Map<Integer, MetricDto> metricsById = getMetricsById(dbSession, conditions);
- Optional<QualityGateDto> defaultQualityGate = qualityGateFinder.getDefault(dbSession);
- writeProtobuf(buildResponse(organization, qualityGate, defaultQualityGate.orElse(null), conditions, metricsById), request, response);
+ QualityGateDto defaultQualityGate = qualityGateFinder.getDefault(dbSession, organization);
+ writeProtobuf(buildResponse(organization, qualityGate, defaultQualityGate, conditions, metricsById), request, response);
}
}
.collect(uniqueIndex(MetricDto::getId));
}
- private ShowWsResponse buildResponse(OrganizationDto organization, QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate,
+ private ShowWsResponse buildResponse(OrganizationDto organization, QualityGateDto qualityGate, QualityGateDto defaultQualityGate,
Collection<QualityGateConditionDto> conditions, Map<Integer, MetricDto> metricsById) {
return ShowWsResponse.newBuilder()
.setId(qualityGate.getId())
json.beginObject();
writeComponent(json, session, component, org, analysis.orElse(null));
writeProfiles(json, session, component);
- writeQualityGate(json, session, component);
+ writeQualityGate(json, session, org, component);
if (userSession.hasComponentPermission(ADMIN, component) ||
userSession.hasPermission(ADMINISTER_QUALITY_PROFILES, org) ||
userSession.hasPermission(ADMINISTER_QUALITY_GATES, org)) {
json.endArray();
}
- private void writeQualityGate(JsonWriter json, DbSession session, ComponentDto component) {
- Optional<QualityGateFinder.QualityGateData> qualityGateData = qualityGateFinder.getQualityGate(session, component.getId());
- if (!qualityGateData.isPresent()) {
- return;
- }
- QualityGateDto qualityGateDto = qualityGateData.get().getQualityGate();
+ private void writeQualityGate(JsonWriter json, DbSession session, OrganizationDto organization, ComponentDto component) {
+ QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, organization, component.getId());
+ QualityGateDto qualityGateDto = qualityGateData.getQualityGate();
json.name("qualityGate").beginObject()
.prop("key", qualityGateDto.getId())
.prop("name", qualityGateDto.getName())
- .prop("isDefault", qualityGateData.get().isDefault())
+ .prop("isDefault", qualityGateData.isDefault())
.endObject();
}
loggedAsSystemAdministrator();
MetricDto customMetric = insertCustomMetric("custom-key");
MetricDto nonCustomMetric = insertMetric(newMetricDto().setEnabled(true).setUserManaged(false).setKey("non-custom"));
- QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
db.qualityGates().addCondition(qualityGate1, customMetric);
- QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
db.qualityGates().addCondition(qualityGate2, customMetric);
db.qualityGates().addCondition(qualityGate2, nonCustomMetric);
import static org.sonar.db.qualitygate.ProjectQgateAssociationQuery.IN;
import static org.sonar.db.qualitygate.ProjectQgateAssociationQuery.OUT;
import static org.sonar.db.qualitygate.ProjectQgateAssociationQuery.builder;
-import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
+import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY;
public class QgateProjectFinderTest {
@Before
public void setUp() throws Exception {
- qualityGateDto = qualityGateDbTester.insertQualityGate();
+ qualityGateDto = qualityGateDbTester.insertQualityGate(db.getDefaultOrganization());
dbClient.metricDao().insert(dbSession, coverageMetricDto, ratingMetricDto);
dbSession.commit();
}
*/
package org.sonar.server.qualitygate;
-import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.exceptions.NotFoundException;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
public class QualityGateFinderTest {
@Test
public void return_default_quality_gate_for_project() {
ComponentDto project = db.components().insertPrivateProject();
- QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate("Sonar way");
+ QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
- Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, project.getId());
+ QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId());
- assertThat(result).isPresent();
- assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId());
- assertThat(result.get().isDefault()).isTrue();
+ assertThat(result).isNotNull();
+ assertThat(result.getQualityGate().getId()).isEqualTo(dbQualityGate.getId());
+ assertThat(result.isDefault()).isTrue();
}
@Test
public void return_project_quality_gate_over_default() {
ComponentDto project = db.components().insertPrivateProject();
- db.qualityGates().createDefaultQualityGate("Sonar way");
- QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate("My team QG");
+ db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(),qg -> qg.setName("Sonar way"));
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
db.qualityGates().associateProjectToQualityGate(project, dbQualityGate);
- Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, project.getId());
+ QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId());
- assertThat(result).isPresent();
- assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId());
- assertThat(result.get().isDefault()).isFalse();
+ assertThat(result).isNotNull();
+ assertThat(result.getQualityGate().getId()).isEqualTo(dbQualityGate.getId());
+ assertThat(result.isDefault()).isFalse();
}
@Test
- public void return_nothing_when_no_default_qgate_and_no_qgate_defined_for_project() {
+ public void fail_when_default_qgate_defined_does_not_exists() {
ComponentDto project = db.components().insertPrivateProject();
+ QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
+ db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession);
+ db.commit();
- Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, project.getId());
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage(format("Unable to find the quality gate [%s] for organization [%s]", dbQualityGate.getUuid(), db.getDefaultOrganization().getUuid()));
- assertThat(result).isNotPresent();
+ underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId());
}
@Test
- public void fail_when_default_qgate_defined_in_properties_does_not_exists() {
+ public void fail_when_project_qgate_defined_does_not_exists() {
ComponentDto project = db.components().insertPrivateProject();
- QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate("Sonar way");
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
+ db.qualityGates().associateProjectToQualityGate(project, dbQualityGate);
db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession);
- assertThat(underTest.getQualityGate(dbSession, project.getId())).isEmpty();
+ expectedException.expect(NotFoundException.class);
+ underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId());
}
@Test
- public void fail_when_project_qgate_defined_in_properties_does_not_exists() {
- ComponentDto project = db.components().insertPrivateProject();
- QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate("My team QG");
- db.qualityGates().associateProjectToQualityGate(project, dbQualityGate);
+ public void fail_when_default_quality_gate_does_not_exists() {
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
+ db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), dbQualityGate);
db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession);
- expectedException.expect(NotFoundException.class);
- underTest.getQualityGate(dbSession, project.getId());
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage(format("Default quality gate [%s] is missing on organization [%s]", dbQualityGate.getUuid(), db.getDefaultOrganization().getUuid()));
+
+ underTest.getDefault(dbSession, db.getDefaultOrganization());
}
}
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new QualityGateModule().configure(container);
- assertThat(container.size()).isEqualTo(22 + 2);
+ assertThat(container.size()).isEqualTo(21 + 2);
}
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 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.qualitygate;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.mockito.ArgumentCaptor;
-import org.sonar.core.util.Uuids;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDao;
-import org.sonar.db.organization.OrganizationTesting;
-import org.sonar.db.permission.OrganizationPermission;
-import org.sonar.db.property.PropertiesDao;
-import org.sonar.db.property.PropertyDto;
-import org.sonar.db.qualitygate.QualityGateConditionDao;
-import org.sonar.db.qualitygate.QualityGateDao;
-import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.tester.UserSessionRule;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
-
-public class QualityGatesTest {
-
- private static final long QUALITY_GATE_ID = 42L;
- private static final String PROJECT_KEY = "SonarQube";
- private static final String PROJECT_UUID = Uuids.UUID_EXAMPLE_01;
- private static final String ORG_UUID = "ORG_UUID";
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
-
- private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.fromUuid(ORG_UUID);
- private DbSession dbSession = mock(DbSession.class);
- private DbClient dbClient = mock(DbClient.class);
- private QualityGateDao dao = mock(QualityGateDao.class);
- private QualityGateConditionDao conditionDao = mock(QualityGateConditionDao.class);
- private PropertiesDao propertiesDao = mock(PropertiesDao.class);
- private ComponentDao componentDao = mock(ComponentDao.class);
- private QualityGates underTest;
-
- @Before
- public void setUp() {
- when(dbClient.openSession(false)).thenReturn(dbSession);
- when(dbClient.qualityGateDao()).thenReturn(dao);
- when(dbClient.gateConditionDao()).thenReturn(conditionDao);
- when(dbClient.propertiesDao()).thenReturn(propertiesDao);
- when(dbClient.componentDao()).thenReturn(componentDao);
-
- when(componentDao.selectOrFailById(eq(dbSession), anyLong())).thenReturn(
- newPrivateProjectDto(OrganizationTesting.newOrganizationDto(), PROJECT_UUID).setId(1L).setDbKey(PROJECT_KEY));
-
- underTest = new QualityGates(dbClient, userSession, organizationProvider);
-
- userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid());
- }
-}
private QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance());
private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
- private RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateUpdater, qualityGateConditionsUpdater, qualityGateFinder,
+ private RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateConditionsUpdater,
UuidFactoryFast.getInstance(), System2.INSTANCE);
private QualityGateDto builtInQG;
underTest.start();
- assertThat(qualityGateFinder.getDefault(dbSession)).isPresent();
- assertThat(qualityGateFinder.getDefault(dbSession).get().getId()).isEqualTo(builtInQG.getId());
-
- assertThat(
- logTester.logs(LoggerLevel.INFO).contains("Built-in quality gate [Sonar way] has been set as default")).isTrue();
+ assertThat(qualityGateFinder.getBuiltInQualityGate(dbSession)).isNotNull();
+ assertThat(qualityGateFinder.getBuiltInQualityGate(dbSession).getId()).isEqualTo(builtInQG.getId());
}
@Test
@Test
public void response_contains_quality_gate() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
QualityGate response = ws.newRequest()
.setParam(PARAM_ID, qualityGate.getId().toString())
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
-import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
+import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY;
public class DeselectActionTest {
private void assertSelected(String qGateId, long projectId) {
assertThat(dbClient.propertiesDao().selectProjectProperty(projectId, SONAR_QUALITYGATE_PROPERTY).getValue()).isEqualTo(qGateId);
}
-
}
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QGateWithOrgDto;
+import org.sonar.db.qualitygate.QualityGateDbTester;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
private DbClient dbClient = db.getDbClient();
private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
+ private QualityGateDbTester qualityGateDbTester = new QualityGateDbTester(db);
private QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider);
private DbSession dbSession = db.getSession();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ qualityGateDbTester.setBuiltInAsDefaultOn(organization);
+
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getId().toString())
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull();
- assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isZero();
+ assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isEqualTo(1); // built-in quality gate
}
@Test
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate();
- db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+ qualityGateDbTester.setBuiltInAsDefaultOn(organization);
+
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGate.getId()))
@Test
public void does_not_delete_built_in_quality_gate() {
OrganizationDto organization = db.organizations().insert();
+ qualityGateDbTester.setBuiltInAsDefaultOn(organization);
+
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
@Test
public void default_organization_is_used_when_no_organization_parameter() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+
+ qualityGateDbTester.setBuiltInAsDefaultOn(db.getDefaultOrganization());
+
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
OrganizationDto otherOrganization = db.organizations().insert();
QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(qualityGate);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The default quality gate cannot be removed");
@Test
public void fail_when_not_quality_gates_administer() {
OrganizationDto organization = db.organizations().insert();
+
+ qualityGateDbTester.setBuiltInAsDefaultOn(organization);
+
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
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.TestComponentFinder;
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My team QG"));
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
+ db.qualityGates().associateProjectToQualityGate(project, qualityGate);
logInAsProjectUser(project);
String result = ws.newRequest()
.isSimilarTo(getClass().getResource("get_by_project-example.json"));
}
- @Test
- public void empty_response() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
- db.qualityGates().insertQualityGate(organization);
- logInAsProjectUser(project);
-
- String result = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
- .execute().getInput();
-
- assertThat(result).isEqualToIgnoringWhitespace("{}");
- }
-
@Test
public void default_quality_gate() {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
- QGateWithOrgDto dbQualityGate = db.qualityGates().insertQualityGate(organization);
- setDefaultQualityGate(dbQualityGate.getId());
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, dbQualityGate);
logInAsProjectUser(project);
GetByProjectResponse result = ws.newRequest()
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
QGateWithOrgDto defaultDbQualityGate = db.qualityGates().insertQualityGate(organization);
- setDefaultQualityGate(defaultDbQualityGate.getId());
+ db.qualityGates().setDefaultQualityGate(organization, defaultDbQualityGate);
QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
+ db.qualityGates().associateProjectToQualityGate(project, qualityGate);
logInAsProjectUser(project);
GetByProjectResponse result = ws.newRequest()
public void get_by_project_key() {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
+
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG"));
+ db.qualityGates().associateProjectToQualityGate(project, qualityGate);
logInAsProjectUser(project);
GetByProjectResponse result = ws.newRequest()
public void get_with_project_admin_permission() {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
GetByProjectResponse result = ws.newRequest()
.setParam("project", project.getKey())
public void get_with_project_user_permission() {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertPrivateProject(organization);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
userSession.logIn().addProjectPermission(UserRole.USER, project);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
GetByProjectResponse result = ws.newRequest()
.setParam("project", project.getKey())
OrganizationDto organization = db.getDefaultOrganization();
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
ComponentDto project = db.components().insertPrivateProject(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
userSession.logIn().addProjectPermission(UserRole.USER, project);
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
GetByProjectResponse result = ws.newRequest()
.setParam("project", project.getKey())
assertThat(result.getQualityGate().getName()).isEqualTo(qualityGate.getName());
}
- @Test
- public void fail_when_project_does_not_not_belong_to_organization() {
- OrganizationDto organization = db.organizations().insert();
- OrganizationDto otherOrganization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(otherOrganization);
- userSession.logIn().addProjectPermission(UserRole.USER, project);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(String.format("Project '%s' doesn't exist in organization '%s'", project.getKey(), organization.getKey()));
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("organization", organization.getKey())
- .execute();
- }
-
@Test
public void fail_when_insufficient_permission() {
OrganizationDto organization = db.organizations().insert();
+ QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), dbQualityGate);
ComponentDto project = db.components().insertPrivateProject(organization);
- QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- associateProjectToQualityGate(project.getId(), qualityGate.getId());
userSession.logIn();
expectedException.expect(ForbiddenException.class);
.execute();
}
- private void associateProjectToQualityGate(long componentId, long qualityGateId) {
- dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
- .setKey("sonar.qualitygate")
- .setResourceId(componentId)
- .setValue(String.valueOf(qualityGateId)));
- db.commit();
- }
-
- private void setDefaultQualityGate(long qualityGateId) {
- dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
- .setKey("sonar.qualitygate")
- .setValue(String.valueOf(qualityGateId)));
- db.commit();
- }
-
private void logInAsProjectUser(ComponentDto project) {
userSession.logIn().addProjectPermission(UserRole.USER, project);
}
userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, organization);
QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setName("Sonar way").setBuiltIn(true));
db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setName("Sonar way - Without Coverage").setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+ db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
String response = ws.newRequest()
.setParam("organization", organization.getKey())
OrganizationDto organization = db.organizations().insert();
QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization);
QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+ db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
ListWsResponse response = ws.newRequest()
.setParam("organization", organization.getKey())
@Test
public void default_organization_is_used_when_no_organization_parameter() {
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), qualityGate);
+
OrganizationDto otherOrganization = db.organizations().insert();
QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
+ db.qualityGates().setDefaultQualityGate(otherOrganization, otherQualityGate);
ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
OrganizationDto organization = db.organizations().insert();
QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setBuiltIn(true));
QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setBuiltIn(false));
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate1);
ListWsResponse response = ws.newRequest()
.setParam("organization", organization.getKey())
public void test_deprecated_default_field() {
OrganizationDto organization = db.organizations().insert();
QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+ db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
ListWsResponse response = ws.newRequest()
.setParam("organization", organization.getKey())
OrganizationDto organization = db.organizations().insert();
QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ expectedException.expect(IllegalStateException.class);
+
ListWsResponse response = ws.newRequest()
.setParam("organization", organization.getKey())
.executeProtobuf(ListWsResponse.class);
- assertThat(response.getQualitygatesList())
- .extracting(QualityGate::getId, QualityGate::getName, QualityGate::getIsDefault)
- .containsExactlyInAnyOrder(tuple(qualityGate.getId(), qualityGate.getName(), false));
}
@Test
QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Default").setBuiltIn(false));
QualityGateDto builtInQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way").setBuiltIn(true));
QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+ db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
ListWsResponse response = ws.newRequest()
.setParam("organization", organization.getKey())
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization);
QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way").setBuiltIn(true));
QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false));
- db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+ db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate);
ListWsResponse response = ws.newRequest()
.setParam("organization", organization.getKey())
tuple(defaultQualityGate.getName(), false, false, false, false, false, false),
tuple(otherQualityGate.getName(), false, false, false, false, false, false));
}
-
- @Test
- public void empty() {
- OrganizationDto organization = db.organizations().insert();
-
- ListWsResponse response = ws.newRequest()
- .setParam("organization", organization.getKey())
- .executeProtobuf(ListWsResponse.class);
-
- assertThat(response.getQualitygatesList()).isEmpty();
- }
}
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
-import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
+import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY;
public class SelectActionTest {
OrganizationDto organization = db.organizations().insert();
userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My Quality Gate"));
+ QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My Quality Gate 2"));
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate2);
MetricDto blockerViolationsMetric = db.measures().insertMetric(m -> m.setKey("blocker_violations"));
MetricDto criticalViolationsMetric = db.measures().insertMetric(m -> m.setKey("critical_violations"));
db.qualityGates().addCondition(qualityGate, blockerViolationsMetric, c -> c.setOperator("GT").setPeriod(null).setErrorThreshold("0").setWarningThreshold(null));
public void show() {
OrganizationDto organization = db.organizations().insert();
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
MetricDto metric = db.measures().insertMetric();
QualityGateConditionDto condition1 = db.qualityGates().addCondition(qualityGate, metric, c -> c.setOperator("GT").setPeriod(null));
QualityGateConditionDto condition2 = db.qualityGates().addCondition(qualityGate, metric, c -> c.setOperator("LT").setPeriod(1));
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
OrganizationDto otherOrganization = db.organizations().insert();
QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
+ db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
public void show_built_in() {
OrganizationDto organization = db.organizations().insert();
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
public void show_by_id() {
OrganizationDto organization = db.organizations().insert();
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("id", qualityGate.getId().toString())
public void no_condition() {
OrganizationDto organization = db.organizations().insert();
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate2);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
- db.qualityGates().setDefaultQualityGate(qualityGate);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
+ QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(false));
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate2);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
ShowWsResponse response = ws.newRequest()
.setParam("name", qualityGate.getName())
public void fail_when_condition_is_on_disabled_metric() {
OrganizationDto organization = db.organizations().insert();
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+ db.qualityGates().setDefaultQualityGate(organization, qualityGate);
MetricDto metric = db.measures().insertMetric();
db.qualityGates().addCondition(qualityGate, metric);
db.getDbClient().metricDao().disableCustomByKey(db.getSession(), metric.getKey());
private ComponentDto project;
private WsActionTester ws;
+ private OrganizationDto organization;
@Before
public void before() {
- OrganizationDto organization = dbTester.organizations().insertForKey("my-org");
+ organization = dbTester.organizations().insertForKey("my-org");
project = newPrivateProjectDto(organization, "abcd")
.setDbKey("polop")
.setName("Polop")
.setDescription("test project")
.setLanguage("xoo");
+ dbTester.qualityGates().setBuiltInAsDefaultOn(organization);
+ dbTester.qualityGates().setBuiltInAsDefaultOn(dbTester.getDefaultOrganization());
}
@Test
- public void check_definition() throws Exception {
+ public void check_definition() {
init();
WebService.Action action = ws.getDef();
}
@Test
- public void fail_on_missing_parameters() throws Exception {
+ public void fail_on_missing_parameters() {
init();
expectedException.expect(IllegalArgumentException.class);
}
@Test
- public void fail_on_unknown_component_key() throws Exception {
+ public void fail_on_unknown_component_key() {
init();
expectedException.expect(NotFoundException.class);
}
@Test
- public void throw_ForbiddenException_if_required_permission_is_not_granted() throws Exception {
+ public void throw_ForbiddenException_if_required_permission_is_not_granted() {
init();
componentDbTester.insertComponent(project);
userSession.logIn();
}
@Test
- public void return_info_if_user_has_browse_permission_on_project() throws Exception {
+ public void return_info_if_user_has_browse_permission_on_project() {
init();
componentDbTester.insertComponent(project);
userSession.logIn().addProjectPermission(UserRole.USER, project);
}
@Test
- public void return_info_if_user_has_administration_permission_on_project() throws Exception {
+ public void return_info_if_user_has_administration_permission_on_project() {
init();
componentDbTester.insertComponent(project);
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
}
@Test
- public void return_info_if_user_is_system_administrator() throws Exception {
+ public void return_info_if_user_is_system_administrator() {
init();
componentDbTester.insertComponent(project);
userSession.logIn().setSystemAdministrator();
}
@Test
- public void return_component_info_when_anonymous_no_snapshot() throws Exception {
+ public void return_component_info_when_anonymous_no_snapshot() {
init();
componentDbTester.insertComponent(project);
userSession.addProjectPermission(UserRole.USER, project);
}
@Test
- public void return_component_info_with_favourite() throws Exception {
+ public void return_component_info_with_favourite() {
init();
UserDto user = dbTester.users().insertUser("obiwan");
componentDbTester.insertComponent(project);
}
@Test
- public void return_component_info_when_snapshot() throws Exception {
+ public void return_component_info_when_snapshot() {
init();
componentDbTester.insertComponent(project);
componentDbTester.insertSnapshot(newAnalysis(project)
}
@Test
- public void return_component_info_when_file_on_master() throws Exception {
+ public void return_component_info_when_file_on_master() {
init();
OrganizationDto organization = dbTester.organizations().insertForKey("my-org2");
+ dbTester.qualityGates().setBuiltInAsDefaultOn(organization);
ComponentDto main = componentDbTester.insertMainBranch(organization, p -> p.setName("Sample"), p -> p.setDbKey("sample"));
userSession.addProjectPermission(UserRole.USER, main);
}
@Test
- public void return_component_info_when_file_on_branch() throws Exception {
+ public void return_component_info_when_file_on_branch() {
init();
OrganizationDto organization = dbTester.organizations().insertForKey("my-org2");
+ dbTester.qualityGates().setBuiltInAsDefaultOn(organization);
ComponentDto project = componentDbTester.insertMainBranch(organization, p -> p.setName("Sample"), p -> p.setDbKey("sample"));
ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey("feature1"));
userSession.addProjectPermission(UserRole.USER, project);
}
@Test
- public void return_quality_profiles() throws Exception {
+ public void return_quality_profiles() {
init();
componentDbTester.insertComponent(project);
addQualityProfiles(project,
}
@Test
- public void return_empty_quality_profiles_when_no_measure() throws Exception {
+ public void return_empty_quality_profiles_when_no_measure() {
init();
componentDbTester.insertComponent(project);
userSession.addProjectPermission(UserRole.USER, project);
}
@Test
- public void return_quality_gate_defined_on_project() throws Exception {
+ public void return_quality_gate_defined_on_project() {
init();
componentDbTester.insertComponent(project);
- QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate("Sonar way");
+ QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate(dbTester.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto);
userSession.addProjectPermission(UserRole.USER, project);
}
@Test
- public void return_default_quality_gate() throws Exception {
+ public void return_default_quality_gate() {
init();
componentDbTester.insertComponent(project);
- dbTester.qualityGates().createDefaultQualityGate("Sonar way");
+ dbTester.qualityGates().createDefaultQualityGate(organization, qg -> qg.setName("Sonar way"));
userSession.addProjectPermission(UserRole.USER, project);
executeAndVerify(project.getDbKey(), "return_default_quality_gate.json");
}
@Test
- public void return_no_quality_gate_when_not_defined_on_project_and_no_default_one() throws Exception {
- init();
- componentDbTester.insertComponent(project);
- userSession.addProjectPermission(UserRole.USER, project);
-
- String json = execute(project.getDbKey());
- assertThat(json).doesNotContain("qualityGate");
- }
-
- @Test
- public void return_extensions() throws Exception {
+ public void return_extensions() {
init(createPages());
componentDbTester.insertComponent(project);
userSession.anonymous().addProjectPermission(UserRole.USER, project);
}
@Test
- public void return_extensions_for_application() throws Exception {
+ public void return_extensions_for_application() {
Page page = Page.builder("my_plugin/app_page")
.setName("App Page")
.setScope(COMPONENT)
init(page);
ComponentDto application = componentDbTester.insertPublicApplication(dbTester.getDefaultOrganization());
+ QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate(dbTester.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
+ dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto);
userSession.registerComponents(application);
String result = ws.newRequest()
}
@Test
- public void return_extensions_for_admin() throws Exception {
+ public void return_extensions_for_admin() {
init(createPages());
componentDbTester.insertComponent(project);
userSession.anonymous()
}
@Test
- public void return_configuration_for_admin() throws Exception {
+ public void return_configuration_for_admin() {
UserDto user = dbTester.users().insertUser();
componentDbTester.insertComponent(project);
userSession.logIn(user)
}
@Test
- public void return_configuration_with_all_properties() throws Exception {
+ public void return_configuration_with_all_properties() {
init();
componentDbTester.insertComponent(project);
userSession.anonymous()
}
@Test
- public void return_breadcrumbs_on_module() throws Exception {
+ public void return_breadcrumbs_on_module() {
init();
ComponentDto project = componentDbTester.insertComponent(this.project);
ComponentDto module = componentDbTester.insertComponent(newModuleDto("bcde", project).setDbKey("palap").setName("Palap"));
}
@Test
- public void return_configuration_for_quality_profile_admin() throws Exception {
+ public void return_configuration_for_quality_profile_admin() {
init();
componentDbTester.insertComponent(project);
userSession.logIn()
}
@Test
- public void return_configuration_for_quality_gate_admin() throws Exception {
+ public void return_configuration_for_quality_gate_admin() {
init();
componentDbTester.insertComponent(project);
userSession.logIn()
}
@Test
- public void return_bread_crumbs_on_several_levels() throws Exception {
+ public void return_bread_crumbs_on_several_levels() {
init();
ComponentDto project = componentDbTester.insertComponent(this.project);
ComponentDto module = componentDbTester.insertComponent(newModuleDto("bcde", project).setDbKey("palap").setName("Palap"));
}
@Test
- public void project_administrator_is_allowed_to_get_information() throws Exception {
+ public void project_administrator_is_allowed_to_get_information() {
init(createPages());
componentDbTester.insertProjectAndSnapshot(project);
userSession.addProjectPermission(UserRole.ADMIN, project);
}
@Test
- public void test_example_response() throws Exception {
+ public void test_example_response() {
init(createPages());
OrganizationDto organizationDto = dbTester.organizations().insertForKey("my-org-1");
ComponentDto project = newPrivateProjectDto(organizationDto, "ABCD")
addQualityProfiles(project,
createQProfile("qp1", "Sonar Way Java", "java"),
createQProfile("qp2", "Sonar Way Xoo", "xoo"));
- QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate("Sonar way");
+ QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate(dbTester.getDefaultOrganization(), qg -> qg.setName("Sonar way"));
dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto);
userSession.logIn(user)
.addProjectPermission(UserRole.USER, project)
}
@Test
- public void should_return_private_flag_for_project() throws Exception {
+ public void should_return_private_flag_for_project() {
init();
OrganizationDto org = dbTester.organizations().insert();
+ dbTester.qualityGates().setBuiltInAsDefaultOn(org);
ComponentDto project = dbTester.components().insertPrivateProject(org);
userSession.logIn()
}
@Test
- public void should_return_public_flag_for_project() throws Exception {
+ public void should_return_public_flag_for_project() {
init();
OrganizationDto org = dbTester.organizations().insert();
+ dbTester.qualityGates().setBuiltInAsDefaultOn(org);
ComponentDto project = dbTester.components().insertPublicProject(org);
userSession.logIn()
}
@Test
- public void should_not_return_private_flag_for_module() throws Exception {
+ public void should_not_return_private_flag_for_module() {
init();
OrganizationDto org = dbTester.organizations().insert();
+ dbTester.qualityGates().setBuiltInAsDefaultOn(org);
ComponentDto project = dbTester.components().insertPrivateProject(org);
ComponentDto module = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
public void canApplyPermissionTemplate_is_true_if_logged_in_as_organization_administrator() {
init(createPages());
OrganizationDto org = dbTester.organizations().insert();
+ dbTester.qualityGates().setBuiltInAsDefaultOn(org);
ComponentDto project = dbTester.components().insertPrivateProject(org);
userSession.logIn()
public void canUpdateProjectVisibilityToPrivate_is_true_if_logged_in_as_project_administrator_and_extension_returns_false() {
init(createPages());
OrganizationDto org = dbTester.organizations().insert();
+ dbTester.qualityGates().setBuiltInAsDefaultOn(org);
ComponentDto project = dbTester.components().insertPublicProject(org);
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);