diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-07-02 17:44:49 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2018-07-09 10:39:32 +0200 |
commit | 0dae853007134cfe1343f8fbe6b2046e19b217df (patch) | |
tree | 3c84fb8a57a0fde0d1c22af06c64d4e3649b3ca4 | |
parent | 4182b00648ddf1157c271d431c938b53b811a76b (diff) | |
download | sonarqube-0dae853007134cfe1343f8fbe6b2046e19b217df.tar.gz sonarqube-0dae853007134cfe1343f8fbe6b2046e19b217df.zip |
move shared Quality Gate classes to server-common
25 files changed, 63 insertions, 83 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 215a3e5544f..7c7445a0dc1 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -139,7 +139,8 @@ import org.sonar.server.platform.serverid.ServerIdModule; import org.sonar.server.plugins.InstalledPluginReferentialFactory; import org.sonar.server.plugins.ServerExtensionInstaller; import org.sonar.server.property.InternalPropertiesImpl; -import org.sonar.server.qualitygate.QualityGateModule; +import org.sonar.server.qualitygate.QualityGateEvaluatorImpl; +import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.rule.DefaultRuleFinder; import org.sonar.server.rule.ExternalRuleCreator; @@ -458,7 +459,8 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { // webhooks WebhookModule.class, - QualityGateModule.class, + QualityGateFinder.class, + QualityGateEvaluatorImpl.class, // cleaning CeCleaningModule.class); diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 56517912918..5802e313002 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -97,8 +97,7 @@ public class ComputeEngineContainerImplTest { assertThat(picoContainer.getComponentAdapters()) .hasSize( CONTAINER_ITSELF - + 70 // level 4 - + 21 // content of QualityGateModule + + 71 // level 4 + 6 // content of CeConfigurationModule + 4 // content of CeQueueModule + 4 // content of CeHttpModule diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java index f154d3414fd..f154d3414fd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluator.java b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluator.java index 3ecf03d08dd..3ecf03d08dd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluator.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluator.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluatorImpl.java b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluatorImpl.java index f0ce9449320..f0ce9449320 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluatorImpl.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateEvaluatorImpl.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java index 89b7e64783e..e0ba6bc326a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java @@ -28,7 +28,7 @@ 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.ws.WsUtils.checkFound; +import static java.util.Optional.ofNullable; public class QualityGateFinder { @@ -45,20 +45,15 @@ public class QualityGateFinder { * * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate of the organization */ - public QualityGateData getQualityGate(DbSession dbSession, OrganizationDto organization, ComponentDto component) { - Optional<Long> qualityGateId = dbClient.projectQgateAssociationDao().selectQGateIdByComponentId(dbSession, component.getId()); - if (qualityGateId.isPresent()) { - QualityGateDto qualityGate = checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId.get()), "No quality gate has been found for id %s", qualityGateId); - return new QualityGateData(qualityGate, false); + public Optional<QualityGateData> getQualityGate(DbSession dbSession, OrganizationDto organization, ComponentDto component) { + Optional<QualityGateData> res = dbClient.projectQgateAssociationDao().selectQGateIdByComponentId(dbSession, component.getId()) + .map(qualityGateId -> dbClient.qualityGateDao().selectById(dbSession, qualityGateId)) + .map(qualityGateDto -> new QualityGateData(qualityGateDto, false)); + if (res.isPresent()) { + return res; } - 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); - } - - public QGateWithOrgDto getByOrganizationAndId(DbSession dbSession, OrganizationDto organization, long qualityGateId) { - return checkFound(dbClient.qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGateId), - "No quality gate has been found for id %s in organization %s", qualityGateId, organization.getName()); + return ofNullable(dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, organization.getDefaultQualityGateUuid())) + .map(qualityGateDto -> new QualityGateData(qualityGateDto, true)); } public QualityGateDto getDefault(DbSession dbSession, OrganizationDto organization) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ConditionEvaluatorTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/qualitygate/ConditionEvaluatorTest.java index 8f73faf337c..8f73faf337c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ConditionEvaluatorTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/qualitygate/ConditionEvaluatorTest.java diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java index fc05daf97d4..8d5905aedc0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.qualitygate; +import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -27,7 +28,6 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; 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; @@ -49,11 +49,11 @@ public class QualityGateFinderTest { ComponentDto project = db.components().insertPrivateProject(); QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way")); - QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); + Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); - assertThat(result).isNotNull(); - assertThat(result.getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); - assertThat(result.isDefault()).isTrue(); + assertThat(result).isPresent(); + assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); + assertThat(result.get().isDefault()).isTrue(); } @Test @@ -63,11 +63,11 @@ public class QualityGateFinderTest { QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); db.qualityGates().associateProjectToQualityGate(project, dbQualityGate); - QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); + Optional<QualityGateFinder.QualityGateData> result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); - assertThat(result).isNotNull(); - assertThat(result.getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); - assertThat(result.isDefault()).isFalse(); + assertThat(result).isPresent(); + assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); + assertThat(result.get().isDefault()).isFalse(); } @Test @@ -77,10 +77,7 @@ public class QualityGateFinderTest { db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); db.commit(); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage(format("Unable to find the quality gate [%s] for organization [%s]", dbQualityGate.getUuid(), db.getDefaultOrganization().getUuid())); - - underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); + assertThat(underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project)).isEmpty(); } @Test @@ -90,8 +87,7 @@ public class QualityGateFinderTest { db.qualityGates().associateProjectToQualityGate(project, dbQualityGate); db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); - expectedException.expect(NotFoundException.class); - underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project); + assertThat(underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project)).isEmpty(); } @Test diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/live/LiveQualityGateComputerImpl.java b/server/sonar-server/src/main/java/org/sonar/server/measure/live/LiveQualityGateComputerImpl.java index 431821f0095..b38fd716b5c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/live/LiveQualityGateComputerImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/live/LiveQualityGateComputerImpl.java @@ -46,6 +46,7 @@ import org.sonar.server.qualitygate.QualityGateEvaluator; import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualitygate.ShortLivingBranchQualityGate; +import static java.lang.String.format; import static org.sonar.core.util.stream.MoreCollectors.toHashSet; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; @@ -68,7 +69,9 @@ public class LiveQualityGateComputerImpl implements LiveQualityGateComputer { } ComponentDto mainProject = project.getMainBranchProjectUuid() == null ? project : dbClient.componentDao().selectOrFailByKey(dbSession, project.getKey()); - QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, organization, mainProject).getQualityGate(); + QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, organization, mainProject) + .orElseThrow(() -> new IllegalStateException(format("Quality Gate not found for project %s", mainProject.getKey()))) + .getQualityGate(); Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getId()); Set<Integer> metricIds = conditionDtos.stream().map(c -> (int) c.getMetricId()) .collect(toHashSet(conditionDtos.size())); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java index 070c7651ae0..9b21a9ae6bd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java @@ -26,7 +26,6 @@ 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; @@ -43,15 +42,13 @@ public class CopyAction implements QualityGatesWsAction { private final DbClient dbClient; private final UserSession userSession; private final QualityGateUpdater qualityGateUpdater; - private final QualityGateFinder qualityGateFinder; private final QualityGatesWsSupport wsSupport; public CopyAction(DbClient dbClient, UserSession userSession, QualityGateUpdater qualityGateUpdater, - QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport) { + QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; this.userSession = userSession; this.qualityGateUpdater = qualityGateUpdater; - this.qualityGateFinder = qualityGateFinder; this.wsSupport = wsSupport; } @@ -88,7 +85,7 @@ public class CopyAction implements QualityGatesWsAction { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); userSession.checkPermission(ADMINISTER_QUALITY_GATES, organization); - QualityGateDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); + QualityGateDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, id); QualityGateDto copy = qualityGateUpdater.copy(dbSession, organization, qualityGate, destinationName); dbSession.commit(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateConditionAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateConditionAction.java index 0b40b6c776e..86fb85dc47f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateConditionAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateConditionAction.java @@ -28,7 +28,6 @@ import org.sonar.db.organization.OrganizationDto; import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.server.qualitygate.QualityGateConditionsUpdater; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonarqube.ws.Qualitygates.CreateConditionResponse; import static com.google.common.base.Strings.emptyToNull; @@ -47,13 +46,11 @@ public class CreateConditionAction implements QualityGatesWsAction { private final DbClient dbClient; private final QualityGateConditionsUpdater qualityGateConditionsUpdater; - private final QualityGateFinder qualityGateFinder; private final QualityGatesWsSupport wsSupport; - public CreateConditionAction(DbClient dbClient, QualityGateConditionsUpdater qualityGateConditionsUpdater, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport) { + public CreateConditionAction(DbClient dbClient, QualityGateConditionsUpdater qualityGateConditionsUpdater, QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; this.qualityGateConditionsUpdater = qualityGateConditionsUpdater; - this.qualityGateFinder = qualityGateFinder; this.wsSupport = wsSupport; } @@ -88,7 +85,7 @@ public class CreateConditionAction implements QualityGatesWsAction { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); - QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, gateId); + QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, gateId); wsSupport.checkCanEdit(qualityGate); QualityGateConditionDto condition = qualityGateConditionsUpdater.createCondition(dbSession, qualityGate, metric, operator, emptyToNull(warning), emptyToNull(error), period); CreateConditionResponse.Builder createConditionResponse = CreateConditionResponse.newBuilder() diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java index 5b71c51c0d4..60e6d5eeebf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java @@ -66,7 +66,7 @@ public class DestroyAction implements QualityGatesWsAction { long qualityGateId = request.mandatoryParamAsLong(QualityGatesWsParameters.PARAM_ID); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); - QGateWithOrgDto qualityGate = finder.getByOrganizationAndId(dbSession, organization, qualityGateId); + QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, qualityGateId); QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization); checkArgument(!defaultQualityGate.getId().equals(qualityGate.getId()), "The default quality gate cannot be removed"); wsSupport.checkCanEdit(qualityGate); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java index 5b78ed31c9f..afc5c057c95 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java @@ -29,11 +29,13 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.component.ComponentFinder; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualitygate.QualityGateFinder.QualityGateData; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Qualitygates.GetByProjectResponse; +import static java.lang.String.format; import static org.sonar.api.web.UserRole.ADMIN; import static org.sonar.api.web.UserRole.USER; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_GET_BY_PROJECT; @@ -75,8 +77,7 @@ public class GetByProjectAction implements QualityGatesWsAction { .setChangelog( new Change("6.6", "The parameter 'projectId' has been removed"), new Change("6.6", "The parameter 'projectKey' has been renamed to 'project'"), - new Change("6.6", "This webservice is now part of the public API") - ); + new Change("6.6", "This webservice is now part of the public API")); action.createParam(PARAM_PROJECT) .setDescription("Project key") @@ -99,7 +100,8 @@ public class GetByProjectAction implements QualityGatesWsAction { throw insufficientPrivilegesException(); } - QualityGateData data = qualityGateFinder.getQualityGate(dbSession, organization, project); + QualityGateData data = qualityGateFinder.getQualityGate(dbSession, organization, project) + .orElseThrow(() -> new NotFoundException(format("Quality gate not found for project %s", project.getKey()))); writeProtobuf(buildResponse(data), request, response); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java index 2279e4263d6..58685554fbc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java @@ -57,6 +57,12 @@ public class QualityGatesWsSupport { this.defaultOrganizationProvider = defaultOrganizationProvider; } + public QGateWithOrgDto getByOrganizationAndId(DbSession dbSession, OrganizationDto organization, long qualityGateId) { + return checkFound( + dbClient.qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGateId), + "No quality gate has been found for id %s in organization %s", qualityGateId, organization.getName()); + } + QualityGateConditionDto getCondition(DbSession dbSession, long id) { return checkFound(dbClient.gateConditionDao().selectById(id, dbSession), "No quality gate condition with id '%d'", id); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java index eac5f039c9b..d94ab3e4dee 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java @@ -27,7 +27,6 @@ import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateDto; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonarqube.ws.Qualitygates.QualityGate; import static com.google.common.base.Preconditions.checkArgument; @@ -41,12 +40,10 @@ import static org.sonar.server.ws.WsUtils.writeProtobuf; public class RenameAction implements QualityGatesWsAction { private final DbClient dbClient; - private final QualityGateFinder qualityGateFinder; private final QualityGatesWsSupport wsSupport; - public RenameAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport) { + public RenameAction(DbClient dbClient, QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; - this.qualityGateFinder = qualityGateFinder; this.wsSupport = wsSupport; } @@ -87,7 +84,7 @@ public class RenameAction implements QualityGatesWsAction { } private QualityGateDto rename(DbSession dbSession, OrganizationDto organization, long id, String name) { - QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); + QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, id); wsSupport.checkCanEdit(qualityGate); checkArgument(!isNullOrEmpty(name), CANT_BE_EMPTY_MESSAGE, "Name"); checkNotAlreadyExists(dbSession, organization, qualityGate, name); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SearchAction.java index 1d1e66273e9..5f33779566d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SearchAction.java @@ -35,7 +35,6 @@ import org.sonar.db.qualitygate.ProjectQgateAssociation; import org.sonar.db.qualitygate.ProjectQgateAssociationDto; import org.sonar.db.qualitygate.ProjectQgateAssociationQuery; import org.sonar.db.qualitygate.QGateWithOrgDto; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Qualitygates; @@ -52,13 +51,11 @@ public class SearchAction implements QualityGatesWsAction { private final DbClient dbClient; private final UserSession userSession; - private QualityGateFinder qualityGateFinder; private final QualityGatesWsSupport wsSupport; - public SearchAction(DbClient dbClient, UserSession userSession, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport) { + public SearchAction(DbClient dbClient, UserSession userSession, QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; this.userSession = userSession; - this.qualityGateFinder = qualityGateFinder; this.wsSupport = wsSupport; } @@ -99,7 +96,7 @@ public class SearchAction implements QualityGatesWsAction { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); - QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, request.mandatoryParamAsLong(PARAM_GATE_ID)); + QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, request.mandatoryParamAsLong(PARAM_GATE_ID)); Association associations = find(dbSession, ProjectQgateAssociationQuery.builder() .qualityGate(qualityGate) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java index 0f02ccd8cbc..94606de76d6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java @@ -33,7 +33,6 @@ import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.component.ComponentFinder.ParamNames; -import org.sonar.server.qualitygate.QualityGateFinder; import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_SELECT; @@ -46,13 +45,11 @@ public class SelectAction implements QualityGatesWsAction { private final DbClient dbClient; private final ComponentFinder componentFinder; private final QualityGatesWsSupport wsSupport; - private final QualityGateFinder qualityGateFinder; - public SelectAction(DbClient dbClient, ComponentFinder componentFinder, QualityGatesWsSupport wsSupport, QualityGateFinder qualityGateFinder) { + public SelectAction(DbClient dbClient, ComponentFinder componentFinder, QualityGatesWsSupport wsSupport) { this.dbClient = dbClient; this.componentFinder = componentFinder; this.wsSupport = wsSupport; - this.qualityGateFinder = qualityGateFinder; } @Override @@ -93,7 +90,7 @@ public class SelectAction implements QualityGatesWsAction { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); - QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, gateId); + QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, gateId); ComponentDto project = getProject(dbSession, organization, projectId, projectKey); wsSupport.checkCanAdminProject(organization, project); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java index ef624a989df..771cfa0436e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java @@ -26,7 +26,6 @@ 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.user.UserSession; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; @@ -37,14 +36,11 @@ public class SetAsDefaultAction implements QualityGatesWsAction { private final DbClient dbClient; private final UserSession userSession; - private final QualityGateFinder qualityGateFinder; private final QualityGatesWsSupport wsSupport; - public SetAsDefaultAction(DbClient dbClient, UserSession userSession, - QualityGateFinder qualityGateFinder, QualityGatesWsSupport qualityGatesWsSupport) { + public SetAsDefaultAction(DbClient dbClient, UserSession userSession, QualityGatesWsSupport qualityGatesWsSupport) { this.dbClient = dbClient; this.userSession = userSession; - this.qualityGateFinder = qualityGateFinder; this.wsSupport = qualityGatesWsSupport; } @@ -72,7 +68,7 @@ public class SetAsDefaultAction implements QualityGatesWsAction { 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); + QualityGateDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, id); organization.setDefaultQualityGateUuid(qualityGate.getUuid()); dbClient.organizationDao().update(dbSession, organization); dbSession.commit(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java index 4c2e3d988eb..4df89144a66 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java @@ -104,7 +104,7 @@ public class ShowAction implements QualityGatesWsAction { return checkFound(dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, name), "No quality gate has been found for name %s", name); } if (id != null) { - return qualityGateFinder.getByOrganizationAndId(dbSession, organization, id); + return wsSupport.getByOrganizationAndId(dbSession, organization, id); } throw new IllegalArgumentException("No parameter has been set to identify a quality gate"); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index 76c55684e0b..c8964af2459 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -50,6 +50,7 @@ import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.component.ComponentFinder; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.BillingValidations; import org.sonar.server.organization.BillingValidationsProxy; import org.sonar.server.project.Visibility; @@ -59,6 +60,7 @@ import org.sonar.server.qualityprofile.QualityProfile; import org.sonar.server.ui.PageRepository; import org.sonar.server.user.UserSession; +import static java.lang.String.format; import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY; import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.api.web.UserRole.ADMIN; @@ -221,7 +223,8 @@ public class ComponentAction implements NavigationWsAction { } private void writeQualityGate(JsonWriter json, DbSession session, OrganizationDto organization, ComponentDto component) { - QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, organization, component); + QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, organization, component) + .orElseThrow(() -> new NotFoundException(format("Quality Gate not found for %s", component.getKey()))); QualityGateDto qualityGateDto = qualityGateData.getQualityGate(); json.name("qualityGate").beginObject() .prop("key", qualityGateDto.getId()) diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java index cdf8ca1bdb2..15e3f0aaedd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java @@ -36,7 +36,6 @@ import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; @@ -66,10 +65,9 @@ public class CopyActionTest { private DbSession dbSession = db.getSession(); private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()); - private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient); private QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider); - private CopyAction underTest = new CopyAction(dbClient, userSession, qualityGateUpdater, qualityGateFinder, wsSupport); + private CopyAction underTest = new CopyAction(dbClient, userSession, qualityGateUpdater, wsSupport); private WsActionTester ws = new WsActionTester(underTest); @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java index 01f3bc60e20..f84312e1fb3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateConditionActionTest.java @@ -36,7 +36,6 @@ import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.qualitygate.QualityGateConditionsUpdater; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Qualitygates.CreateConditionResponse; @@ -69,7 +68,7 @@ public class CreateConditionActionTest { private DbClient dbClient = db.getDbClient(); private DbSession dbSession = db.getSession(); private CreateConditionAction underTest = new CreateConditionAction(dbClient, new QualityGateConditionsUpdater(dbClient), - new QualityGateFinder(dbClient), new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider)); + new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider)); private WsActionTester ws = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java index 04126263575..e90ff463653 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java @@ -31,7 +31,6 @@ import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Qualitygates.QualityGate; @@ -54,7 +53,7 @@ public class RenameActionTest { private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private WsActionTester ws = new WsActionTester( - new RenameAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); + new RenameAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); @Test public void verify_definition() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java index 72983d3761f..ab581808b24 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SearchActionTest.java @@ -31,7 +31,6 @@ import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Qualitygates.SearchResponse; @@ -66,7 +65,7 @@ public class SearchActionTest { private DbClient dbClient = db.getDbClient(); private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private SearchAction underTest = new SearchAction(dbClient, userSession, new QualityGateFinder(dbClient), + private SearchAction underTest = new SearchAction(dbClient, userSession, new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider)); private WsActionTester ws = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java index 0b27f7c606f..063a57487be 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java @@ -33,7 +33,6 @@ import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; @@ -55,8 +54,7 @@ public class SelectActionTest { private DbClient dbClient = db.getDbClient(); private SelectAction underTest = new SelectAction(dbClient, TestComponentFinder.from(db), - new QualityGatesWsSupport(db.getDbClient(), userSession, TestDefaultOrganizationProvider.from(db)), - new QualityGateFinder(db.getDbClient())); + new QualityGatesWsSupport(db.getDbClient(), userSession, TestDefaultOrganizationProvider.from(db))); private WsActionTester ws = new WsActionTester(underTest); @Test |