Web API: search history won't return values for metrics on new code

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    SonarQube Enterprise Edition 8.9.2 LTS

  • what are you trying to achieve
    Retrieve values of metrics on new code using /api/measures/search_history

  • what have you tried so far to achieve this
    https://<sonarqube-server>/api/measures/search_history?component=<project-key>&from=2021-10-13T14:47:51%2B0200&to=2021-10-13T14:47:51%2B0200&metrics=new_coverage,new_maintainability_rating,new_reliability_rating,new_security_rating,new_duplicated_lines_density,coverage,major_violations,critical_violations,blocker_violations,test_errors,test_failures

Calling the URL above will return the JSON response below. Note that metrics on new code won’t return any value. Is this the expected behavior? Are metrics on new code not stored in the history?

{"paging":{"pageIndex":1,"pageSize":100,"total":1},"measures":[{"metric":"test_errors","history":[{"date":"2021-10-13T14:47:51+0200","value":"0"}]},{"metric":"test_failures","history":[{"date":"2021-10-13T14:47:51+0200","value":"0"}]},{"metric":"coverage","history":[{"date":"2021-10-13T14:47:51+0200","value":"86.9"}]},{"metric":"new_coverage","history":[{"date":"2021-10-13T14:47:51+0200"}]},{"metric":"blocker_violations","history":[{"date":"2021-10-13T14:47:51+0200","value":"0"}]},{"metric":"critical_violations","history":[{"date":"2021-10-13T14:47:51+0200","value":"0"}]},{"metric":"major_violations","history":[{"date":"2021-10-13T14:47:51+0200","value":"87"}]},{"metric":"new_duplicated_lines_density","history":[{"date":"2021-10-13T14:47:51+0200"}]},{"metric":"new_maintainability_rating","history":[{"date":"2021-10-13T14:47:51+0200"}]},{"metric":"new_reliability_rating","history":[{"date":"2021-10-13T14:47:51+0200"}]},{"metric":"new_security_rating","history":[{"date":"2021-10-13T14:47:51+0200"}]}]}

Hi,

No, they’re not. Why? Because once they’re “history”, they’re no longer “on new code”.

 
:woman_shrugging:
Ann

Well, overall measures on a past date are no longer the current ones and still are in the history. Shouldn’t the history show the values on a given date regardless of the scope of the metrics? How could we retrieve the whole picture (all the quality gate conditions) on a past date, otherwise?

Thanks,
Bàrbara

A finding: I see that using the quality_date_details metric, values for metrics on new code are returned. It seems that values for new code are stored, right?

https://<sonarqube-server>/api/measures/search_history?component=<project-key>&from=2021-10-13T14:47:51%2B0200&to=2021-10-13T14:47:51%2B0200&metrics=quality_gate_details

{"paging":{"pageIndex":1,"pageSize":100,"total":1},"measures":[{"metric":"quality_gate_details","history":[{"date":"2021-10-13T14:47:51+0200","value":"{\"level\":\"ERROR\",\"conditions\":[{\"metric\":\"new_reliability_rating\",\"op\":\"GT\",\"period\":1,\"error\":\"1\",\"actual\":\"3\",\"level\":\"ERROR\"},{\"metric\":\"new_security_rating\",\"op\":\"GT\",\"period\":1,\"error\":\"1\",\"actual\":\"2\",\"level\":\"ERROR\"},{\"metric\":\"new_maintainability_rating\",\"op\":\"GT\",\"period\":1,\"error\":\"1\",\"actual\":\"1\",\"level\":\"OK\"},{\"metric\":\"new_coverage\",\"op\":\"LT\",\"period\":1,\"error\":\"80\",\"actual\":\"86.56838656838657\",\"level\":\"OK\"},{\"metric\":\"coverage\",\"op\":\"LT\",\"error\":\"70\",\"actual\":\"86.9\",\"level\":\"OK\"},{\"metric\":\"new_duplicated_lines_density\",\"op\":\"GT\",\"period\":1,\"error\":\"3\",\"actual\":\"5.379339905700815\",\"level\":\"ERROR\"},{\"metric\":\"blocker_violations\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"0\",\"level\":\"OK\"},{\"metric\":\"critical_violations\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"0\",\"level\":\"OK\"},{\"metric\":\"test_errors\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"0\",\"level\":\"OK\"},{\"metric\":\"test_failures\",\"op\":\"GT\",\"error\":\"0\",\"actual\":\"0\",\"level\":\"OK\"}],\"ignoredConditions\":false}"}]}]}

Not all values on new code. Unless, that is, you’ve added every single ‘on New Code’ metric to your Quality Gate.

 
Ann

I see. So, for the values on new code that are stored, shouldn’t the call return them when requested separately?:

metrics=new_coverage,new_maintainability_rating,new_reliability_rating,new_security_rating,new_duplicated_lines_density

Hi,

They are returned where they’re stored. They’re not stored as historical metrics. They’re stored as ancillary data on a QG change when they may be relevant to the QG status.

 
:woman_shrugging:
Ann

Thank you, Ann. Just to make sure I got it right :sweat_smile:

Do you mean that the quality_gate_details history stores a snapshot of the relevant quality gate measures (which may include measures on new code), whereas the measures on new code are not stored in the history?

Correct.

Thanks for the clarification, Ann. I now see that requesting the quality_gate_details is the way to retrieve past quality gate measures on new code.

Thank you!
Bàrbara