How to get the results of an analysis

Hey, I am pretty new to the sonarcube and API stuff so sorry.

I want to get the results of an analysis to a JSON file via a Java Programm.

And in the web API doc, I found measures, but currently, I don’t know how to use the API in a Java Programm?

Do I have to import something? And what would the method be I have to use?
The analysis works fine and gets perfectly uploaded to localhost
I would appreciate it if you would provide me some pseudocode.

I am using the newest sonarqube version and my java project is a maven project.

1 Like

Welcome :slight_smile:

you may use webhooks, see https://docs.sonarqube.org/latest/project-administration/webhooks/

Gilbert

Hey Gilbert,

I checked that out. So if I understand that right a webhook will give me the json file as soon an analysis got completet and uploaded to localhost right?

But maybe the Web API could help me more?
But currently I have the problem how can I use it or what would be an example if my project name is Sonar-Results to get the issues?

My idea was to make a GET request via Java. But which Url do I have to use then?
Would this be the right URL if the project is called Sonar-Results?

http://localhost:9000/api/issues/search?pageSize=500&componentKeys=Sonar-Results componentKeys reffers to my project right?

1 Like

From the API doc:
Comma-separated list of component keys. Retrieve issues associated to a specific list of components (and all its descendants). A component can be a portfolio, project, module, directory or file.

Yes, your URL will return the issues associated with your project.

Note: If you have more than 500 issues you’ll have to also specify the p and ps properties, inspect the paging data returned from the web call and make additional calls to get the rest. There’s a limit of 10,000 issues returned for a project.

http://localhost:9000/api/issues/search?pageSize=500&componentKeys=Sonar-Results&ps=500&p=1

Then if paging.total in the results is greater than 500 you increment the p property and make more calls.

1 Like

Okay first of all thanks!

My problem is that I get this JSON:

{“total”:0,“p”:1,“ps”:100,“paging”:{“pageIndex”:1,“pageSize”:100,“total”:0},“effortTotal”:0,“debtTotal”:0,“issues”:,“components”:,“facets”:}

But it looks pretty empty right? Because my code 24 code smells and 1 security hotspot. (which is wanted)

Do you have an idea why for example the 24 code smells arent listed?

I can’t see what “issues” actually looks like. It’s a collection of issues so just printing the JSON won’t show it. You’ll need to do something like:

issuesList=json.issues

Then you may be able see what’s in issuesList.

1 Like

issues looks like this:

“issues”:, just 2 empty brackets

EDIT: ok you can not see it just two […] empty brackets without the dots.

And if I understood you right, adding issuesList=json.issues to the URL at the end did not help

EDIT2: it also says totaldebt=0 even tho my added debt is 3.30h

I should have seen the “total: 0” the first time I looked at it. So it returned no issues. Are you certain that “Sonar-Results” is the project key (not the project name)? Go to the project page in Sonarqube. On the right at the bottom of the “About this project” section is the project key.

1 Like

omg it is the Project-Name I will check your approach, man sorry but I never worked with this stuff before.

EDIT: it gave me a huuuge Json file and it seems it printed the same thing over and over again.
Has it to do with the pagesize=500?

I mean the page when I use the URL in chrome

No, don’t add issuesList=json.issues to the end of the URL, it’s java code you run after you get back your result. I would be surprised if you didn’t get errors back from the API call with that at the end of the URL.

Something like this:

def issuesList = json.issues
issuesList.each {println it}

I’m not a Java dev so I’m guessing at that. It’s Groovy code from our Jenkins pipeline so it’s at least close.

1 Like

okay thanks!

I will check the Java-Code tomorrow, first I needed the right URL and the key was indeed wrong! So thanks for pointing that out.

If I use the webAPI and there are more paramters do I add them with an ‘&’ in the URL?

Because api/search/issues has a lot of optional parameters.

You’re going to have to implement paging at some point in time. Without the ps parameter you will never get back more than 100 issues. Without paging you’ll never get more than 500. Paging would look something like this (again Groovy, not Java):

List issuesList=New List()
int p = 1
int pages = 1
while (p <= pages) {
	String urlToConnect = "${sonarBaseUrl}/api/issues/search?pageSize=500&componentKeys=Sonar-Results&ps=500&p=${p}"
	bauth = 'Basic ' + javax.xml.bind.DatatypeConverter.printBase64Binary(rawToken.getBytes())
	url = new URL(urlToConnect)
	HttpURLConnection conn = url.openConnection()
	conn.setRequestMethod('GET')
	conn.setRequestProperty("Authorization", bauth)
	conn.setRequestProperty( 'Accept', 'application/json' )
	conn.connect()
	def json = readJSON text: conn.inputStream.text
	conn.disconnect()
	double dPages = json.paging.total/json.paging.pageSize
	pages = Math.floor(dPages)+1
	issuesList += json.issues
	p = p + 1
}

When it’s done issuesList has all the issues.

The most useful besides this might be createdAfter if you want to run this daily and just see new issues.

1 Like

Thanks mate! You are helping a lot!
I will continue tomorrow in this thread, and I would appreciate it if you could help me again!

No problem.

QuickUpdate:

I receive the json file via a ‘GET’-method.
I parsed the json response I receive. This is actually huge for me and everything works fine!

Now I try to list each issue . Like for every issue a new line!

But another question. The api offers an option to add more parameteres like severities for example. How would I change the url?

Like this?

http://localhost:9000/api/issues/search/severities?pageSize=500&componentKeys={project_key}
Because this will end in an error

EDIT http://localhost:9000/api/issues/search?pageSize=500&severities=MINOR&componentKeys={project_key}

Okay this works. Do I always add a parameter with & in the URL?

Yes. Any parameters in the API doc are added to the URL with &. Everything is case-sensitive so make sure you get the case right for both the parameter and value.

1 Like