12

Previously, I asked about how to export custom data from SonarQube Database, and the Sonar Team suggests me that I should use Web API.

After some research, I'm still struggling on how to use the Web API. ( I'm very unfamiliar with how the Web API works)

After reading this post, I realise that I can use Java code to do that. (I've just gone through how to use Apache Http Client) However, after run

HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");(copied from that post)

I got:

HTTP/1.1 404 {"errors":[{"msg":"Unknown url : /api/issues"}]}

After I change this line to:

HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");

I got:

HTTP/1.1 200 <!DOCTYPE html><html lang="en"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180x180.png"><link rel="icon" type="image/x-icon" href="/favicon.ico"><meta name="application-name" content="SonarQube"/><meta name="msapplication-TileColor" content="#FFFFFF"/><meta name="msapplication-TileImage" content="/mstile-512x512.png"/><link href="/css/sonar.bf342fee.css" rel="stylesheet"><title>SonarQube</title></head><body><div id="content"><div class="global-loading"><i class="spinner global-loading-spinner"></i> <span class="global-loading-text">Loading...</span></div></div><script>window.baseUrl=""</script><script src="/js/vendor.0ba4fd94.js"></script><script src="/js/app.bf342fee.js"></script></body></html>

Which is not what I expect as well.

I'm wondering what's the right way to use the Web API? For example, if I want to get the code smells for a project. How the code should be in Java?

Here is the code I'm using at the moment:

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Test {
    public static void main(String[] args) throws ClientProtocolException, IOException {

        //HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");
        HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");

        try(CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = httpClient.execute(httpGet);) {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
        }
    }
}

Appreciate for any help or guidance!

MaXon
  • 465
  • 2
  • 8
  • 17

2 Answers2

21

The SonarQube web API lives under the /api context path, as per the SonarQube documentation, along with the section and the operation (which you seem to be missing).

As an example, to search for issues on localhost running on port 9000, send a GET to http://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT and parse the JSON response.

You may need to provide authorization as well, which is sent as either a BASIC username password combo, or an access token which you can retrieve via the web client.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
  • Thank you so much, that's what I need! – MaXon Sep 20 '17 at 12:48
  • Could you also explain what should I put after the '?' mark? In your case, you put pageSize=500 and componentKeys=YOUR_COMPONENT. I've found that componentKeys is one of the parameters under 'issues/search'. What about pageSize? Where can I find this kind of parameters? – MaXon Sep 20 '17 at 12:52
  • 1
    `pageSize` just controls the number of results you get back, 500 is the maximum. The deprecated docs list them, although I'd be surprised if they were removed: https://docs.sonarqube.org/pages/viewpage.action?pageId=2392181 – Evan Knowles Sep 20 '17 at 14:43
  • That's absolutely useful! – MaXon Sep 21 '17 at 00:25
  • Hi, Evan, will you be able to help me on this [question](https://stackoverflow.com/questions/46538316/sonarqube-how-to-login-with-the-web-api?noredirect=1#comment80041941_46538316)? Thank you! – MaXon Oct 03 '17 at 12:35
  • Hey guys. When I run the url `http://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT`, it gives proper search issues in the JSON format on the browser. But when I try to run it through the fetch API from Javascript, it doesn't seem to be responsing with ant results. Any idea why? – Aakash Thakur May 28 '18 at 14:19
  • @AakashThakur Best bet is to post a new question with your Fetch API code and we'll look from there. – Evan Knowles May 28 '18 at 20:49
  • I have posted a new question and here's a link:https://stackoverflow.com/questions/50577479/access-soanrqube-api-with-fetchapi-doesnt-return-any-result. Please do have a look if you get time. – Aakash Thakur May 29 '18 at 06:17
  • /web-api/ path? (https://docs.sonarqube.org/latest/extend/web-api/) –  Feb 09 '21 at 14:04
  • 1
    Any idea how the API_TOKEN is passed in the request? – pkaramol Jun 16 '22 at 08:10
6

http://localhost:9000/web_api/ can be helpful, explain the parameters, have examples of response and trace changes with the Sonarqube versions.

Jarvars
  • 73
  • 2
  • 3