How to get test coverage information by REST in SonarQube 5.2?

3,313 views
Skip to first unread message

Michael Rüegg

unread,
Oct 30, 2015, 12:14:38 PM10/30/15
to SonarQube
Hi,

I've just realized that the old REST endpoint /api/coverage/show disappeared in SonarQube 5.2 (at least in 5.2 RC3+RC4).

Is /api/tests/covered_files the new replacement for this? If yes, how should this be used then? As an input for this REST endpoint, I need a testId. How can I get that?

More general question: is there a Wiki page or something similar which describes the changes of the REST API of 5.2 compared to older versions? And is there also one for SonarQube plug-in developers?

Thanks in advance,
Michael

Teryk Bellahsene

unread,
Nov 2, 2015, 7:59:51 AM11/2/15
to SonarQube
Hi Michael,

The tests WS documentation is available here: http://nemo.sonarqube.org/api_documentation/api/tests
You could use http://nemo.sonarqube.org/api_documentation/api/tests/list to get a test id.
What information do you need? What do you want to do?

HTH,

Teryk

Michael Rüegg

unread,
Nov 2, 2015, 8:10:47 AM11/2/15
to Teryk Bellahsene, SonarQube
Hi Teryk,

Thanks for your reply. But in order to call /api/tests/list, I need one of 
  • testFileId
  • testId
  • sourceFileId and sourceFileLineNumber
I only have the component key of a source file. I want to get coverage information for a given source file by component key. How can I (efficiently) achieve that? Before 5.2, this was possible with *one* REST call (/api/coverage/show).

Best regards,
Michael

--
You received this message because you are subscribed to a topic in the Google Groups "SonarQube" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarqube/qml094rxwjA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/a0a25331-9c30-4ec9-8c27-cb59b48ad9c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Teryk Bellahsene

unread,
Nov 3, 2015, 10:22:05 AM11/3/15
to Michael Rüegg, SonarQube
Hi Michael,

All the cases should be covered by the existing WS (old -> new):
- api/tests/covered_files -> api/tests/covered_files
- api/tests/show -> api/tests/list with testFileKey
- api/tests/test_cases -> api/tests/list with sourceFileId and sourceLineNumber

I created a ticket to be able to use api/tests/list with sourceFileKey and sourceLineNumber
In 5.2, you'll need to get the sourceFileId, using for example the internal (so subject to change) WS api/components/search using the q parameter (with the whole component key).

HTH

Teryk Bellahsene | SonarSource
Software Developer
http://sonarsource.com

Michael Rüegg

unread,
Nov 3, 2015, 11:35:06 AM11/3/15
to Teryk Bellahsene, SonarQube
Hi Teryk,

Thanks for your reply. This is a bit unfortunate because /api/components/search is 1) internal API 2) inefficient because I can only use a file name (not the full path) for the search parameter which could yield thousands of results for a common file name like e.g. “Component.java” and I would have to manually parse them to get the one I’m interested for (beside being an error prone task).

Before 5.2, I could just execute one REST call (/api/coverage/show?key=sourceFileComponentKey) and got something like this 

"coverage": [
         [
             51,   // line number
             true, // covered?
             null,
             null,
             null
          ],
          // …
]

To get the same coverage info (all lines and if they are covered or not) of a source file in SonarQube 5.2, I now have to:

1. call /api/components/search
2. parse the whole list and try to get the source file ID for the file I’m interested for
3. call /api/tests/list with this source file ID and sourceFileNumber (but the latter I don’t have! I want to get the coverage info for all lines of the file) => any ideas?

Is there any chance to get this changed before 5.3? Otherwise, I cannot use this feature anymore.

Beside that, could you please explain the difference between source file key and source file ID? The source file key has the advantage for me that I can deduce it from the build files (e.g., group and artifact ID from the pom.xml + path in the repository). With the source file ID, I don’t have this possibility anymore as this just seams to be an UUID. Am i right?

Best regards,
Michael

Apart from that, I don’t think it will work. I want to get the covered lines of a source file. While I can get the source ID with api/components/search, I could call 

Michael Rüegg

unread,
Nov 4, 2015, 4:02:59 AM11/4/15
to Teryk Bellahsene, SonarQube
Hi Teryk,

Thanks for your feedback, I wasn’t aware of the new /api/components/search endpoint.

My use case is that I want to know which lines are covered by tests for a given source file. If I now have to call this service with every line number of a file, then I have to call this service n times for a file with line length n. There must be a better way to do this. Before 5.2, I could just do *one* call with /api/coverage/show to get this information.

Any ideas?

Best regards,
Michael
 
On 04 Nov 2015, at 09:53, Teryk Bellahsene <teryk.be...@sonarsource.com> wrote:

 Hi Michael,

The sourceFileKey won't be added in 5.2. It will be added in 5.3. Feel free to follow the corresponding ticket.
In SonarQube 5.2, you now have to:
  • call api/components/search providing your component key to the 'q' parameter and choosing the appropriate qualifiers to filter by component qualifier (project, file, directory etc.). Please look at the documentation for more information
  • no 2nd step, there should be only one component
  • call api/tests/list with the source file id and the source line number. That's what you had to do before. There's no straightforward way to get all lines of the file. What's your use case?

See the picture showing the line number was needed in 4.5.6

<image.png>

HTH,


Teryk Bellahsene | SonarSource
Software Developer
http://sonarsource.com

Teryk Bellahsene

unread,
Nov 4, 2015, 4:12:12 AM11/4/15
to Michael Rüegg, SonarQube
Sorry, but I doubt you just had to do one call to api/tests/show providing the source file key. For api/coverage/show you had to put a test file key. In that case, just use the 5.2 api/tests/list with you test file key.
Inline images 1

Otherwise, checks how it's done today in the UI.

HTH,

Teryk Bellahsene | SonarSource
Software Developer
http://sonarsource.com

Michael Rüegg

unread,
Nov 4, 2015, 4:48:52 AM11/4/15
to Teryk Bellahsene, SonarQube
Hi Teryk,

I always used /api/coverage/show with only the source file key (and without specifying line numbers with from/to), and it worked perfectly:



I’ve seen that the SonarQube UI uses /api/sources/lines. Although this is an internal API, at least it will give me all the information with just one call (including duplication infos).

Now the only problem is that I need a UUID for this. Could you please show me a way to get the UUID for a component key with SonarQube’s REST interface?

I cannot use /api/components/search for this, because for that I would also need a componentUUID which I don’t have. All I have is the component key of the source file I want to annotate with coverage and line duplication information.

Best regards,
Michael



On 04 Nov 2015, at 10:11, Teryk Bellahsene <teryk.be...@sonarsource.com> wrote:

Sorry, but I doubt you just had to do one call to api/tests/show providing the source file key. For api/coverage/show you had to put a test file key. In that case, just use the 5.2 api/tests/list with you test file key.
<image.png>

HTH,

Teryk Bellahsene

unread,
Nov 4, 2015, 4:55:41 AM11/4/15
to Michael Rüegg, SonarQube
Hi,

If you're going to use api/sources/lines please note it's subject to change in future versions of SonarQube.
And then, you just have to use the 'key' parameter, no need for the ill-named uuid parameter.
The uuid is actually what I call the id. So if you use api/components/search, the id in the response is the 'uuid' in api/sources/lines.
But if my understanding is correct, you don't need the component id anymore.

HTH,

Teryk Bellahsene | SonarSource
Software Developer
http://sonarsource.com

Michael Rüegg

unread,
Nov 4, 2015, 5:01:54 AM11/4/15
to Teryk Bellahsene, SonarQube
Perfect, thanks! Just for reference:

/api/sources/lines?key=componentKey

did the trick. You might want to add this to the web service description, as only the UUID parameter is documented.

Thanks again,
Michael


On 04 Nov 2015, at 10:55, Teryk Bellahsene <teryk.be...@sonarsource.com> wrote:

/sources/lines

Teryk Bellahsene

unread,
Nov 4, 2015, 5:10:30 AM11/4/15
to Michael Rüegg, SonarQube
Not sure, what you mean. See the WS description.
The uuid and the key are both documented.

Teryk Bellahsene | SonarSource
Software Developer
http://sonarsource.com

Michael Rüegg

unread,
Nov 4, 2015, 5:12:40 AM11/4/15
to Teryk Bellahsene, SonarQube
Excuse me, I was looking at the web service description of SonarQube 5.1.2 where this parameter was not mentioned.

All good in 5.2.

Best regards,
Michael

On 04 Nov 2015, at 11:10, Teryk Bellahsene <teryk.be...@sonarsource.com> wrote:


Teryk Bellahsene | SonarSource

Reply all
Reply to author
Forward
0 new messages