*/
package org.sonar.ce.task.projectanalysis.component;
-import java.util.List;
+import java.util.Arrays;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
-import org.sonar.api.config.Configuration;
import org.sonar.api.resources.Qualifiers;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.protobuf.DbProjectBranches;
-import org.sonar.server.setting.ProjectConfigurationLoader;
-import static java.lang.String.format;
-import static java.util.Arrays.asList;
-import static java.util.Optional.ofNullable;
import static org.sonar.core.config.PurgeConstants.BRANCHES_TO_KEEP_WHEN_INACTIVE;
/**
private final DbClient dbClient;
private final TreeRootHolder treeRootHolder;
private final AnalysisMetadataHolder analysisMetadataHolder;
- private final ProjectConfigurationLoader projectConfigurationLoader;
+ private final ConfigurationRepository configurationRepository;
- public BranchPersisterImpl(DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
- ProjectConfigurationLoader projectConfigurationLoader) {
+ public BranchPersisterImpl(DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder, ConfigurationRepository configurationRepository) {
this.dbClient = dbClient;
this.treeRootHolder = treeRootHolder;
this.analysisMetadataHolder = analysisMetadataHolder;
- this.projectConfigurationLoader = projectConfigurationLoader;
+ this.configurationRepository = configurationRepository;
}
public void persist(DbSession dbSession) {
.orElseThrow(() -> new IllegalStateException("Component has been deleted by end-user during analysis"));
// insert or update in table project_branches
- dbClient.branchDao().upsert(dbSession, toBranchDto(dbSession, branchComponentDto, branch, checkIfExcludedFromPurge(dbSession)));
+ dbClient.branchDao().upsert(dbSession, toBranchDto(dbSession, branchComponentDto, branch, checkIfExcludedFromPurge()));
}
- private boolean checkIfExcludedFromPurge(DbSession dbSession) {
+ private boolean checkIfExcludedFromPurge() {
if (analysisMetadataHolder.getBranch().isMain()) {
return true;
}
return false;
}
- ComponentDto projectDto = dbClient.componentDao().selectByUuid(dbSession, analysisMetadataHolder.getProject().getUuid())
- .orElseThrow(() -> new IllegalStateException(format("Component '%s' is missing.", analysisMetadataHolder.getProject().getKey())));
- Configuration projectConfiguration = projectConfigurationLoader.loadProjectConfiguration(dbSession, projectDto);
- String[] branchesToKeep = projectConfiguration.getStringArray(BRANCHES_TO_KEEP_WHEN_INACTIVE);
- List<String> excludeFromPurgeEntries = asList(ofNullable(branchesToKeep).orElse(new String[0]));
- return excludeFromPurgeEntries.stream()
+ String[] branchesToKeep = configurationRepository.getConfiguration().getStringArray(BRANCHES_TO_KEEP_WHEN_INACTIVE);
+ return Arrays.stream(branchesToKeep)
.map(Pattern::compile)
.anyMatch(excludePattern -> excludePattern.matcher(analysisMetadataHolder.getBranch().getName()).matches());
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.sonar.api.config.internal.ConfigurationBridge;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.System2;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.db.property.PropertyDto;
import org.sonar.db.protobuf.DbProjectBranches;
import org.sonar.server.project.Project;
-import org.sonar.server.setting.ProjectConfigurationLoader;
-import org.sonar.server.setting.ProjectConfigurationLoaderImpl;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
@Rule
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
- private final MapSettings globalSettings = new MapSettings();
- private final ProjectConfigurationLoader projectConfiguration = new ProjectConfigurationLoaderImpl(globalSettings, dbTester.getDbClient());
- private final BranchPersister underTest = new BranchPersisterImpl(dbTester.getDbClient(), treeRootHolder, analysisMetadataHolder,
- projectConfiguration);
+ private final MapSettings settings = new MapSettings();
+ private final ConfigurationRepository configurationRepository = new TestSettingsRepository(new ConfigurationBridge(settings));
+ private final BranchPersister underTest = new BranchPersisterImpl(dbTester.getDbClient(), treeRootHolder, analysisMetadataHolder, configurationRepository);
@Test
public void persist_fails_with_ISE_if_no_component_for_main_branches() {
@Test
public void non_main_branch_is_excluded_from_branch_purge_if_matches_sonar_dbcleaner_keepFromPurge_property() {
- globalSettings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "BRANCH.*");
+ settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "BRANCH.*");
analysisMetadataHolder.setProject(PROJECT);
analysisMetadataHolder.setBranch(createBranch(BRANCH, false, "BRANCH_KEY"));
treeRootHolder.setRoot(BRANCH1);
}
@Test
- public void branch_is_excluded_from_purge_when_it_matches_project_level_but_not_global_level_keepFromPurge_setting() {
+ public void branch_is_excluded_from_purge_when_it_matches_setting() {
analysisMetadataHolder.setProject(PROJECT);
analysisMetadataHolder.setBranch(createBranch(BRANCH, false, "BRANCH_KEY"));
treeRootHolder.setRoot(BRANCH1);
ComponentDto component = ComponentTesting.newBranchComponent(mainComponent,
new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(BRANCH));
dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component);
- globalSettings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "abc.*");
- insertProjectProperty(mainComponent, BRANCHES_TO_KEEP_WHEN_INACTIVE, "BRANCH.*");
+ settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "BRANCH.*");
dbTester.commit();
underTest.persist(dbTester.getSession());
}
@Test
- public void branch_is_not_excluded_from_purge_when_it_does_not_match_overriden_global_keepFromPurge_setting_on_project_level() {
+ public void branch_is_not_excluded_from_purge_when_it_does_not_match_setting() {
analysisMetadataHolder.setProject(PROJECT);
analysisMetadataHolder.setBranch(createBranch(BRANCH, false, "BRANCH_KEY"));
treeRootHolder.setRoot(BRANCH1);
ComponentDto component = ComponentTesting.newBranchComponent(mainComponent,
new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(BRANCH));
dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component);
- globalSettings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "BRANCH.*");
- insertProjectProperty(mainComponent, BRANCHES_TO_KEEP_WHEN_INACTIVE, "abc.*");
+ settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "abc.*");
+
dbTester.commit();
underTest.persist(dbTester.getSession());
@Test
public void pull_request_is_never_excluded_from_branch_purge_even_if_its_source_branch_name_matches_sonar_dbcleaner_keepFromPurge_property() {
- globalSettings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "develop");
+ settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "develop");
analysisMetadataHolder.setBranch(createPullRequest(PR1.getKey(), MAIN.getUuid()));
analysisMetadataHolder.setPullRequestKey(PR1.getKey());
treeRootHolder.setRoot(PR1);
@Test
public void non_main_branch_is_included_in_branch_purge_if_branch_name_does_not_match_sonar_dbcleaner_keepFromPurge_property() {
- globalSettings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "foobar-.*");
+ settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "foobar-.*");
analysisMetadataHolder.setProject(PROJECT);
analysisMetadataHolder.setBranch(createBranch(BRANCH, false, "BRANCH_KEY"));
treeRootHolder.setRoot(BRANCH1);
.isInstanceOf(IllegalStateException.class)
.hasMessage("Component has been deleted by end-user during analysis");
}
-
- private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) {
- dbTester.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setComponentUuid(project.uuid()));
- }
}