import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsResponse;
+import static org.sonar.core.config.ScannerProperties.BRANCH_NAME;
import static org.sonar.core.config.ScannerProperties.ORGANIZATION;
import static org.sonar.core.util.FileUtils.deleteQuietly;
post.setParam("characteristic", "incremental=true");
}
+ settings.get(BRANCH_NAME).ifPresent(b -> post.setParam("characteristic", "branch=" + b));
+
WsResponse response;
try {
response = wsClient.call(post).failIfNotSuccessful();
metadata.put("projectKey", effectiveKey);
metadata.put("serverUrl", publicUrl);
metadata.put("serverVersion", server.getVersion());
+ settings.get(BRANCH_NAME).ifPresent(branch -> metadata.put("branch", branch));
URL dashboardUrl = httpUrl.newBuilder()
.addPathSegment("dashboard").addPathSegment("index").addPathSegment(effectiveKey)
*/
package org.sonar.scanner.scan;
+import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.List;
-
import javax.annotation.Nullable;
-
import org.apache.commons.lang.StringUtils;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
import org.sonar.core.component.ComponentKeys;
+import org.sonar.core.config.ScannerProperties;
import org.sonar.scanner.analysis.DefaultAnalysisMode;
-import com.google.common.base.Joiner;
-
/**
* This class aims at validating project reactor
* @since 3.6
*/
public class ProjectReactorValidator {
private final DefaultAnalysisMode mode;
+ private final ProjectSettings settings;
- public ProjectReactorValidator(DefaultAnalysisMode mode) {
+ public ProjectReactorValidator(DefaultAnalysisMode mode, ProjectSettings settings) {
this.mode = mode;
+ this.settings = settings;
}
public void validate(ProjectReactor reactor) {
}
}
+ validateBranchParams(validationMessages, branch, settings.get(ScannerProperties.BRANCH_NAME).orElse(null));
+
validateBranch(validationMessages, branch);
if (!validationMessages.isEmpty()) {
}
}
+ private static void validateBranchParams(List<String> validationMessages, @Nullable String deprecatedBranch, @Nullable String branchName) {
+ if (StringUtils.isNotEmpty(deprecatedBranch) && StringUtils.isNotEmpty(branchName)) {
+ validationMessages.add(String.format("The %s parameter must not be used together with the deprecated sonar.branch parameter", ScannerProperties.BRANCH_NAME));
+ }
+ }
+
}
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.config.ScannerProperties;
import org.sonar.core.metric.ScannerMetrics;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.scanner.ProjectAnalysisInfo;
}
String branch = tree.root().definition().getBranch();
if (branch != null) {
- LOG.info("Branch key: {}", branch);
+ LOG.info("Branch key (deprecated): {}", branch);
+ }
+
+ String branchName = props.property(ScannerProperties.BRANCH_NAME);
+ if (branchName != null) {
+ LOG.info("Branch name: {}", branchName);
}
LOG.debug("Start recursive analysis of project modules");
.execute();
assertThat(logs.getAllAsString()).contains("Project key: com.foo.project");
- assertThat(logs.getAllAsString()).contains("Branch key: my-branch");
+ assertThat(logs.getAllAsString()).contains("Branch key (deprecated): my-branch");
}
@Test
entry("organization", "MyOrg"),
entry("projectKey", "struts"));
}
-
+
@Test
public void test_send_characteristics() throws Exception {
ReportPublisher underTest = new ReportPublisher(settings.asConfig(), wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class),
new ReportPublisherStep[0]);
-
+
when(mode.isIncremental()).thenReturn(true);
- settings.setProperty(ScannerProperties.ORGANIZATION, "MyOrg");
-
+ String orgName = "MyOrg";
+ settings.setProperty(ScannerProperties.ORGANIZATION, orgName);
+
+ String branchName = "feature";
+ settings.setProperty(ScannerProperties.BRANCH_NAME, branchName);
+
WsResponse response = mock(WsResponse.class);
PipedOutputStream out = new PipedOutputStream();
verify(wsClient).call(capture.capture());
WsRequest wsRequest = capture.getValue();
- assertThat(wsRequest.getParams()).containsOnly(
- entry("organization", "MyOrg"),
- entry("projectKey", "struts"),
- entry("characteristic", "incremental=true"));
+ assertThat(wsRequest.getParameters().getKeys()).hasSize(3);
+ assertThat(wsRequest.getParameters().getValues("organization")).containsExactly(orgName);
+ assertThat(wsRequest.getParameters().getValues("projectKey")).containsExactly("struts");
+ assertThat(wsRequest.getParameters().getValues("characteristic"))
+ .containsExactly("incremental=true", "branch=" + branchName);
}
}
*/
package org.sonar.scanner.scan;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Optional;
+
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
+import org.sonar.core.config.ScannerProperties;
import org.sonar.scanner.analysis.DefaultAnalysisMode;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
public class ProjectReactorValidatorTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
- private ProjectReactorValidator validator;
private DefaultAnalysisMode mode;
+ private ProjectSettings settings;
+ private ProjectReactorValidator validator;
@Before
public void prepare() {
mode = mock(DefaultAnalysisMode.class);
- validator = new ProjectReactorValidator(mode);
+
+ settings = mock(ProjectSettings.class);
+ when(settings.get(ScannerProperties.BRANCH_NAME)).thenReturn(Optional.empty());
+
+ validator = new ProjectReactorValidator(mode, settings);
}
@Test
validator.validate(reactor);
}
+ @Test
+ public void should_fail_when_both_old_and_new_branch_property_present() {
+ thrown.expect(MessageException.class);
+ thrown.expectMessage("The sonar.branch.name parameter must not be used together with the deprecated sonar.branch parameter");
+ when(settings.get(ScannerProperties.BRANCH_NAME)).thenReturn(Optional.of("feature"));
+ validator.validate(createProjectReactor("foo", "branch1"));
+ }
+
private ProjectReactor createProjectReactor(String projectKey) {
ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, projectKey);
ProjectReactor reactor = new ProjectReactor(def);