From 8f7b126ac4b464e73cb92d6f1c86120e6e92b91c Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 20 Oct 2017 15:28:40 +0200 Subject: [PATCH] SONAR-9991 Clean up remaining references to P/R --- .../settings/ProjectConfigurationFactory.java | 17 ++------- .../analysis/AnalysisMetadataHolder.java | 8 ++--- .../analysis/AnalysisMetadataHolderImpl.java | 7 ++-- .../task/projectanalysis/analysis/Branch.java | 4 +-- .../MutableAnalysisMetadataHolder.java | 2 +- .../PostProjectAnalysisTasksExecutor.java | 7 ++-- .../component/BranchPersister.java | 8 +---- .../component/DefaultBranchImpl.java | 3 +- .../component/MergeBranchComponentUuids.java | 2 +- ...ossProjectDuplicationStatusHolderImpl.java | 5 +-- .../projectanalysis/issue/IssueLifecycle.java | 4 +-- .../issue/IssueTrackingDelegator.java | 14 +++----- .../projectanalysis/scm/ScmInfoDbLoader.java | 5 +-- .../step/BuildComponentTreeStep.java | 19 ++++------ .../step/PersistComponentsStep.java | 21 +++++------ .../step/QualityGateEventsStep.java | 7 ++-- .../step/SendIssueNotificationsStep.java | 3 +- .../ProjectConfigurationFactoryTest.java | 20 +++++------ .../AnalysisMetadataHolderImplTest.java | 2 +- .../analysis/AnalysisMetadataHolderRule.java | 7 ++-- .../MutableAnalysisMetadataHolderRule.java | 3 +- .../PostProjectAnalysisTasksExecutorTest.java | 13 ++----- .../component/BranchLoaderTest.java | 12 +++---- .../component/BranchPersisterTest.java | 7 ---- .../ConfigurationRepositoryTest.java | 21 +++++++---- .../issue/IntegrateIssuesVisitorTest.java | 2 +- .../issue/IssueTrackingDelegatorTest.java | 18 +++++----- .../scm/ScmInfoDbLoaderTest.java | 9 +++-- .../step/BuildComponentTreeStepTest.java | 35 +++---------------- .../step/QualityGateEventsStepTest.java | 2 +- .../step/ReportPersistComponentsStepTest.java | 8 ++--- 31 files changed, 113 insertions(+), 182 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java b/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java index 2c53384e744..7c99ff5a172 100644 --- a/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java @@ -19,7 +19,6 @@ */ 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; @@ -41,11 +40,10 @@ public class ProjectConfigurationFactory { this.dbClient = dbClient; } - public Configuration newProjectConfiguration(String projectKey, Optional 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); } @@ -54,15 +52,4 @@ public class ProjectConfigurationFactory { .selectProjectProperties(componentDbKey) .forEach(property -> settings.setProperty(property.getKey(), property.getValue())); } - - private static Optional getBranchName(Optional branchOpt) { - if (!branchOpt.isPresent()) { - return Optional.empty(); - } - Branch branch = branchOpt.get(); - if (!branch.isLegacyFeature() && !branch.isMain()) { - return Optional.of(branch.getName()); - } - return Optional.empty(); - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java index a1bf583932c..b8913710463 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java @@ -20,7 +20,6 @@ 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; @@ -84,12 +83,9 @@ public interface AnalysisMetadataHolder { 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 getBranch(); + Branch getBranch(); /** * The project as represented by the main branch. It is used to load settings diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java index fae786e5d59..5924ff97bf4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java @@ -21,7 +21,6 @@ package org.sonar.server.computation.task.projectanalysis.analysis; 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; @@ -123,16 +122,16 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder } @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 getBranch() { + public Branch getBranch() { checkState(branch.isInitialized(), BRANCH_NOT_SET); - return Optional.ofNullable(branch.getProperty()); + return branch.getProperty(); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java index 7cbc50bac6a..a9af89b90f7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java @@ -43,8 +43,8 @@ public interface Branch extends ComponentKeyGenerator { 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 getMergeBranchUuid(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java index 4b5477caf8c..5e89e938320 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java @@ -53,7 +53,7 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder { /** * @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 diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java index 8c8f367a88b..ab8eef8c465 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java @@ -167,10 +167,9 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor @CheckForNull private BranchImpl createBranch() { - Optional 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; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java index 2f6cd178826..7c3764bac7c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java @@ -20,7 +20,6 @@ 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; @@ -47,12 +46,7 @@ public class BranchPersister { } public void persist(DbSession dbSession) { - Optional 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 branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java index 3f4461ba006..2f0c4d35a48 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java @@ -35,8 +35,7 @@ import static org.apache.commons.lang.StringUtils.trimToNull; /** * 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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/MergeBranchComponentUuids.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/MergeBranchComponentUuids.java index e91d1fc8c4a..15a861d68d1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/MergeBranchComponentUuids.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/MergeBranchComponentUuids.java @@ -46,7 +46,7 @@ public class MergeBranchComponentUuids { 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)) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java index 4a463e16c69..d175b4d3db6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/duplication/CrossProjectDuplicationStatusHolderImpl.java @@ -19,13 +19,11 @@ */ 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; @@ -50,8 +48,7 @@ public class CrossProjectDuplicationStatusHolderImpl implements CrossProjectDupl @Override public void start() { boolean enabledInReport = analysisMetadataHolder.isCrossProjectDuplicationEnabled(); - Optional 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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueLifecycle.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueLifecycle.java index b8f51767ed0..16025603b0a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueLifecycle.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueLifecycle.java @@ -75,12 +75,12 @@ public class IssueLifecycle { 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) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegator.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegator.java index 655b482c58d..5e2275fc1f7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegator.java @@ -19,11 +19,6 @@ */ 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; @@ -31,6 +26,9 @@ import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetada 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; @@ -65,10 +63,8 @@ public class IssueTrackingDelegator { */ private boolean isFirstAnalysisSecondaryLongLivingBranch() { if (analysisMetadataHolder.isFirstAnalysis()) { - Optional 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; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoader.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoader.java index f20a90f3bf0..84cf8790054 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoader.java @@ -71,8 +71,9 @@ public class ScmInfoDbLoader { return Optional.of(file.getUuid()); } - Optional 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())); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java index 4c5b96896aa..fffb6cb5db0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java @@ -19,8 +19,6 @@ */ 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; @@ -92,22 +90,17 @@ public class BuildComponentTreeStep implements ComputationStep { } 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 = 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 diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java index 216843767c0..a49b6eef2ae 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java @@ -19,13 +19,7 @@ */ 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; @@ -33,11 +27,9 @@ import java.util.Optional; 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; @@ -49,7 +41,6 @@ import org.sonar.db.DbSession; 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; @@ -62,7 +53,12 @@ import org.sonar.server.computation.task.projectanalysis.component.PathAwareVisi 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 @@ -127,8 +123,7 @@ public class PersistComponentsStep implements ComputationStep { */ @CheckForNull private String loadProjectUuidOfMainBranch() { - Optional branch = analysisMetadataHolder.getBranch(); - if (branch.isPresent() && !branch.get().isMain()) { + if (!analysisMetadataHolder.getBranch().isMain()) { return analysisMetadataHolder.getProject().getUuid(); } return null; diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java index c49a13c453d..c8ec6f298a5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java @@ -26,6 +26,7 @@ import org.sonar.api.notifications.Notification; 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; @@ -136,8 +137,10 @@ public class QualityGateEventsStep implements ComputationStep { .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); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java index b54344cc019..eb3f044c9e3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java @@ -186,7 +186,8 @@ public class SendIssueNotificationsStep implements ComputationStep { } 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(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java b/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java index 1388284bec0..2cf1dd37759 100644 --- a/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java +++ b/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java @@ -19,7 +19,6 @@ */ package org.sonar.ce.settings; -import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.sonar.api.config.Configuration; @@ -27,6 +26,7 @@ import org.sonar.api.config.internal.MapSettings; 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; @@ -47,7 +47,7 @@ public class ProjectConfigurationFactoryTest { @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"); } @@ -60,7 +60,7 @@ public class ProjectConfigurationFactoryTest { 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"); @@ -73,7 +73,7 @@ public class ProjectConfigurationFactoryTest { 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"); } @@ -84,7 +84,7 @@ public class ProjectConfigurationFactoryTest { 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"); } @@ -96,7 +96,7 @@ public class ProjectConfigurationFactoryTest { 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"); @@ -109,7 +109,7 @@ public class ProjectConfigurationFactoryTest { 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"); @@ -122,7 +122,7 @@ public class ProjectConfigurationFactoryTest { 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"); } @@ -134,7 +134,7 @@ public class ProjectConfigurationFactoryTest { 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"); } @@ -146,7 +146,7 @@ public class ProjectConfigurationFactoryTest { 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"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java index 998087518f3..ec05e37eba3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java @@ -220,7 +220,7 @@ public class AnalysisMetadataHolderImplTest { underTest.setBranch(new DefaultBranchImpl("master")); - assertThat(underTest.getBranch().get().getName()).isEqualTo("master"); + assertThat(underTest.getBranch().getName()).isEqualTo("master"); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java index df4faae3fcf..f1fb6e2a42c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java @@ -21,7 +21,6 @@ package org.sonar.server.computation.task.projectanalysis.analysis; 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; @@ -143,15 +142,15 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta } @Override - public AnalysisMetadataHolderRule setBranch(@Nullable Branch branch) { + public AnalysisMetadataHolderRule setBranch(Branch branch) { this.branch.setProperty(branch); return this; } @Override - public Optional getBranch() { + public Branch getBranch() { checkState(branch.isInitialized(), "Branch has not been set"); - return Optional.ofNullable(branch.getProperty()); + return branch.getProperty(); } @Override diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java index 787d5ef3e0d..49c69382509 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java @@ -20,7 +20,6 @@ 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; @@ -100,7 +99,7 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen } @Override - public Optional getBranch() { + public Branch getBranch() { return delegate.getBranch(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java index 1c9a6f786e9..484180fabb2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java @@ -101,7 +101,9 @@ public class PostProjectAnalysisTasksExecutorTest { 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 @@ -215,15 +217,6 @@ public class PostProjectAnalysisTasksExecutorTest { 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")); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java index 3ed00537c4d..84ac5dd0d25 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java @@ -58,9 +58,9 @@ public class BranchLoaderTest { 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); } @@ -73,9 +73,9 @@ public class BranchLoaderTest { 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"); } @@ -89,9 +89,9 @@ public class BranchLoaderTest { 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"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java index 46e56b62973..3bb6b653b72 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java @@ -64,13 +64,6 @@ public class BranchPersisterTest { 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")); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java index 139407dd6f5..a1a8dd2d395 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java @@ -19,6 +19,7 @@ */ 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; @@ -46,12 +47,20 @@ public class ConfigurationRepositoryTest { 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(); @@ -62,7 +71,7 @@ public class ConfigurationRepositoryTest { @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(); @@ -72,7 +81,7 @@ public class ConfigurationRepositoryTest { @Test public void call_twice_get_project_settings() { - analysisMetadataHolder.setProject(PROJECT).setBranch(null); + analysisMetadataHolder.setProject(PROJECT); globalSettings.setProperty("key", "value"); Configuration config = underTest.getConfiguration(); @@ -87,7 +96,7 @@ public class ConfigurationRepositoryTest { 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"); @@ -97,7 +106,7 @@ public class ConfigurationRepositoryTest { 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"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java index 0f695c19d65..e9438b1f167 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java @@ -267,7 +267,7 @@ public class IntegrateIssuesVisitorTest { 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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegatorTest.java index 966f98ef623..841cf0e0546 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegatorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/IssueTrackingDelegatorTest.java @@ -19,13 +19,6 @@ */ 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; @@ -37,6 +30,11 @@ import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetada 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; @@ -65,7 +63,7 @@ public class IssueTrackingDelegatorTest { @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); @@ -79,7 +77,7 @@ public class IssueTrackingDelegatorTest { 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); @@ -94,7 +92,7 @@ public class IssueTrackingDelegatorTest { 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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoaderTest.java index 22b950770fb..ca10c46e0f7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoaderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoDbLoaderTest.java @@ -22,6 +22,7 @@ package org.sonar.server.computation.task.projectanalysis.scm; 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; @@ -94,6 +95,7 @@ public class ScmInfoDbLoaderTest { 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); @@ -109,6 +111,7 @@ public class ScmInfoDbLoaderTest { 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); @@ -131,9 +134,11 @@ public class ScmInfoDbLoaderTest { } @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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java index 11ad345a608..0afa4664e2b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java @@ -24,7 +24,6 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner; 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; @@ -229,11 +228,7 @@ public class BuildComponentTreeStepTest { @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)) @@ -246,10 +241,10 @@ public class BuildComponentTreeStepTest { 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 @@ -272,26 +267,6 @@ public class BuildComponentTreeStepTest { 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(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java index c5e2801cd3a..9fea5a5dd0e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStepTest.java @@ -88,7 +88,7 @@ public class QualityGateEventsStepTest { 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); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java index c03e8826dc9..09c708162a4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java @@ -901,14 +901,14 @@ public class ReportPersistComponentsStepTest extends BaseStepTest { private ComponentDto prepareBranch(String branchName, Consumer... 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; } @@ -944,7 +944,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest { @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; } -- 2.39.5