*/
package org.sonar.ce.settings;
-import java.util.Optional;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.Settings;
this.dbClient = dbClient;
}
- public Configuration newProjectConfiguration(String projectKey, Optional<Branch> branch) {
+ public Configuration newProjectConfiguration(String projectKey, Branch branch) {
Settings projectSettings = new ChildSettings(globalSettings);
addSettings(projectSettings, projectKey);
- getBranchName(branch).ifPresent(
- b -> addSettings(projectSettings, generateBranchKey(projectKey, b)));
+ addSettings(projectSettings, generateBranchKey(projectKey, branch.getName()));
return new ConfigurationBridge(projectSettings);
}
.selectProjectProperties(componentDbKey)
.forEach(property -> settings.setProperty(property.getKey(), property.getValue()));
}
-
- private static Optional<String> getBranchName(Optional<Branch> branchOpt) {
- if (!branchOpt.isPresent()) {
- return Optional.empty();
- }
- Branch branch = branchOpt.get();
- if (!branch.isLegacyFeature() && !branch.isMain()) {
- return Optional.of(branch.getName());
- }
- return Optional.empty();
- }
}
package org.sonar.server.computation.task.projectanalysis.analysis;
import java.util.Map;
-import java.util.Optional;
import javax.annotation.CheckForNull;
import org.sonar.server.qualityprofile.QualityProfile;
boolean isCrossProjectDuplicationEnabled();
/**
- * Branch is present whatever the type of branch (long or short, main or not). However
- * it is absent when analyzing a pull request.
- *
- * @throws IllegalStateException if branch has not been set
+ * Branch being analyzed. Can be of any type: long or short, main or not.
*/
- Optional<Branch> getBranch();
+ Branch getBranch();
/**
* The project as represented by the main branch. It is used to load settings
import com.google.common.collect.ImmutableMap;
import java.util.Map;
-import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.db.component.BranchType;
}
@Override
- public MutableAnalysisMetadataHolder setBranch(@Nullable Branch branch) {
+ public MutableAnalysisMetadataHolder setBranch(Branch branch) {
checkState(!this.branch.isInitialized(), "Branch has already been set");
this.branch.setProperty(branch);
return this;
}
@Override
- public Optional<Branch> getBranch() {
+ public Branch getBranch() {
checkState(branch.isInitialized(), BRANCH_NOT_SET);
- return Optional.ofNullable(branch.getProperty());
+ return branch.getProperty();
}
@Override
String getName();
/**
- * For short living branches, indicates the branch from which it was forked.
- * It will be empty for other types of branches.
+ * Indicates the branch from which it was forked.
+ * It will be empty for main branches or legacy branches.
*/
Optional<String> getMergeBranchUuid();
/**
* @throws IllegalStateException if branch has already been set
*/
- MutableAnalysisMetadataHolder setBranch(@Nullable Branch branch);
+ MutableAnalysisMetadataHolder setBranch(Branch branch);
/**
* @throws IllegalStateException if project has already been set
@CheckForNull
private BranchImpl createBranch() {
- Optional<org.sonar.server.computation.task.projectanalysis.analysis.Branch> analysisBranchOpt = analysisMetadataHolder.getBranch();
- if (analysisBranchOpt.isPresent() && !analysisBranchOpt.get().isLegacyFeature()) {
- org.sonar.server.computation.task.projectanalysis.analysis.Branch branch = analysisBranchOpt.get();
- return new BranchImpl(branch.isMain(), branch.getName(), Branch.Type.valueOf(branch.getType().name()));
+ org.sonar.server.computation.task.projectanalysis.analysis.Branch analysisBranch = analysisMetadataHolder.getBranch();
+ if (!analysisBranch.isLegacyFeature()) {
+ return new BranchImpl(analysisBranch.isMain(), analysisBranch.getName(), Branch.Type.valueOf(analysisBranch.getType().name()));
}
return null;
}
package org.sonar.server.computation.task.projectanalysis.component;
import java.util.Date;
-import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
}
public void persist(DbSession dbSession) {
- Optional<Branch> branchOpt = analysisMetadataHolder.getBranch();
- if (!branchOpt.isPresent()) {
- return;
- }
-
- Branch branch = branchOpt.get();
+ Branch branch = analysisMetadataHolder.getBranch();
String branchUuid = treeRootHolder.getRoot().getUuid();
com.google.common.base.Optional<ComponentDto> branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid);
/**
* The default (and legacy) implementation of {@link Branch}. It is used
* when scanner is configured with parameter "sonar.branch" or when no branch is provided and the branch plugin is not installed.
- * A legacy branch is implemented as a fork of the project, so any branch
- * is considered as "main".
+ * A legacy branch is implemented as a fork of the project, so any branch is considered as "main".
*/
public class DefaultBranchImpl implements Branch {
private final String branchName;
private void lazyInit() {
if (uuidsByKey == null) {
- String mergeBranchUuid = analysisMetadataHolder.getBranch().get().getMergeBranchUuid().get();
+ String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get();
uuidsByKey = new HashMap<>();
try (DbSession dbSession = dbClient.openSession(false)) {
*/
package org.sonar.server.computation.task.projectanalysis.duplication;
-import java.util.Optional;
import javax.annotation.CheckForNull;
import org.picocontainer.Startable;
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.analysis.Branch;
import static com.google.common.base.Preconditions.checkState;
@Override
public void start() {
boolean enabledInReport = analysisMetadataHolder.isCrossProjectDuplicationEnabled();
- Optional<Branch> branch = analysisMetadataHolder.getBranch();
- boolean supportedByBranch = !branch.isPresent() || branch.get().supportsCrossProjectCpd();
+ boolean supportedByBranch = analysisMetadataHolder.getBranch().supportsCrossProjectCpd();
if (enabledInReport && supportedByBranch) {
LOGGER.debug("Cross project duplication is enabled");
this.enabled = true;
raw.setKey(Uuids.create());
raw.setNew(false);
copyIssueAttributes(raw, base);
- raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_LONG_BRANCH, fromLongBranchName, analysisMetadataHolder.getBranch().get().getName());
+ raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_LONG_BRANCH, fromLongBranchName, analysisMetadataHolder.getBranch().getName());
}
public void mergeConfirmedOrResolvedFromShortLivingBranch(DefaultIssue raw, DefaultIssue base, String fromShortBranchName) {
copyIssueAttributes(raw, base);
- raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_SHORT_BRANCH, fromShortBranchName, analysisMetadataHolder.getBranch().get().getName());
+ raw.setFieldChange(changeContext, IssueFieldsSetter.FROM_SHORT_BRANCH, fromShortBranchName, analysisMetadataHolder.getBranch().getName());
}
private void copyIssueAttributes(DefaultIssue to, DefaultIssue from) {
*/
package org.sonar.server.computation.task.projectanalysis.issue;
-import static java.util.Collections.emptyList;
-import static java.util.Collections.emptyMap;
-
-import java.util.Optional;
-
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.tracking.Tracking;
import org.sonar.db.component.BranchType;
import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
import org.sonar.server.computation.task.projectanalysis.component.Component;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.emptyMap;
+
public class IssueTrackingDelegator {
private final ShortBranchTrackerExecution shortBranchTracker;
private final TrackerExecution tracker;
*/
private boolean isFirstAnalysisSecondaryLongLivingBranch() {
if (analysisMetadataHolder.isFirstAnalysis()) {
- Optional<Branch> branch = analysisMetadataHolder.getBranch();
- if (branch.isPresent()) {
- return !branch.get().isMain() && branch.get().getType() == BranchType.LONG;
- }
+ Branch branch = analysisMetadataHolder.getBranch();
+ return !branch.isMain() && branch.getType() == BranchType.LONG;
}
return false;
}
return Optional.of(file.getUuid());
}
- Optional<Branch> branch = analysisMetadataHolder.getBranch();
- if (branch.isPresent()) {
+ // at this point, it's the first analysis but had copyFromPrevious flag true
+ Branch branch = analysisMetadataHolder.getBranch();
+ if (branch.getMergeBranchUuid().isPresent()) {
return Optional.ofNullable(mergeBranchComponentUuid.getUuid(file.getKey()));
}
*/
package org.sonar.server.computation.task.projectanalysis.step;
-import java.util.Optional;
-import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.db.DbClient;
}
private ComponentKeyGenerator loadKeyGenerator() {
- return Stream.of(analysisMetadataHolder.getBranch(), Optional.of(new DefaultBranchImpl()))
- .filter(Optional::isPresent)
- .flatMap(x -> x.map(Stream::of).orElseGet(Stream::empty))
- .findFirst()
- .get();
+ return analysisMetadataHolder.getBranch();
}
private ComponentKeyGenerator loadPublicKeyGenerator() {
- Optional<Branch> branch = analysisMetadataHolder.getBranch();
- if (!branch.isPresent()) {
- // Used for pull request
+ Branch branch = analysisMetadataHolder.getBranch();
+
+ // for non-legacy branches, the public key is different from the DB key.
+ if (!branch.isLegacyFeature() && !branch.isMain()) {
return new DefaultBranchImpl();
}
- return branch.filter(Branch::isLegacyFeature)
- .map(b -> new DefaultBranchImpl(b.getName()))
- .orElseGet(DefaultBranchImpl::new);
+ return branch;
}
@CheckForNull
*/
package org.sonar.server.computation.task.projectanalysis.step;
-import static com.google.common.collect.FluentIterable.from;
-import static java.util.Optional.ofNullable;
-import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
-import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
-import static org.sonar.db.component.ComponentDto.formatUuidPathFromParent;
-import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
-
+import com.google.common.base.Predicate;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
-
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.resources.Qualifiers;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentUpdateDto;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
-import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
import org.sonar.server.computation.task.projectanalysis.component.BranchPersister;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
import org.sonar.server.computation.task.step.ComputationStep;
-import com.google.common.base.Predicate;
+import static com.google.common.collect.FluentIterable.from;
+import static java.util.Optional.ofNullable;
+import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
+import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
+import static org.sonar.db.component.ComponentDto.formatUuidPathFromParent;
+import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
/**
* Persist report components
*/
@CheckForNull
private String loadProjectUuidOfMainBranch() {
- Optional<Branch> branch = analysisMetadataHolder.getBranch();
- if (branch.isPresent() && !branch.get().isMain()) {
+ if (!analysisMetadataHolder.getBranch().isMain()) {
return analysisMetadataHolder.getProject().getUuid();
}
return null;
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.analysis.Branch;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor;
import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
.setFieldValue("alertText", rawStatus.getText())
.setFieldValue("alertLevel", rawStatus.getStatus().toString())
.setFieldValue("isNewAlert", Boolean.toString(isNewAlert));
- analysisMetadataHolder.getBranch().filter(b -> !b.isMain())
- .ifPresent(branch -> notification.setFieldValue("branch", branch.getName()));
+ Branch branch = analysisMetadataHolder.getBranch();
+ if (!branch.isMain()) {
+ notification.setFieldValue("branch", branch.getName());
+ }
notificationService.deliver(notification);
}
}
private String getBranchName() {
- return analysisMetadataHolder.getBranch().filter(b -> !b.isMain()).map(Branch::getName).orElse(null);
+ Branch branch = analysisMetadataHolder.getBranch();
+ return branch.isMain() ? null : branch.getName();
}
}
*/
package org.sonar.ce.settings;
-import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.Configuration;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
+import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@Test
public void return_global_settings() {
settings.setProperty("key", "value");
- Configuration config = underTest.newProjectConfiguration(PROJECT_KEY, Optional.empty());
+ Configuration config = underTest.newProjectConfiguration(PROJECT_KEY, new DefaultBranchImpl());
assertThat(config.get("key")).hasValue("value");
}
newComponentPropertyDto(project).setKey("2").setValue("val2"),
newComponentPropertyDto(project).setKey("3").setValue("val3"));
- Configuration config = underTest.newProjectConfiguration(project.getDbKey(), Optional.empty());
+ Configuration config = underTest.newProjectConfiguration(project.getDbKey(), new DefaultBranchImpl());
assertThat(config.get("1")).hasValue("val1");
assertThat(config.get("2")).hasValue("val2");
ComponentDto project = db.components().insertPrivateProject();
db.properties().insertProperties(newComponentPropertyDto(project).setKey("key").setValue("value2"));
- Configuration projectConfig = underTest.newProjectConfiguration(project.getDbKey(), Optional.empty());
+ Configuration projectConfig = underTest.newProjectConfiguration(project.getDbKey(), new DefaultBranchImpl());
assertThat(projectConfig.get("key")).hasValue("value2");
}
ComponentDto branch = db.components().insertProjectBranch(project);
db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1"));
- Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false)));
+ Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
assertThat(config.get("sonar.leak.period")).hasValue("1");
}
ComponentDto branch = db.components().insertProjectBranch(project);
db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1"));
- Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false)));
+ Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
assertThat(config.get("global")).hasValue("global_value");
assertThat(config.get("sonar.leak.period")).hasValue("1");
ComponentDto branch = db.components().insertProjectBranch(project);
db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1"));
- Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false)));
+ Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
assertThat(config.get("key")).hasValue("value");
assertThat(config.get("sonar.leak.period")).hasValue("1");
ComponentDto branch = db.components().insertProjectBranch(project);
db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("2"));
- Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false)));
+ Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
assertThat(config.get("sonar.leak.period")).hasValue("2");
}
Branch branch = createBranch("master", true);
when(branch.isMain()).thenReturn(true);
- Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getName(), true)));
+ Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getName(), true));
assertThat(config.get("sonar.leak.period")).hasValue("1");
}
Branch branch = createBranch("legacy", true);
when(branch.isLegacyFeature()).thenReturn(true);
- Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getName(), true)));
+ Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getName(), true));
assertThat(config.get("sonar.leak.period")).hasValue("1");
}
underTest.setBranch(new DefaultBranchImpl("master"));
- assertThat(underTest.getBranch().get().getName()).isEqualTo("master");
+ assertThat(underTest.getBranch().getName()).isEqualTo("master");
}
@Test
import java.util.Date;
import java.util.Map;
-import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.junit.rules.ExternalResource;
}
@Override
- public AnalysisMetadataHolderRule setBranch(@Nullable Branch branch) {
+ public AnalysisMetadataHolderRule setBranch(Branch branch) {
this.branch.setProperty(branch);
return this;
}
@Override
- public Optional<Branch> getBranch() {
+ public Branch getBranch() {
checkState(branch.isInitialized(), "Branch has not been set");
- return Optional.ofNullable(branch.getProperty());
+ return branch.getProperty();
}
@Override
package org.sonar.server.computation.task.projectanalysis.analysis;
import java.util.Map;
-import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.junit.rules.ExternalResource;
}
@Override
- public Optional<Branch> getBranch() {
+ public Branch getBranch() {
return delegate.getBranch();
}
qualityGateStatusHolder.setStatus(QualityGateStatus.OK, ImmutableMap.of(
CONDITION_1, ConditionStatus.create(ConditionStatus.EvaluationStatus.OK, "value"),
CONDITION_2, ConditionStatus.NO_VALUE_STATUS));
- analysisMetadataHolder.setBranch(null);
+ Branch branch = mock(Branch.class);
+ when(branch.getType()).thenReturn(BranchType.LONG);
+ analysisMetadataHolder.setBranch(branch);
}
@Test
assertThat(projectAnalysisArgumentCaptor.getValue().getAnalysis()).isEmpty();
}
- @Test
- public void branch_is_empty_when_not_set_in_AnalysisMetadataHolder() {
- underTest.finished(false);
-
- verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
-
- assertThat(projectAnalysisArgumentCaptor.getValue().getBranch()).isEmpty();
- }
-
@Test
public void branch_is_empty_when_legacy_branch_implementation_is_used() {
analysisMetadataHolder.setBranch(new DefaultBranchImpl("feature/foo"));
new BranchLoader(metadataHolder).load(metadata);
- assertThat(metadataHolder.getBranch()).isPresent();
+ assertThat(metadataHolder.getBranch()).isNotNull();
- Branch branch = metadataHolder.getBranch().get();
+ Branch branch = metadataHolder.getBranch();
assertThat(branch.isMain()).isTrue();
assertThat(branch.getName()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
}
new BranchLoader(metadataHolder).load(metadata);
- assertThat(metadataHolder.getBranch()).isPresent();
+ assertThat(metadataHolder.getBranch()).isNotNull();
- Branch branch = metadataHolder.getBranch().get();
+ Branch branch = metadataHolder.getBranch();
assertThat(branch.isMain()).isTrue();
assertThat(branch.getName()).isEqualTo("foo");
}
FakeDelegate delegate = new FakeDelegate();
new BranchLoader(metadataHolder, delegate).load(metadata);
- assertThat(metadataHolder.getBranch()).isPresent();
+ assertThat(metadataHolder.getBranch()).isNotNull();
- Branch branch = metadataHolder.getBranch().get();
+ Branch branch = metadataHolder.getBranch();
assertThat(branch.isMain()).isTrue();
assertThat(branch.getName()).isEqualTo("foo");
}
underTest.persist(dbTester.getSession());
}
- @Test
- public void skip_if_no_branch() {
- analysisMetadataHolder.setBranch(null);
- underTest.persist(dbTester.getSession());
- dbTester.assertTableDoesNotExist("project_branch");
- }
-
@Test
public void persist_secondary_branch() {
analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, false, "branch"));
*/
package org.sonar.server.computation.task.projectanalysis.component;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.Configuration;
private DbClient dbClient = db.getDbClient();
private MapSettings globalSettings = new MapSettings();
+ private Branch branch = mock(Branch.class);
private AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
- private ConfigurationRepository underTest = new ConfigurationRepositoryImpl(analysisMetadataHolder, new ProjectConfigurationFactory(globalSettings, dbClient));
+ private ConfigurationRepository underTest;
+
+ @Before
+ public void setUp() {
+ when(branch.getName()).thenReturn("branchName");
+ analysisMetadataHolder.setBranch(branch);
+ underTest = new ConfigurationRepositoryImpl(analysisMetadataHolder, new ProjectConfigurationFactory(globalSettings, dbClient));
+ }
@Test
public void get_project_settings_from_global_settings() {
- analysisMetadataHolder.setProject(PROJECT).setBranch(null);
+ analysisMetadataHolder.setProject(PROJECT);
globalSettings.setProperty("key", "value");
Configuration config = underTest.getConfiguration();
@Test
public void get_project_settings_from_db() {
ComponentDto project = db.components().insertPrivateProject();
- analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
+ analysisMetadataHolder.setProject(Project.copyOf(project));
insertProjectProperty(project, "key", "value");
Configuration config = underTest.getConfiguration();
@Test
public void call_twice_get_project_settings() {
- analysisMetadataHolder.setProject(PROJECT).setBranch(null);
+ analysisMetadataHolder.setProject(PROJECT);
globalSettings.setProperty("key", "value");
Configuration config = underTest.getConfiguration();
globalSettings.setProperty("key", "value1");
ComponentDto project = db.components().insertPrivateProject();
insertProjectProperty(project, "key", "value2");
- analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
+ analysisMetadataHolder.setProject(Project.copyOf(project));
Configuration config = underTest.getConfiguration();
assertThat(config.get("key")).hasValue("value2");
public void project_settings_are_cached_to_avoid_db_access() {
ComponentDto project = db.components().insertPrivateProject();
insertProjectProperty(project, "key", "value");
- analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
+ analysisMetadataHolder.setProject(Project.copyOf(project));
Configuration config = underTest.getConfiguration();
assertThat(config.get("key")).hasValue("value");
Branch branch = mock(Branch.class);
when(branch.isMain()).thenReturn(false);
when(branch.getType()).thenReturn(BranchType.LONG);
- when(analysisMetadataHolder.getBranch()).thenReturn(java.util.Optional.of(branch));
+ when(analysisMetadataHolder.getBranch()).thenReturn(branch);
RuleKey ruleKey = RuleTesting.XOO_X1;
// Issue from main branch has severity major
*/
package org.sonar.server.computation.task.projectanalysis.issue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
-
-import java.util.Optional;
-
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
import org.sonar.server.computation.task.projectanalysis.component.Component;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
public class IssueTrackingDelegatorTest {
@Mock
private ShortBranchTrackerExecution shortBranchTracker;
@Test
public void delegate_regular_tracker() {
when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
- when(analysisMetadataHolder.getBranch()).thenReturn(Optional.empty());
+ when(analysisMetadataHolder.getBranch()).thenReturn(mock(Branch.class));
underTest.track(component);
Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.LONG);
when(branch.isMain()).thenReturn(false);
- when(analysisMetadataHolder.getBranch()).thenReturn(Optional.of(branch));
+ when(analysisMetadataHolder.getBranch()).thenReturn(branch);
when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true);
underTest.track(component);
public void delegate_short_branch_tracker() {
Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(BranchType.SHORT);
- when(analysisMetadataHolder.getBranch()).thenReturn(Optional.empty());
+ when(analysisMetadataHolder.getBranch()).thenReturn(mock(Branch.class));
when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(true);
underTest.track(component);
import com.google.common.collect.ImmutableList;
import java.util.Iterator;
import java.util.List;
+import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
analysisMetadataHolder.setBaseAnalysis(null);
analysisMetadataHolder.setBranch(branch);
String mergeFileUuid = "mergeFileUuid";
+ when(branch.getMergeBranchUuid()).thenReturn(Optional.of("mergeBranchUuid"));
when(mergeBranchComponentUuids.getUuid(FILE.getKey())).thenReturn(mergeFileUuid);
addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1), mergeFileUuid);
analysisMetadataHolder.setBaseAnalysis(null);
analysisMetadataHolder.setBranch(branch);
String mergeFileUuid = "mergeFileUuid";
+ when(branch.getMergeBranchUuid()).thenReturn(Optional.of("mergeBranchUuid"));
when(mergeBranchComponentUuids.getUuid(FILE.getKey())).thenReturn(mergeFileUuid);
addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1) + "dif", mergeFileUuid);
}
@Test
- public void not_read_in_db_on_first_analysis() throws Exception {
+ public void not_read_in_db_on_first_analysis_and_no_merge_branch() throws Exception {
+ Branch branch = mock(Branch.class);
+ when(branch.getMergeBranchUuid()).thenReturn(Optional.empty());
analysisMetadataHolder.setBaseAnalysis(null);
- analysisMetadataHolder.setBranch(null);
+ analysisMetadataHolder.setBranch(branch);
addFileSourceInReport(1);
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.HashMap;
import java.util.Map;
-import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
@Test
public void generate_keys_when_using_main_branch() {
- Branch branch = mock(Branch.class);
- when(branch.getName()).thenReturn("origin/master");
- when(branch.isMain()).thenReturn(true);
- when(branch.isLegacyFeature()).thenReturn(false);
- when(branch.generateKey(any(ScannerReport.Component.class), any(ScannerReport.Component.class))).thenReturn("generated");
+ Branch branch = new DefaultBranchImpl();
analysisMetadataHolder.setRootComponentRef(ROOT_REF)
.setAnalysisDate(ANALYSIS_DATE)
.setProject(new Project("U1", REPORT_PROJECT_KEY, REPORT_PROJECT_KEY))
underTest.execute();
- verifyComponent(ROOT_REF, "generated", REPORT_PROJECT_KEY, null);
- verifyComponent(MODULE_REF, "generated", REPORT_MODULE_KEY, null);
- verifyComponent(DIR_REF_1, "generated", REPORT_MODULE_KEY + ":" + REPORT_DIR_KEY_1, null);
- verifyComponent(FILE_1_REF, "generated", REPORT_MODULE_KEY + ":" + REPORT_FILE_KEY_1, null);
+ verifyComponent(ROOT_REF, REPORT_PROJECT_KEY, REPORT_PROJECT_KEY, null);
+ verifyComponent(MODULE_REF, REPORT_MODULE_KEY, REPORT_MODULE_KEY, null);
+ verifyComponent(DIR_REF_1, REPORT_MODULE_KEY + ":" + REPORT_DIR_KEY_1, REPORT_MODULE_KEY + ":" + REPORT_DIR_KEY_1, null);
+ verifyComponent(FILE_1_REF, REPORT_MODULE_KEY + ":" + REPORT_FILE_KEY_1, REPORT_MODULE_KEY + ":" + REPORT_FILE_KEY_1, null);
}
@Test
verifyComponent(FILE_1_REF, REPORT_MODULE_KEY + ":origin/feature:" + REPORT_FILE_KEY_1, null);
}
- @Test
- public void compute_keys_when_no_branch() {
- analysisMetadataHolder.setRootComponentRef(ROOT_REF)
- .setAnalysisDate(ANALYSIS_DATE)
- .setProject(new Project("U1", REPORT_PROJECT_KEY, REPORT_PROJECT_KEY))
- .setBranch(null);
- BuildComponentTreeStep underTest = new BuildComponentTreeStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder);
- reportReader.putComponent(componentWithKey(ROOT_REF, PROJECT, REPORT_PROJECT_KEY, MODULE_REF));
- reportReader.putComponent(componentWithKey(MODULE_REF, MODULE, REPORT_MODULE_KEY, DIR_REF_1));
- reportReader.putComponent(componentWithPath(DIR_REF_1, DIRECTORY, REPORT_DIR_KEY_1, FILE_1_REF));
- reportReader.putComponent(componentWithPath(FILE_1_REF, FILE, REPORT_FILE_KEY_1));
-
- underTest.execute();
-
- verifyComponent(ROOT_REF, REPORT_PROJECT_KEY);
- verifyComponent(MODULE_REF, REPORT_MODULE_KEY);
- verifyComponent(DIR_REF_1, REPORT_MODULE_KEY + ":" + REPORT_DIR_KEY_1);
- verifyComponent(FILE_1_REF, REPORT_MODULE_KEY + ":" + REPORT_FILE_KEY_1);
- }
-
@Test
public void compute_keys_and_uuids_on_project_having_module_and_directory() {
setAnalysisMetadataHolder();
public void setUp() {
when(metricRepository.getByKey(ALERT_STATUS_KEY)).thenReturn(alertStatusMetric);
analysisMetadataHolder.setProject(new Project(PROJECT_COMPONENT.getUuid(), PROJECT_COMPONENT.getKey(), PROJECT_COMPONENT.getName()));
- analysisMetadataHolder.setBranch(null);
+ analysisMetadataHolder.setBranch(mock(Branch.class));
treeRootHolder.setRoot(PROJECT_COMPONENT);
}
private ComponentDto prepareBranch(String branchName, Consumer<ComponentDto>... populators) {
ComponentDto dto = db.components().insertPrivateProject(db.organizations().insert(), populators);
analysisMetadataHolder.setProject(Project.copyOf(dto));
- analysisMetadataHolder.setBranch(new BranchImpl(branchName));
+ analysisMetadataHolder.setBranch(new TestBranch(branchName));
return dto;
}
- private static class BranchImpl implements Branch {
+ private static class TestBranch implements Branch {
private final String name;
- public BranchImpl(String name) {
+ public TestBranch(String name) {
this.name = name;
}
@Override
public String generateKey(ScannerReport.Component module, @Nullable ScannerReport.Component fileOrDir) {
- String moduleKey = module.getKey();
+ String moduleKey = module.getKey();
if (fileOrDir == null || isEmpty(fileOrDir.getPath())) {
return moduleKey;
}