Skip to main content

Command Palette

Search for a command to run...

SonarMatt; Application to Forward SonarQube’s Webhooks to Mattermost or Slack

Utility tools for productivity

Updated
2 min read
SonarMatt; Application to Forward SonarQube’s Webhooks to Mattermost or Slack

SonarQube is an open-source platform to do continuous inspection on code for automatic review. Based on the need for notifications to see the SonarQube results in realtime after scanning, and because SonarQube does not have a special webhook format for Slack, I made a simple application to forward SonarQube’s webhook to GITS Indonesia chat based on Mattermost. I called this application as “SonarMatt”.

This application is made with node.js and express.js. It listens to completed scan event from SonarQube and accepts its request. The requests that are received from SonarQube will be filtered according to the format needed by Mattermost’s or Slack’s incoming webhook. In this, data object in the form of JSON is forwarded by making a post request to the endpoint of the Mattermost’s incoming webhook URL.

I am going to explain how to edit the application from the source code and how to use it.

How to Develop or Edit the Application from Source Code

So, these are the steps to develop or edit the application from source code.

  • Clone the repository
git clone https://git.gits.id/devops/sonarmatt.git
  • Install requirement dependencies
cd sonarmatt & npm install

Default webhook payload from SonarQube after a completed task is like this:

{
    "serverUrl": "http://localhost:9000",
    "taskId": "AVh21JS2JepAEhwQ-b3u",
    "status": "SUCCESS",
    "analysedAt": "2016-11-18T10:46:28+0100",
    "revision": "c739069ec7105e01303e8b3065a81141aad9f129",
    "project": {
        "key": "myproject",
        "name": "My Project",
        "url": "https://mycompany.com/sonarqube/dashboard?id=myproject"
    },
    "properties": {
    },
    "qualityGate": {
        "conditions": [
            {
                "errorThreshold": "1",
                "metric": "new_security_rating",
                "onLeakPeriod": true,
                "operator": "GREATER_THAN",
                "status": "OK",
                "value": "1"
            },
            {
                "errorThreshold": "1",
                "metric": "new_reliability_rating",
                "onLeakPeriod": true,
                "operator": "GREATER_THAN",
                "status": "OK",
                "value": "1"
            },
            {
                "errorThreshold": "1",
                "metric": "new_maintainability_rating",
                "onLeakPeriod": true,
                "operator": "GREATER_THAN",
                "status": "OK",
                "value": "1"
            },
            {
                "errorThreshold": "80",
                "metric": "new_coverage",
                "onLeakPeriod": true,
                "operator": "LESS_THAN",
                "status": "NO_VALUE"
            }
        ],
        "name": "SonarQube way",
        "status": "OK"
    }
}

Supported Payload for Mattermost’s incoming webhook:

{
  "channel": "town-square",
  "username": "test-automation",
  "icon_url": "https://custom-icon-url.png",
  "text": "text message here"
}
  • filter the payload within SonarMatt 1*NSPJQixFFvquJHv08rlDPg.png

    Custom Your Message

  • Make post request within payload above 1*OTv8z6U6Jgn4Yuj1ke3Kjw.png

    Post Request to Mattermost Incoming Webhook Using Token URL

The result will be like this: 1*NuMa72VJb1RxctxbEySQ8g.png

Received Message After Has Completed Sonar Scan

How to Use SonarMatt

Go to your SonarQube Project > Administration > Webhooks > Create

1*NTSoN7cY4N2QJQEYK78M8Q.png

Forwarding SonarQube Webhook to Mattermost Using SonarMatt Application

Yes, that’s it!

Best regards and thank you!