Skip to content

Commit

Permalink
#1: Only throw error if no branches exist and branch parameters speci…
Browse files Browse the repository at this point in the history
…fied

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.
  • Loading branch information
mc1arke committed Mar 25, 2019
1 parent 409c4ad commit cc17135
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public class CommunityBranchConfigurationLoader implements BranchConfigurationLo
@Override
public BranchConfiguration load(Map<String, String> localSettings, Supplier<Map<String, String>> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> parameters = new HashMap<>();
parameters.put("dummy", "dummy");


assertEquals(DefaultBranchConfiguration.class,
testCase.load(parameters, supplier, branchInfo, mock(ProjectPullRequests.class)).getClass());
}

@Test
Expand Down

0 comments on commit cc17135

Please sign in to comment.