Access quality gate status from sonarqube api
Asked Answered
A

4

15

My question:

I would like to obtain the current status of the quality gate for my projects in SonarQube via the SonarQube web api, but I do not see quality gate as an available metric in the documentation. What is the best way to access this information to include in a report?

Background:

I am using the SonarQube API to access information about project analyses. The output from the API is used to generate a weekly report. I can access the information about each project with the following SonarQube API call.

http://my-sonar-site/api/resources/index/?resource=$PROJECT_KEY&metrics=$METRIC_LIST

The metric list is populated from the SonarQube API documentation at

http://docs.sonarqube.org/display/SONAR/Metric+definitions

This gets me everything I want with the exception of the status of the quality gate.

What I've tried:

  • I have looked through the documentation for metrics at the above link, but failed to find quality gate status in that part of the api.
  • I have also looked at the quality gate API documented at http://nemo.sonarqube.org/api_documentation#api/qualitygates - but the quality gate API seems to be used for creating and managing quality gates and project associations rather than obtaining the current status of the gate on a per-project basis.
Abott answered 12/12, 2014 at 14:17 Comment(0)
P
3

You can retrieve what you want with the "quality_gate_details" metric. As an example on Nemo: http://nemo.sonarqube.org/api/resources/index/?resource=org.codehaus.sonar:sonar&metrics=quality_gate_details

[{"id":48569,"key":"org.codehaus.sonar:sonar","name":"SonarQube","scope":"PRJ","qualifier":"TRK","date":"2014-11-30T16:13:17+0000","creationDate":null,"lname":"SonarQube","version":"5.0-SNAPSHOT","description":"Open source platform for continuous inspection of code quality","msr":[{"key":"quality_gate_details","data":"{\"level\":\"ERROR\",\"conditions\":[{\"metric\":\"blocker_violations\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"3.0\",\"level\":\"ERROR\"},{\"metric\":\"critical_violations\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"15.0\",\"level\":\"ERROR\"},{\"metric\":\"test_failures\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\",\"actual\":\"0.0\",\"level\":\"OK\"},{\"metric\":\"skipped_tests\",\"op\":\"GT\",\"warning\":\"0\",\"actual\":\"0.0\",\"level\":\"OK\"},{\"metric\":\"coverage\",\"op\":\"LT\",\"warning\":\"\",\"error\":\"80\",\"actual\":\"81.5\",\"level\":\"OK\"},{\"metric\":\"test_errors\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\",\"actual\":\"0.0\",\"level\":\"OK\"},{\"metric\":\"sqale_effort_to_grade_a\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\",\"actual\":\"0.0\",\"level\":\"OK\"}]}"}]}]

For now, to get a full list of metrics, you can browse the "metrics" database table. Feel free to watch https://jira.codehaus.org/browse/SONAR-5378 that should make the detailed list of metrics available directly on the SonarQube web interface.

Printable answered 14/12, 2014 at 12:32 Comment(1)
unfortunately, api/resources API is deprecated since Sonarqube 5.4Feathercut
C
41

Since SonarQube 5.3 you can get the Qualitygate Status using the API: http://[sonarhost]/api/qualitygates/project_status?analysesId=[ID]

Since SonarQube 5.4: http://[sonarhost]/api/qualitygates/project_status?projectKey=[key]

Chekhov answered 29/3, 2016 at 15:15 Comment(2)
It is possible to get result from different branches, like: ?branch=master&projectKey=projectnameDoing
Hi Pwnstart, wondering if you know how to run it from a branch? thanksBegotten
P
3

You can retrieve what you want with the "quality_gate_details" metric. As an example on Nemo: http://nemo.sonarqube.org/api/resources/index/?resource=org.codehaus.sonar:sonar&metrics=quality_gate_details

[{"id":48569,"key":"org.codehaus.sonar:sonar","name":"SonarQube","scope":"PRJ","qualifier":"TRK","date":"2014-11-30T16:13:17+0000","creationDate":null,"lname":"SonarQube","version":"5.0-SNAPSHOT","description":"Open source platform for continuous inspection of code quality","msr":[{"key":"quality_gate_details","data":"{\"level\":\"ERROR\",\"conditions\":[{\"metric\":\"blocker_violations\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"3.0\",\"level\":\"ERROR\"},{\"metric\":\"critical_violations\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"15.0\",\"level\":\"ERROR\"},{\"metric\":\"test_failures\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\",\"actual\":\"0.0\",\"level\":\"OK\"},{\"metric\":\"skipped_tests\",\"op\":\"GT\",\"warning\":\"0\",\"actual\":\"0.0\",\"level\":\"OK\"},{\"metric\":\"coverage\",\"op\":\"LT\",\"warning\":\"\",\"error\":\"80\",\"actual\":\"81.5\",\"level\":\"OK\"},{\"metric\":\"test_errors\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\",\"actual\":\"0.0\",\"level\":\"OK\"},{\"metric\":\"sqale_effort_to_grade_a\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\",\"actual\":\"0.0\",\"level\":\"OK\"}]}"}]}]

For now, to get a full list of metrics, you can browse the "metrics" database table. Feel free to watch https://jira.codehaus.org/browse/SONAR-5378 that should make the detailed list of metrics available directly on the SonarQube web interface.

Printable answered 14/12, 2014 at 12:32 Comment(1)
unfortunately, api/resources API is deprecated since Sonarqube 5.4Feathercut
T
1

This curl command gets response for a special project key and branch and writes in a json file:

curl -u $YOUR_TOKEN: -G --data-urlencode "branch=$YOUR_BRANCH" --data-urlencode "projectKey=$YOUR_PROJECT_KEY" ${SONAR_HOST}/api/qualitygates/project_status > result.json
Tabbie answered 22/11, 2021 at 13:54 Comment(0)
U
0

Not sure how above api worked. It should be curl -k -u ${TOKEN}: -G --data-urlencode --data-urlencode https://${SONAR_HOST}/api/qualitygates/project_status?projectKey=${PROJECT_KEY}

Also you can pretty print by adding | jq '.'

Ulloa answered 14/1, 2022 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.