Skip to content

Instantly share code, notes, and snippets.

@fdlk
Last active June 4, 2018 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdlk/52f187bf277de5e36347bb39bfe0d755 to your computer and use it in GitHub Desktop.
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
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);
});
};
@fdlk
Copy link
Author

fdlk commented Jun 16, 2017

This gist is a hook.io microservice that transforms a sonarqube webhook notification into a slack webhook notification.

sonarqube => hook.io => slack

  • Register an incoming webhook integration in your slack channel
  • Register at http://hook.io (it's free!) and create a new public microservice for this gist.
  • Create an env variable in hook.io with name slackhook and as value your slack webhook URL.
  • In sonarqube, register the hook.io microservice webhook as one of the sonar build notification webhooks.

Now, you're all set!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment