Metric extraction


(Huginn87) #1

Hello,
sorry but I’m a beginner in using a web API. I would like to ask for your help as I would have to extract the technical debt metric but not quite understand how to do it (maybe using python). I also tried to do a search in the community but I did not find anything explanatory. Many thanks in advance.


(Julien Henry) #2

Hi,

You can call this web service:
https:///api/measures/component?component=&metricKeys=sqale_rating


(Huginn87) #3

Hello, thank you very much for your reply. I tried to make a python script to get the values that I will have to manipulate, but it gives me the following error:

ValueError: No JSON object could be decoded

and as a status code I have 401 and I have seen that means that the credentials are not valid for the target resource, but I have checked them and they are correct :-(.

The code I wrote is this:

import requests
r = requests.get (‘https://sonarcloud.io/api/measures/component?component=&metricKeys=sqale_rating’, auth = (‘user’, ‘pwd’))
code = r.status_code
print “Status code:”, code
data = r.json ()
print data [‘key’] [0]

I do not understand where I’m wrong :frowning:
Many thanks in advance


(Huginn87) #4

Hi, me again. I resolved with the status code 401 using the following url

https://sonarcloud.io/component_measures?id=Huginn87_jabref&metric=sqale_index

but I still have the problem in taking the value of the measure. I tried to change the code like this:

import requests
import json

r = requests.get (‘https://sonarcloud.io/component_measures?id=Huginn87_jabref&metric=sqale_index’,auth= (’ usr ‘,’ pwd '))
code = r.status_code
print “Status code:”, code
json_data = json.loads (r.text)
print json_data

and as a result I have this

Traceback (most recent call last):
File “test.py”, line 7, in
json_data = json.loads (r.text)
File “/usr/lib/python2.7/json/init.py”, line 339, in loads
return _default_decoder.decode (s)
File “/usr/lib/python2.7/json/decoder.py”, line 364, in decode
obj, end = self.raw_decode (s, idx = _w (s, 0) .end ())
File “/usr/lib/python2.7/json/decoder.py”, line 382, ​​in raw_decode
raise ValueError (“No JSON object could be decoded”)
ValueError: No JSON object could be decoded

I tried to look also on the sonarqube documentation but I can not find a solution to the problem. Do you have any suggestions? Thanks a lot in advance for your kind help. Greetings


(Huginn87) #5

Hi, I solved the problem. I report below the solution in case it served someone else. It is not possible to extract the metric with a python script neither with wget nor with curl, but you have to put the url in the address bar

https://sonarcloud.io/api/measures/component_tree?component=keyJabref4.2&metricKeys=sqale_index&ps=100&p=1

Changing the parameters
component -> the key to your project
metricKeys -> the metric you want to take
ps -> indicates the page size and therefore the number of elements that, at most, can be equal to 500 (from 0 to 499)
p -> indicates the page (Eg for projects of 1500 classes, you must first save the values with p = 1, then with p = 2 and then with p = 3)

I hope he can help you. Greetings