Monitor SonarQube projects in Catalog with the Konnect API

TL;DR

Install the SonarQube integration in Konnect and authorize access with your SonarQube personal access token, then link a project to your Catalog service.

Prerequisites

If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.

  1. The following Konnect items are required to complete this tutorial:
    • Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Set the personal access token as an environment variable:

    export KONNECT_TOKEN='YOUR KONNECT TOKEN'
    

You need the Integration Admin role in Konnect to install and authorize Catalog integrations.

You need to configure the following in SonarQube Cloud:

SonarQube Server isn’t supported.

Export your SonarQube personal access token:

export SONARQUBE_PAT='YOUR SONARQUBE PERSONAL ACCESS TOKEN'

Configure the SonarQube integration

Before you can discover SonarQube projects in Catalog, you must configure the SonarQube integration.

First, install the SonarQube integration:

curl -X POST "https://us.api.konghq.com/v1/integration-instances" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN" \
     --json '{
       "integration_name": "sonarqube",
       "name": "sonarqube",
       "display_name": "SonarQube",
       "config": {}
     }'

Export the ID of your SonarQube integration:

export SONARQUBE_INTEGRATION_ID='YOUR-INTEGRATION-ID'

Next, authorize the SonarQube integration with your SonarQube personal access token:

curl -X POST "https://us.api.konghq.com/v1/integration-instances/$SONARQUBE_INTEGRATION_ID/auth-credential" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN" \
     --json '{
       "type": "multi_key_auth",
       "config": {
         "headers": [
           {
             "name": "authorization",
             "key": "'$SONARQUBE_PAT'"
           }
         ]
       }
     }'

Once authorized, resources from your SonarQube account will be discoverable in the UI.

Create a service in Catalog

Create a service that you’ll map to your SonarQube resources:

curl -X POST "https://us.api.konghq.com/v1/catalog-services" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN" \
     --json '{
       "name": "billing",
       "display_name": "Billing Service"
     }'

Export the service ID:

export SONARQUBE_SERVICE_ID='YOUR-SERVICE-ID'

List SonarQube resources

Before you can map your SonarQube resources to a service in Catalog, you first need to find the resources that are pulled in from SonarQube:

curl -X GET "https://us.api.konghq.com/v1/resources?filter%5Bintegration.name%5D=sonarqube" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN"

You may need to manually sync your SonarQube integration for resources to appear. From the Konnect UI by navigating to the SonarQube integration you just installed and selecting Sync Now from the Actions dropdown menu.

Export the resource ID you want to map to the service:

export SONARQUBE_RESOURCE_ID='YOUR-RESOURCE-ID'

Map resources to a service

Now, you can map the SonarQube resource to the service:

curl -X POST "https://us.api.konghq.com/v1/resource-mappings" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN" \
     --json '{
       "service": "billing",
       "resource": "'$SONARQUBE_RESOURCE_ID'"
     }'

Validate the mapping

To confirm that the SonarQube resource is now mapped to the intended service, list the service’s mapped resources:

curl -X GET "https://us.api.konghq.com/v1/catalog-services/$SONARQUBE_SERVICE_ID/resources" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN"
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!