From cc171357383a97ab97101dcdffda0175c51243f3 Mon Sep 17 00:00:00 2001 From: Michael Clarke Date: Mon, 25 Mar 2019 21:50:58 +0000 Subject: [PATCH] #1: Only throw error if no branches exist and branch parameters specified SonarQube returns an empty branch list if the project doesn't already exist which results in an error being thrown telling the user to re-run the scan with no branch parameters set, even if no branch analysis parameters were specified. However, when a project is created through the Web interface, SonarQube automatically creates a default branch named `master` which the scanner picks up as the primary branch and targets if no other parameters are specified. This inconsistency means users can't have SonarQube automatically create projects, but have to use the Web interface to set-up any projects first. To make the setup consistent, the check for no branches existing is extended to also check if any of the branch or pull request parameters have also been specified. This way, if no branches exist but no branch or pull request parameters were specified then a `DefaultBranchConfiguration` instance is returned which points the analysis at the a default `master` branch which SonarQube will create. --- .../CommunityBranchConfigurationLoader.java | 4 +- ...ommunityBranchConfigurationLoaderTest.java | 57 ++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoader.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoader.java index f0c64e3d4..53cd2f5d6 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoader.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoader.java @@ -53,7 +53,9 @@ public class CommunityBranchConfigurationLoader implements BranchConfigurationLo @Override public BranchConfiguration load(Map localSettings, Supplier> supplier, ProjectBranches projectBranches, ProjectPullRequests projectPullRequests) { - if (projectBranches.isEmpty()) { + if (projectBranches.isEmpty() && + (PULL_REQUEST_ANALYSIS_PARAMETERS.stream().anyMatch(localSettings::containsKey) || + BRANCH_ANALYSIS_PARAMETERS.stream().anyMatch(localSettings::containsKey))) { // it would be nice to identify the 'primary' branch directly, but different projects work differently: using any of master, develop, main etc as primary // A project/global configuration entry could be used to drive this in the future, but the current documented SonarQube parameters need followed for now throw MessageException diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoaderTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoaderTest.java index 28129de56..e59c279c3 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoaderTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/CommunityBranchConfigurationLoaderTest.java @@ -58,16 +58,69 @@ public ExpectedException expectedException() { } @Test - public void testExceptionWhenNoExistingBranch() { + public void testExceptionWhenNoExistingBranchAndBranchParamsPresent() { CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(); ProjectBranches branchInfo = mock(ProjectBranches.class); when(branchInfo.isEmpty()).thenReturn(true); + Map parameters = new HashMap<>(); + parameters.put("sonar.branch.name", "dummy"); + + expectedException.expect(MessageException.class); + expectedException.expectMessage(IsEqual.equalTo( + "No branches currently exist in this project. Please scan the main branch without passing any branch parameters.")); + + testCase.load(parameters, supplier, branchInfo, mock(ProjectPullRequests.class)); + } + + @Test + public void testExceptionWhenNoExistingBranchAndPulRequestParamsPresent() { + CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(); + ProjectBranches branchInfo = mock(ProjectBranches.class); + when(branchInfo.isEmpty()).thenReturn(true); + + Map parameters = new HashMap<>(); + parameters.put("sonar.pullrequest.branch", "dummy2"); + + expectedException.expect(MessageException.class); + expectedException.expectMessage(IsEqual.equalTo( + "No branches currently exist in this project. Please scan the main branch without passing any branch parameters.")); + + testCase.load(parameters, supplier, branchInfo, mock(ProjectPullRequests.class)); + } + + @Test + public void testExceptionWhenNoExistingBranchAndPullRequestAndBranchParametersPresent() { + CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(); + ProjectBranches branchInfo = mock(ProjectBranches.class); + when(branchInfo.isEmpty()).thenReturn(true); + + + Map parameters = new HashMap<>(); + parameters.put("sonar.branch.name", "dummy"); + parameters.put("sonar.pullrequest.branch", "dummy2"); + + expectedException.expect(MessageException.class); expectedException.expectMessage(IsEqual.equalTo( "No branches currently exist in this project. Please scan the main branch without passing any branch parameters.")); - testCase.load(new HashMap<>(), supplier, branchInfo, mock(ProjectPullRequests.class)); + testCase.load(parameters, supplier, branchInfo, mock(ProjectPullRequests.class)); + } + + @Test + public void testDefaultBranchInfoWhenNoBranchParametersSpecifiedAndNoBranchesExist() { + CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(); + + ProjectBranches branchInfo = mock(ProjectBranches.class); + when(branchInfo.isEmpty()).thenReturn(true); + + Map parameters = new HashMap<>(); + parameters.put("dummy", "dummy"); + + + assertEquals(DefaultBranchConfiguration.class, + testCase.load(parameters, supplier, branchInfo, mock(ProjectPullRequests.class)).getClass()); } @Test