php-censor/src/PHPCensor/Plugin/Deployer.php

72 lines
1.8 KiB
PHP
Raw Normal View History

2015-10-06 13:00:58 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
2015-10-06 13:00:58 +02:00
use b8\HttpClient;
2016-07-19 20:28:11 +02:00
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
2015-10-06 13:00:58 +02:00
/**
* Integrates PHPCI with Deployer: https://github.com/rebelinblue/deployer
*
2017-03-04 16:39:56 +01:00
* @author Dan Cryer <dan@block8.co.uk>
*/
2016-07-11 18:00:04 +02:00
class Deployer extends Plugin
2015-10-06 13:00:58 +02:00
{
protected $webhookUrl;
protected $reason;
protected $updateOnly;
2015-10-06 13:00:58 +02:00
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'deployer';
}
2015-10-06 13:00:58 +02:00
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
2015-10-06 13:00:58 +02:00
*/
public function __construct(Builder $builder, Build $build, array $options = [])
2015-10-06 13:00:58 +02:00
{
parent::__construct($builder, $build, $options);
2015-10-06 13:00:58 +02:00
2016-07-11 18:00:04 +02:00
$this->reason = 'PHP Censor Build #%BUILD% - %COMMIT_MESSAGE%';
2015-10-06 13:00:58 +02:00
if (isset($options['webhook_url'])) {
$this->webhookUrl = $options['webhook_url'];
}
if (isset($options['reason'])) {
$this->reason = $options['reason'];
}
2016-07-11 18:00:04 +02:00
$this->updateOnly = isset($options['update_only']) ? (bool) $options['update_only'] : true;
2015-10-06 13:00:58 +02:00
}
/**
* Copies files from the root of the build directory into the target folder
*/
public function execute()
{
if (empty($this->webhookUrl)) {
$this->builder->logFailure('You must specify a webhook URL.');
2015-10-06 13:00:58 +02:00
return false;
}
$http = new HttpClient();
2016-04-21 06:58:09 +02:00
$response = $http->post($this->webhookUrl, [
'reason' => $this->builder->interpolate($this->reason),
2016-07-19 13:05:02 +02:00
'source' => 'PHP Censor',
'url' => $this->builder->interpolate('%BUILD_URI%'),
'branch' => $this->builder->interpolate('%BRANCH%'),
2017-01-06 04:04:39 +01:00
'commit' => $this->builder->interpolate('%COMMIT%'),
'update_only' => $this->updateOnly,
2016-04-21 06:58:09 +02:00
]);
2015-10-06 13:00:58 +02:00
return $response['success'];
}
}