2015-04-29 05:51:00 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2015-04-29 05:51:00 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\Model\Build;
|
2015-04-29 05:51:00 +02:00
|
|
|
use Mremi\Flowdock\Api\Push\Push;
|
|
|
|
use Mremi\Flowdock\Api\Push\TeamInboxMessage;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Plugin;
|
2015-04-29 05:51:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flowdock Plugin
|
2018-02-04 08:22:07 +01:00
|
|
|
*
|
2017-10-21 10:51:05 +02:00
|
|
|
* @author Petr Cervenka <petr@nanosolutions.io>
|
2015-04-29 05:51:00 +02:00
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class FlowdockNotify extends Plugin
|
2015-04-29 05:51:00 +02:00
|
|
|
{
|
2018-02-04 08:22:07 +01:00
|
|
|
protected $apiKey;
|
2016-07-11 18:00:04 +02:00
|
|
|
protected $email;
|
|
|
|
protected $message;
|
|
|
|
|
2015-04-29 05:51:00 +02:00
|
|
|
const MESSAGE_DEFAULT = 'Build %BUILD% has finished for commit <a href="%COMMIT_URI%">%SHORT_COMMIT%</a>
|
|
|
|
(%COMMIT_EMAIL%)> on branch <a href="%BRANCH_URI%">%BRANCH%</a>';
|
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'flowdock_notify';
|
|
|
|
}
|
2018-02-04 08:22:07 +01:00
|
|
|
|
2015-04-29 05:51:00 +02:00
|
|
|
/**
|
2016-07-11 18:00:04 +02:00
|
|
|
* {@inheritdoc}
|
2015-04-29 05:51:00 +02:00
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2015-04-29 05:51:00 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2016-07-11 18:00:04 +02:00
|
|
|
|
2015-04-29 05:51:00 +02:00
|
|
|
if (!is_array($options) || !isset($options['api_key'])) {
|
|
|
|
throw new \Exception('Please define the api_key for Flowdock Notify plugin!');
|
|
|
|
}
|
2018-02-04 08:22:07 +01:00
|
|
|
$this->apiKey = trim($options['api_key']);
|
2015-04-29 05:51:00 +02:00
|
|
|
$this->message = isset($options['message']) ? $options['message'] : self::MESSAGE_DEFAULT;
|
2016-07-11 18:00:04 +02:00
|
|
|
$this->email = isset($options['email']) ? $options['email'] : 'PHP Censor';
|
2015-04-29 05:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the Flowdock plugin.
|
|
|
|
* @return bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
|
2018-02-04 08:22:07 +01:00
|
|
|
$message = $this->builder->interpolate($this->message);
|
2015-04-29 05:51:00 +02:00
|
|
|
$successfulBuild = $this->build->isSuccessful() ? 'Success' : 'Failed';
|
2018-02-04 08:22:07 +01:00
|
|
|
$push = new Push($this->apiKey);
|
2018-03-06 14:14:37 +01:00
|
|
|
$flowMessage = TeamInboxMessage::create()
|
2016-07-19 20:28:11 +02:00
|
|
|
->setSource("PHPCensor")
|
2015-04-29 05:51:00 +02:00
|
|
|
->setFromAddress($this->email)
|
|
|
|
->setFromName($this->build->getProject()->getTitle())
|
|
|
|
->setSubject($successfulBuild)
|
|
|
|
->setTags(['#ci'])
|
|
|
|
->setLink($this->build->getBranchLink())
|
|
|
|
->setContent($message);
|
|
|
|
|
2016-04-21 06:58:09 +02:00
|
|
|
if (!$push->sendTeamInboxMessage($flowMessage, ['connect_timeout' => 5000, 'timeout' => 5000])) {
|
2015-04-29 05:51:00 +02:00
|
|
|
throw new \Exception(sprintf('Flowdock Failed: %s', $flowMessage->getResponseErrors()));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|