Browse Source

SONAR-12624 Scanner no longer supports short living branches

tags/8.1.0.31237
Duarte Meneses 4 years ago
parent
commit
1beafb8282
26 changed files with 81 additions and 148 deletions
  1. 1
    2
      server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java
  2. 1
    1
      sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java
  3. 4
    6
      sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java
  4. 5
    20
      sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java
  5. 1
    12
      sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java
  6. 2
    2
      sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java
  7. 1
    1
      sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java
  8. 1
    1
      sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java
  9. 1
    1
      sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
  10. 1
    1
      sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java
  11. 1
    1
      sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java
  12. 1
    1
      sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
  13. 2
    2
      sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
  14. 8
    8
      sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
  15. 9
    9
      sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/log/LogListenerTest.java
  16. 2
    2
      sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
  17. 1
    1
      sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java
  18. 10
    25
      sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
  19. 5
    25
      sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java
  20. 1
    1
      sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java
  21. 5
    7
      sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java
  22. 5
    6
      sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesTest.java
  23. 7
    7
      sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java
  24. 4
    4
      sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
  25. 1
    2
      sonar-scanner-protocol/src/main/protobuf/scanner_report.proto
  26. 1
    0
      sonar-ws/src/main/protobuf/ws-commons.proto

+ 1
- 2
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java View File

@@ -153,8 +153,7 @@ public class TaskFormatter {
Common.BranchType branchType = componentDtoCache.getBranchType(taskUuid)
.orElseThrow(() -> new IllegalStateException(format("Could not find branch type of task '%s'", taskUuid)));
switch (branchType) {
case LONG:
case SHORT:
case BRANCH:
builder.setBranchType(branchType);
builder.setBranch(b);
break;

+ 1
- 1
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java View File

@@ -58,7 +58,7 @@ public class ChangedLinesPublisher implements ReportPublisherStep {
@Override
public void publish(ScannerReportWriter writer) {
String targetBranchName = branchConfiguration.targetBranchName();
if (scmConfiguration.isDisabled() || !branchConfiguration.isShortOrPullRequest() || targetBranchName == null) {
if (scmConfiguration.isDisabled() || !branchConfiguration.isPullRequest() || targetBranchName == null) {
return;
}


+ 4
- 6
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java View File

@@ -25,6 +25,8 @@ import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.scm.ScmProvider;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
@@ -32,8 +34,6 @@ import org.sonar.scanner.ProjectInfo;
import org.sonar.scanner.bootstrap.ScannerPlugin;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.cpd.CpdSettings;
import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.scanner.fs.InputModuleHierarchy;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.Metadata.BranchType;
@@ -173,10 +173,8 @@ public class MetadataPublisher implements ReportPublisherStep {
if (branchType == org.sonar.scanner.scan.branch.BranchType.PULL_REQUEST) {
return BranchType.PULL_REQUEST;
}
if (branchType == org.sonar.scanner.scan.branch.BranchType.LONG) {
return BranchType.LONG;
}
return BranchType.SHORT;

return BranchType.BRANCH;
}

private static String toSonarQubePath(Path path) {

+ 5
- 20
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java View File

@@ -47,6 +47,7 @@ import org.sonar.scanner.protocol.output.ScannerReportReader;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.scan.ScanProperties;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.branch.BranchType;
import org.sonarqube.ws.Ce;
import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.client.HttpException;
@@ -57,9 +58,7 @@ import static java.net.URLEncoder.encode;
import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.sonar.core.util.FileUtils.deleteQuietly;
import static org.sonar.scanner.scan.branch.BranchType.LONG;
import static org.sonar.scanner.scan.branch.BranchType.PULL_REQUEST;
import static org.sonar.scanner.scan.branch.BranchType.SHORT;

public class ReportPublisher implements Startable {

@@ -254,7 +253,7 @@ public class ReportPublisher implements Startable {
.url();
}

if (onLongLivingBranch(branchConfiguration)) {
if (onBranch(branchConfiguration)) {
return httpUrl.newBuilder()
.addPathSegment(DASHBOARD)
.addEncodedQueryParameter(ID, encoded(effectiveKey))
@@ -263,16 +262,6 @@ public class ReportPublisher implements Startable {
.url();
}

if (onShortLivingBranch(branchConfiguration)) {
return httpUrl.newBuilder()
.addPathSegment(DASHBOARD)
.addEncodedQueryParameter(ID, encoded(effectiveKey))
.addEncodedQueryParameter(BRANCH, encoded(branchConfiguration.branchName()))
.addQueryParameter(RESOLVED, "false")
.build()
.url();
}

if (onMainBranch(branchConfiguration)) {
return httpUrl.newBuilder()
.addPathSegment(DASHBOARD)
@@ -288,12 +277,8 @@ public class ReportPublisher implements Startable {
return branchConfiguration.branchName() != null && (branchConfiguration.branchType() == PULL_REQUEST);
}

private static boolean onShortLivingBranch(BranchConfiguration branchConfiguration) {
return branchConfiguration.branchName() != null && (branchConfiguration.branchType() == SHORT);
}

private static boolean onLongLivingBranch(BranchConfiguration branchConfiguration) {
return branchConfiguration.branchName() != null && (branchConfiguration.branchType() == LONG);
private static boolean onBranch(BranchConfiguration branchConfiguration) {
return branchConfiguration.branchName() != null && (branchConfiguration.branchType() == BranchType.BRANCH);
}

private static boolean onMainBranch(BranchConfiguration branchConfiguration) {
@@ -305,7 +290,7 @@ public class ReportPublisher implements Startable {
return EMPTY;
}
try {
return encode(queryParameter, "UTF-8");
return encode(queryParameter, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Unable to urlencode " + queryParameter, e);
}

+ 1
- 12
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java View File

@@ -340,7 +340,7 @@ public class ProjectScanContainer extends ComponentContainer {
LOG.info("Pull request {} for merge into {} from {}", branchConfig.pullRequestKey(), pullRequestBaseToDisplayName(branchConfig.targetBranchName()),
branchConfig.branchName());
} else if (branchConfig.branchName() != null) {
LOG.info("Branch name: {}, type: {}", branchConfig.branchName(), branchTypeToDisplayName(branchConfig.branchType()));
LOG.info("Branch name: {}", branchConfig.branchName());
}

getComponentByType(ProjectFileIndexer.class).index();
@@ -374,17 +374,6 @@ public class ProjectScanContainer extends ComponentContainer {
return pullRequestBase != null ? pullRequestBase : "default branch";
}

private static String branchTypeToDisplayName(BranchType branchType) {
switch (branchType) {
case LONG:
return "long living";
case SHORT:
return "short living";
default:
throw new UnsupportedOperationException("unknown branch type: " + branchType);
}
}

private void scanRecursively(InputModuleHierarchy tree, DefaultInputModule module) {
for (DefaultInputModule child : tree.children(module)) {
scanRecursively(tree, child);

+ 2
- 2
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java View File

@@ -35,8 +35,8 @@ public interface BranchConfiguration {
*/
BranchType branchType();

default boolean isShortOrPullRequest() {
return branchType() == BranchType.PULL_REQUEST || branchType() == BranchType.SHORT;
default boolean isPullRequest() {
return branchType() == BranchType.PULL_REQUEST;
}

/**

+ 1
- 1
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java View File

@@ -20,5 +20,5 @@
package org.sonar.scanner.scan.branch;

public enum BranchType {
SHORT, LONG, PULL_REQUEST
BRANCH, PULL_REQUEST
}

+ 1
- 1
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java View File

@@ -26,7 +26,7 @@ import javax.annotation.concurrent.Immutable;
public class DefaultBranchConfiguration implements BranchConfiguration {
@Override
public BranchType branchType() {
return BranchType.LONG;
return BranchType.BRANCH;
}

@CheckForNull

+ 1
- 1
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java View File

@@ -79,7 +79,7 @@ public class InputComponentStore extends DefaultFileSystem.Cache {

public Iterable<DefaultInputFile> allChangedFilesToPublish() {
return allFilesToPublishStream()
.filter(f -> !branchConfiguration.isShortOrPullRequest() || f.status() != InputFile.Status.SAME)::iterator;
.filter(f -> !branchConfiguration.isPullRequest() || f.status() != InputFile.Status.SAME)::iterator;
}

@Override

+ 1
- 1
sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java View File

@@ -56,7 +56,7 @@ public class ScmChangedFilesProvider extends ProviderAdapter {
@CheckForNull
private static Collection<Path> loadChangedFilesIfNeeded(ScmConfiguration scmConfiguration, BranchConfiguration branchConfiguration, Path rootBaseDir) {
final String targetBranchName = branchConfiguration.targetBranchName();
if (branchConfiguration.isShortOrPullRequest() && targetBranchName != null) {
if (branchConfiguration.isPullRequest() && targetBranchName != null) {
ScmProvider scmProvider = scmConfiguration.provider();
if (scmProvider != null) {
Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);

+ 1
- 1
sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java View File

@@ -98,7 +98,7 @@ public final class ScmPublisher {
for (DefaultInputFile f : componentStore.allFilesToPublish()) {
if (configuration.forceReloadAll() || f.status() != Status.SAME) {
addIfNotEmpty(filesToBlame, f);
} else if (!branchConfiguration.isShortOrPullRequest()) {
} else if (!branchConfiguration.isPullRequest()) {
FileData fileData = projectRepositoriesSupplier.get().fileData(componentStore.findModule(f).key(), f);
if (fileData == null || StringUtils.isEmpty(fileData.revision())) {
addIfNotEmpty(filesToBlame, f);

+ 1
- 1
sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java View File

@@ -214,7 +214,7 @@ public class DefaultSensorStorage implements SensorStorage {
}

private boolean shouldSkipStorage(DefaultInputFile defaultInputFile) {
return branchConfiguration.isShortOrPullRequest() && defaultInputFile.status() == InputFile.Status.SAME;
return branchConfiguration.isPullRequest() && defaultInputFile.status() == InputFile.Status.SAME;
}

/**

+ 2
- 2
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java View File

@@ -380,7 +380,7 @@ public class ScannerMediumTester extends ExternalResource {

private static class FakeBranchConfiguration implements BranchConfiguration {

private BranchType branchType = BranchType.LONG;
private BranchType branchType = BranchType.BRANCH;
private String branchName = null;
private String branchTarget = null;
private String longLivingSonarReferenceBranch = null;
@@ -410,7 +410,7 @@ public class ScannerMediumTester extends ExternalResource {

@Override
public String pullRequestKey() {
throw new UnsupportedOperationException();
return "1'";
}
}


+ 8
- 8
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java View File

@@ -75,7 +75,7 @@ public class BranchMediumTest {
}

@Test
public void should_not_skip_report_for_unchanged_files_in_short_branch() {
public void should_not_skip_report_for_unchanged_files_in_pr() {
// sanity check, normally report gets generated
AnalysisResult result = getResult(tester);
final DefaultInputFile file = (DefaultInputFile) result.inputFile(FILE_PATH);
@@ -85,11 +85,11 @@ public class BranchMediumTest {
assertThat(result.getReportReader().hasCoverage(fileId)).isTrue();
assertThat(result.getReportReader().readFileSource(fileId)).isNotNull();

// file is not skipped for short branches (need coverage, duplications coming soon)
AnalysisResult result2 = getResult(tester.setBranchType(BranchType.SHORT));
final DefaultInputFile fileOnShortBranch = (DefaultInputFile) result2.inputFile(FILE_PATH);
assertThat(result2.getReportComponent(fileOnShortBranch)).isNotNull();
fileId = fileOnShortBranch.scannerId();
// file is not skipped for pull requests (need coverage, duplications coming soon)
AnalysisResult result2 = getResult(tester.setBranchType(BranchType.PULL_REQUEST));
final DefaultInputFile fileInPr = (DefaultInputFile) result2.inputFile(FILE_PATH);
assertThat(result2.getReportComponent(fileInPr)).isNotNull();
fileId = fileInPr.scannerId();
assertThat(result2.getReportReader().readChangesets(fileId)).isNull();
assertThat(result2.getReportReader().hasCoverage(fileId)).isTrue();
assertThat(result2.getReportReader().readFileSource(fileId)).isNull();
@@ -104,11 +104,11 @@ public class BranchMediumTest {
.setBranchName(branchName)
.setBranchTarget(branchTarget)
.setLongLivingSonarReferenceBranch(branchTarget)
.setBranchType(BranchType.SHORT));
.setBranchType(BranchType.BRANCH));

ScannerReport.Metadata metadata = result.getReportReader().readMetadata();
assertThat(metadata.getBranchName()).isEqualTo(branchName);
assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.SHORT);
assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.BRANCH);
assertThat(metadata.getMergeBranchName()).isEqualTo(branchTarget);
}


+ 9
- 9
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/log/LogListenerTest.java View File

@@ -24,6 +24,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -67,7 +68,6 @@ public class LogListenerTest {
.setLogOutput(new SimpleLogListener());

private File baseDir;

private ImmutableMap.Builder<String, String> builder;

@BeforeClass
@@ -87,13 +87,13 @@ public class LogListenerTest {
}

@Before
public void prepare() throws IOException {
public void prepare() {
stdOutTarget = new ByteArrayOutputStream();
stdErrTarget = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOutTarget));
System.setErr(new PrintStream(stdErrTarget));
// logger from the batch might write to it asynchronously
logOutput = Collections.synchronizedList(new LinkedList<LogEvent>());
logOutput = Collections.synchronizedList(new LinkedList<>());
logOutputStr = new StringBuilder();

baseDir = temp.getRoot();
@@ -130,12 +130,12 @@ public class LogListenerTest {
}

@Test
public void testChangeLogForAnalysis() throws IOException, InterruptedException {
public void testChangeLogForAnalysis() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();

File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "Sample xoo\ncontent");
FileUtils.write(xooFile, "Sample xoo\ncontent", StandardCharsets.UTF_8);

tester.newAnalysis()
.properties(builder
@@ -158,7 +158,7 @@ public class LogListenerTest {
srcDir.mkdir();

File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "Sample xoo\ncontent");
FileUtils.write(xooFile, "Sample xoo\ncontent", StandardCharsets.UTF_8);

tester.newAnalysis()
.properties(builder
@@ -182,7 +182,7 @@ public class LogListenerTest {
srcDir.mkdir();

File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "Sample xoo\ncontent");
FileUtils.write(xooFile, "Sample xoo\ncontent", StandardCharsets.UTF_8);

tester.newAnalysis()
.properties(builder
@@ -209,7 +209,7 @@ public class LogListenerTest {
File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "Sample xoo\ncontent");
File xooFileMeasure = new File(srcDir, "sample.xoo.measures");
FileUtils.write(xooFileMeasure, "foo:bar");
FileUtils.write(xooFileMeasure, "foo:bar", StandardCharsets.UTF_8);

try {
tester.newAnalysis()
@@ -228,7 +228,7 @@ public class LogListenerTest {
for (LogEvent e : logOutput) {
if (e.level == Level.ERROR) {
assertThat(e.msg).contains("Error processing line 1 of file", "src" + File.separator + "sample.xoo.measures",
"java.lang.IllegalStateException: Unknow metric with key: foo",
"java.lang.IllegalStateException: Unknown metric with key: foo",
"at org.sonar.xoo.lang.MeasureSensor.saveMeasure");

}

+ 2
- 2
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java View File

@@ -66,7 +66,7 @@ public class ChangedLinesPublisherTest {
@Before
public void setUp() {
writer = new ScannerReportWriter(temp.getRoot());
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);
when(scmConfiguration.isDisabled()).thenReturn(false);
when(scmConfiguration.provider()).thenReturn(provider);
when(branchConfiguration.targetBranchName()).thenReturn(TARGET_BRANCH);
@@ -83,7 +83,7 @@ public class ChangedLinesPublisherTest {

@Test
public void skip_if_not_pr_or_slb() {
when(branchConfiguration.isShortOrPullRequest()).thenReturn(false);
when(branchConfiguration.isPullRequest()).thenReturn(false);
publisher.publish(writer);
verifyZeroInteractions(inputComponentStore, inputModuleHierarchy, provider);
assertNotPublished();

+ 1
- 1
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java View File

@@ -129,7 +129,7 @@ public class ComponentsPublisherTest {

@Test
public void publish_unchanged_components_even_in_short_branches() throws IOException {
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);
ProjectInfo projectInfo = mock(ProjectInfo.class);
when(projectInfo.getAnalysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));


+ 10
- 25
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java View File

@@ -38,14 +38,14 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.scm.ScmProvider;
import org.sonar.scanner.ProjectInfo;
import org.sonar.scanner.bootstrap.ScannerPlugin;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.cpd.CpdSettings;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.scanner.fs.InputModuleHierarchy;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReportReader;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
@@ -221,28 +221,13 @@ public class MetadataPublisherTest {
}

@Test
public void write_long_lived_branch_info() throws Exception {
String branchName = "long-lived";
when(branches.branchName()).thenReturn(branchName);
when(branches.branchType()).thenReturn(BranchType.LONG);

File outputDir = temp.newFolder();
underTest.publish(new ScannerReportWriter(outputDir));
public void write_branch_info() throws Exception {
String branchName = "name";
String targetName = "target";

ScannerReportReader reader = new ScannerReportReader(outputDir);
ScannerReport.Metadata metadata = reader.readMetadata();
assertThat(metadata.getBranchName()).isEqualTo(branchName);
assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.LONG);
}

@Test
public void write_short_lived_branch_info() throws Exception {
String branchName = "feature";
String targetBranchName = "short-lived";
String longLivingSonarReferenceBranch = "long-lived";
when(branches.branchName()).thenReturn(branchName);
when(branches.targetBranchName()).thenReturn(targetBranchName);
when(branches.longLivingSonarReferenceBranch()).thenReturn(longLivingSonarReferenceBranch);
when(branches.branchType()).thenReturn(BranchType.BRANCH);
when(branches.targetBranchName()).thenReturn(targetName);

File outputDir = temp.newFolder();
underTest.publish(new ScannerReportWriter(outputDir));
@@ -250,9 +235,9 @@ public class MetadataPublisherTest {
ScannerReportReader reader = new ScannerReportReader(outputDir);
ScannerReport.Metadata metadata = reader.readMetadata();
assertThat(metadata.getBranchName()).isEqualTo(branchName);
assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.SHORT);
assertThat(metadata.getMergeBranchName()).isEqualTo(longLivingSonarReferenceBranch);
assertThat(metadata.getTargetBranchName()).isEqualTo(targetBranchName);
assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.BRANCH);
assertThat(metadata.getMergeBranchName()).isEmpty();
assertThat(metadata.getTargetBranchName()).isEqualTo(targetName);
}

@Test

+ 5
- 25
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java View File

@@ -57,9 +57,8 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.scanner.scan.branch.BranchType.LONG;
import static org.sonar.scanner.scan.branch.BranchType.BRANCH;
import static org.sonar.scanner.scan.branch.BranchType.PULL_REQUEST;
import static org.sonar.scanner.scan.branch.BranchType.SHORT;

public class ReportPublisherTest {

@@ -139,9 +138,9 @@ public class ReportPublisherTest {
}

@Test
public void dump_public_url_if_defined_for_long_living_branches() throws IOException {
public void dump_public_url_if_defined_for_branches() throws IOException {
when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
when(branchConfiguration.branchType()).thenReturn(LONG);
when(branchConfiguration.branchType()).thenReturn(BRANCH);
when(branchConfiguration.branchName()).thenReturn("branch-6.7");
ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class),
new ReportPublisherStep[0], branchConfiguration, reportMetadataHolder);
@@ -157,25 +156,6 @@ public class ReportPublisherTest {
"ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n");
}

@Test
public void dump_public_url_if_defined_for_short_living_branches() throws IOException {
when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
when(branchConfiguration.branchType()).thenReturn(SHORT);
when(branchConfiguration.branchName()).thenReturn("branch-6.7");
ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class),
new ReportPublisherStep[0], branchConfiguration, reportMetadataHolder);

underTest.prepareAndDumpMetadata("TASK-123");

assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo(
"projectKey=org.sonarsource.sonarqube:sonarqube\n" +
"serverUrl=https://publicserver/sonarqube\n" +
"serverVersion=6.4\n" +
"dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7&resolved=false\n" +
"ceTaskId=TASK-123\n" +
"ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n");
}

@Test
public void dump_public_url_if_defined_for_pull_request() throws IOException {
when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
@@ -300,7 +280,7 @@ public class ReportPublisherTest {

String branchName = "feature";
when(branchConfiguration.branchName()).thenReturn(branchName);
when(branchConfiguration.branchType()).thenReturn(SHORT);
when(branchConfiguration.branchType()).thenReturn(BRANCH);

WsResponse response = mock(WsResponse.class);

@@ -323,7 +303,7 @@ public class ReportPublisherTest {
assertThat(wsRequest.getParameters().getValues("organization")).containsExactly(orgName);
assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("org.sonarsource.sonarqube:sonarqube");
assertThat(wsRequest.getParameters().getValues("characteristic"))
.containsExactlyInAnyOrder("branch=" + branchName, "branchType=" + SHORT.name());
.containsExactlyInAnyOrder("branch=" + branchName, "branchType=" + BRANCH.name());
}

@Test

+ 1
- 1
sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java View File

@@ -61,7 +61,7 @@ public class DefaultActiveRulesLoaderTest {
public void setUp() {
wsClient = mock(DefaultScannerWsClient.class);
BranchConfiguration branchConfig = mock(BranchConfiguration.class);
when(branchConfig.isShortOrPullRequest()).thenReturn(false);
when(branchConfig.isPullRequest()).thenReturn(false);
loader = new DefaultActiveRulesLoader(wsClient);
}


+ 5
- 7
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java View File

@@ -29,9 +29,7 @@ import org.mockito.Captor;
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.scanner.bootstrap.GlobalServerSettings;
import org.sonar.scanner.scan.ProjectConfiguration;
import org.sonar.scanner.scan.ProjectServerSettings;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
@@ -45,11 +43,11 @@ public class BranchConfigurationProviderTest {
private BranchConfiguration config = mock(BranchConfiguration.class);
private ProjectBranches branches = mock(ProjectBranches.class);
private ProjectPullRequests pullRequests = mock(ProjectPullRequests.class);
private ProjectReactor reactor = mock(ProjectReactor.class);;
private Map<String, String> projectSettings = new HashMap<>();;
private ProjectReactor reactor = mock(ProjectReactor.class);
;
private Map<String, String> projectSettings = new HashMap<>();
;
private ProjectDefinition root = mock(ProjectDefinition.class);
private GlobalServerSettings globalServerSettings = mock(GlobalServerSettings.class);
private ProjectServerSettings projectServerSettings = mock(ProjectServerSettings.class);

@Captor
private ArgumentCaptor<Supplier<Map<String, String>>> settingsCaptor;
@@ -81,6 +79,6 @@ public class BranchConfigurationProviderTest {
BranchConfiguration result = provider.provide(null, projectConfiguration, branches, pullRequests);

assertThat(result.targetBranchName()).isNull();
assertThat(result.branchType()).isEqualTo(BranchType.LONG);
assertThat(result.branchType()).isEqualTo(BranchType.BRANCH);
}
}

+ 5
- 6
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesTest.java View File

@@ -34,14 +34,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(DataProviderRunner.class)
public class ProjectBranchesTest {

private static final BranchInfo mainBranch = new BranchInfo("main", BranchType.LONG, true, null);
private static final BranchInfo shortBranch = new BranchInfo("short", BranchType.SHORT, false, null);
private static final BranchInfo longBranch = new BranchInfo("long", BranchType.LONG, false, null);
private static final BranchInfo mainBranch = new BranchInfo("main", BranchType.BRANCH, true, null);
private static final BranchInfo branch = new BranchInfo("branch", BranchType.BRANCH, false, null);
private static final BranchInfo pullRequest = new BranchInfo("pull-request", BranchType.PULL_REQUEST, false, null);

private static final List<BranchInfo> nonMainBranches = Arrays.asList(shortBranch, longBranch, pullRequest);
private static final List<BranchInfo> nonMainBranches = Arrays.asList(branch, pullRequest);

private static final List<BranchInfo> allBranches = Arrays.asList(shortBranch, longBranch, pullRequest, mainBranch);
private static final List<BranchInfo> allBranches = Arrays.asList(branch, pullRequest, mainBranch);

private final ProjectBranches underTest = new ProjectBranches(allBranches);

@@ -63,7 +62,7 @@ public class ProjectBranchesTest {
@DataProvider
public static Object[][] branchNamesAndBranches() {
return allBranches.stream()
.map(b -> new Object[]{b.name(), b})
.map(b -> new Object[] {b.name(), b})
.toArray(Object[][]::new);
}


+ 7
- 7
sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java View File

@@ -65,7 +65,7 @@ public class ScmChangedFilesProviderTest {

@Test
public void testNoScmProvider() {
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);
when(branchConfiguration.targetBranchName()).thenReturn("target");

ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, project);
@@ -77,7 +77,7 @@ public class ScmChangedFilesProviderTest {
@Test
public void testFailIfRelativePath() {
when(branchConfiguration.targetBranchName()).thenReturn("target");
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);
when(scmConfiguration.provider()).thenReturn(scmProvider);
when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(Collections.singleton(Paths.get("changedFile")));

@@ -89,7 +89,7 @@ public class ScmChangedFilesProviderTest {
@Test
public void testProviderDoesntSupport() {
when(branchConfiguration.targetBranchName()).thenReturn("target");
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);
when(scmConfiguration.provider()).thenReturn(scmProvider);
when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(null);
ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, project);
@@ -100,7 +100,7 @@ public class ScmChangedFilesProviderTest {

@Test
public void testNoOpInNonShortLivedBranch() {
when(branchConfiguration.isShortOrPullRequest()).thenReturn(false);
when(branchConfiguration.isPullRequest()).thenReturn(false);
ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, project);

assertThat(scmChangedFiles.get()).isNull();
@@ -117,7 +117,7 @@ public class ScmChangedFilesProviderTest {
};

when(scmConfiguration.provider()).thenReturn(legacy);
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);
when(branchConfiguration.targetBranchName()).thenReturn("target");

ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, project);
@@ -129,7 +129,7 @@ public class ScmChangedFilesProviderTest {
@Test
public void testReturnChangedFiles() {
when(branchConfiguration.targetBranchName()).thenReturn("target");
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);
when(scmConfiguration.provider()).thenReturn(scmProvider);
when(scmProvider.branchChangedFiles("target", rootBaseDir)).thenReturn(Collections.singleton(Paths.get("changedFile").toAbsolutePath()));
ScmChangedFiles scmChangedFiles = provider.provide(scmConfiguration, branchConfiguration, project);
@@ -142,7 +142,7 @@ public class ScmChangedFilesProviderTest {
public void testCacheObject() {
provider.provide(scmConfiguration, branchConfiguration, project);
provider.provide(scmConfiguration, branchConfiguration, project);
verify(branchConfiguration).isShortOrPullRequest();
verify(branchConfiguration).isPullRequest();
}

}

+ 4
- 4
sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java View File

@@ -175,7 +175,7 @@ public class DefaultSensorStorageTest {
@Test
public void should_skip_issue_on_short_branch_when_file_status_is_SAME() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);

DefaultIssue issue = new DefaultIssue(project).at(new DefaultIssueLocation().on(file));
underTest.store(issue);
@@ -199,7 +199,7 @@ public class DefaultSensorStorageTest {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php")
.setContents("// comment")
.setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);

DefaultHighlighting highlighting = new DefaultHighlighting(underTest).onFile(file).highlight(0, 1, TypeOfText.KEYWORD);
underTest.store(highlighting);
@@ -225,7 +225,7 @@ public class DefaultSensorStorageTest {
@Test
public void should_not_skip_file_measures_on_short_lived_branch_or_pull_request_when_file_status_is_SAME() {
DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);

underTest.store(new DefaultMeasure()
.on(file)
@@ -243,7 +243,7 @@ public class DefaultSensorStorageTest {
.setStatus(InputFile.Status.SAME)
.setContents("foo")
.build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
when(branchConfiguration.isPullRequest()).thenReturn(true);

underTest.store(new DefaultSignificantCode()
.onFile(file)

+ 1
- 2
sonar-scanner-protocol/src/main/protobuf/scanner_report.proto View File

@@ -70,8 +70,7 @@ message Metadata {

enum BranchType {
UNSET = 0;
LONG = 1;
SHORT = 2;
BRANCH = 1;
PULL_REQUEST = 3;
}
}

+ 1
- 0
sonar-ws/src/main/protobuf/ws-commons.proto View File

@@ -129,6 +129,7 @@ enum BranchType {
LONG = 1;
SHORT = 2;
PULL_REQUEST = 3;
BRANCH = 4;
}

message Organization {

Loading…
Cancel
Save