From d2ab7b300de6ebf25b9137ef67cbad37fc0b9792 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 16:04:16 +0100 Subject: [PATCH 1/2] Add the ability to hide the slack attachment --- PHPCI/Plugin/SlackNotify.php | 69 +++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/PHPCI/Plugin/SlackNotify.php b/PHPCI/Plugin/SlackNotify.php index 65a190fd..93a2e272 100644 --- a/PHPCI/Plugin/SlackNotify.php +++ b/PHPCI/Plugin/SlackNotify.php @@ -24,6 +24,7 @@ class SlackNotify implements \PHPCI\Plugin private $username; private $message; private $icon; + private $show_status; /** * Set up the plugin, configure options, etc. @@ -60,6 +61,12 @@ class SlackNotify implements \PHPCI\Plugin $this->username = 'PHPCI'; } + if (isset($options['show_status'])) { + $this->show_status = (bool) $options['show_status']; + } else { + $this->show_status = true; + } + if (isset($options['icon'])) { $this->icon = $options['icon']; } @@ -74,31 +81,7 @@ class SlackNotify implements \PHPCI\Plugin */ public function execute() { - $message = $this->phpci->interpolate($this->message); - - $successfulBuild = $this->build->isSuccessful(); - - if ($successfulBuild) { - $status = 'Success'; - $color = 'good'; - } else { - $status = 'Failed'; - $color = 'danger'; - } - - // Build up the attachment data - $attachment = new \Maknz\Slack\Attachment(array( - 'fallback' => $message, - 'pretext' => $message, - 'color' => $color, - 'fields' => array( - new \Maknz\Slack\AttachmentField(array( - 'title' => 'Status', - 'value' => $status, - 'short' => false - )) - ) - )); + $body = $this->phpci->interpolate($this->message); $client = new \Maknz\Slack\Client($this->webHook); @@ -116,12 +99,40 @@ class SlackNotify implements \PHPCI\Plugin $message->setIcon($this->icon); } - $message->attach($attachment); + // Include an attachment which shows the status and hide the message + if ($this->show_status) { - $success = true; + $successfulBuild = $this->build->isSuccessful(); - $message->send(''); + if ($successfulBuild) { + $status = 'Success'; + $color = 'good'; + } else { + $status = 'Failed'; + $color = 'danger'; + } - return $success; + // Build up the attachment data + $attachment = new \Maknz\Slack\Attachment(array( + 'fallback' => $body, + 'pretext' => $body, + 'color' => $color, + 'fields' => array( + new \Maknz\Slack\AttachmentField(array( + 'title' => 'Status', + 'value' => $status, + 'short' => false + )) + ) + )); + + $message->attach($attachment); + + $body = ''; + } + + $message->send($body); + + return true; } } From 2d667d477d32ad04d1eff8d7cd2a936bd1a9f9a7 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 16:08:44 +0100 Subject: [PATCH 2/2] Fix PHPCS error --- PHPCI/Plugin/SlackNotify.php | 1 - 1 file changed, 1 deletion(-) diff --git a/PHPCI/Plugin/SlackNotify.php b/PHPCI/Plugin/SlackNotify.php index 93a2e272..0e8d1cad 100644 --- a/PHPCI/Plugin/SlackNotify.php +++ b/PHPCI/Plugin/SlackNotify.php @@ -101,7 +101,6 @@ class SlackNotify implements \PHPCI\Plugin // Include an attachment which shows the status and hide the message if ($this->show_status) { - $successfulBuild = $this->build->isSuccessful(); if ($successfulBuild) {