]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13586 Specify target pull request with the remote name
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 14 Dec 2020 21:09:06 +0000 (15:09 -0600)
committersonartech <sonartech@sonarsource.com>
Tue, 15 Dec 2020 20:07:25 +0000 (20:07 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java
sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java

index 19b81e1642a0104d6a7d9c69d27bf7d9c3a16e98..e1ad2ca465513bbdc6909ab40ae570a3818f3dfc 100644 (file)
@@ -63,7 +63,7 @@ import org.sonar.api.utils.log.Loggers;
 public class GitScmProvider extends ScmProvider {
 
   private static final Logger LOG = Loggers.get(GitScmProvider.class);
-
+  private static final String COULD_NOT_FIND_REF = "Could not find ref '%s' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin";
   private final JGitBlameCommand jgitBlameCommand;
   private final AnalysisWarnings analysisWarnings;
   private final GitIgnoreCommand gitIgnoreCommand;
@@ -103,8 +103,8 @@ public class GitScmProvider extends ScmProvider {
     try (Repository repo = buildRepo(rootBaseDir)) {
       Ref targetRef = resolveTargetRef(targetBranchName, repo);
       if (targetRef == null) {
-        analysisWarnings.addUnique(String.format("Could not find ref '%s' in refs/heads, refs/remotes/upstream or refs/remotes/origin. "
-          + "You may see unexpected issues and changes. "
+        analysisWarnings.addUnique(String.format(COULD_NOT_FIND_REF
+          + "You may see unexpected issues and changes. "
           + "Please make sure to fetch this ref before pull request analysis.", targetBranchName));
         return null;
       }
@@ -147,8 +147,8 @@ public class GitScmProvider extends ScmProvider {
     try (Repository repo = buildRepo(projectBaseDir)) {
       Ref targetRef = resolveTargetRef(targetBranchName, repo);
       if (targetRef == null) {
-        analysisWarnings.addUnique(String.format("Could not find ref '%s' in refs/heads, refs/remotes/upstream or refs/remotes/origin. "
-          + "You may see unexpected issues and changes. "
+        analysisWarnings.addUnique(String.format(COULD_NOT_FIND_REF
+          + "You may see unexpected issues and changes. "
           + "Please make sure to fetch this ref before pull request analysis.", targetBranchName));
         return null;
       }
@@ -240,20 +240,21 @@ public class GitScmProvider extends ScmProvider {
   @CheckForNull
   private Ref resolveTargetRef(String targetBranchName, Repository repo) throws IOException {
     String localRef = "refs/heads/" + targetBranchName;
-    String remoteRef = "refs/remotes/origin/" + targetBranchName;
+    String remotesRef = "refs/remotes/" + targetBranchName;
+    String originRef = "refs/remotes/origin/" + targetBranchName;
     String upstreamRef = "refs/remotes/upstream/" + targetBranchName;
 
     Ref targetRef;
     // Because circle ci destroys the local reference to master, try to load remote ref first.
     // https://discuss.circleci.com/t/git-checkout-of-a-branch-destroys-local-reference-to-master/23781
     if (runningOnCircleCI()) {
-      targetRef = getFirstExistingRef(repo, remoteRef, localRef, upstreamRef);
+      targetRef = getFirstExistingRef(repo, originRef, localRef, upstreamRef, remotesRef);
     } else {
-      targetRef = getFirstExistingRef(repo, localRef, remoteRef, upstreamRef);
+      targetRef = getFirstExistingRef(repo, localRef, originRef, upstreamRef, remotesRef);
     }
-    
+
     if (targetRef == null) {
-      LOG.warn("Could not find ref: {} in refs/heads, refs/remotes/upstream or refs/remotes/origin", targetBranchName);
+      LOG.warn(COULD_NOT_FIND_REF, targetBranchName);
     }
 
     return targetRef;
index 7f62b8912fa716cc17ee13f13900a1a44f254361..e4ddcab275c7258d2c3239a047ef9e6e1dbd89f8 100644 (file)
@@ -66,7 +66,7 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
 import static org.sonar.scm.git.Utils.javaUnzip;
 
@@ -468,7 +468,7 @@ public class GitScmProviderTest {
 
     assertThat(newScmProvider().branchChangedFiles("master", worktree2))
       .containsOnly(worktree2.resolve("file-b1"));
-    verifyZeroInteractions(analysisWarnings);
+    verifyNoInteractions(analysisWarnings);
   }
 
   @Test
@@ -489,7 +489,7 @@ public class GitScmProviderTest {
 
     assertThat(newScmProvider().branchChangedFiles("master", worktree2))
       .containsOnly(worktree2.resolve("file-b1"));
-    verifyZeroInteractions(analysisWarnings);
+    verifyNoInteractions(analysisWarnings);
   }
 
   @Test
@@ -509,7 +509,7 @@ public class GitScmProviderTest {
 
     assertThat(newScmProvider().branchChangedFiles("local-only", worktree2))
       .containsOnly(worktree2.resolve("file-b1"));
-    verifyZeroInteractions(analysisWarnings);
+    verifyNoInteractions(analysisWarnings);
   }
 
   @Test
@@ -527,10 +527,28 @@ public class GitScmProviderTest {
 
     assertThat(newScmProvider().branchChangedFiles("master", worktree2))
       .containsOnly(worktree2.resolve("file-b1"));
-    verifyZeroInteractions(analysisWarnings);
+    verifyNoInteractions(analysisWarnings);
 
   }
 
+  @Test
+  public void branchChangedFiles_finds_branch_in_specific_origin() throws IOException, GitAPIException {
+    git.branchCreate().setName("b1").call();
+    git.checkout().setName("b1").call();
+    createAndCommitFile("file-b1");
+
+    Path worktree2 = temp.newFolder().toPath();
+    Git.cloneRepository()
+      .setURI(worktree.toString())
+      .setRemote("upstream")
+      .setDirectory(worktree2.toFile())
+      .call();
+
+    assertThat(newScmProvider().branchChangedFiles("upstream/master", worktree2))
+      .containsOnly(worktree2.resolve("file-b1"));
+    verifyNoInteractions(analysisWarnings);
+  }
+
   @Test
   public void branchChangedFiles_should_return_null_when_branch_nonexistent() {
     assertThat(newScmProvider().branchChangedFiles("nonexistent", worktree)).isNull();
@@ -559,7 +577,7 @@ public class GitScmProviderTest {
       }
     };
     assertThat(provider.branchChangedFiles("branch", worktree)).isNull();
-    verifyZeroInteractions(analysisWarnings);
+    verifyNoInteractions(analysisWarnings);
   }
 
   @Test
@@ -577,7 +595,7 @@ public class GitScmProviderTest {
     };
     assertThat(provider.branchChangedFiles("branch", worktree)).isNull();
 
-    String warning = "Could not find ref 'branch' in refs/heads, refs/remotes/upstream or refs/remotes/origin."
+    String warning = "Could not find ref 'branch' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin."
       + " You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis.";
     verify(analysisWarnings).addUnique(warning);
   }