diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-01-13 16:12:38 +0100 |
---|---|---|
committer | Julien HENRY <henryju@yahoo.fr> | 2017-01-16 21:05:24 +0100 |
commit | e975c40d789273ca15e86c2b646359c5d9206580 (patch) | |
tree | 7492783815fd61ef2faba3fcc185f7e0f002f68c | |
parent | 501e5915a092dbb886f942be47ee4adfa3d7c873 (diff) | |
download | sonarqube-e975c40d789273ca15e86c2b646359c5d9206580.tar.gz sonarqube-e975c40d789273ca15e86c2b646359c5d9206580.zip |
SONAR-8652 Pass value of sonar.organization in the scanner report
5 files changed, 75 insertions, 4 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index 178e37336e3..c62cdb26c60 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -127,6 +127,11 @@ public interface CoreProperties { String PROJECT_VERSION_PROPERTY = "sonar.projectVersion"; /** + * @since 6.3 + */ + String PROJECT_ORGANIZATION_PROPERTY = "sonar.organization"; + + /** * @since 2.6 */ String PROJECT_KEY_PROPERTY = "sonar.projectKey"; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java index 7cb5611cdba..72fd40673fd 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java @@ -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); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java index e082bcecfd9..e0c50ecb416 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java @@ -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); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index c79cef9cbaa..95dc51a8aef 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -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"); + } + } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java index 35e4c6a986e..f924df08f05 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java @@ -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")); + } + } |