-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathFlowdockNotify.php
More file actions
84 lines (72 loc) · 2.55 KB
/
Copy pathFlowdockNotify.php
File metadata and controls
84 lines (72 loc) · 2.55 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
<?php
namespace PHPCensor\Plugin;
use Exception;
use FlowdockClient\Api\Push\Push;
use FlowdockClient\Api\Push\TeamInboxMessage;
use PHPCensor\Builder;
use PHPCensor\Common\Exception\InvalidArgumentException;
use PHPCensor\Common\Exception\RuntimeException;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Flowdock Plugin
*
* @package PHP Censor
* @subpackage Application
*
* @author Petr Cervenka <petr@nanosolutions.io>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class FlowdockNotify extends Plugin
{
protected $authToken;
protected $email;
protected $message;
public const MESSAGE_DEFAULT = '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>';
/**
* @return string
*/
public static function pluginName()
{
return 'flowdock_notify';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
if (empty($options['auth_token'])) {
throw new InvalidArgumentException('Please define the "auth_token" for Flowdock Notify plugin!');
}
if (\array_key_exists('auth_token', $options)) {
$this->authToken = $this->builder->interpolate($options['auth_token'], true);
}
$this->message = isset($options['message']) ? $options['message'] : self::MESSAGE_DEFAULT;
$this->email = isset($options['email']) ? $options['email'] : 'PHP Censor';
}
/**
* Run the Flowdock plugin.
* @return bool
* @throws Exception
*/
public function execute()
{
$message = $this->builder->interpolate($this->message);
$successfulBuild = $this->build->isSuccessful() ? 'Success' : 'Failed';
$push = new Push($this->authToken);
$flowMessage = TeamInboxMessage::create()
->setSource("PHPCensor")
->setFromAddress($this->email)
->setFromName($this->build->getProject()->getTitle())
->setSubject($successfulBuild)
->setTags(['#ci'])
->setLink($this->build->getBranchLink())
->setContent($message);
if (!$push->sendTeamInboxMessage($flowMessage, ['connect_timeout' => 5000, 'timeout' => 5000])) {
throw new RuntimeException(\sprintf('Flowdock Failed: %s', $flowMessage->getResponseErrors()));
}
return true;
}
}