import org.sonar.api.batch.BatchSide;
import org.sonar.api.config.Settings;
import org.sonar.api.platform.Server;
-import org.sonar.batch.bootstrap.GlobalProperties;
+import org.sonar.batch.bootstrap.BatchWsClient;
+
+import static org.apache.commons.lang.StringUtils.trimToEmpty;
@BatchSide
public class DefaultServer extends Server {
private Settings settings;
- private GlobalProperties props;
+ private BatchWsClient client;
- public DefaultServer(Settings settings, GlobalProperties props) {
+ public DefaultServer(Settings settings, BatchWsClient client) {
this.settings = settings;
- this.props = props;
+ this.client = client;
}
@Override
@Override
public String getPublicRootUrl() {
- return null;
+ String baseUrl = trimToEmpty(settings.getString(CoreProperties.SERVER_BASE_URL));
+ if (baseUrl.isEmpty()) {
+ // If server base URL was not configured in Sonar server then is is better to take URL configured on batch side
+ baseUrl = client.baseUrl();
+ }
+ return StringUtils.removeEnd(baseUrl, "/");
}
@Override
@Override
public String getURL() {
- return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
+ return StringUtils.removeEnd(client.baseUrl(), "/");
}
@Override
import javax.annotation.Nullable;
import org.apache.commons.io.FileUtils;
import org.picocontainer.Startable;
-import org.sonar.api.CoreProperties;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.Settings;
+import org.sonar.api.platform.Server;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.TempFolder;
import org.sonar.api.utils.ZipUtils;
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsResponse;
-import static org.apache.commons.lang.StringUtils.trimToEmpty;
import static org.sonar.core.util.FileUtils.deleteQuietly;
@BatchSide
private final DefaultAnalysisMode analysisMode;
private final TempFolder temp;
private final ReportPublisherStep[] publishers;
+ private final Server server;
private File reportDir;
private ScannerReportWriter writer;
- public ReportPublisher(Settings settings, BatchWsClient wsClient, AnalysisContextReportPublisher contextPublisher,
+ public ReportPublisher(Settings settings, BatchWsClient wsClient, Server server, AnalysisContextReportPublisher contextPublisher,
ImmutableProjectReactor projectReactor, DefaultAnalysisMode analysisMode, TempFolder temp, ReportPublisherStep[] publishers) {
this.settings = settings;
this.wsClient = wsClient;
+ this.server = server;
this.contextPublisher = contextPublisher;
this.projectReactor = projectReactor;
this.analysisMode = analysisMode;
contextPublisher.init(writer);
if (!analysisMode.isIssues() && !analysisMode.isMediumTest()) {
- String publicUrl = publicUrl();
+ String publicUrl = server.getPublicRootUrl();
if (HttpUrl.parse(publicUrl) == null) {
throw MessageException.of("Failed to parse public URL set in SonarQube server: " + publicUrl);
}
if (taskId == null) {
LOG.info("ANALYSIS SUCCESSFUL");
} else {
- String publicUrl = publicUrl();
+ String publicUrl = server.getPublicRootUrl();
HttpUrl httpUrl = HttpUrl.parse(publicUrl);
Map<String, String> metadata = new LinkedHashMap<>();
throw new IllegalStateException("Unable to dump " + file, e);
}
}
-
- /**
- * The public URL is optionally configured on server. If not, then the regular URL is returned.
- * See https://jira.sonarsource.com/browse/SONAR-4239
- */
- private String publicUrl() {
- String baseUrl = trimToEmpty(settings.getString(CoreProperties.SERVER_BASE_URL));
- if (baseUrl.isEmpty()) {
- // If server base URL was not configured in Sonar server then is is better to take URL configured on batch side
- baseUrl = wsClient.baseUrl();
- }
- return baseUrl.replaceAll("(/)+$", "");
- }
}
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
-import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
+import org.sonar.api.platform.Server;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.TempFolder;
import org.sonar.api.utils.log.LogTester;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
-
+
@Rule
public ExpectedException exception = ExpectedException.none();
DefaultAnalysisMode mode = mock(DefaultAnalysisMode.class);
Settings settings = new Settings(new PropertyDefinitions(CorePropertyDefinitions.all()));
BatchWsClient wsClient = mock(BatchWsClient.class, Mockito.RETURNS_DEEP_STUBS);
+ Server server = mock(Server.class);
ImmutableProjectReactor reactor = mock(ImmutableProjectReactor.class);
ProjectDefinition root;
AnalysisContextReportPublisher contextPublisher = mock(AnalysisContextReportPublisher.class);
public void setUp() {
root = ProjectDefinition.create().setKey("struts").setWorkDir(temp.getRoot());
when(reactor.getRoot()).thenReturn(root);
- when(wsClient.baseUrl()).thenReturn("https://localhost/");
+ when(server.getPublicRootUrl()).thenReturn("https://localhost");
}
@Test
public void log_and_dump_information_about_report_uploading() throws IOException {
- ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+ ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
underTest.logSuccess("TASK-123");
File detailsFile = new File(temp.getRoot(), "report-task.txt");
assertThat(readFileToString(detailsFile)).isEqualTo(
"projectKey=struts\n" +
- "serverUrl=https://localhost\n" +
- "dashboardUrl=https://localhost/dashboard/index/struts\n" +
- "ceTaskId=TASK-123\n" +
- "ceTaskUrl=https://localhost/api/ce/task?id=TASK-123\n"
- );
+ "serverUrl=https://localhost\n" +
+ "dashboardUrl=https://localhost/dashboard/index/struts\n" +
+ "ceTaskId=TASK-123\n" +
+ "ceTaskUrl=https://localhost/api/ce/task?id=TASK-123\n");
}
@Test
public void log_public_url_if_defined() throws IOException {
- settings.setProperty(CoreProperties.SERVER_BASE_URL, "https://publicserver/sonarqube");
- ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+ when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
+ ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
underTest.logSuccess("TASK-123");
File detailsFile = new File(temp.getRoot(), "report-task.txt");
assertThat(readFileToString(detailsFile)).isEqualTo(
"projectKey=struts\n" +
- "serverUrl=https://publicserver/sonarqube\n" +
- "dashboardUrl=https://publicserver/sonarqube/dashboard/index/struts\n" +
- "ceTaskId=TASK-123\n" +
- "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n"
- );
+ "serverUrl=https://publicserver/sonarqube\n" +
+ "dashboardUrl=https://publicserver/sonarqube/dashboard/index/struts\n" +
+ "ceTaskId=TASK-123\n" +
+ "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n");
}
-
+
@Test
public void fail_if_public_url_malformed() throws IOException {
- settings.setProperty(CoreProperties.SERVER_BASE_URL, "invalid");
- ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
-
+ when(server.getPublicRootUrl()).thenReturn("invalid");
+ ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+
exception.expect(MessageException.class);
exception.expectMessage("Failed to parse public URL set in SonarQube server: invalid");
underTest.start();
@Test
public void log_but_not_dump_information_when_report_is_not_uploaded() {
- ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+ ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
underTest.logSuccess(/* report not uploaded, no server task */null);
settings.setProperty("sonar.batch.keepReport", true);
Path reportDir = temp.getRoot().toPath().resolve("batch-report");
Files.createDirectory(reportDir);
- ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+ ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
underTest.start();
underTest.stop();
public void should_delete_report_by_default() throws IOException {
Path reportDir = temp.getRoot().toPath().resolve("batch-report");
Files.createDirectory(reportDir);
- ReportPublisher job = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+ ReportPublisher job = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
job.start();
job.stop();