diff options
author | Jacek <jacek.poreda@sonarsource.com> | 2020-12-15 09:05:56 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-12-22 20:09:37 +0000 |
commit | d32d574c7ef425dd95ac15414347f9f546fc8263 (patch) | |
tree | ba8cb380ce08b972ecde8539c67c99be3024d9b7 /server/sonar-ce-task-projectanalysis/src | |
parent | 019d8c66ada8a01c96733bed0c771a8b88cfe3cd (diff) | |
download | sonarqube-d32d574c7ef425dd95ac15414347f9f546fc8263.tar.gz sonarqube-d32d574c7ef425dd95ac15414347f9f546fc8263.zip |
SONAR-13999 drop organization from QualityGate WS
Diffstat (limited to 'server/sonar-ce-task-projectanalysis/src')
5 files changed, 48 insertions, 36 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateService.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateService.java index aa02034c48c..7cee09fb0c7 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateService.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateService.java @@ -20,7 +20,6 @@ package org.sonar.ce.task.projectanalysis.qualitygate; import java.util.Optional; -import org.sonar.ce.task.projectanalysis.analysis.Organization; import org.sonar.server.project.Project; public interface QualityGateService { @@ -31,10 +30,10 @@ public interface QualityGateService { Optional<QualityGate> findByUuid(String uuid); /** - * Retrieve the {@link QualityGate} from the database using organization. + * Retrieve the {@link QualityGate} from the database. * @throws IllegalStateException if database is corrupted and default gate can't be found. */ - QualityGate findDefaultQualityGate(Organization organizationDto); + QualityGate findDefaultQualityGate(); /** * Retrieve the {@link QualityGate} from the database associated with project. diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java index b86fc56d1e5..3774b3e2b68 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImpl.java @@ -22,10 +22,10 @@ package org.sonar.ce.task.projectanalysis.qualitygate; import java.util.Collection; import java.util.Objects; import java.util.Optional; -import org.sonar.ce.task.projectanalysis.analysis.Organization; import org.sonar.ce.task.projectanalysis.metric.MetricRepository; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.project.Project; @@ -33,6 +33,7 @@ import org.sonar.server.project.Project; import static org.sonar.core.util.stream.MoreCollectors.toList; public class QualityGateServiceImpl implements QualityGateService { + private static final String DEFAULT_QUALITY_GATE_PROPERTY_NAME = "qualitygate.default"; private final DbClient dbClient; private final MetricRepository metricRepository; @@ -54,16 +55,24 @@ public class QualityGateServiceImpl implements QualityGateService { } @Override - public QualityGate findDefaultQualityGate(Organization organization) { + public QualityGate findDefaultQualityGate() { try (DbSession dbSession = dbClient.openSession(false)) { - QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization.toDto(), organization.getDefaultQualityGateUuid()); + QualityGateDto qualityGateDto = getDefaultQualityGate(dbSession); if (qualityGateDto == null) { - throw new IllegalStateException("The default Quality gate is missing on organization " + organization.getKey()); + throw new IllegalStateException("The default Quality gate is missing"); } return toQualityGate(dbSession, qualityGateDto); } } + private QualityGateDto getDefaultQualityGate(DbSession dbSession) { + PropertyDto propertyDto = Optional.ofNullable(dbClient.propertiesDao() + .selectGlobalProperty(dbSession, DEFAULT_QUALITY_GATE_PROPERTY_NAME)) + .orElseThrow(() -> new IllegalStateException("The default Quality Gate property is missing")); + return Optional.ofNullable(dbClient.qualityGateDao().selectByUuid(dbSession, propertyDto.getValue())) + .orElseThrow(() -> new IllegalStateException("The default Quality gate is missing")); + } + @Override public Optional<QualityGate> findQualityGate(Project project) { try (DbSession dbSession = dbClient.openSession(false)) { diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStep.java index fca307772a6..0f2616f5128 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStep.java @@ -50,8 +50,8 @@ public class LoadQualityGateStep implements ComputationStep { public void execute(ComputationStep.Context context) { Optional<QualityGate> qualityGate = getProjectQualityGate(); if (!qualityGate.isPresent()) { - // No QG defined for the project, let's retrieve the QG on the organization - qualityGate = Optional.of(getOrganizationDefaultQualityGate()); + // No QG defined for the project, let's retrieve the default QG + qualityGate = Optional.of(getDefaultQualityGate()); } if (analysisMetadataHolder.isPullRequest()) { @@ -71,8 +71,8 @@ public class LoadQualityGateStep implements ComputationStep { return qualityGateService.findQualityGate(project); } - private QualityGate getOrganizationDefaultQualityGate() { - return qualityGateService.findDefaultQualityGate(analysisMetadataHolder.getOrganization()); + private QualityGate getDefaultQualityGate() { + return qualityGateService.findDefaultQualityGate(); } @Override diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java index 50dbc1fc221..2a32693650b 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java @@ -24,11 +24,11 @@ import java.util.Collections; import java.util.Optional; import org.junit.Before; import org.junit.Test; -import org.sonar.ce.task.projectanalysis.analysis.Organization; import org.sonar.ce.task.projectanalysis.metric.Metric; import org.sonar.ce.task.projectanalysis.metric.MetricRepository; import org.sonar.db.DbClient; -import org.sonar.db.qualitygate.QGateWithOrgDto; +import org.sonar.db.property.PropertiesDao; +import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDao; @@ -54,16 +54,18 @@ public class QualityGateServiceImplTest { private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setUuid("456").setMetricUuid(METRIC_UUID_2).setOperator("GT") .setErrorThreshold("error_th"); - private QualityGateDao qualityGateDao = mock(QualityGateDao.class); - private QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class); - private MetricRepository metricRepository = mock(MetricRepository.class); - private DbClient dbClient = mock(DbClient.class); - private QualityGateServiceImpl underTest = new QualityGateServiceImpl(dbClient, metricRepository); + private final QualityGateDao qualityGateDao = mock(QualityGateDao.class); + private final QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class); + private final PropertiesDao propertiesDao = mock(PropertiesDao.class); + private final MetricRepository metricRepository = mock(MetricRepository.class); + private final DbClient dbClient = mock(DbClient.class); + private final QualityGateServiceImpl underTest = new QualityGateServiceImpl(dbClient, metricRepository); @Before public void setUp() { when(dbClient.qualityGateDao()).thenReturn(qualityGateDao); when(dbClient.gateConditionDao()).thenReturn(qualityGateConditionDao); + when(dbClient.propertiesDao()).thenReturn(propertiesDao); when(METRIC_1.getKey()).thenReturn("metric"); when(METRIC_2.getKey()).thenReturn("new_metric"); @@ -123,23 +125,25 @@ public class QualityGateServiceImplTest { } @Test(expected = IllegalStateException.class) - public void findDefaultQualityGate_by_organization_not_found() { - when(qualityGateDao.selectByOrganizationAndUuid(any(), any(), any())).thenReturn(null); + public void findDefaultQualityGate_by_property_not_found() { + when(propertiesDao.selectGlobalProperty(any(), any())).thenReturn(null); - underTest.findDefaultQualityGate(mock(Organization.class)); + underTest.findDefaultQualityGate(); } @Test - public void findDefaultQualityGate_by_organization_found() { - QGateWithOrgDto qGateWithOrgDto = new QGateWithOrgDto(); - qGateWithOrgDto.setUuid(QUALITY_GATE_DTO.getUuid()); - qGateWithOrgDto.setName(QUALITY_GATE_DTO.getName()); - when(qualityGateDao.selectByOrganizationAndUuid(any(), any(), any())).thenReturn(qGateWithOrgDto); + public void findDefaultQualityGate_by_property_found() { + QualityGateDto qualityGateDto = new QualityGateDto(); + qualityGateDto.setUuid(QUALITY_GATE_DTO.getUuid()); + qualityGateDto.setName(QUALITY_GATE_DTO.getName()); + when(propertiesDao.selectGlobalProperty(any(), any())).thenReturn(new PropertyDto().setValue(QUALITY_GATE_DTO.getUuid())); + + when(qualityGateDao.selectByUuid(any(), any())).thenReturn(qualityGateDto); when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_UUID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2)); when(metricRepository.getOptionalByUuid(METRIC_UUID_1)).thenReturn(Optional.empty()); when(metricRepository.getOptionalByUuid(METRIC_UUID_2)).thenReturn(Optional.of(METRIC_2)); - QualityGate result = underTest.findDefaultQualityGate(mock(Organization.class)); + QualityGate result = underTest.findDefaultQualityGate(); assertThat(result).isNotNull(); assertThat(result.getUuid()).isEqualTo(QUALITY_GATE_DTO.getUuid()); @@ -156,10 +160,10 @@ public class QualityGateServiceImplTest { @Test public void findQualityGate_by_project_found() { - QGateWithOrgDto qGateWithOrgDto = new QGateWithOrgDto(); - qGateWithOrgDto.setUuid(QUALITY_GATE_DTO.getUuid()); - qGateWithOrgDto.setName(QUALITY_GATE_DTO.getName()); - when(qualityGateDao.selectByProjectUuid(any(), any())).thenReturn(qGateWithOrgDto); + QualityGateDto qualityGateDto = new QualityGateDto(); + qualityGateDto.setUuid(QUALITY_GATE_DTO.getUuid()); + qualityGateDto.setName(QUALITY_GATE_DTO.getName()); + when(qualityGateDao.selectByProjectUuid(any(), any())).thenReturn(qualityGateDto); when(qualityGateConditionDao.selectForQualityGate(any(), eq(SOME_UUID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2)); when(metricRepository.getOptionalByUuid(METRIC_UUID_1)).thenReturn(Optional.empty()); when(metricRepository.getOptionalByUuid(METRIC_UUID_2)).thenReturn(Optional.of(METRIC_2)); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStepTest.java index ea942fb5a12..2b399921f9d 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadQualityGateStepTest.java @@ -49,10 +49,10 @@ public class LoadQualityGateStepTest { @Rule public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule(); - private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class); - private QualityGateService qualityGateService = mock(QualityGateService.class); + private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class); + private final QualityGateService qualityGateService = mock(QualityGateService.class); - private LoadQualityGateStep underTest = new LoadQualityGateStep(qualityGateService, mutableQualityGateHolder, analysisMetadataHolder); + private final LoadQualityGateStep underTest = new LoadQualityGateStep(qualityGateService, mutableQualityGateHolder, analysisMetadataHolder); @Before public void setUp() { @@ -68,7 +68,7 @@ public class LoadQualityGateStepTest { when(analysisMetadataHolder.isPullRequest()).thenReturn(true); QualityGate defaultGate = new QualityGate("1", "qg", Arrays.asList(variation, condition)); - when(qualityGateService.findDefaultQualityGate(any(Organization.class))).thenReturn(defaultGate); + when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate); underTest.execute(new TestComputationStepContext()); @@ -78,7 +78,7 @@ public class LoadQualityGateStepTest { @Test public void execute_sets_default_QualityGate_when_project_has_no_settings() { QualityGate defaultGate = mock(QualityGate.class); - when(qualityGateService.findDefaultQualityGate(any(Organization.class))).thenReturn(defaultGate); + when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate); underTest.execute(new TestComputationStepContext()); |