-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHipchatNotify.php
More file actions
101 lines (85 loc) · 2.65 KB
/
Copy pathHipchatNotify.php
File metadata and controls
101 lines (85 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Notification;
use HipChat\HipChat;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Exception\Exception;
use PHPCensor\Common\Plugin\Plugin;
/**
* HipchatNotify Plugin
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author James Inman <james@jamesinman.co.uk>
*/
class HipchatNotify extends Plugin
{
private string $authToken;
private string $color = 'yellow';
private bool $notify = false;
private string $message = '%PROJECT_TITLE% built at %BUILD_LINK%';
/**
* @var string|string[]
*/
private $room;
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'hipchat_notify';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$hipChat = new HipChat($this->authToken);
$message = $this->variableInterpolator->interpolate($this->message);
$result = true;
if (\is_array($this->room)) {
foreach ($this->room as $room) {
if (!$hipChat->message_room($room, 'PHP Censor', $message, $this->notify, $this->color)) {
$result = false;
}
}
} else {
if (!$hipChat->message_room($this->room, 'PHP Censor', $message, $this->notify, $this->color)) {
$result = false;
}
}
return $result;
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (\in_array($stage, [
BuildInterface::STAGE_BROKEN,
BuildInterface::STAGE_COMPLETE,
BuildInterface::STAGE_FAILURE,
BuildInterface::STAGE_FIXED,
BuildInterface::STAGE_SUCCESS,
], true)) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
if (!$this->options->all() || !$this->options->get('auth_token') || !$this->options->get('room')) {
throw new Exception("Please define 'room' and 'auth_token' for HipchatNotify plugin!");
}
$this->authToken = $this->variableInterpolator->interpolate((string)$this->options->get('auth_token'));
$this->room = (string)$this->options->get('room');
$this->message = (string)$this->options->get('message', $this->message);
$this->color = (string)$this->options->get('color', $this->color);
$this->notify = (bool)$this->options->get('notify', $this->notify);
}
}