After scan is finished it's not possible to get /api/measures/component?metricKeys=coverage

SonarQube: Enterprise Edition Version 9.2.4 (build 50792)
Sonar client: 4.7.0.2747

Scan is launched for merge request in gitlab. I am requesting coverage for pull request.

Imidietly after scan (using scanner client) is finished I try to get coverage by following call:
http:///api/measures/component?metricKeys=coverage&component=&pullRequest=

I am getting:
404 : “{“errors”:[{“msg”:“Component \u0027u0027 of pull request \u0027\u0027 not found”}]}”

Interestingly if I put some sleep (1 second) after scan is finished and before i do a call to get coverage everything is fine.

It seems it has to do something with the fact that it’s a new pull request and regardless scan is finished and it generates link with results, it still requires some time before it will be possible for the api call i mentioned to be able to return coverage. Also, if i retry the operation(scan and get results) on already existing pull request there are no issues like this.

Could you please elaborate on this issue, is such behavior is expected or maybe there are some other ways I can get coverage right away after the scan is finished without adding any sleeps…

As a side observation under same circumstances if i do scan on new pull request and call another api (/issues/search?) to get list of detected issues and it successfully works without any additional sleeps,

Thank you.

Hi,

There are two parts to analysis: what happens on your CI agent, and what happens server-side. CI-side analysis produces an analysis report that is submitted to the server, queued for processing, (eventually) popped off the queue, and processed. So it’s quite normal that results aren’t available immediately after analysis is through on the CI side.

 
HTH,
Ann

Got a bit more helpful reply on stackOverflow, if someone needs please see below:

After the call from the scanner client completes, SonarQube executes a “background task” in the project that finalizes the computations of measures. When the background task is complete, your measures will be available. This is why adding a “sleep” appears to work for you. In reality, it’s just luck that you’re sleeping long enough. The proper way to do this is to either manually check the status of the background task, or use tools that check for the background task completion under the covers.

If you’re using Jenkins pipelines, and you have the “webhook” properly configured in SonarQube to notify completion of the background task, then the “waitForQualityGate” pipeline step does this, first checking to see if the task is already complete, and if not, going into a polling loop waiting for it to complete.

The machinery uses the “report-task.txt” file that should be written by the scanner. This is in the form of a Java properties file, but there’s only one property in the file that you care about, which is the “ceTaskId” property. That is the id of the background task. You can then make an api call to “/api/ce/task?id=”, which returns a block that tells you whether the background task is complete or not.