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!
The Sonarqube Web API endpoint is located at https://sonarqube.com/api/. You can find the SonarSource portal / hompage here. If you need Sonarqube Web API support, you can visit developer support here, contact support directly at [email protected], or reach out to their Twitter account at @SonarQube.
As shown in postman you need to add the bearer token in the Username of the Postman and once you Preview the request you will be able to authenticate to Sonar.
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.
http://localhost:9000/web_api/
can be helpful, explain the parameters, have examples of response and trace changes with the Sonarqube versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With