Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit fe9e0c4

Browse files
committed
Always call sonarqubes api with localhost
To allow installations where the public url is blocked
1 parent 336d619 commit fe9e0c4

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/main/java/com/github/goober/sonarqube/plugin/decorator/sonarqube/SonarQubeClient.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import com.github.goober.sonarqube.plugin.decorator.sonarqube.model.MeasureResponse;
88
import com.github.goober.sonarqube.plugin.decorator.sonarqube.model.MetricsResponse;
99
import com.github.goober.sonarqube.plugin.decorator.sonarqube.model.SearchResponse;
10+
import lombok.RequiredArgsConstructor;
1011
import okhttp3.OkHttpClient;
1112
import okhttp3.Request;
1213
import okhttp3.Response;
1314
import org.sonar.api.ce.ComputeEngineSide;
15+
import org.sonar.api.config.Configuration;
1416
import org.sonar.api.platform.Server;
1517
import org.sonar.api.utils.log.Logger;
1618
import org.sonar.api.utils.log.Loggers;
@@ -21,23 +23,21 @@
2123
import static java.lang.String.format;
2224

2325
@ComputeEngineSide
26+
@RequiredArgsConstructor
2427
public class SonarQubeClient {
2528

2629
private static final Logger LOGGER = Loggers.get(SonarQubeClient.class);
2730

2831
private final Server server;
32+
private final Configuration configuration;
2933

3034
private OkHttpClient client;
3135

3236
private ObjectMapper objectMapper;
3337

34-
public SonarQubeClient(Server server) {
35-
this.server = server;
36-
}
37-
3838
public SearchResponse listOpenIssues(String project, String pullRequestId) throws IOException {
3939
Request request = new Request.Builder()
40-
.url(String.format("%s/api/issues/search?projects=%s&pullRequest=%s", server.getPublicRootUrl(), project, pullRequestId))
40+
.url(String.format("%s/api/issues/search?projects=%s&pullRequest=%s", getLocalUrl(), project, pullRequestId))
4141
.build();
4242

4343
try (Response response = getClient().newCall(request).execute()) {
@@ -54,7 +54,7 @@ public SearchResponse listOpenIssues(String project, String pullRequestId) throw
5454

5555
public MetricsResponse listMetrics() throws IOException {
5656
Request request = new Request.Builder()
57-
.url(String.format("%s/api/metrics/search?ps=500", server.getPublicRootUrl()))
57+
.url(String.format("%s/api/metrics/search?ps=500", getLocalUrl()))
5858
.build();
5959
try (Response response = getClient().newCall(request).execute()) {
6060
if (response.isSuccessful()) {
@@ -68,7 +68,7 @@ public MetricsResponse listMetrics() throws IOException {
6868
public MeasureResponse listMeasures(String project, String pullRequestId, String... measures) throws IOException {
6969
Request request = new Request.Builder()
7070
.url(String.format("%s/api/measures/component?component=%s&pullRequest=%s&metricKeys=%s",
71-
server.getPublicRootUrl(),
71+
getLocalUrl(),
7272
project,
7373
pullRequestId,
7474
String.join(",", measures)))
@@ -89,6 +89,10 @@ private String getDashboardUrl(String project, String pullRequestId) {
8989
return String.format("%s/dashboard?id=%s&pullRequest=%s", server.getPublicRootUrl(), project, pullRequestId);
9090
}
9191

92+
private String getLocalUrl() {
93+
return "http://localhost:" + configuration.get("sonar.web.port").orElse("9000") + server.getContextPath();
94+
}
95+
9296
private OkHttpClient getClient() {
9397
client = Optional.ofNullable(client).orElseGet(OkHttpClient::new);
9498
return client;

0 commit comments

Comments
 (0)