-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowdockNotify.php
More file actions
102 lines (88 loc) · 2.83 KB
/
Copy pathFlowdockNotify.php
File metadata and controls
102 lines (88 loc) · 2.83 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
102
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Notification;
use FlowdockClient\Api\Push\Push;
use FlowdockClient\Api\Push\TeamInboxMessage;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Exception\Exception;
use PHPCensor\Common\Plugin\Plugin;
/**
* FlowdockNotify Plugin
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Petr Cervenka <petr@nanosolutions.io>
*/
class FlowdockNotify extends Plugin
{
/**
*/
private string $authToken;
/**
*/
private string $email = 'PHP Censor';
/**
*/
private string $message = 'Build %BUILD_ID% has finished for commit <a href="%COMMIT_LINK%">%SHORT_COMMIT_ID%</a>
(%COMMITTER_EMAIL%)> on branch <a href="%BRANCH_LINK%">%BRANCH%</a>';
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'flowdock_notify';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$message = $this->variableInterpolator->interpolate($this->message);
$successfulBuild = $this->build->isSuccessful() ? 'Success' : 'Failed';
$push = new Push($this->authToken);
$flowMessage = TeamInboxMessage::create()
->setSource("PHPCensor")
->setFromAddress($this->email)
->setFromName($this->project->getTitle())
->setSubject($successfulBuild)
->setTags(['#ci'])
->setLink($this->build->getBranchLink())
->setContent($message);
if (!$push->sendTeamInboxMessage($flowMessage, ['connect_timeout' => 5000, 'timeout' => 5000])) {
throw new Exception(
\sprintf('Flowdock Failed: %s', \json_encode($flowMessage->getResponseErrors()))
);
}
return true;
}
/**
* {@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')) {
throw new Exception("Please define the 'auth_token' for FlowdockNotify plugin!");
}
$this->authToken = $this->variableInterpolator->interpolate((string)$this->options->get('auth_token'));
$this->message = (string)$this->options->get('message', $this->message);
$this->email = (string)$this->options->get('email', $this->email);
}
}