Skip to content

Commit

Permalink
Added better handling for new projects
Browse files Browse the repository at this point in the history
  • Loading branch information
pawel-sw committed Apr 18, 2018
1 parent 10b06df commit d4f9b24
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.sonarsource</groupId>
<artifactId>branch</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<description>Branch Plugin for SonarQube Community Edition</description>
<packaging>sonar-plugin</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public BranchConfiguration load(Map<String, String> localSettings, Supplier<Map<

private BranchConfiguration loadConfiguration(String branchName, @Nullable String targetBranch, Supplier<Map<String, String>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -42,7 +44,11 @@ private List<BranchInfo> 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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d4f9b24

Please sign in to comment.