API - How to obtain project's metrics and last analysis date - Question

Sonarqube 8.9 Enterprise Edition

How can we obtain for a given project, using Sonarqube API, the last analysis date and the subset of metrics associated with that last analysis date?

So far we are using the GET api/measures/component_tree method which has the following example response:

{
  "paging": {
    "pageIndex": 1,
    "pageSize": 100,
    "total": 3
  },
  "baseComponent": {
    "key": "MY_PROJECT",
    "name": "My Project",
    "qualifier": "TRK",
    "measures": [
      {
        "metric": "new_violations",
        "period": {
          "value": "255"
        }
      },
      {
        "metric": "complexity",
        "value": "42"
      },
      {
        "metric": "ncloc",
        "value": "1984"
      }
    ]
  },
  "components": [
    {
      "key": "com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/ElementImpl.java",
      "name": "ElementImpl.java",
      "qualifier": "FIL",
      "language": "java",
      "path": "src/main/java/com/sonarsource/markdown/impl/ElementImpl.java",
      "measures": [
        {
          "metric": "new_violations",
          "period": {
            "value": "25"
          }
        },
        {
          "metric": "complexity",
          "value": "12"
        },
        {
          "metric": "ncloc",
          "value": "114"
        }
      ]
    },
    {
      "key": "com.sonarsource:java-markdown:src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java",
      "name": "ElementImplTest.java",
      "qualifier": "UTS",
      "language": "java",
      "path": "src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java",
      "measures": [
        {
          "metric": "new_violations",
          "period": {
            "value": "0"
          }
        }
      ]
    },
    {
      "key": "com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl",
      "name": "src/main/java/com/sonarsource/markdown/impl",
      "qualifier": "DIR",
      "path": "src/main/java/com/sonarsource/markdown/impl",
      "measures": [
        {
          "metric": "new_violations",
          "period": {
            "value": "25"
          }
        },
        {
          "metric": "complexity",
          "value": "35",
          "period": {
            "value": "0"
          }
        },
        {
          "metric": "ncloc",
          "value": "217",
          "period": {
            "value": "0"
          }
        }
      ]
    }
  ],
  "metrics": [
    {
      "key": "complexity",
      "name": "Complexity",
      "description": "Cyclomatic complexity",
      "domain": "Complexity",
      "type": "INT",
      "higherValuesAreBetter": false,
      "qualitative": false,
      "hidden": false,
      "custom": false
    },
    {
      "key": "ncloc",
      "name": "Lines of code",
      "description": "Non Commenting Lines of Code",
      "domain": "Size",
      "type": "INT",
      "higherValuesAreBetter": false,
      "qualitative": false,
      "hidden": false,
      "custom": false
    },
    {
      "key": "new_violations",
      "name": "New issues",
      "description": "New Issues",
      "domain": "Issues",
      "type": "INT",
      "higherValuesAreBetter": false,
      "qualitative": true,
      "hidden": false,
      "custom": false,
      "bestValue": "0"
    }
  ],
  "period": {
    "mode": "previous_version",
    "date": "2016-01-11T10:49:50+0100",
    "parameter": "1.0-SNAPSHOT"
  }
}

Is there a better way to obtain last analysis date and respective metrics?

Thank you in advance.

Hi,

Unless you’re specifically looking at a ‘history’ web service, you’re always going to get the latest values. To get metadata about the analysis, take a look at the api/project_analyses/search service.

 
HTH,
Ann

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.