Useful Slack notifications for developers

Posted by Eddo on September 11, 2015

As our local development team has become a geographically distributed scrum development team, we were facing some challenges with regard to keep open communication channels between all members. Our idea to use a persistent chat channel soon emerged, and we were looking for tools.

If we lived now in the early 90s we would have chosen IRC as our tool of choice, though we are now in 2015, so we would have to find something else. My employer has employed Microsoft Lync as chat application, though the version we have doesn’t support persistent chat channels, therefore we as a team decided to move forward with Slack. Personally, I just liked the nice introduction video, thus had to give it a try :).

A few months later, we have a few developers in Spain and a few developers in the Netherlands, and we’re using Slack for both work related chats and non-work related nonsense ;). Adding new developers to our team meant also that we would have to update the email notifications in TFS for our continuous builds, and after a bit of brainstorming we decided we could improve.

slack_notification_example

Thus, a #build channel was soon created in Slack, together with an incoming webhook called ‘Bob’. It then took a bit of coding and testing to figure out how the incoming webhook worked, and eventually we created the code below. This notifies all developers about whether the last build passed all unit tests and integration tests, both the continuous builds as well as the nightly ones. We are planning to increase the scope of our nightly builds to also include full security scans, but for now the difference between continuous and nightly is the creation of a code coverage report. The latter is a real performance hog, therefore we decided it would be good enough to generate that once every 24 hours.

For the nitty-gritty integration to TFS; we’re calling a web service from the build definition in TFS to run the tests of our PHP code.

PHP code snippet used to show the Slack notification:

private function _notifySlackChannel($success)
{
$attachment = new stdClass();
$attachment->fallback = "Build tests ". ($success == true? "passed" : "failed") . " for last '" . $this->_testType . "' build";
$attachment->title    = "Last '" . $this->_testType . "' build " . ($success == true? "OK" : "failed" );
$attachment->text     = "Check <http://tfs_url/tfs/DefaultCollection/RoosterWeb/_build|build log> for more details";
$attachment->color    = $success == true? "good" : "danger" ;
$params = array(
    'payload' => json_encode(array('attachments' => array($attachment)))
);

$ch = curl_init("https://hooks.slack.com/services/something" );
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

curl_exec($ch);
curl_close($ch);
}

P.S. If you’ve enjoyed this article or found it helpful, please share it, or check out my other articles. I’m on Instagram and Twitter too if you’d like to follow along on my adventures and other writings, or comment on the article.