REST api to request quality gate status by branch?

Hi Sonar Team!

The web api currently supports requesting the quality gate status of a project:
GET api/qualitygates/get_by_project
https://next.sonarqube.com/sonarqube/web_api/api/qualitygates?query=quality

With recent developments now branches, both long and short lived, also have QG statuses, not just the master.
Are there plans to support requesting the branch specific quality gate status from the server?

Motivation
The sonar-build-breaker is unmaintained and not compatible with latest releases. ( though a solution is present in a PR)
If the quality gate status could be requested for all kinds of builds on REST, a lightweight solution for to reimplement the QG build breaker might be possible, relying only on the public API, and no dependence on the Sonar Java package structure.

Thanks and cheers
Balázs

P.s. thanks to the blogpost I see webhooks are also a possibility to solve this, and you do not suggest using buildbreakers.

Hi,

you may use the web api, api/project_analyses/search with the internal parameter branch and category QUALITY_GATE, see https://yoursonarhost/web_api/api/project_analyses?internal=true

Regards,
Gilbert

2 Likes

I can’t get this to work. What does the full curl call look like?

I am doing ‘curl -u username:pass <sonarqube_url>/api/project_analyses/search?project=<my_project>&internal=true&branch=<my_branch>‘

Hi,

note to me = don’t you ever use real clickable hyperlinks for example urls :skull: again,
use preformatted text instead !!
Unfortunately, i can’t edit those older posts anymore to fix this - gets me a lot of
‘Popular Link’ badges though :flushed:

David, this https://yoursonarhost/web_api/api/project_analyses?internal=true link is simply the link to the builtin web api doc, simply replace yoursonarhost with the name of your instance. It’s also in the footer of your instance, see the ‘Web API’ link.

internal=true means show internal parameters also, there is a checkbox to activate this and also for deprecated stuff.

Whereas the web api api/project_analyses/search has no parameter internal=true.
See the web api docs for parameters and examples and try with browser or a tool as postman.
In your url the internal=true should be deleted and the category QUALITY_GATE is missing.

If you want to dig deeper in the Sonarqube web api you may use the developer tools of your browser
and see what’s going on interacting with Sonarqube web ui.

Gilbert

1 Like

Do we know if there are any bugs with this? I’m trying to use this to pull a quality gate for a specific branch in my CI/CD pipeline; however, the URL links me to the branch I’m trying to pull, but what the curl is pulling in for JSON doesn’t match my branch and after further inspection, it is pulling the quality gate json for the master branch.

Welcome :slight_smile:

what’s your Sonarqube edition = Enterprise / Developer / Community
and your Sonarqube version ?

Gilbert

Enterprise 7.9.1

7.9.1 is very way end of life. You should update to either Sonarqube 8.9.8 LTS or
latest 9.4

I agree, but that doesn’t address my question. I don’t control or have any say in the version. If I did, it would absolutely be updated…

Since i no longer have an instance with 7.9.1 i tried on Sonarqube Enterprise 9.4.0
with
https://somesonarhost/api/project_analyses/search?project=org.sonarqube:module1&category=QUALITY_GATE&branch=foobar

and the repsonse has

{
	"paging": {
		"pageIndex": 1,
		"pageSize": 100,
		"total": 2
	},
	"analyses": [
		{
			"key": "AX-HroypygL5Jzv1xWJq",
			"date": "2022-03-14T10:07:13+0100",
			"events": [
				{
					"key": "AX-HrpSYygL5Jzv1xWQw",
					"category": "QUALITY_GATE",
					"name": "Passed",
					"description": ""
				}
			],
			"projectVersion": "1.0-SNAPSHOT",
			"manualNewCodePeriodBaseline": false,
			"revision": "ac7beac7b5b7e0f2fea6a06dc9956ea29b10efbd",
			"detectedCI": "Jenkins"
		},	
 ...

so it works as expected. The wording changed though, now it’s either ‘Passed’ or ‘Failed’.
Before Sonarqube 8.9.x it was ‘Red (was Green)’.

Ok, that’s similar to what I see; however the URL that I navigate to doesn’t make the actual JSON that I pull into a variable. Here’s my use case so you understand:

I’m using the api call similar to the below to query SonarQube from a Gitlab pipeline

I’m storing the json results in a variable which I parse through to see if the quality gate passes or fails. When I use that api call, the URL will display the info I expect, but the json that gets stored in the variable is the results from a master branch instead of the branch I specify.

Given my version is so old, I don’t expect anyone to have a solution for me. I was just hoping someone could confirm it was an issue. Ideally, I can convince them to upgrade.

I guess that’s because Sonarqube 7.9.1 still used so called long lived and short lived branches.
Only long lived branches have those events and that might be the reason you get results from the
main branch which is default master and a long lived branch.
i.e. a short lived branch can’t have a sonar.projectVersion

Since Sonarqube 8.x a branch is a branch.
As a customer using the Enterprise edition you need to either use the LTS or the latest version to get support from Sonarsource.
We have been driving with the Enterprise edition since 2016 and switched to using the latest version with the new branching feature in Sonarqube 7.
Beside some glitches in early Sonarqube 8.x version we didn’t have real problems.

1 Like