Adding Notifications to MS Teams when a SonarCloud Quality Gate Changes the Status
Are you using SonarCloud (or SonarQube) and MS Teams? Would you like to get a notification within your MS Teams if your Quality Gate on…
Are you using SonarCloud (or SonarQube) and MS Teams? Would you like to get a notification within your MS Teams if your Quality Gate on Sonar changes? Using the built-in notifications from Sonar and Microsoft Power Automate, you can get an instant notification if your Quality Gate changes within your Teams Group chat or channel.
Read on to learn how to set this up yourself.
SonarCloud
SonarCloud (or its on-premise version “SonarQube”) allows you to analyze your code and find potential problems. This ranges from security-related things to “unclean” code (according to the rules you set up) and untested parts of your code.
SonarCloud Online Code Review as a Service Tool
SonarCloud extends your CI/CD workflow with an online code review solution that easily integrates into your cloud…www.sonarsource.com
Sonar helps us keep our code clean, by running an analysis of the code every time something is pushed.
Quality Gates
A quality gate can be set up for a project on Sonar, and has one of two states: “Passed” or “Failed”. It’s there to ensure that your code meets the “minimum quality criteria” as defined by your quality gate settings.
A quality gate is an indicator that tells you whether your code meets the minimum level of quality required for your project. It consists of a set of conditions that are applied to the results of each analysis. If the analysis results meet or exceed the quality gate conditions then it shows a Passed status otherwise, it shows a Failed status. — Sonar Docs
Why an MS Teams Notification?
If you are using MS Teams, chances are that this is your main tool for communication. You might have a group chat or a dedicated team/channel for the relevant communication within your team. Instead of having to monitor a pipeline, having Sonar open in a dedicated tab all the time, or continuously checking your emails, it’s simply more convenient to get a notification in MS Teams.
An additional benefit is that you can directly discuss the notification about what might be the issue and which team members can check out what happened (in case the quality gate turned from passed to failed).
Why Power Automate?
If you are in an environment that relies heavily on Microsoft technologies (and chances are you do, given that you are using MS Teams), Power Automate is simply a good choice. It’s rather simple, yet powerful. And it integrates nicely with different Microsoft applications.
Adding Notifications to MS Teams when Azure DevOps Work Items are Waiting using Power Automate
Are you using Azure DevOps and MS Teams? Would you like to improve your efficiency and be notified as soon as a Work…medium.com
Furthermore, we already have notifications done via Power Automate to MS Teams, so it made sense to reuse that knowledge.
Prerequisite
To run this, you should have at least one project on SonarCloud already. Furthermore, you should set up the notifications for failed quality gates as described in the docs.
Quality gates & SonarCloud
A SonarCloud quality gate is an indicator that tells you whether your code meets the minimum level of quality required…docs.sonarsource.com
To test your notification, you may want to have your quality gate status changed at least once so you get the notification email from Sonar and can use it as an example to test your flow.
I’ll be providing the rough template so you can use this for quick verification and testing. However, I advise you to double-check with the real email from Sonar as things might change or look different for you.
At the time of writing, the flow presented in this post was not requiring any premium capabilities. So anyone could run it without having a dedicated subscription.
Flow
Before we start implementing, we should have a general idea of the flow of our Flow (projects in Power Automate are called Flows). Let’s break down what we would like to achieve:
We want to get a notification in MS Teams.
We want to get a notification when the quality gate status of a SonarCloud project we care about is changed
We want to base this on the Email notification that Sonar offers out-of-the-box
So far so good, that would lead to a flow similar to this:
Trigger
The trigger for our Flow will be an incoming email. We’re using the built-in trigger “When an Email arrives” within our Office 365 inbox with a filter for the subject. If you are not using Outlook, there might be other options for triggers. However, I’ve only ever tried with this one.
You can filter for various things. For demo purposes, I only included the subject. You may adjust your filters based on the real email you are getting from Sonar.
SonarCloud Email Notification
Following is the Email I get whenever the project “My Project 1337” changes the Quality Gate status. The subject line looks like this:
Subject: [SonarCloud] Quality gate status changed on "My Project 1337"
And the body looks like this:
Project: My Project 1337
Version: 1.0
Quality gate status: Red (was Green)
Quality gate threshold: New Code Smells > 0
More details at: <link>
Parse Email
What we want to extract from the email are two things:
The Project Name
The Quality gate status
To do so, we first want to create plain text out of the HTML email we got from Sonar. For this, we can use the “Html to Text” action and convert the Body of the trigger to plain text:
After this, we can extract the two properties (project name and quality gate status) into our variables using the compose action for each of the two properties.
You can add the following inputs to extract the correct data. For the project, we want to get the part after “Project:” and before “Version:”:
trim(split(split(body('Html_to_text'), 'Project:')[1], 'Version:')[0])
For the quality gate status, we want to get the part after the colon and before the parenthesis.
trim(split(split(body('Html_to_text'),'Quality gate status:')[1],'(')[0])
For our example email, we would now have the values:
“My Project 1337” for the Project
“Red” for the Quality Gate Status
Condition
Now that we have the properties, we can create a condition. For example, you might have different SonarCloud projects and want to post in different channels depending on the project. Or you want to check the quality gate status and only post if it’s red, not if it’s green.
In our case, we filter for the project name. As we have multiple SonarCloud Projects, we follow a naming scheme. We can then decide that we only want to do something if the project name starts with a given string, for example “My Project”.
To do this, you can add a “Condition” and check the output of your compose action:
If you get the notification for “My Project 1337”, you’d fall into the “true” branch. While emails you get for “My Other Project” would fall into the “false” branch.
Send Notification in Teams
Last but not least, we have to create the notification itself. We want to do this via an “adaptive card”. Adaptive Cards are defined via JSON and will be rendered “nicely” in MS Teams. They allow to not only display text but also have actions associated with them via buttons.
There is a designer that can help you design your card.
In our example, we’re posting the following card:
It’s a very simple card, we just show the name of the project and the new status. We can achieve this using the following JSON:
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "medium",
"weight": "bolder",
"text": "Sonar Cloud Quality Gate Status Change"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"text": "📢 @{outputs('Project')} Quality Gate is @{outputs('Quality_Gate_Status')} 📢",
"wrap": true,
"color": "Attention",
"weight": "Bolder"
}
],
"width": "stretch"
}
]
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.4"
}
You can use this JSON within an MS Teams “Post card” action. You can send it to yourself, to a group chat you are part of, or to a channel. Whatever makes the most sense in your environment.
And that’s it, once this is done, your flow should work.
Conclusion
Having an automatic notification when your SonarCloud Quality Gate changes status can help raise awareness. Using the approach via MS Teams removes the need for everyone to monitor Sonar on their own or even to bother and set up notifications themselves. Instead, you can keep the focus on a single tool for communication within your team (in this case MS Teams) and discuss & take action as soon as something changes on Sonar.