Connect GitHub repository (service) to a SonarQube project
This guide aims to demonstrate how to connect a GitHub repository (referred to as Service for the rest of the guide) to a SonarQube project in Port.
- This guide assumes you have a Port account and that you have finished the onboarding process. We will use the
Service
blueprint that was created during the onboarding process. - Ensure you have SonarQube installed and configured in your environment.
- Ensure you have GitHub installed and configured
Add topics to services in GitHub
Tagging repositories with topics allows you to categorize, filter and add metadata information to them. You can use topics to group repositories related to a SonarQube project inp ort. In this guide, we will add a topic to tell us what SonarQube project the issue is related to:
- Log in to GitHub as a user with sufficient permissions to edit topics for a repository.
- Navigate to the repository you want to add a topic to.
- Click on the settings icon
-
Click on the
Topics
field to add a topic. -
Add a topic that represents the SonarQube project the service is related to,
port-sonarqube-kafka
. For this guide, let's assume there is a service entity identified bykafka
in yoursonarQubeProject
blueprint in Port.
Since our GitHub repository may already have several labels, we will need a mechanism to control how these topics will be related to our sonarQubeProject
blueprint. A way to achieve this relation is to prefix the topic name with the keyword port-sonarqube-
. We will then use JQ to select the topics that starts with this keyword. So, our example topic will be named port-sonarqube-kafka
, which will correspond to a SonarQube Project entity identified by kafka
in Port.
Create the Project relation
Now that Port is synced with our GitHub resources, let's reflect the topic in our services to display the SonarQube project associated with a service. First, we will need to create a relation between our services and the corresponding SonarQube Project.
- Head back to the Builder, choose the
Service
(GitHub repository) blueprint, and click onNew relation
:
- Fill out the form like this, then click
Create
:
Now that the blueprints are related, we need to assign the relevant Services to each of our SonarQube Projects. This can be done by adding some mapping logic. Go to your data sources page, and click on your GitHub integration:
Under the resources
key, modify the mapping for the service
kind by using the following YAML block. Then click Save & Resync
:
Relation mapping (Click to expand)
- kind: repository
selector:
query: 'true'
collaborators: true
port:
entity:
mappings:
identifier: .name
title: .name
blueprint: '"githubRepository"'
properties:
readme: file://README.md
url: .html_url
defaultBranch: .default_branch
relations:
sonarQubeProject: .topics | map(select(startswith("port-sonarqube-"))) | map(sub("port-sonarqube-"; ""; "g"))
The JQ below selects all topics that start with the keyword port-sonarqube-
. It then removes "port-sonarqube-" from each topic, leaving only the part that comes after it. It then selects the first match, which is equivalent to the SonarQube Project in Port.
sonarQubeProject: .topics | map(select(startswith("port-sonarqube-"))) | map(sub("port-sonarqube-"; ""; "g"))
What we just did was map the SonarQube Project
to the relation between it and our Services
.
Now, if our SonarQube Project
identifier is equal to the GitHub service label, the service
will automatically be linked to it 🎉
Conclusion
By following these steps, you can seamlessly connect a GitHub repository with an existing SonarQube Project blueprint in Port using topics.
More relevant guides and examples: