Add the ability to hide the slack attachment

This commit is contained in:
Stephen Ball 2015-10-13 16:04:16 +01:00
parent 6aea2bdb88
commit d2ab7b300d

View file

@ -24,6 +24,7 @@ class SlackNotify implements \PHPCI\Plugin
private $username; private $username;
private $message; private $message;
private $icon; private $icon;
private $show_status;
/** /**
* Set up the plugin, configure options, etc. * Set up the plugin, configure options, etc.
@ -60,6 +61,12 @@ class SlackNotify implements \PHPCI\Plugin
$this->username = 'PHPCI'; $this->username = 'PHPCI';
} }
if (isset($options['show_status'])) {
$this->show_status = (bool) $options['show_status'];
} else {
$this->show_status = true;
}
if (isset($options['icon'])) { if (isset($options['icon'])) {
$this->icon = $options['icon']; $this->icon = $options['icon'];
} }
@ -74,31 +81,7 @@ class SlackNotify implements \PHPCI\Plugin
*/ */
public function execute() public function execute()
{ {
$message = $this->phpci->interpolate($this->message); $body = $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
))
)
));
$client = new \Maknz\Slack\Client($this->webHook); $client = new \Maknz\Slack\Client($this->webHook);
@ -116,12 +99,40 @@ class SlackNotify implements \PHPCI\Plugin
$message->setIcon($this->icon); $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;
} }
} }