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.
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?
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.
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.
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.
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.
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.