Liste of issue for a rule with WEB API

Hello,
I search an sample of use the WEB API “GET api/issues/search” to obtain the number of issue for a rule.
I try this :

http://sonarqube:9000/api/issues/search?rules=Aragonite2&resolved=false&rules=c%3AS1131
Where Aragonite is the project name in SonarQube
AS1131 is the rule code in SonarQube.

But I have this error message :
ERROR code=400 {"error:[{“msg”: "Invalid rule key: Aragonite2}]}

Thanks a lot for yours answers.

Fred.

Hi,

your api call is wrong, you have parameter rules two times
see yoursonarinstance/web_api/api/issues for the web api documentation.

Gilbert

1 Like

Hi,
Thank for your answer.
I changed my api call and it works now.
I have another question, how can I read the answer to the API call ?
do I have to write a parser JSON ?
Thank
Fred

Hi,
it’s json parsing as usual.
First you’ll have to get the json by web api call, then you’ll need to parse it.
My generic Groovy snippet for Sonarqube web api calls is like that:

import groovy.json.*

def sonarRest(url,method,usertoken) {
  jsonSlurper = new JsonSlurper()
  raw = usertoken + ':'
  bauth = 'Basic ' + javax.xml.bind.DatatypeConverter.printBase64Binary(raw.getBytes())
  conn = new URL(url).openConnection() as HttpURLConnection
  conn.setRequestMethod(method)
  conn.setRequestProperty("Authorization", bauth)
  conn.connect()
  httpstatus = conn.responseCode
  object = jsonSlurper.parse(conn.content)
}

I took Groovy because it’s my weapon of choice and best for Jenkins pipelines + scripting.
For Jenkins wrap it up in a shared library but !! use JsonSlurperClassic() with @NonCPS annotation
see
http://groovy-lang.org/json.html
https://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html

Gilbert