From d4f9b24fd909b9a9a63c74db65082001857f4531 Mon Sep 17 00:00:00 2001 From: Pawel Switalski Date: Tue, 17 Apr 2018 19:11:47 -0700 Subject: [PATCH] Added better handling for new projects --- README.md | 2 +- pom.xml | 2 +- .../scanner/BranchConfigurationLoaderImpl.java | 7 +++++-- .../branch/scanner/ProjectBranchesLoaderImpl.java | 8 +++++++- .../scanner/BranchConfigurationLoaderImplTest.java | 14 +++++++++++++- .../scanner/ProjectBranchesLoaderImplTest.java | 13 +++++++++++++ 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ffeac0f..7710b5c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This plugin is an open source alternative to the [Branch Plugin](https://docs.so ## Usage -For the new project perform regular analysis without `sonar.branch.*` parameters for the first time. +Project must exists before scanning branches other than `master`. There are currently no usage differences to [Branch Plugin](https://docs.sonarqube.org/display/PLUG/Branch+Plugin). ### Installation diff --git a/pom.xml b/pom.xml index 2f04834..5ec4517 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.sonarsource branch - 1.0.0 + 1.0.1 Branch Plugin for SonarQube Community Edition sonar-plugin diff --git a/src/main/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImpl.java b/src/main/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImpl.java index c986455..2ee9755 100644 --- a/src/main/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImpl.java +++ b/src/main/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImpl.java @@ -35,8 +35,11 @@ public BranchConfiguration load(Map localSettings, Supplier> remoteSettingsSupplier, ProjectBranches branches) { if (branches.isEmpty()) { - // SonarQube doesn't support using branch properties for a new project even if scanning main branch - throw MessageException.of("No branches found for a project. Run regular analysis on main branch before using branch plugin."); + if ("master".equals(branchName)) { + return new DefaultBranchConfiguration(); + } else { + throw MessageException.of("Project not found. Run analysis for master branch before analysing other branches."); + } } else { String base = targetBranch; BranchInfo branchInfo = branches.get(branchName); diff --git a/src/main/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImpl.java b/src/main/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImpl.java index b43c6fe..bffe5ca 100644 --- a/src/main/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImpl.java +++ b/src/main/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImpl.java @@ -17,12 +17,14 @@ import org.sonar.scanner.scan.branch.ProjectBranches; import org.sonar.scanner.scan.branch.ProjectBranchesLoader; import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.WsResponse; /** * Retrieves list of project's branches using API call. */ public class ProjectBranchesLoaderImpl implements ProjectBranchesLoader { + private static final int HTTP_NOT_FOUND_CODE = 404; private static final Logger LOGGER = Loggers.get(ProjectBranchesLoaderImpl.class); private static final Gson GSON = new Gson(); @@ -42,7 +44,11 @@ private List loadListOfBranches(final String projectKey) { try (WsResponse response = wsClient.call(request)) { return readResponse(response); } catch (IOException | RuntimeException e) { - LOGGER.warn("Unable to load list of branches. Using empty list.", e); + if (e instanceof HttpException && ((HttpException) e).code() == HTTP_NOT_FOUND_CODE) { + LOGGER.info("Project not found when loading list of branches. Using empty list."); + } else { + LOGGER.warn("Unable to load list of branches. Using empty list.", e); + } return Collections.emptyList(); } } diff --git a/src/test/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImplTest.java b/src/test/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImplTest.java index 041e322..f78fdd1 100644 --- a/src/test/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImplTest.java +++ b/src/test/java/org/sonarsource/branch/scanner/BranchConfigurationLoaderImplTest.java @@ -31,9 +31,21 @@ public void load_default() { assertTrue(configuration instanceof DefaultBranchConfiguration); } + @Test + public void load_default_master() { + BranchConfiguration configuration = branchConfigurationLoader.load( + ImmutableMap.of("sonar.branch.name", "master"), + () -> ImmutableMap.of("",""), + new ProjectBranches(Collections.emptyList()) + ); + + assertNotNull(configuration); + assertTrue(configuration instanceof DefaultBranchConfiguration); + } + @Test(expected = MessageException.class) public void load_no_project_branches() { - BranchConfiguration configuration = branchConfigurationLoader.load( + branchConfigurationLoader.load( ImmutableMap.of("sonar.branch.name", "test"), () -> ImmutableMap.of("",""), new ProjectBranches(Collections.emptyList()) diff --git a/src/test/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImplTest.java b/src/test/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImplTest.java index 086c8e5..a0f1768 100644 --- a/src/test/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImplTest.java +++ b/src/test/java/org/sonarsource/branch/scanner/ProjectBranchesLoaderImplTest.java @@ -8,6 +8,7 @@ import org.sonar.scanner.scan.branch.BranchInfo; import org.sonar.scanner.scan.branch.BranchType; import org.sonar.scanner.scan.branch.ProjectBranches; +import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.MockWsResponse; import static org.junit.Assert.assertEquals; @@ -51,6 +52,18 @@ public void load_io_exception() { assertTrue(projectBranches.isEmpty()); } + @Test + public void load_404_exception() { + when(wsClient.call(any())).thenThrow(new HttpException("", 404, "")); + ProjectBranchesLoaderImpl branchesLoader = new ProjectBranchesLoaderImpl(wsClient); + + Loggers.get(ProjectBranchesLoaderImpl.class).setLevel(LoggerLevel.ERROR); + ProjectBranches projectBranches = branchesLoader.load(PROJECT_KEY); + Loggers.get(ProjectBranchesLoaderImpl.class).setLevel(LoggerLevel.INFO); + + assertTrue(projectBranches.isEmpty()); + } + @Test public void load_branch() { MockWsResponse response = new MockWsResponse();