private final String uuid;
private final String key;
private final String name;
+ private final String defaultQualityGateUuid;
- private Organization(String uuid, String key, String name) {
+ private Organization(String uuid, String key, String name, String defaultQualityGateUuid) {
this.uuid = requireNonNull(uuid, "uuid can't be null");
this.key = requireNonNull(key, "key can't be null");
this.name = requireNonNull(name, "name can't be null");
+ this.defaultQualityGateUuid = requireNonNull(defaultQualityGateUuid, "defaultQualityGateUuid can't be null");
}
public String getUuid() {
return name;
}
+ public String getDefaultQualityGateUuid() {
+ return defaultQualityGateUuid;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
"uuid='" + uuid + '\'' +
", key='" + key + '\'' +
", name='" + name + '\'' +
+ ", defaultQualityGateUuid='" + defaultQualityGateUuid + '\'' +
'}';
}
+ public OrganizationDto toDto() {
+ return new OrganizationDto()
+ .setName(name)
+ .setKey(key)
+ .setUuid(uuid)
+ .setDefaultQualityGateUuid(defaultQualityGateUuid);
+ }
+
public static Organization from(OrganizationDto organizationDto) {
- return new Organization(organizationDto.getUuid(), organizationDto.getKey(), organizationDto.getName());
+ return new Organization(organizationDto.getUuid(), organizationDto.getKey(), organizationDto.getName(), organizationDto.getDefaultQualityGateUuid());
}
}
*/
package org.sonar.server.computation.task.projectanalysis.qualitygate;
-import com.google.common.base.Optional;
+import java.util.Optional;
+import org.sonar.server.computation.task.projectanalysis.analysis.Organization;
public interface QualityGateService {
/**
- * Retrieve the {@link QualityGate} from the database with the specified id, it it exists.
+ * Retrieve the {@link QualityGate} from the database with the specified id, if it exists.
*/
Optional<QualityGate> findById(long id);
+
+ /**
+ * Retrieve the {@link QualityGate} from the database with the specified uuid, if it exists.
+ */
+ Optional<QualityGate> findDefaultQualityGate(Organization organizationDto);
+
}
*/
package org.sonar.server.computation.task.projectanalysis.qualitygate;
-import com.google.common.base.Optional;
import java.util.Collection;
import java.util.Objects;
+import java.util.Optional;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.computation.task.projectanalysis.analysis.Organization;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
try (DbSession dbSession = dbClient.openSession(false)) {
QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectById(dbSession, id);
if (qualityGateDto == null) {
- return Optional.absent();
+ return Optional.empty();
+ }
+ return Optional.of(toQualityGate(dbSession, qualityGateDto));
+ }
+ }
+
+ @Override
+ public Optional<QualityGate> findDefaultQualityGate(Organization organization) {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization.toDto(), organization.getDefaultQualityGateUuid());
+ if (qualityGateDto == null) {
+ return Optional.empty();
}
return Optional.of(toQualityGate(dbSession, qualityGateDto));
}
*/
package org.sonar.server.computation.task.projectanalysis.step;
-import com.google.common.base.Optional;
+import java.util.Optional;
import org.sonar.api.config.Configuration;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolder;
* {@link MutableQualityGateHolder}.
*/
public class LoadQualityGateStep implements ComputationStep {
- private static final Logger LOGGER = Loggers.get(LoadQualityGateStep.class);
-
- private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
+ private static final String PROPERTY_PROJECT_QUALITY_GATE = "sonar.qualitygate";
private final ConfigurationRepository configRepository;
private final QualityGateService qualityGateService;
@Override
public void execute() {
+ Optional<QualityGate> qualityGate = getShortLivingBranchQualityGate();
+ if (!qualityGate.isPresent()) {
+ // Not on a short living branch, let's retrieve the QG of the project
+ qualityGate = getProjectQualityGate();
+ if (!qualityGate.isPresent()) {
+ // No QG defined for the project, let's retrieve the QG on the organization
+ qualityGate = getOrganizationDefaultQualityGate();
+ }
+ }
+
+ if (qualityGate.isPresent()) {
+ qualityGateHolder.setQualityGate(qualityGate.get());
+ } else {
+ qualityGateHolder.setNoQualityGate();
+ }
+ }
+
+ private Optional<QualityGate> getShortLivingBranchQualityGate() {
if (analysisMetadataHolder.isShortLivingBranch()) {
Optional<QualityGate> qualityGate = qualityGateService.findById(ShortLivingBranchQualityGate.ID);
if (qualityGate.isPresent()) {
- qualityGateHolder.setQualityGate(qualityGate.get());
+ return qualityGate;
} else {
throw new IllegalStateException("Failed to retrieve hardcoded short living branch Quality Gate");
}
- return;
+ } else {
+ return Optional.empty();
}
+ }
+
+ private Optional<QualityGate> getProjectQualityGate() {
Configuration config = configRepository.getConfiguration();
- String qualityGateSetting = config.get(PROPERTY_QUALITY_GATE).orElse(null);
+ String qualityGateSetting = config.get(PROPERTY_PROJECT_QUALITY_GATE).orElse(null);
if (isBlank(qualityGateSetting)) {
- LOGGER.debug("No quality gate is configured");
- qualityGateHolder.setNoQualityGate();
- return;
+ return Optional.empty();
}
try {
long qualityGateId = Long.parseLong(qualityGateSetting);
- Optional<QualityGate> qualityGate = qualityGateService.findById(qualityGateId);
- if (qualityGate.isPresent()) {
- qualityGateHolder.setQualityGate(qualityGate.get());
- } else {
- qualityGateHolder.setNoQualityGate();
- }
+ return qualityGateService.findById(qualityGateId);
} catch (NumberFormatException e) {
throw new IllegalStateException(
- String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_QUALITY_GATE),
- e);
+ String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_PROJECT_QUALITY_GATE), e);
}
}
+ private Optional<QualityGate> getOrganizationDefaultQualityGate() {
+ return qualityGateService.findDefaultQualityGate(analysisMetadataHolder.getOrganization());
+ }
+
@Override
public String getDescription() {
return "Load Quality gate";
@Test
public void setOrganization_throws_ISE_if_called_twice() {
- Organization organization = Organization.from(new OrganizationDto().setUuid("uuid").setKey("key").setName("name"));
+ Organization organization = Organization.from(new OrganizationDto().setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("anyuuidr"));
underTest.setOrganization(organization);
expectedException.expect(IllegalStateException.class);
return this;
}
- public AnalysisMetadataHolderRule setOrganizationUuid(String uuid) {
+ public AnalysisMetadataHolderRule setOrganizationUuid(String uuid, String defaultQualityGateUuid) {
requireNonNull(uuid, "organization uuid can't be null");
- this.organization.setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid)));
+ this.organization.setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid).setDefaultQualityGateUuid(defaultQualityGateUuid)));
return this;
}
Organization.from(underTest);
}
+ @Test
+ public void build_throws_NPE_if_defaultQualityGateUuid_is_null() {
+ underTest.setUuid("uuid").setKey("key").setName("name");
+
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("defaultQualityGateUuid can't be null");
+
+ Organization.from(underTest);
+ }
+
@Test
public void build_throws_NPE_if_name_is_null() {
underTest.setUuid("uuid").setKey("key");
@Test
public void verify_getters() {
- Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name"));
+ Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
assertThat(organization.getUuid()).isEqualTo("uuid");
assertThat(organization.getKey()).isEqualTo("key");
assertThat(organization.getName()).isEqualTo("name");
+ assertThat(organization.getDefaultQualityGateUuid()).isEqualTo("uuid1");
}
@Test
public void verify_toString() {
- Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name"));
+ Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
- assertThat(organization.toString()).isEqualTo("Organization{uuid='uuid', key='key', name='name'}");
+ assertThat(organization.toString()).isEqualTo("Organization{uuid='uuid', key='key', name='name', defaultQualityGateUuid='uuid1'}");
}
@Test
public void equals_is_based_on_uuid_only() {
- Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name"));
+ Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
- assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name")));
- assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("other key").setName("name")));
- assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("other name")));
- assertThat(organization).isNotEqualTo(Organization.from(underTest.setUuid("other uuid").setKey("key").setName("name")));
+ assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1")));
+ assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("other key").setName("name").setDefaultQualityGateUuid("uuid1")));
+ assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("other name").setDefaultQualityGateUuid("uuid1")));
+ assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("other uuid")));
+ assertThat(organization).isNotEqualTo(Organization.from(underTest.setUuid("other uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1")));
assertThat(organization).isNotEqualTo(null);
assertThat(organization).isNotEqualTo("toto");
}
@Test
public void hashcode_is_based_on_uuid_only() {
- Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name"));
+ Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
assertThat(organization.hashCode()).isEqualTo("uuid".hashCode());
}
public static final String PROJECT_KEY = "PROJECT_KEY";
public static final String ORGANIZATION_UUID = "ORGANIZATION_UUID";
+ public static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID";
@Rule
public DbTester db = DbTester.create();
@Before
public void setUp() throws Exception {
organizationDto = db.organizations().insertForUuid(ORGANIZATION_UUID);
- analysisMetadataHolder.setOrganization(Organization.from(new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name")));
+ analysisMetadataHolder.setOrganization(Organization.from(
+ new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name").setDefaultQualityGateUuid(QUALITY_GATE_UUID)));
}
@Test
private static final RuleKey AC_RULE_KEY = RuleKey.of("a", "c");
private static final int AC_RULE_ID = 684;
private static final String ORGANIZATION_UUID = "org-1";
+ public static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID";
@org.junit.Rule
public ExpectedException expectedException = ExpectedException.none();
@org.junit.Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
- .setOrganizationUuid(ORGANIZATION_UUID);
+ .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID);
private DbClient dbClient = mock(DbClient.class);
private DbSession dbSession = mock(DbSession.class);
*/
package org.sonar.server.computation.task.projectanalysis.qualitygate;
-import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import java.util.Collections;
+import java.util.Optional;
import java.util.Random;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
-import static org.assertj.guava.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
@Test
public void findById_returns_absent_when_QualityGateDto_does_not_exist() {
- assertThat(underTest.findById(SOME_ID)).isAbsent();
+ assertThat(underTest.findById(SOME_ID)).isNotPresent();
}
@Test
when(qualityGateDao.selectById(any(DbSession.class), eq(SOME_ID))).thenReturn(QUALITY_GATE_DTO);
when(qualityGateConditionDao.selectForQualityGate(any(DbSession.class), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
// metrics are always supposed to be there
- when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(java.util.Optional.of(METRIC_1));
- when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(java.util.Optional.of(METRIC_2));
+ when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.of(METRIC_1));
+ when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2));
Optional<QualityGate> res = underTest.findById(SOME_ID);
when(qualityGateDao.selectById(any(DbSession.class), eq(SOME_ID))).thenReturn(QUALITY_GATE_DTO);
when(qualityGateConditionDao.selectForQualityGate(any(DbSession.class), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
// metrics are always supposed to be there
- when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(java.util.Optional.empty());
- when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(java.util.Optional.of(METRIC_2));
+ when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.empty());
+ when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2));
Optional<QualityGate> res = underTest.findById(SOME_ID);
*/
package org.sonar.server.computation.task.projectanalysis.step;
-import com.google.common.base.Optional;
import java.util.Collections;
+import java.util.Optional;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
public class LoadQualityGateStepTest {
@Test
public void execute_sets_default_QualityGate_when_project_has_no_settings() {
when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
+ QualityGate qualityGate = mock(QualityGate.class);
+ when(qualityGateService.findDefaultQualityGate(any())).thenReturn(Optional.of(qualityGate));
underTest.execute();
- verifyNoQualityGate();
-
- // verify only project is processed
- verify(settingsRepository).getConfiguration();
- verifyNoMoreInteractions(settingsRepository);
+ assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
}
@Test
}
@Test
- public void execute_sets_default_QualityGate_if_it_can_not_be_found_by_service() {
- when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
- when(qualityGateService.findById(10)).thenReturn(Optional.absent());
+ public void execute_sets_default_QualityGate_if_no_default_quality_gate_on_organization() {
+ when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
+ when(qualityGateService.findDefaultQualityGate(any())).thenReturn(Optional.empty());
underTest.execute();
@Test
public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
- QualityGate qualityGate = new QualityGate(465, "name", Collections.emptyList());
+ QualityGate qualityGate = new QualityGate(10, "name", Collections.emptyList());
when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
public BatchReportReaderRule reportReader = new BatchReportReaderRule();
@Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
- .setOrganizationUuid("org-1");
+ .setOrganizationUuid("org-1","qg-uuid-1");
private DbSession session = db.getSession();
private DbClient dbClient = db.getDbClient();
private static final String PROJECT_KEY = "PROJECT_KEY";
private static final String MODULE_KEY = "MODULE_KEY";
private static final String ORGANIZATION_UUID = "org1";
+ private static final String QUALITY_GATE_UUID = "gg1";
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
@Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
- .setOrganizationUuid(ORGANIZATION_UUID);
+ .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID);
private System2 system2 = mock(System2.class);
private DbClient dbClient = db.getDbClient();
private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
private static final String ORGANIZATION_UUID = "org1";
+ private static final String QUALITY_GATE_UUID = "qg1";
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
@Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
- .setOrganizationUuid(ORGANIZATION_UUID);
+ .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID);
private System2 system2 = mock(System2.class);
private DbClient dbClient = dbTester.getDbClient();