@@ -126,6 +126,11 @@ public interface CoreProperties { | |||
String PROJECT_BRANCH_PROPERTY = "sonar.branch"; | |||
String PROJECT_VERSION_PROPERTY = "sonar.projectVersion"; | |||
/** | |||
* @since 6.3 | |||
*/ | |||
String PROJECT_ORGANIZATION_PROPERTY = "sonar.organization"; | |||
/** | |||
* @since 2.6 | |||
*/ |
@@ -19,6 +19,7 @@ | |||
*/ | |||
package org.sonar.scanner.report; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.batch.bootstrap.ProjectDefinition; | |||
import org.sonar.api.config.Settings; | |||
import org.sonar.api.resources.Project; | |||
@@ -55,6 +56,12 @@ public class MetadataPublisher implements ReportPublisherStep { | |||
.setProjectKey(root.getKey()) | |||
.setCrossProjectDuplicationActivated(SonarCpdBlockIndex.isCrossProjectDuplicationEnabled(settings)) | |||
.setRootComponentRef(rootProject.batchId()); | |||
String organization = settings.getString(CoreProperties.PROJECT_ORGANIZATION_PROPERTY); | |||
if (organization != null) { | |||
builder.setOrganizationKey(organization); | |||
} | |||
String branch = root.getBranch(); | |||
if (branch != null) { | |||
builder.setBranch(branch); |
@@ -22,7 +22,6 @@ package org.sonar.scanner.report; | |||
import com.google.common.annotations.VisibleForTesting; | |||
import com.google.common.base.Throwables; | |||
import com.google.common.io.Files; | |||
import okhttp3.HttpUrl; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
@@ -32,8 +31,10 @@ import java.nio.charset.StandardCharsets; | |||
import java.util.LinkedHashMap; | |||
import java.util.Map; | |||
import javax.annotation.Nullable; | |||
import okhttp3.HttpUrl; | |||
import org.apache.commons.io.FileUtils; | |||
import org.picocontainer.Startable; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.batch.ScannerSide; | |||
import org.sonar.api.batch.bootstrap.ProjectDefinition; | |||
import org.sonar.api.config.Settings; | |||
@@ -166,6 +167,7 @@ public class ReportPublisher implements Startable { | |||
PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, report); | |||
PostRequest post = new PostRequest("api/ce/submit") | |||
.setMediaType(MediaTypes.PROTOBUF) | |||
.setParam("organization", settings.getString(CoreProperties.PROJECT_ORGANIZATION_PROPERTY)) | |||
.setParam("projectKey", projectDefinition.getKey()) | |||
.setParam("projectName", projectDefinition.getOriginalName()) | |||
.setParam("projectBranch", projectDefinition.getBranch()) | |||
@@ -191,6 +193,9 @@ public class ReportPublisher implements Startable { | |||
Map<String, String> metadata = new LinkedHashMap<>(); | |||
String effectiveKey = projectReactor.getRoot().getKeyWithBranch(); | |||
if (settings.hasKey(CoreProperties.PROJECT_ORGANIZATION_PROPERTY)) { | |||
metadata.put("organization", settings.getString(CoreProperties.PROJECT_ORGANIZATION_PROPERTY)); | |||
} | |||
metadata.put("projectKey", effectiveKey); | |||
metadata.put("serverUrl", publicUrl); | |||
@@ -27,8 +27,8 @@ import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.batch.bootstrap.ProjectDefinition; | |||
import org.sonar.api.config.Settings; | |||
import org.sonar.api.config.MapSettings; | |||
import org.sonar.api.config.Settings; | |||
import org.sonar.api.resources.Project; | |||
import org.sonar.scanner.index.BatchComponentCache; | |||
import org.sonar.scanner.protocol.output.ScannerReport; | |||
@@ -118,4 +118,18 @@ public class MetadataPublisherTest { | |||
assertThat(metadata.getCrossProjectDuplicationActivated()).isFalse(); | |||
} | |||
@Test | |||
public void write_project_organization() throws Exception { | |||
settings.setProperty(CoreProperties.PROJECT_ORGANIZATION_PROPERTY, "SonarSource"); | |||
File outputDir = temp.newFolder(); | |||
ScannerReportWriter writer = new ScannerReportWriter(outputDir); | |||
underTest.publish(writer); | |||
ScannerReportReader reader = new ScannerReportReader(outputDir); | |||
ScannerReport.Metadata metadata = reader.readMetadata(); | |||
assertThat(metadata.getOrganizationKey()).isEqualTo("SonarSource"); | |||
} | |||
} |
@@ -21,6 +21,8 @@ package org.sonar.scanner.report; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.PipedInputStream; | |||
import java.io.PipedOutputStream; | |||
import java.nio.file.Files; | |||
import java.nio.file.Path; | |||
import org.junit.Before; | |||
@@ -28,11 +30,13 @@ import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.mockito.ArgumentCaptor; | |||
import org.mockito.Mockito; | |||
import org.sonar.api.CoreProperties; | |||
import org.sonar.api.batch.bootstrap.ProjectDefinition; | |||
import org.sonar.api.config.MapSettings; | |||
import org.sonar.api.config.PropertyDefinitions; | |||
import org.sonar.api.config.Settings; | |||
import org.sonar.api.config.MapSettings; | |||
import org.sonar.api.platform.Server; | |||
import org.sonar.api.utils.MessageException; | |||
import org.sonar.api.utils.TempFolder; | |||
@@ -42,10 +46,16 @@ import org.sonar.core.config.CorePropertyDefinitions; | |||
import org.sonar.scanner.analysis.DefaultAnalysisMode; | |||
import org.sonar.scanner.bootstrap.ScannerWsClient; | |||
import org.sonar.scanner.scan.ImmutableProjectReactor; | |||
import org.sonarqube.ws.WsCe; | |||
import org.sonarqube.ws.client.WsRequest; | |||
import org.sonarqube.ws.client.WsResponse; | |||
import static org.apache.commons.io.FileUtils.readFileToString; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.api.Assertions.entry; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.when; | |||
public class ReportPublisherTest { | |||
@@ -77,6 +87,7 @@ public class ReportPublisherTest { | |||
@Test | |||
public void log_and_dump_information_about_report_uploading() throws IOException { | |||
ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); | |||
settings.setProperty(CoreProperties.PROJECT_ORGANIZATION_PROPERTY, "MyOrg"); | |||
underTest.logSuccess("TASK-123"); | |||
@@ -87,7 +98,8 @@ public class ReportPublisherTest { | |||
File detailsFile = new File(temp.getRoot(), "report-task.txt"); | |||
assertThat(readFileToString(detailsFile)).isEqualTo( | |||
"projectKey=struts\n" + | |||
"organization=MyOrg\n" + | |||
"projectKey=struts\n" + | |||
"serverUrl=https://localhost\n" + | |||
"dashboardUrl=https://localhost/dashboard/index/struts\n" + | |||
"ceTaskId=TASK-123\n" + | |||
@@ -161,4 +173,32 @@ public class ReportPublisherTest { | |||
assertThat(reportDir).doesNotExist(); | |||
} | |||
@Test | |||
public void test_ws_parameters() throws Exception { | |||
ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); | |||
settings.setProperty(CoreProperties.PROJECT_ORGANIZATION_PROPERTY, "MyOrg"); | |||
WsResponse response = mock(WsResponse.class); | |||
PipedOutputStream out = new PipedOutputStream(); | |||
PipedInputStream in = new PipedInputStream(out); | |||
WsCe.SubmitResponse.newBuilder().build().writeTo(out); | |||
out.close(); | |||
when(response.failIfNotSuccessful()).thenReturn(response); | |||
when(response.contentStream()).thenReturn(in); | |||
when(wsClient.call(any(WsRequest.class))).thenReturn(response); | |||
underTest.upload(temp.newFile()); | |||
ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); | |||
verify(wsClient).call(capture.capture()); | |||
WsRequest wsRequest = capture.getValue(); | |||
assertThat(wsRequest.getParams()).containsOnly( | |||
entry("organization", "MyOrg"), | |||
entry("projectKey", "struts")); | |||
} | |||
} |