Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly report coverage back to Gitlab #145

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
[GITLAB] auto config branch/MR
tisoft authored and mc1arke committed Feb 17, 2020
commit edb6e126a43fad1eb16b9da1f2da72f64d1cb735
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@

import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.System2;
import org.sonar.core.config.ScannerProperties;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.branch.BranchConfigurationLoader;
@@ -30,8 +31,11 @@
import org.sonar.scanner.scan.branch.ProjectPullRequests;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
@@ -45,10 +49,18 @@ public class CommunityBranchConfigurationLoader implements BranchConfigurationLo
private static final Set<String> PULL_REQUEST_ANALYSIS_PARAMETERS = new HashSet<>(
Arrays.asList(ScannerProperties.PULL_REQUEST_BRANCH, ScannerProperties.PULL_REQUEST_KEY,
ScannerProperties.PULL_REQUEST_BASE));
private final System2 system2;

public CommunityBranchConfigurationLoader(System2 system2) {
this.system2 = system2;
}


@Override
public BranchConfiguration load(Map<String, String> localSettings, ProjectBranches projectBranches,
ProjectPullRequests pullRequests) {
localSettings = autoConfigure(localSettings);

if (projectBranches.isEmpty()) {
if (isTargetingDefaultBranch(localSettings)) {
return new DefaultBranchConfiguration();
@@ -73,6 +85,27 @@ public BranchConfiguration load(Map<String, String> localSettings, ProjectBranch
return new DefaultBranchConfiguration();
}

private Map<String, String> autoConfigure(Map<String, String> localSettings) {
Map<String, String> mutableLocalSettings=new HashMap<>(localSettings);
if (Boolean.parseBoolean(system2.envVariable("GITLAB_CI"))) {
//GitLab CI auto configuration
if (system2.envVariable("CI_MERGE_REQUEST_IID") != null) {
// we are inside a merge request
Optional.ofNullable(system2.envVariable("CI_MERGE_REQUEST_IID")).ifPresent(
v -> mutableLocalSettings.putIfAbsent(ScannerProperties.PULL_REQUEST_KEY, v));
Optional.ofNullable(system2.envVariable("CI_MERGE_REQUEST_SOURCE_BRANCH_NAME")).ifPresent(
v -> mutableLocalSettings.putIfAbsent(ScannerProperties.PULL_REQUEST_BRANCH, v));
Optional.ofNullable(system2.envVariable("CI_MERGE_REQUEST_TARGET_BRANCH_NAME")).ifPresent(
v -> mutableLocalSettings.putIfAbsent(ScannerProperties.PULL_REQUEST_BASE, v));
} else {
// branch or tag
Optional.ofNullable(system2.envVariable("CI_COMMIT_REF_NAME")).ifPresent(
v -> mutableLocalSettings.putIfAbsent(ScannerProperties.BRANCH_NAME, v));
}
}
return Collections.unmodifiableMap(mutableLocalSettings);
}

private static boolean isTargetingDefaultBranch(Map<String, String> localSettings) {
String name = StringUtils.trimToNull(localSettings.get(ScannerProperties.BRANCH_NAME));
String target = StringUtils.trimToNull(localSettings.get(ScannerProperties.BRANCH_TARGET));
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.System2;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.branch.BranchInfo;
import org.sonar.scanner.scan.branch.BranchType;
@@ -55,7 +56,7 @@ public ExpectedException expectedException() {

@Test
public void testExceptionWhenNoExistingBranchAndBranchParamsPresent() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
ProjectBranches branchInfo = mock(ProjectBranches.class);
when(branchInfo.isEmpty()).thenReturn(true);

@@ -71,7 +72,7 @@ public void testExceptionWhenNoExistingBranchAndBranchParamsPresent() {

@Test
public void testDefaultConfigWhenNoExistingBranchAndBranchNameParamMaster() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
ProjectBranches branchInfo = mock(ProjectBranches.class);
when(branchInfo.isEmpty()).thenReturn(true);

@@ -84,7 +85,7 @@ public void testDefaultConfigWhenNoExistingBranchAndBranchNameParamMaster() {

@Test
public void testErrorWhenNoExistingBranchAndBranchTargetMasterButNoSourceBranch() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
ProjectBranches branchInfo = mock(ProjectBranches.class);
when(branchInfo.isEmpty()).thenReturn(true);

@@ -103,7 +104,7 @@ public void testErrorWhenNoExistingBranchAndBranchTargetMasterButNoSourceBranch(

@Test
public void testDefaultConfigWhenNoExistingBranchAndBranchParamsAllMaster() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
ProjectBranches branchInfo = mock(ProjectBranches.class);
when(branchInfo.isEmpty()).thenReturn(true);

@@ -117,7 +118,7 @@ public void testDefaultConfigWhenNoExistingBranchAndBranchParamsAllMaster() {

@Test
public void testExceptionWhenNoExistingBranchAndPullRequestAndBranchParametersPresent() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
ProjectBranches branchInfo = mock(ProjectBranches.class);
when(branchInfo.isEmpty()).thenReturn(true);

@@ -136,7 +137,7 @@ public void testExceptionWhenNoExistingBranchAndPullRequestAndBranchParametersPr

@Test
public void testDefaultBranchInfoWhenNoBranchParametersSpecifiedAndNoBranchesExist() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);

ProjectBranches branchInfo = mock(ProjectBranches.class);
when(branchInfo.isEmpty()).thenReturn(true);
@@ -151,14 +152,14 @@ public void testDefaultBranchInfoWhenNoBranchParametersSpecifiedAndNoBranchesExi

@Test
public void testDefaultBranchInfoWhenNoParametersSpecified() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
assertEquals(DefaultBranchConfiguration.class, testCase.load(new HashMap<>(), mock(ProjectBranches.class),
mock(ProjectPullRequests.class)).getClass());
}

@Test
public void testValidBranchInfoWhenAllBranchParametersSpecified() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.branch.name", "feature/shortLivedFeatureBranch");
parameters.put("sonar.branch.target", "master");
@@ -186,7 +187,7 @@ public void testValidBranchInfoWhenAllBranchParametersSpecified() {

@Test
public void testValidBranchInfoWhenOnlySourceBranchSpecifiedAndMasterExists() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.branch.name", "feature/shortLivedBranch");

@@ -208,7 +209,7 @@ public void testValidBranchInfoWhenOnlySourceBranchSpecifiedAndMasterExists() {

@Test
public void testValidBranchInfoWhenOnlySourceBranchSpecifiedAndMasterExists2() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.branch.name", "feature/shortLivedBranch");
parameters.put("sonar.branch.target", "");
@@ -231,7 +232,7 @@ public void testValidBranchInfoWhenOnlySourceBranchSpecifiedAndMasterExists2() {

@Test
public void testExceptionWhenOnlySourceBranchSpecifiedAndNoMasterExists() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.branch.name", "feature/shortLivedBranch");

@@ -250,7 +251,7 @@ public void testExceptionWhenOnlySourceBranchSpecifiedAndNoMasterExists() {

@Test
public void testUnknownTargetBranch() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.branch.name", "feature/shortLivedBranch");
parameters.put("sonar.branch.target", "feature/otherShortLivedBranch");
@@ -272,7 +273,7 @@ public boolean matches(Object item) {

@Test
public void testExistingBranchOnlySourceParameters() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.branch.name", "longLivedBranch");

@@ -294,7 +295,7 @@ public void testExistingBranchOnlySourceParameters() {

@Test
public void testPullRequestAllParameters() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.pullrequest.branch", "feature/sourceBranch");
parameters.put("sonar.pullrequest.base", "target");
@@ -320,7 +321,7 @@ public void testPullRequestAllParameters() {

@Test
public void testPullRequestMandatoryParameters() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.pullrequest.branch", "feature/sourceBranch");
parameters.put("sonar.pullrequest.key", "pr-key");
@@ -344,7 +345,7 @@ public void testPullRequestMandatoryParameters() {

@Test
public void testPullRequestMandatoryParameters2() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.pullrequest.branch", "feature/sourceBranch");
parameters.put("sonar.pullrequest.key", "pr-key");
@@ -370,7 +371,7 @@ public void testPullRequestMandatoryParameters2() {

@Test
public void testPullRequestNoSuchTarget() {
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader();
CommunityBranchConfigurationLoader testCase = new CommunityBranchConfigurationLoader(System2.INSTANCE);
Map<String, String> parameters = new HashMap<>();
parameters.put("sonar.pullrequest.branch", "feature/sourceBranch");
parameters.put("sonar.pullrequest.base", "missingTarget");