Skip to main content

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.

Prerequisites

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:

  1. Log in to GitHub as a user with sufficient permissions to edit topics for a repository.
  2. Navigate to the repository you want to add a topic to.
  3. Click on the settings icon
  1. Click on the Topics field to add a topic.

  2. 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 by kafka in your sonarQubeProject blueprint in Port.

Control the topic name

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.

  1. Head back to the Builder, choose the Service (GitHub repository) blueprint, and click on New relation:


  1. 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"))
JQ explanation

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: