Last active
June 4, 2018 16:21
-
-
Save fdlk/52f187bf277de5e36347bb39bfe0d755 to your computer and use it in GitHub Desktop.
A hook.io microservice that transforms a sonarqube webhook into a slack notification webhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var slack = require('slack-notify') | |
var moment = require('moment-timezone') | |
function formatFloat(value) { | |
return (value && !isNaN(value)) ? parseFloat(value).toFixed(1) : value | |
} | |
function formatValue(value) { | |
if(typeof value == 'number') { | |
return value.toFixed(1) | |
} | |
switch(value){ | |
case '1': | |
return 'A'; | |
case '2': | |
return 'B'; | |
case '3': | |
return 'C'; | |
case '4': | |
return 'D'; | |
case '5': | |
return 'E'; | |
default: | |
return formatFloat(value) || ''; | |
} | |
} | |
module.exports = function(hook) { | |
var msg = hook.params; | |
var text = `[${msg.qualityGate.status}] <${msg.serverUrl}/project/activity?id=${encodeURIComponent(msg.project.key)}|Sonar> analyzed project ${msg.project.name}.` | |
var fields = msg.qualityGate.conditions.map(function(condition) { | |
return { | |
short: true, | |
fallback: `${condition.metric}: ${condition.status}`, | |
title: condition.metric, | |
value: formatValue(condition.value) | |
}; | |
}) | |
var analysedAt = moment(msg.analysedAt); | |
var payload = { attachments: [{ | |
fallback: text, | |
text, | |
fields, | |
ts: analysedAt.format('X'), | |
color: (msg.qualityGate.status === 'OK' ? 'good' : 'danger') | |
}] | |
}; | |
slack(hook.env.slackhook).request(payload, function(response) { | |
hook.res.end(payload); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is a hook.io microservice that transforms a sonarqube webhook notification into a slack webhook notification.
sonarqube => hook.io => slack
slackhook
and as value your slack webhook URL.Now, you're all set!