77import com .github .goober .sonarqube .plugin .decorator .sonarqube .model .MeasureResponse ;
88import com .github .goober .sonarqube .plugin .decorator .sonarqube .model .MetricsResponse ;
99import com .github .goober .sonarqube .plugin .decorator .sonarqube .model .SearchResponse ;
10+ import lombok .RequiredArgsConstructor ;
1011import okhttp3 .OkHttpClient ;
1112import okhttp3 .Request ;
1213import okhttp3 .Response ;
1314import org .sonar .api .ce .ComputeEngineSide ;
15+ import org .sonar .api .config .Configuration ;
1416import org .sonar .api .platform .Server ;
1517import org .sonar .api .utils .log .Logger ;
1618import org .sonar .api .utils .log .Loggers ;
2123import static java .lang .String .format ;
2224
2325@ ComputeEngineSide
26+ @ RequiredArgsConstructor
2427public 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