How to use API for getting build details in shell script?

web_api
jenkins
sonarqube

(Ashutosh) #1

Hello There,

I want to write a shell script to login and get bugs for a project. I want the dashboard values like bugs, Vulnerabilities, code smells and coverage.
The url of dashboard is: http://www.example.com/dashboard?id=example_project.

Here is what I found online: curl -u MY_LOGIN:MY_PASSWORD https://sonarqube.com/api/user_tokens/search

But when I use it in terminal: curl -X POST -u username:password http://www.example.com/api/user_tokens/search

I get an error: {“errors”:[{“msg”:“Authentication is required”}]}.

Here is the URL from which I used the WebAPI: https://docs.sonarqube.org/display/DEV/Web+API

Please help me use WebAPI in Sonarqube.

Basically What I want to achieve is that I’m using a sonarqube plugin in Jenkins, so I use extended email plugin to send email for job execution and in that email I want to give details like number of bugs in the repository after the build.

Is there any other way?


(Ashutosh) #2

Finally after reading the documentation carefully, I got the values. Here is the script that I created:

#!/bin/bash
vul=$(curl -sX GET -u username:password ‘http://www.example.com/api/issues/search?projectKeys=ssl-dev&types=VULNERABILITY’);
bug=$(curl -sX GET -u username:password ‘http://www.example.com/api/issues/search?projectKeys=ssl-dev&types=BUG’);
no_vul=$(echo $vul | jq -r .total);
no_bug=$(echo $bug | jq -r .total);
echo “Total number of VULNERABILITIES are $no_vul”
echo “Total number of BUGS are $no_bug”

Here is the API documentation URL: https://codeen-app.euclid-ec.org/sonar/web_api/api/issues


(Nicolas Bontoux) #3

Thanks for the follow-through. Quick comment on this:

Note that the WebAPI documentation is embedded in all SonarQube instances ! To all readers: it’s important to check the documentation in the actual instance you’re using, to be sure that what you’re reading corresponds to the SonarQube version you’ll be querying.