]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11365 Fix PR on PR
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 16 Oct 2018 12:49:12 +0000 (14:49 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 16 Oct 2018 14:41:53 +0000 (16:41 +0200)
13 files changed:
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositoriesProvider.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmChangedFilesProvider.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java

index 5a60eeb52903525e2dd1daa14a42387e071dd33d..c70841cd4e8ebcf96d7778d0f9db730ccd9beb4f 100644 (file)
@@ -56,7 +56,8 @@ public class ChangedLinesPublisher implements ReportPublisherStep {
   }
 
   @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;
     }
 
@@ -66,22 +67,18 @@ public class ChangedLinesPublisher implements ReportPublisherStep {
     }
 
     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) {
index 174ea26d78a9f57d995a9f2d9ec394a2fa8c97cb..43c23eabfe465e62899d21a1d56f8a064f19e783 100644 (file)
@@ -134,9 +134,9 @@ public class MetadataPublisher implements ReportPublisherStep {
     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());
index 4d5cc21c4676de82366452ea15542a146d327f8a..fb993d4b344de9a63d3af5f5fb7c2e9f2695fbcf 100644 (file)
@@ -36,7 +36,7 @@ public class ProjectRepositoriesProvider extends ProviderAdapter {
     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();
     }
index e3e313974fceab622deda26f9ed250d404fb2bf6..d3eecc84024ef08aaf23f4b5166bf3c829fcbd40 100644 (file)
@@ -279,7 +279,7 @@ public class ProjectScanContainer extends ComponentContainer {
 
     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()));
     }
index 695c3c6d62d8573e91c4dcfe9c13521365e3a196..783b9abb21c6d665e2ea2508bf0c4bdb5184a750 100644 (file)
@@ -41,27 +41,32 @@ public interface BranchConfiguration {
   }
 
   /**
-   * 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.
@@ -69,4 +74,5 @@ public interface BranchConfiguration {
    * @throws IllegalStateException if this branch configuration is not a pull request.
    */
   String pullRequestKey();
+
 }
index 2e048e6c68907133fb6f849481f6e2a50b48c2d4..93dd2652b3232fe77622747de95bbb427ff2d408 100644 (file)
@@ -37,13 +37,13 @@ public class DefaultBranchConfiguration implements BranchConfiguration {
 
   @CheckForNull
   @Override
-  public String branchTarget() {
+  public String targetScmBranch() {
     return null;
   }
 
   @CheckForNull
   @Override
-  public String branchBase() {
+  public String longLivingSonarReferenceBranch() {
     return null;
   }
 
index af13e28575cedd5601ac9b78ac31595201a5a9b1..df8710e49737f87ca7e9ffe56c639965171df882 100644 (file)
@@ -63,11 +63,11 @@ public class ScmChangedFilesProvider extends ProviderAdapter {
 
   @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()));
index bae2b4a81f776f0407336f2ce1351f608ed2d2ff..db8b4fec76f8cf583ada3fe5bc33d3cfb4733849 100644 (file)
@@ -52,11 +52,9 @@ import org.sonar.api.utils.DateUtils;
 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;
@@ -397,7 +395,7 @@ public class ScannerMediumTester extends ExternalResource {
     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() {
@@ -412,14 +410,14 @@ public class ScannerMediumTester extends ExternalResource {
 
     @CheckForNull
     @Override
-    public String branchTarget() {
+    public String targetScmBranch() {
       return branchTarget;
     }
 
     @CheckForNull
     @Override
-    public String branchBase() {
-      return branchBase;
+    public String longLivingSonarReferenceBranch() {
+      return longLivingSonarReferenceBranch;
     }
 
     @Override
@@ -443,6 +441,11 @@ public class ScannerMediumTester extends ExternalResource {
     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) {
index efc99a743f835c1e920a178084289f8559f154ec..9a6f06a1f677267f889320ec6cf3ceeb3e8b26b3 100644 (file)
@@ -95,6 +95,7 @@ public class BranchMediumTest {
     TaskResult result = getResult(tester
       .setBranchName(branchName)
       .setBranchTarget(branchTarget)
+      .setLongLivingSonarReferenceBranch(branchTarget)
       .setBranchType(BranchType.SHORT));
 
     ScannerReport.Metadata metadata = result.getReportReader().readMetadata();
index 6bb328a02226ad2a95b8e364aacf3cc3cafdb4ff..05015007559fcaefbc33a53ce3aa428fcaef8517 100644 (file)
@@ -68,7 +68,7 @@ public class ChangedLinesPublisherTest {
     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);
@@ -92,7 +92,7 @@ public class ChangedLinesPublisherTest {
 
   @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();
index 46290e6a4bf558ca71857061a9fb6d2ae28aefbc..e0d734856569da3f969fd30a65becb3738541a71 100644 (file)
@@ -36,8 +36,6 @@ import org.sonar.api.batch.fs.internal.DefaultInputModule;
 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;
@@ -187,7 +185,7 @@ public class MetadataPublisherTest {
     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));
index ba55c21b7c61cfa0f9b1b3da4d91d88f8929d932..a3f1db882b3179c1ca7cb47ae8839c4bc241b515 100644 (file)
@@ -98,7 +98,7 @@ public class BranchConfigurationProviderTest {
   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);
   }
 }
index bc212aa9489549390a573d8d8d71ad6bb700f449..170a4fd5b788bbf4fe4e2f728b0c440d5c20e3d2 100644 (file)
@@ -67,7 +67,7 @@ public class ScmChangedFilesProviderTest {
   @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);
 
@@ -77,7 +77,7 @@ public class ScmChangedFilesProviderTest {
 
   @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")));
@@ -89,7 +89,7 @@ public class ScmChangedFilesProviderTest {
 
   @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);
@@ -119,7 +119,7 @@ public class ScmChangedFilesProviderTest {
 
     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);
 
@@ -129,7 +129,7 @@ public class ScmChangedFilesProviderTest {
 
   @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()));