Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from mrbase/slack-bot-integration
Browse files Browse the repository at this point in the history
restructured the whole lot
  • Loading branch information
ulrik nielsen authored Aug 3, 2016
2 parents 96a47da + 62a00b7 commit 1b8bea5
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 269 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This package adds [Slack](https://slack.com) integration to your [Cachet](https://cachethq.io/) installation.

When set up it will send notifications to a slack channel when a incident is either added or updated - and when
When set up it will send notifications to a slack channel when a incident is either added or updated - and when
components are added or updated.

## Install
Expand All @@ -18,7 +18,7 @@ Add provider to your config/app.php providers
],

And to aliases:

'aliases' => [
...
'Slack' => Maknz\Slack\Facades\Slack::class,
Expand All @@ -30,14 +30,15 @@ Publish config and translations:

## Setup

Edit the `.env` file and add the following, replace with your own settings:

SLACK_ENDPOINT=https://hooks.slack.com/services/XXXX/XXXX/XXX
SLACK_CHANNEL=#channelname
SLACK_USERNAME=Mr.Cachet
SLACK_ICON=:skull:
Edit `config/slack.php` and replace the following with your own settings, update any other settings as toy see fit:

'endpoint' => '',

The endpoint url is something like: https://hooks.slack.com/services/XXXX/XXXX/XXX

Remember to clear cache and config.

Done, Cachet will now send notifications to your Slack channel on incident events.
Done, Cachet will now send notifications to your Slack channel on incident events.


## Note
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
}
],
"require": {
"php": ">=5.5.0",
"cachethq/cachet": "2.*",
"maknz/slack": "1.*"
"maknz/slack-laravel": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
106 changes: 57 additions & 49 deletions src/Utils.php → src/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,44 @@
<?php
/**
* This file is part of the CachetSlackIntegration package.
*
* (c) Ulrik Nielsen <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Mrbase\CachetSlackIntegration;
namespace Mrbase\CachetSlackIntegration\Handlers;

use CachetHQ\Cachet\Models\Component;
use Maknz\Slack\Facades\Slack;

/**
* Class Utils
* Class BaseHandler
*
* @package Mrbase\CachetSlackIntegration
* @author Ulrik Nielsen <[email protected]>
*/
class Utils
abstract class BaseHandler
{
/**
* @var array
*/
private static $changes = [];

/**
* @param string $model
* @param array $data
* @return mixed
*/
public static function registerChanges($model, array $data)
{
// For some reason, the changed/saving event is called twice on the model, we only need the first.
if (!empty(self::$changes[$model])) {
return;
}

self::$changes[$model] = $data;
}
abstract public function send();

/**
* @param string $model
* Map status ids to a slack color.
*
* @param int $status
*
* @return array
* @return string
*/
public static function getChanges($model)
protected function statusToColor($status)
{
if (isset(self::$changes[$model])) {
return self::$changes[$model];
$colormap = [
0 => 'good', // 'Scheduled'
1 => 'danger', // 'Investigating'
2 => 'warning', // 'Identified'
3 => 'warning', // 'Watching'
4 => 'good', // 'Fixed'
];

if (isset($colormap[$status])) {
return $colormap[$status];
}

return [];
return '';
}

/**
Expand All @@ -60,7 +48,7 @@ public static function getChanges($model)
*
* @return string
*/
public static function getComponentStatus($componentId = '')
protected function getComponentStatus($componentId = '')
{
if ('' == $componentId) {
return 'n/a';
Expand All @@ -77,26 +65,46 @@ public static function getComponentStatus($componentId = '')
}

/**
* Map status ids to a slack color.
* @param int $id
*
* @param int $status
* @return string
*/
protected function translateIncidentStatus($id)
{
return trans('cachet.incidents.status')[$id];
}

/**
* @param int $id
*
* @return string
*/
public static function statusToColor($status)
protected function translateComponentStatus($id)
{
$colormap = [
0 => 'good', // 'Scheduled'
1 => 'danger', // 'Investigating'
2 => 'warning', // 'Identified'
3 => 'warning', // 'Watching'
4 => 'good', // 'Fixed'
];
return trans('cachet.components.status')[$id];
}

if (isset($colormap[$status])) {
return $colormap[$status];
}
/**
* Send Slack message as attachment.
*
* @param array $attachment
* @param string $title
*
* @return mixed
*/
protected function sendAttachment($attachment, $title)
{
return Slack::attach($attachment)->send($title);
}

return '';
/**
* Send plain Slack message.
*
* @param string $message
* @return mixed
*/
protected function sendMessage($message)
{
return Slack::send($message);
}
}
40 changes: 40 additions & 0 deletions src/Handlers/ComponentWasUpdatedHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Mrbase\CachetSlackIntegration\Handlers;

/**
* Class ComponentWasUpdatedHandler
*
* @package Mrbase\CachetSlackIntegration
*/
class ComponentWasUpdatedHandler extends BaseHandler
{
/**
* @var string
*/
private $message;

/**
* ComponentWasUpdatedHandler constructor.
*
* @param int $status
* @param string $name
*/
public function __construct($status, $name)
{
$this->message = trans('slack::messages.component.status_update', [
'name' => $name,
'status' => $this->translateComponentStatus($status),
]);
}

/**
* Send the slack message.
*
* @return mixed
*/
public function send()
{
$this->sendMessage($this->message);
}
}
42 changes: 0 additions & 42 deletions src/Handlers/Events/Component/ComponentUpdated.php

This file was deleted.

62 changes: 0 additions & 62 deletions src/Handlers/Events/Incident/IncidentReported.php

This file was deleted.

Loading

0 comments on commit 1b8bea5

Please sign in to comment.