Skip to content

Instantly share code, notes, and snippets.

@jscharber
Last active July 24, 2024 14:16
Show Gist options
  • Save jscharber/e4478b897a1dd3a874e6c2773ab03c06 to your computer and use it in GitHub Desktop.
Save jscharber/e4478b897a1dd3a874e6c2773ab03c06 to your computer and use it in GitHub Desktop.

SonarCloud API Examples

All examples use curl Sonarcloud API

Object relationships

Organizations contain projects. When you set up your SonarCloud account, a default organization is created. You can create new organizations via the user interface. There appears to be no method of creating an organization via the API. org -> projects

Authenticating

SonarCloud supports basic authentication if the user has a SonarCloud login. Otherwise, you must create a token for the user.

Using the token in basic auth, note the trailing ":" is required

curl -s -u XXXXXXXXXXXXXXXXXXXXXXXX: https://sonarcloud.io/api/projects/search?organization=acme-demo | jq .

Using the token in host

curl -s https://##################@sonarcloud.io/api/projects/search?organization=acme-demo | jq .

Working with projects

GET a project or list of projects

Pass the query parameter organization to return a list of all its projects

curl -s -u XXXXXXXXXXXXXXXXXX: https://sonarcloud.io/api/projects/search?organization=acme-demo | jq .
{
  "paging": {
    "pageIndex": 1,
    "pageSize": 100,
    "total": 1
  },
  "components": [
    {
      "organization": "acme-demo",
      "key": "films",
      "name": "films",
      "qualifier": "TRK",
      "visibility": "public"
    }
  ]
}

GET a specified list of projects

To get one or more projects add &projects=one,two,three

curl -s -u XXXXXXXXXXXXXXXXXX: https://sonarcloud.io/api/projects/search?organization=acme-demo\&projects=films | jq .

{
  "paging": {
    "pageIndex": 1,
    "pageSize": 100,
    "total": 1
  },
  "components": [
    {
      "organization": "acme-demo",
      "key": "films",
      "name": "films",
      "qualifier": "TRK",
      "visibility": "public"
    }
  ]
}

POST to create new project

curl -X POST -s -u ###############: https://sonarcloud.io/api/projects/create -d "name=sequal&project=films3&organization=acme-demo" | jq .
{
  "project": {
    "key": "films3",
    "name": "sequal",
    "qualifier": "TRK",
    "visibility": "public"
  }
}

Project key

Project keys differ based on the way the organization was created.

If the organization is the default, aka the one created when registering with SonarCloud, the key includes the name of the organization and the project key. If you create another organization, like acme-demo, in the following examples, the key is ONLY the name of the project.

Example of default organization

pavedroad-io_kevlarweb

curl -s -u ########################: https://sonarcloud.io/api/projects/search?organization=pavedroad-io\&projects=pavedroad-io_kevlar-web | jq .
{
  "paging": {
    "pageIndex": 1,
    "pageSize": 100,
    "total": 1
  },
  "components": [
    {
      "organization": "pavedroad-io",
      "key": "pavedroad-io_kevlar-web",
      "name": "Kevlar web token manager",
      "qualifier": "TRK",
      "visibility": "public",
      "lastAnalysisDate": "2019-08-05T15:11:10+0200",
      "revision": "ebf62f763da9b6aa15e0c1e50fa9955fb9160f5b"
    }
  ]
}

Example for non-default organization

Note the organization is acme-demo but the key is only films

curl -s -u ######################: https://sonarcloud.io/api/projects/search?organization=acme-demo\&projects=films | jq .
{
  "paging": {
    "pageIndex": 1,
    "pageSize": 100,
    "total": 1
  },
  "components": [
    {
      "organization": "acme-demo",
      "key": "films",
      "name": "films",
      "qualifier": "TRK",
      "visibility": "public",
      "lastAnalysisDate": "2019-10-27T18:00:09+0100",
      "revision": "8755e7f63af2b71d8b4dffb19f945b5dddb2de29"
    }
  ]
}

Working with Tokens

Get a list of tokens for the authenticated users

curl -s -u #####################: https://sonarcloud.io/api/user_tokens/search | jq .
{
  "login": "jscharber@github",
  "userTokens": [
    {
      "name": "Analyze \"films\"",
      "createdAt": "2019-10-24T16:38:08+0200"
    },
........
    {
      "name": "jms-test",
      "createdAt": "2019-10-25T16:59:54+0200",
      "lastConnectionDate": "2019-10-29T14:17:22+0100"
    }
  ]
}
~~~

### Create a new Token

```bash
curl -s -X POST -u ##############: https://sonarcloud.io/api/user_tokens/generate -d "name=Sample token2" | jq .
{
  "login": "foo@github",
  "name": "Sample token2",
  "token": "834cb69aaf384c10040ac5691871917aa0ef7d41",
  "createdAt": "2019-10-29T14:26:02+0100"
}

Revoke a token

Revoking a token returns HTTP status code of 204, You can confirm the deletion by searching. The revoked token will no longer appear

curl -s -X POST -u ##############################: https://sonarcloud.io/api/user_tokens/revoke -d "name=Sample token" | jq .

Badges

Return metrics badge as HTML SVG

Where metric is one of:

  • bugs
  • code_smells
  • coverage
  • duplicated_lines_density
  • ncloc
  • sqale_rating
  • alert_status
  • reliability_rating
  • security_rating
  • sqale_index = vulnerabilities

project must be specified branch is optional

curl -s -u ##############################: https://sonarcloud.io/api/project_badges/measure?project=pavedroad-io_kevlar-web\&metric=bug
<svg xmlns="http://www.w3.org/2000/svg" height="20" width="78">
    <!-- SONARCLOUD MEASURE -->
    <linearGradient id="b" x2="0" y2="100%">
        <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
        <stop offset="1" stop-opacity=".1"/>
    </linearGradient>
    <clipPath id="a">
        <rect width="78" height="20" rx="3" fill="#fff"/>
    </clipPath>
    <g clip-path="url(#a)">
        <rect fill="#555" height="20" width="59"/>
        <rect fill="#999999" height="20" width="19" x="59"/>
        <rect fill="url(#b)" height="20" width="78"/>
    </g>
    <g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11" text-anchor="left">
        <text x="26" y="15" textLength="27" fill="#010101" fill-opacity=".3">bugs</text>
        <text x="26" y="14" textLength="27">bugs</text>
        <text x="65" y="15" textLength="7" fill="#010101" fill-opacity=".3">0</text>
        <text x="65" y="14" textLength="7">0</text>
    </g>
    <path fill="#010101" fill-opacity=".3" d="M19.58682 9.81722A3.78893 3.78893 0 0 0 17.78048 8.603v-.04378a3.77165 3.77165 0 1 0-7.5433 0v.05069a3.77165 3.77165 0 1 0 3.78317 6.28186 3.7705 3.7705 0 0 0 5.56416-5.0688zm-2.91802 5.21164a2.8247 2.8247 0 0 1-2.8224-2.82124.47463.47463 0 0 0-.94925 0 3.7486 3.7486 0 0 0 .53223 1.9296 2.8201 2.8201 0 1 1-1.08058-4.57805h.01152a1.3202 1.3202 0 0 1 .30758.14745.4747.4747 0 0 0 .61748-.72115 2.1266 2.1266 0 0 0-.61402-.32371 3.75898 3.75898 0 0 0-1.29946-.2304h-.182a2.82125 2.82125 0 1 1 4.45478 2.4192.47462.47462 0 1 0 .55066.773 3.78317 3.78317 0 0 0 1.44806-2.0736 2.8224 2.8224 0 0 1-.97574 5.46969z"/>
    <path fill="#f3702a" d="M19.58682 9.09722A3.78893 3.78893 0 0 0 17.78048 7.883v-.04378a3.77165 3.77165 0 1 0-7.5433 0v.05069a3.77165 3.77165 0 1 0 3.78317 6.28186 3.7705 3.7705 0 0 0 5.56416-5.0688zm-2.91802 5.21164a2.8247 2.8247 0 0 1-2.8224-2.82124.47463.47463 0 0 0-.94925 0 3.7486 3.7486 0 0 0 .53223 1.9296 2.8201 2.8201 0 1 1-1.08058-4.57805h.01152a1.3202 1.3202 0 0 1 .30758.14745.4747.4747 0 0 0 .61748-.72115 2.1266 2.1266 0 0 0-.61402-.32371 3.75898 3.75898 0 0 0-1.29946-.2304h-.182a2.82125 2.82125 0 1 1 4.45478 2.4192.47462.47462 0 1 0 .55066.773 3.78317 3.78317 0 0 0 1.44806-2.0736 2.8224 2.8224 0 0 1-.97574 5.46969z"/>
</svg>

Generate quality gate badge

curl -s -u ####################################://sonarcloud.io/api/project_badges/quality_gate?project=pavedroad-io_kevlar-web
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 350 262.5" width="128px" height="96px">
    <!-- SONARCLOUD QUALITY GATE PASS -->
    <path fill="#fff" d="M328.4 259.5H21.5C10.9 259.5 2 250.8 2 240V21.9C2 11.3 10.7 2.4 21.5 2.4h307.1c10.6 0 19.5 8.7 19.5 19.5V240c-.2 10.8-8.8 19.5-19.7 19.5z"/>
    <path fill="#cfd3d7" d="M328.4 260.4H21.5c-11.2 0-20.4-9.2-20.4-20.4V21.9c0-11.2 9-20.4 20.4-20.4h307.1c11.2 0 20.4 9.2 20.4 20.4V240c-.2 11.2-9.2 20.4-20.6 20.4zM21.5 3.3c-10.3 0-18.6 8.3-18.6 18.6V240c0 10.3 8.3 18.6 18.6 18.6h307.1c10.3 0 18.6-8.3 18.6-18.6V21.9c0-10.3-8.3-18.6-18.6-18.6H21.5z"/>
............... omitted content
</g>
</svg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment