}
@Override public void publish(ScannerReportWriter writer) {
- if (scmConfiguration.isDisabled() || !branchConfiguration.isShortOrPullRequest() || branchConfiguration.branchTarget() == null) {
+ String targetScmBranch = branchConfiguration.targetScmBranch();
+ if (scmConfiguration.isDisabled() || !branchConfiguration.isShortOrPullRequest() || targetScmBranch == null) {
return;
}
}
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- int count = writeChangedLines(provider, writer);
+ int count = writeChangedLines(provider, writer, targetScmBranch);
LOG.debug("SCM reported changed lines for {} {} in the branch", count, ScannerUtils.pluralize("file", count));
profiler.stopInfo();
}
- private int writeChangedLines(ScmProvider provider, ScannerReportWriter writer) {
- String targetBranchName = branchConfiguration.branchTarget();
- if (targetBranchName == null) {
- return 0;
- }
+ private int writeChangedLines(ScmProvider provider, ScannerReportWriter writer, String targetScmBranch) {
Path rootBaseDir = inputModuleHierarchy.root().getBaseDir();
Map<Path, DefaultInputFile> changedFiles = StreamSupport.stream(inputComponentStore.allChangedFilesToPublish().spliterator(), false)
.collect(Collectors.toMap(DefaultInputFile::path, f -> f));
- Map<Path, Set<Integer>> pathSetMap = provider.branchChangedLines(targetBranchName, rootBaseDir, changedFiles.keySet());
+ Map<Path, Set<Integer>> pathSetMap = provider.branchChangedLines(targetScmBranch, rootBaseDir, changedFiles.keySet());
int count = 0;
if (pathSetMap == null) {
builder.setBranchName(branchConfiguration.branchName());
BranchType branchType = toProtobufBranchType(branchConfiguration.branchType());
builder.setBranchType(branchType);
- String branchTarget = branchConfiguration.branchTarget();
- if (branchTarget != null) {
- builder.setMergeBranchName(branchTarget);
+ String referenceBranch = branchConfiguration.longLivingSonarReferenceBranch();
+ if (referenceBranch != null) {
+ builder.setMergeBranchName(referenceBranch);
}
if (branchType == BranchType.PULL_REQUEST) {
builder.setPullRequestKey(branchConfiguration.pullRequestKey());
if (project == null) {
boolean isIssuesMode = mode.isIssues();
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- project = loader.load(projectKey.get(), isIssuesMode, branchConfig.branchBase());
+ project = loader.load(projectKey.get(), isIssuesMode, branchConfig.longLivingSonarReferenceBranch());
checkProject(isIssuesMode);
profiler.stopInfo();
}
BranchConfiguration branchConfig = getComponentByType(BranchConfiguration.class);
if (branchConfig.branchType() == BranchType.PULL_REQUEST) {
- LOG.info("Pull request {} for merge into {} from {}", branchConfig.pullRequestKey(), pullRequestBaseToDisplayName(branchConfig.branchBase()), branchConfig.branchName());
+ LOG.info("Pull request {} for merge into {} from {}", branchConfig.pullRequestKey(), pullRequestBaseToDisplayName(branchConfig.targetScmBranch()), branchConfig.branchName());
} else if (branchConfig.branchName() != null) {
LOG.info("Branch name: {}, type: {}", branchConfig.branchName(), branchTypeToDisplayName(branchConfig.branchType()));
}
}
/**
- * The name of the branch.
+ * For long/short living branches, this is the value of sonar.branch.name, and fallback on the default branch name configured in SQ
+ * For PR: the name of the branch containing PR changes (sonar.pullrequest.branch)
+ * Only @null if the branch feature is not available.
*/
@CheckForNull
String branchName();
/**
- * The name of the target branch to merge into.
+ * The long living server branch from which we should load project settings/quality profiles/compare changed files/...
+ * For long living branches, this is the sonar.branch.target (default to default branch) in case of first analysis,
+ * otherwise it's the branch itself.
+ * For short living branches, we look at sonar.branch.target (default to default branch). If it exists but is a short living branch or PR, we will
+ * transitively use its own target.
+ * For PR, we look at sonar.pullrequest.base (default to default branch). If it exists but is a short living branch or PR, we will
+ * transitively use its own target.
+ * Only @null if the branch feature is not available.
*/
@CheckForNull
- String branchTarget();
+ String longLivingSonarReferenceBranch();
/**
- * The name of the base branch to determine project repository and changed files.
- *
- * Note: this is important for the scanner during the analysis of long living branches.
- * For short living branches, branchBase is always the same as branchTarget.
- * For long living branches, branchBase is the target in case of first analysis,
- * otherwise it's the branch itself.
+ * Raw value of sonar.branch.target or sonar.pullrequest.base (fallback to the default branch), will be used by the SCM to compute changed files and changed lines.
+ * @null for long living branches and if the branch feature is not available
*/
@CheckForNull
- String branchBase();
+ String targetScmBranch();
/**
* The key of the pull request.
* @throws IllegalStateException if this branch configuration is not a pull request.
*/
String pullRequestKey();
+
}
@CheckForNull
@Override
- public String branchTarget() {
+ public String targetScmBranch() {
return null;
}
@CheckForNull
@Override
- public String branchBase() {
+ public String longLivingSonarReferenceBranch() {
return null;
}
@CheckForNull
private static Collection<Path> loadChangedFilesIfNeeded(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, Path rootBaseDir) {
- if (branchConfiguration.isShortOrPullRequest() && branchConfiguration.branchTarget() != null) {
+ if (branchConfiguration.isShortOrPullRequest() && branchConfiguration.targetScmBranch() != null) {
ScmProvider scmProvider = scmConfiguration.provider();
if (scmProvider != null) {
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
- Collection<Path> changedFiles = scmProvider.branchChangedFiles(branchConfiguration.branchTarget(), rootBaseDir);
+ Collection<Path> changedFiles = scmProvider.branchChangedFiles(branchConfiguration.targetScmBranch(), rootBaseDir);
profiler.stopInfo();
if (changedFiles != null) {
LOG.debug("SCM reported {} {} changed in the branch", changedFiles.size(), ScannerUtils.pluralize("file", changedFiles.size()));
import org.sonar.batch.bootstrapper.Batch;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.batch.bootstrapper.LogOutput;
-import org.sonar.core.config.ScannerProperties;
import org.sonar.scanner.bootstrap.GlobalAnalysisMode;
import org.sonar.scanner.issue.tracking.ServerLineHashesLoader;
import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue;
-import org.sonar.scanner.report.ReportPublisher;
import org.sonar.scanner.repository.FileData;
import org.sonar.scanner.repository.MetricsRepository;
import org.sonar.scanner.repository.MetricsRepositoryLoader;
private BranchType branchType = BranchType.LONG;
private String branchName = null;
private String branchTarget = null;
- private String branchBase = null;
+ private String longLivingSonarReferenceBranch = null;
@Override
public BranchType branchType() {
@CheckForNull
@Override
- public String branchTarget() {
+ public String targetScmBranch() {
return branchTarget;
}
@CheckForNull
@Override
- public String branchBase() {
- return branchBase;
+ public String longLivingSonarReferenceBranch() {
+ return longLivingSonarReferenceBranch;
}
@Override
return this;
}
+ public ScannerMediumTester setLongLivingSonarReferenceBranch(String longLivingSonarReferenceBranch) {
+ this.branchConfiguration.longLivingSonarReferenceBranch = longLivingSonarReferenceBranch;
+ return this;
+ }
+
private class FakeBranchConfigurationLoader implements BranchConfigurationLoader {
@Override
public BranchConfiguration load(Map<String, String> localSettings, Supplier<Map<String, String>> settingsSupplier, ProjectBranches branches, ProjectPullRequests pullRequests) {
TaskResult result = getResult(tester
.setBranchName(branchName)
.setBranchTarget(branchTarget)
+ .setLongLivingSonarReferenceBranch(branchTarget)
.setBranchType(BranchType.SHORT));
ScannerReport.Metadata metadata = result.getReportReader().readMetadata();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(scmConfiguration.isDisabled()).thenReturn(false);
when(scmConfiguration.provider()).thenReturn(provider);
- when(branchConfiguration.branchTarget()).thenReturn(TARGET_BRANCH);
+ when(branchConfiguration.targetScmBranch()).thenReturn(TARGET_BRANCH);
DefaultInputModule root = mock(DefaultInputModule.class);
when(root.getBaseDir()).thenReturn(BASE_DIR);
when(inputModuleHierarchy.root()).thenReturn(root);
@Test
public void skip_if_target_branch_is_null() {
- when(branchConfiguration.branchTarget()).thenReturn(null);
+ when(branchConfiguration.targetScmBranch()).thenReturn(null);
publisher.publish(writer);
verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
assertNotPublished();
import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.scm.ScmProvider;
-import org.sonar.api.config.internal.MapSettings;
-import org.sonar.core.config.ScannerProperties;
import org.sonar.scanner.ProjectAnalysisInfo;
import org.sonar.scanner.bootstrap.ScannerPlugin;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
String branchName = "feature";
String branchTarget = "short-lived";
when(branches.branchName()).thenReturn(branchName);
- when(branches.branchTarget()).thenReturn(branchTarget);
+ when(branches.longLivingSonarReferenceBranch()).thenReturn(branchTarget);
File outputDir = temp.newFolder();
underTest.publish(new ScannerReportWriter(outputDir));
public void should_return_default_if_no_loader() {
BranchConfiguration result = provider.provide(null, globalConfiguration, reactor, settingsLoader, branches, pullRequests);
- assertThat(result.branchTarget()).isNull();
+ assertThat(result.targetScmBranch()).isNull();
assertThat(result.branchType()).isEqualTo(BranchType.LONG);
}
}
@Test
public void testNoScmProvider() {
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
- when(branchConfiguration.branchTarget()).thenReturn("target");
+ when(branchConfiguration.targetScmBranch()).thenReturn("target");
ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy);
@Test
public void testFailIfRelativePath() {
- when(branchConfiguration.branchTarget()).thenReturn("target");
+ when(branchConfiguration.targetScmBranch()).thenReturn("target");
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(scmConfiguration.provider()).thenReturn(scmProvider);
when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(Collections.singleton(Paths.get("changedFile")));
@Test
public void testProviderDoesntSupport() {
- when(branchConfiguration.branchTarget()).thenReturn("target");
+ when(branchConfiguration.targetScmBranch()).thenReturn("target");
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(scmConfiguration.provider()).thenReturn(scmProvider);
when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(null);
when(scmConfiguration.provider()).thenReturn(legacy);
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
- when(branchConfiguration.branchTarget()).thenReturn("target");
+ when(branchConfiguration.targetScmBranch()).thenReturn("target");
ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, inputModuleHierarchy);
@Test
public void testReturnChangedFiles() {
- when(branchConfiguration.branchTarget()).thenReturn("target");
+ when(branchConfiguration.targetScmBranch()).thenReturn("target");
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(scmConfiguration.provider()).thenReturn(scmProvider);
when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(Collections.singleton(Paths.get("changedFile").toAbsolutePath()));