Publishing SonarQube badges using Azure Functions

Copenhagen based cloud developer. Love to code and to make complex solutions simple!
Search for a command to run...

Copenhagen based cloud developer. Love to code and to make complex solutions simple!
No comments yet. Be the first to comment.
This series will cover lots of practical and hard learned experiences developing Azure Functions. Read about how to design your microservice setup, how to implement the functions using triggers.
...and some SonarCloud code analysis!
And examples for .NET projects also

...and why Windows is not going to sleep when left idle

TL;DR Keep trying the firmware update - after around 30 attempts doing the same thing, it just worked 🤯🎉🪅 Awesome Ultrawide Monitor, not only for gaming For once I'm going to write an article which is not directly related to programming, but if yo...

When using GitHub actions it's pretty neat that you can use the hosted GitHub runners. However, this can get costly if you use a lot of runners and they are not always that fast at running the actions. Luckily you can also host your own runners and w...

...and some SonarCloud code analysis!

We have started to use SonarQube, an awesome tool for static code analysis, and due to several reason we have to use the on-prem version and not the cloud version.
Now that we use SonarQube we want maximum visibility of the metrics that SonarQube produces, there for we want to add badges to our repositories, you probably seen them several places, looks like this:

So the problem is that SonarQube does not natively support getting these badges when without authentication when hosting SonarQube on-prem. This is a problem when if you want to add the badges to a Github repos's readme file where all you can do is make a HTTP request to get the badge. However, SonarQube does provide these badges via their API so I figured; why not setup a simple Azure Function that acts as a plain old GET HTTP endpoint and then calls the SonarQube API with the required params?
public static class SonarQubeFunctions
{
[Function("GetBadge")]
public static async Task<HttpResponseData> GetBadge([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
FunctionContext executionContext)
{
var logger = executionContext.GetLogger("SoarQubeFunctions");
var query = System.Web.HttpUtility.ParseQueryString(req.Url.Query);
var metric= query["metric"];
var project= query["project"];
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Basic abcd...");
var sqResponse = await httpClient.GetAsync(
$"https://on-prem-sonarqube-instance/api/project_badges/measure?metric={metric}&project={project}");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", sqResponse.Content.Headers.GetValues("Content-Type").First());
await response.WriteBytesAsync(await sqResponse.Content.ReadAsByteArrayAsync());
return response;
}
}
This is a .net 5 Azure Function which is hosted on Azure using isolation. What we do here is straight forward;
metric and project Authorization header, see this on how to generate a tokenproject_badges/measure and pass the parametersDeploy this function to Azure and you will be able to call it like this:
https://your-function-app.azurewebsites.net/api/GetBadge?metric=requestedMetric&project=sonarqube-project-key
For a list of metrics to pass, see the API docs at: sonarqube-server/web_api/api/project_badges?query=badge, or here:


Here's an example of the markdown to put in the readme:
[](https://sonarqube-server/dashboard?id=sonarqube-project-key)