How to integrate SonarQube with Rocket.Chat

  1. SonarQube can send webhooks.
  2. Rocket.Chat ca can receive and parse webhooks

You can script it like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Script {

    process_incoming_request({
        request
    }) {

        // console is a global helper to improve debug
        //console.log(request.content);
        if (request.content.status != "SUCCESS") {
            return {
                error: {
                    success: false,
                    message: "analysis finished with error, task id: " + request.content.taskId
                }
            };
        }

        let msgText = "Analysis " + request.content.project.name + " completed";

        let attColor = "gray";
        switch (request.content.qualityGate.status) {
        case "OK":
            attColor = "#00FF00";
            break;
        case "ERROR":
            attColor = "#FF0000";
            break;
        default:
            break;
        }

        let att2 = [];
        request.content.qualityGate.conditions.forEach(function (condition) {
            switch (condition.metric) {
            case "new_reliability_rating":
                att2.push({
                    title: "Reliability",
                    value: condition.status,
                    short: true
                });
                break;
            case "new_security_rating":
                att2.push({
                    title: "Security",
                    value: condition.status,
                    short: true
                });
                break;
            case "new_maintainability_rating":
                att2.push({
                    title: "Maintainability",
                    value: condition.status,
                    short: true
                });
                break;
            case "new_duplicated_lines_density":
                att2.push({
                    title: "Duplicated lines",
                    value: condition.status,
                    short: true
                });
                break;
            case "new_coverage":
                att2.push({
                    title: "Code coverage",
                    value: condition.status,
                    short: true
                });
                break;
            default:
                break;
            }
        })

        let result = {
            content: {
                username: "SonarQube",
                icon_url: "/avatar/SonarQube.jpg",
                attachments: [{
                        color: attColor,
                        title: msgText,
                        title_link: request.content.branch.url,
                        fields: [{
                                title: "Quality Gate Status",
                                value: request.content.qualityGate.status,
                                short: true
                            }, {
                                title: "Analysed At",
                                value: request.content.analysedAt,
                                short: true
                            }, {
                                title: "Revision",
                                value: request.content.revision,
                                short: true
                            }, {
                                title: "Branch",
                                value: request.content.branch.name,
                                short: true
                            }
                        ]
                    }
                ]
            }
        };

        if (att2.length > 0) {
            result.content.attachments.push({
                color: attColor,
                title: "Quality Indicators",
                fields: att2
            });
        }

        return result;
    }
}

Happy chatting with bots :)