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:
- 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.
- 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 IntegrationThis 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
-
Go to your Builder page.
-
Click on
+ Blueprint
. -
Click on the
{...}
button in the top right corner, and choose "Edit JSON". -
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": {}
} -
Click "Save" to create the blueprint.
Implementationโ
- Synced webhook
- GitHub workflow
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
If you have already installed Port's SonarQube integration, these secrets should already exist in your portal.
To view your existing secrets:
- Click on the
...
button in the top right corner of your Port application. - Choose Credentials, then click on the
Secrets
tab.
To add these secrets to your portal:
-
Click on the
...
button in the top right corner of your Port application. -
Click on Credentials.
-
Click on the
Secrets
tab. -
Click on
+ Secret
and add the following secrets:SONARQUBE_HOST_URL
: Your SonarQube instance URLSONARQUBE_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:
-
Head to the self-service page.
-
Click on the
+ New Action
button. -
Click on the
{...} Edit JSON
button. -
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
} -
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:
-
Head to the automation page.
-
Click on the
+ Automation
button. -
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
} -
Click
Save
.
Now when you execute the webhook action, the project tags in Port will be automatically updated with the latest information from SonarQube.
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.
To implement this use-case using a GitHub workflow, follow these steps:
Add GitHub secrets
In your GitHub repository, go to Settings > Secrets and add the following secrets:
SONARQUBE_HOST_URL
- SonarQube instance URL.https://sonarqube.com
if using Sonarcloud.SONARQUBE_API_TOKEN
- SonarQube API token. This can be a Project Analysis token for the specific project, a Global Analysis token or a user token.PORT_CLIENT_ID
- Your portclient id
How to get the credentials.PORT_CLIENT_SECRET
- Your portclient secret
How to get the credentials.
Add GitHub workflow
Create the file .github/workflows/add-tags-to-sonarqube-project.yml
in the .github/workflows
folder of your repository.
We recommend creating a dedicated repository for the workflows that are used by Port actions.
GitHub Workflow (Click to expand)
name: Add tags to SonarQube project
on:
workflow_dispatch:
inputs:
tags:
type: string
required: true
port_context:
required: true
type: string
jobs:
create-entity-in-port-and-update-run:
runs-on: ubuntu-latest
steps:
- name: Inform Port of start of request to SonarQube
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
baseUrl: https://api.getport.io
operation: PATCH_RUN
runId: ${{ fromJson(inputs.port_context).run_id}}
logMessage: Starting request to add tags to SonarQube project
- name: Add tags to SonarQube project
uses: fjogeleit/http-request-action@v1
with:
url: "${{ secrets.SONARQUBE_HOST_URL }}/api/project_tags/set?project=${{ fromJson(inputs.port_context).entity }}&tags=${{ inputs.tags }}"
method: "POST"
bearerToken: ${{ secrets.SONARQUBE_API_TOKEN }}
customHeaders: '{"Content-Type": "application/json"}'
- name: Inform Port of completion of request to SonarQube
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
baseUrl: https://api.getport.io
operation: PATCH_RUN
runId: ${{ fromJson(inputs.port_context).run_id }}
logMessage: Finished request to add tags to SonarQube project
Set up self-service action
We will create a self-service action to handle adding tags to SonarQube projects. To create a self-service action follow these steps:
-
Head to the self-service page.
-
Click on the
+ New Action
button. -
Click on the
{...} Edit JSON
button. -
Copy and paste the following JSON configuration into the editor.
Add Tags to SonarQube Project (Click to expand)
Modification RequiredMake sure to replace
<GITHUB_ORG>
and<GITHUB_REPO>
with your GitHub organization and repository names respectively.{
"identifier": "sonarQubeProject_add_tags_to_sonar_qube_project",
"title": "Add Tags to SonarQube project",
"icon": "sonarqube",
"description": "Adds additional tags to a project in SonarQube",
"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": "GITHUB",
"org": "<GITHUB_ORG>",
"repo": "<GITHUB_REPO>",
"workflow": "add-tags-to-sonarqube-project.yml",
"workflowInputs": {
"tags": "{{.inputs.\"tags\"}}",
"port_context": {
"entity": "{{.entity.identifier}}",
"run_id": "{{.run.id}}"
}
},
"reportWorkflowStatus": true
},
"requiredApproval": false
} -
Click
Save
.
Now you should see the Add Tags to SonarQube project
action in the self-service page. ๐
Let's test it!โ
-
Head to the self-service page of your portal
-
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)
- For GitHub workflow: Click on
-
Select the SonarQube project where you want to add tags
-
Enter the required information:
- Enter a comma-separated list of tags (e.g., "backend,java,microservice")
-
Click on
Execute
-
Done! Wait for the tags to be added to the SonarQube project