This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mrbase/slack-bot-integration
restructured the whole lot
- Loading branch information
Showing
10 changed files
with
369 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''; | ||
} | ||
|
||
/** | ||
|
@@ -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'; | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.