Search code examples
sonarqubesonarqube-web

SonarQube 5.5 API - componentKey convention


I'm trying to extract coverage data from our SonarQube instance using the 'api/measures/component' endpoint. This is for a multi module java project. Using POSTMAN i can query for a single java file using a 'componentKey' which is build up of string like this

[maven-groupId]:[maven-artifactId]:[path to java file]

which generates a string like

com.i.pc.e.components.o:om-wf-e:src/main/java/com/i/e/o/wf/actions/Xxxx.java

and returns this json response

{
"component": {
  "id": "AVci_6G7elHqFlTqG_OC",
  "key": "com.i.pc.e.components.o:om-wfl-e:src/main/java/com/i/e/o/wf/actions/Xxxx.java",
  "name": "Xxxx.java",
  "qualifier": "FIL",
  "path": "src/main/java/com/i/e/om/wf/actions/Xxxxx.java",
  "language": "java",
  "measures": []
},
"metrics": [
  {
    "key": "coverage",
    "name": "Coverage",
    "description": "Coverage by unit tests",
    "domain": "Coverage",
    "type": "PERCENT",
    "higherValuesAreBetter": true,
    "qualitative": true,
    "hidden": false,
    "custom": false,
    "decimalScale": 1,
    "bestValue": "100.0",
    "worstValue": "0.0"
  }
]

}

This is fine for a one-off request but I need to do this for a number of Java source files and I won't have the Maven details to hand.

Is there a convention or documentation on the format of the 'componentKey'?

Ideally i'd like to define the full package and java source file name in the 'compomentKey' or is there a way to look up the 'componentId' via some other REST API call?


Solution

  • Use the components service to iterate through your tree (or sub-trees within your tree) starting from your project and feeding your sonar.projectKey value into the baseComponentKey parameter. Pay particular attention to the strategy parameter, which determines what sub-set of children a call retrieves.

    Once you have the data on each child, it should be simple to iterate through them to get their measures.