Skip to main content

Check out Port for yourselfย 

Add Tags to SonarQube Project

Overviewโ€‹

This guide will help you implement a self-service action in Port that allows you to add tags to SonarQube projects directly from Port. This functionality streamlines project management by enabling users to categorize and label projects based on various attributes such as technology stack, business domain, or team ownership without leaving Port.

You can implement this action in two ways:

  1. GitHub workflow: A more flexible approach that allows for complex workflows and custom logic, suitable for teams that want to maintain their automation in Git.
  2. Synced webhooks: A simpler approach that directly interacts with SonarQube's API through Port, ideal for quick implementation and minimal setup.

Prerequisitesโ€‹

  • Complete the onboarding process.

  • Access to your SonarQube instance with permissions to manage projects.

  • SonarQube API token with 'Administer' rights on the specified project. Check SonarQube's documentation on how to retrieve your API Token.

  • Optional - Install Port's SonarQube integration learn more

    SonarQube Integration

    This step is not required for this example, but it will create all the blueprint boilerplate for you, and also update the catalog in real time with your SonarQube projects.

Set up data modelโ€‹

If you haven't installed the SonarQube integration, you'll need to create a blueprint for SonarQube projects. However, we highly recommend you install the SonarQube integration to have these automatically set up for you.

Create the SonarQube project blueprint

  1. Go to your Builder page.

  2. Click on + Blueprint.

  3. Click on the {...} button in the top right corner, and choose "Edit JSON".

  4. Add this JSON schema:

    SonarQube Project Blueprint (Click to expand)
    {
    "identifier": "sonarQubeProject",
    "title": "SonarQube Project",
    "icon": "sonarqube",
    "schema": {
    "properties": {
    "organization": {
    "type": "string",
    "title": "Organization",
    "icon": "TwoUsers"
    },
    "link": {
    "type": "string",
    "format": "url",
    "title": "Link",
    "icon": "Link"
    },
    "lastAnalysisDate": {
    "type": "string",
    "format": "date-time",
    "icon": "Clock",
    "title": "Last Analysis Date"
    },
    "numberOfBugs": {
    "type": "number",
    "title": "Number Of Bugs"
    },
    "numberOfCodeSmells": {
    "type": "number",
    "title": "Number Of CodeSmells"
    },
    "numberOfVulnerabilities": {
    "type": "number",
    "title": "Number Of Vulnerabilities"
    },
    "numberOfHotSpots": {
    "type": "number",
    "title": "Number Of HotSpots"
    },
    "numberOfDuplications": {
    "type": "number",
    "title": "Number Of Duplications"
    },
    "coverage": {
    "type": "number",
    "title": "Coverage"
    },
    "mainBranch": {
    "type": "string",
    "icon": "Git",
    "title": "Main Branch"
    },
    "tags": {
    "type": "array",
    "title": "Tags"
    }
    },
    "required": []
    },
    "mirrorProperties": {},
    "calculationProperties": {},
    "relations": {}
    }
  5. Click "Save" to create the blueprint.

Implementationโ€‹

You can add tags to SonarQube projects by leveraging Port's synced webhooks and secrets to directly interact with the SonarQube's API. This method simplifies the setup by handling everything within Port.

Add Port secrets

Existing secrets

If you have already installed Port's SonarQube integration, these secrets should already exist in your portal.
To view your existing secrets:

  1. Click on the ... button in the top right corner of your Port application.
  2. Choose Credentials, then click on the Secrets tab.

To add these secrets to your portal:

  1. Click on the ... button in the top right corner of your Port application.

  2. Click on Credentials.

  3. Click on the Secrets tab.

  4. Click on + Secret and add the following secrets:

    • SONARQUBE_HOST_URL: Your SonarQube instance URL
    • SONARQUBE_API_TOKEN: Your SonarQube API token

Set up self-service action

We will create a self-service action to handle adding tags to SonarQube projects using webhooks. To create a self-service action follow these steps:

  1. Head to the self-service page.

  2. Click on the + New Action button.

  3. Click on the {...} Edit JSON button.

  4. Copy and paste the following JSON configuration into the editor.

    Add Tags to SonarQube Project (Webhook) (Click to expand)
    {
    "identifier": "add_tags_to_sonarqube_project_webhook",
    "title": "Add Tags to SonarQube Project (Webhook)",
    "icon": "sonarqube",
    "description": "Add tags to a SonarQube project using a webhook",
    "trigger": {
    "type": "self-service",
    "operation": "DAY-2",
    "userInputs": {
    "properties": {
    "tags": {
    "title": "Tags",
    "description": "Comma separated list of tags",
    "icon": "DefaultProperty",
    "type": "string"
    }
    },
    "required": [
    "tags"
    ],
    "order": [
    "tags"
    ]
    },
    "blueprintIdentifier": "sonarQubeProject"
    },
    "invocationMethod": {
    "type": "WEBHOOK",
    "url": "{{.secrets.SONARQUBE_HOST_URL}}/api/project_tags/set",
    "agent": false,
    "synchronized": true,
    "method": "POST",
    "headers": {
    "Authorization": "Bearer {{.secrets.SONARQUBE_API_TOKEN}}",
    "Content-Type": "application/x-www-form-urlencoded"
    },
    "queryParams": {
    "project": "{{.entity.identifier}}",
    "tags": "{{.inputs.tags}}"
    }
    },
    "requiredApproval": false
    }
  5. Click Save.

Now you should see the Add Tags to SonarQube Project (Webhook) actions in the self-service page. ๐ŸŽ‰

Create an automation to update entity in port

To keep your catalog updated with the latest tags, you can create an automation that will update the SonarQube project entity in Port immediately after the webhook action completes successfully.

Follow these steps to add the automation:

  1. Head to the automation page.

  2. Click on the + Automation button.

  3. Copy and paste the following JSON configuration into the editor.

    Update SonarQube project tags in Port automation (Click to expand)
    {
    "identifier": "sonarQubeProject_sync_tags",
    "title": "Sync SonarQube Project Tags",
    "description": "Update SonarQube project tags in Port after adding tags",
    "trigger": {
    "type": "automation",
    "event": {
    "type": "RUN_UPDATED",
    "actionIdentifier": "add_tags_to_sonarqube_project_webhook"
    },
    "condition": {
    "type": "JQ",
    "expressions": [
    ".diff.after.status == \"SUCCESS\""
    ],
    "combinator": "and"
    }
    },
    "invocationMethod": {
    "type": "UPSERT_ENTITY",
    "blueprintIdentifier": "sonarQubeProject",
    "mapping": {
    "identifier": "{{.event.diff.after.entity.identifier}}",
    "properties": {
    "tags": "{{.event.diff.after.inputs.tags | split(\",\")}}"
    }
    }
    },
    "publish": true
    }
  4. Click Save.

Now when you execute the webhook action, the project tags in Port will be automatically updated with the latest information from SonarQube.

API limitations

Due to SonarQube API's limitation, this action replaces the tags on the project with the new ones specified. If you want to add to the already existing tags, copy the existing tags and add them with the new ones you are adding.

Let's test it!โ€‹

  1. Head to the self-service page of your portal

  2. Choose either the GitHub workflow or webhook implementation:

    • For GitHub workflow: Click on Add Tags to SonarQube project
    • For webhook: Click on Add Tags to SonarQube Project (Webhook)
  3. Select the SonarQube project where you want to add tags

  4. Enter the required information:

    • Enter a comma-separated list of tags (e.g., "backend,java,microservice")
  5. Click on Execute

  6. Done! Wait for the tags to be added to the SonarQube project

More relevant guides and examplesโ€‹