diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2020-12-14 15:09:06 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-12-15 20:07:25 +0000 |
commit | 316c95e86b0215f99d313e56964659cb3a25ece5 (patch) | |
tree | 9fcfddce1c0e516159f4d840ed8ba2e6600348fe /sonar-scanner-engine | |
parent | 2ed2e647067ffa08496c18d3541b213009d57a40 (diff) | |
download | sonarqube-316c95e86b0215f99d313e56964659cb3a25ece5.tar.gz sonarqube-316c95e86b0215f99d313e56964659cb3a25ece5.zip |
SONAR-13586 Specify target pull request with the remote name
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java | 21 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java | 32 |
2 files changed, 36 insertions, 17 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java index 19b81e1642a..e1ad2ca4655 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java @@ -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; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java index 7f62b8912fa..e4ddcab275c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java @@ -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,11 +527,29 @@ 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); } |