php-censor/src/Plugin/FlowdockNotify.php

74 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
2016-07-19 20:28:11 +02:00
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use Mremi\Flowdock\Api\Push\Push;
use Mremi\Flowdock\Api\Push\TeamInboxMessage;
2016-07-19 20:28:11 +02:00
use PHPCensor\Plugin;
/**
* Flowdock Plugin
2018-02-04 08:22:07 +01:00
*
2017-10-21 10:51:05 +02:00
* @author Petr Cervenka <petr@nanosolutions.io>
*/
2016-07-11 18:00:04 +02:00
class FlowdockNotify extends Plugin
{
2018-02-04 08:22:07 +01:00
protected $apiKey;
2016-07-11 18:00:04 +02:00
protected $email;
protected $message;
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
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
2016-07-11 18:00:04 +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']);
$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';
}
/**
* 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);
$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")
->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])) {
throw new \Exception(sprintf('Flowdock Failed: %s', $flowMessage->getResponseErrors()));
}
return true;
}
}