Add a plugin to handle sending notifications to Slack (https://slack.com/)
Closes #720
This commit is contained in:
parent
ead24da1a5
commit
5837c3a530
|
@ -36,21 +36,33 @@ class BuildInterpolator
|
|||
$this->interpolation_vars = array();
|
||||
$this->interpolation_vars['%PHPCI%'] = 1;
|
||||
$this->interpolation_vars['%COMMIT%'] = $build->getCommitId();
|
||||
$this->interpolation_vars['%SHORT_COMMIT%'] = substr($build->getCommitId(), 0, 7);
|
||||
$this->interpolation_vars['%COMMIT_EMAIL%'] = $build->getCommitterEmail();
|
||||
$this->interpolation_vars['%COMMIT_URI%'] = $build->getCommitLink();
|
||||
$this->interpolation_vars['%BRANCH%'] = $build->getBranch();
|
||||
$this->interpolation_vars['%BRANCH_URI%'] = $build->getBranchLink();
|
||||
$this->interpolation_vars['%PROJECT%'] = $build->getProjectId();
|
||||
$this->interpolation_vars['%BUILD%'] = $build->getId();
|
||||
$this->interpolation_vars['%PROJECT_TITLE%'] = $build->getProjectTitle();
|
||||
$this->interpolation_vars['%PROJECT_URI%'] = $phpCiUrl . "project/view/" . $build->getProjectId();
|
||||
$this->interpolation_vars['%BUILD_PATH%'] = $buildPath;
|
||||
$this->interpolation_vars['%BUILD_URI%'] = $phpCiUrl . "build/view/" . $build->getId();
|
||||
$this->interpolation_vars['%PHPCI_COMMIT%'] = $this->interpolation_vars['%COMMIT%'];
|
||||
$this->interpolation_vars['%PHPCI_SHORT_COMMIT%'] = $this->interpolation_vars['%SHORT_COMMIT%'];
|
||||
$this->interpolation_vars['%PHPCI_COMMIT_EMAIL%'] = $this->interpolation_vars['%COMMIT_EMAIL%'];
|
||||
$this->interpolation_vars['%PHPCI_COMMIT_URI%'] = $this->interpolation_vars['%COMMIT_URI%'];
|
||||
$this->interpolation_vars['%PHPCI_PROJECT%'] = $this->interpolation_vars['%PROJECT%'];
|
||||
$this->interpolation_vars['%PHPCI_BUILD%'] = $this->interpolation_vars['%BUILD%'];
|
||||
$this->interpolation_vars['%PHPCI_PROJECT_TITLE%'] = $this->interpolation_vars['%PROJECT_TITLE%'];
|
||||
$this->interpolation_vars['%PHPCI_PROJECT_URI%'] = $this->interpolation_vars['%PROJECT_URI%'];
|
||||
$this->interpolation_vars['%PHPCI_BUILD_PATH%'] = $this->interpolation_vars['%BUILD_PATH%'];
|
||||
$this->interpolation_vars['%PHPCI_BUILD_URI%'] = $this->interpolation_vars['%BUILD_URI%'];
|
||||
|
||||
putenv('PHPCI=1');
|
||||
putenv('PHPCI_COMMIT=' . $this->interpolation_vars['%COMMIT%']);
|
||||
putenv('PHPCI_SHORT_COMMIT=' . $this->interpolation_vars['%SHORT_COMMIT%']);
|
||||
putenv('PHPCI_COMMIT_EMAIL=' . $this->interpolation_vars['%COMMIT_EMAIL%']);
|
||||
putenv('PHPCI_COMMIT_URI=' . $this->interpolation_vars['%COMMIT_URI%']);
|
||||
putenv('PHPCI_PROJECT=' . $this->interpolation_vars['%PROJECT%']);
|
||||
putenv('PHPCI_BUILD=' . $this->interpolation_vars['%BUILD%']);
|
||||
putenv('PHPCI_PROJECT_TITLE=' . $this->interpolation_vars['%PROJECT_TITLE%']);
|
||||
|
|
|
@ -68,8 +68,6 @@ class Lint implements PHPCI\Plugin
|
|||
$success = true;
|
||||
|
||||
$php = $this->phpci->findBinary('php');
|
||||
|
||||
$this->phpci->logExecOutput(false);
|
||||
|
||||
foreach ($this->directories as $dir) {
|
||||
if (!$this->lintDirectory($php, $dir)) {
|
||||
|
@ -78,8 +76,6 @@ class Lint implements PHPCI\Plugin
|
|||
}
|
||||
|
||||
$this->phpci->quiet = false;
|
||||
|
||||
$this->phpci->logExecOutput(true);
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
|
127
PHPCI/Plugin/SlackNotify.php
Normal file
127
PHPCI/Plugin/SlackNotify.php
Normal file
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCI\Plugin;
|
||||
|
||||
use PHPCI\Builder;
|
||||
use PHPCI\Model\Build;
|
||||
|
||||
/**
|
||||
* Slack Plugin
|
||||
* @author Stephen Ball <phpci@stephen.rebelinblue.com>
|
||||
* @package PHPCI
|
||||
* @subpackage Plugins
|
||||
*/
|
||||
class SlackNotify implements \PHPCI\Plugin
|
||||
{
|
||||
private $webHook;
|
||||
private $room;
|
||||
private $username;
|
||||
private $message;
|
||||
private $icon;
|
||||
|
||||
/**
|
||||
* Set up the plugin, configure options, etc.
|
||||
* @param Builder $phpci
|
||||
* @param Build $build
|
||||
* @param array $options
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(Builder $phpci, Build $build, array $options = array())
|
||||
{
|
||||
$this->phpci = $phpci;
|
||||
$this->build = $build;
|
||||
|
||||
if (is_array($options) && isset($options['webhook_url'])) {
|
||||
$this->webHook = trim($options['webhook_url']);
|
||||
|
||||
if (isset($options['message'])) {
|
||||
$this->message = $options['message'];
|
||||
} else {
|
||||
$this->message = '<%PROJECT_URI%|%PROJECT_TITLE%> - <%BUILD_URI%|Build #%BUILD%> has finished ';
|
||||
$this->message .= 'for commit <%COMMIT_URI%|%SHORT_COMMIT% (%COMMIT_EMAIL%)> ';
|
||||
$this->message .= 'on branch <%BRANCH_URI%|%BRANCH%>';
|
||||
}
|
||||
|
||||
if (isset($options['room'])) {
|
||||
$this->room = $options['room'];
|
||||
} else {
|
||||
$this->room = '#phpci';
|
||||
}
|
||||
|
||||
if (isset($options['username'])) {
|
||||
$this->username = $options['username'];
|
||||
} else {
|
||||
$this->username = 'PHPCI';
|
||||
}
|
||||
|
||||
if (isset($options['icon'])) {
|
||||
$this->icon = $options['icon'];
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new \Exception('Please define the webhook_url for slack_notify plugin!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the Slack plugin.
|
||||
* @return bool
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$message = $this->phpci->interpolate($this->message);
|
||||
|
||||
$successfulBuild = $this->build->isSuccessful();
|
||||
|
||||
if ($successfulBuild) {
|
||||
$message = 'Success';
|
||||
$color = 'good';
|
||||
} else {
|
||||
$message = 'Failed';
|
||||
$color = 'danger';
|
||||
}
|
||||
|
||||
// Build up the attachment data
|
||||
$attachment = new \Maknz\Slack\Attachment(array(
|
||||
'fallback' => $message,
|
||||
'pretext' => $message,
|
||||
'color' => $color,
|
||||
'fields' => array(
|
||||
new \Maknz\Slack\AttachmentField(array(
|
||||
'title' => 'Status',
|
||||
'value' => $message,
|
||||
'short' => false
|
||||
))
|
||||
)
|
||||
));
|
||||
|
||||
$client = new \Maknz\Slack\Client($this->webHook);
|
||||
|
||||
if (!empty($this->room)) {
|
||||
$client->setChannel($this->room);
|
||||
}
|
||||
|
||||
if (!empty($this->username)) {
|
||||
$client->setUsername($this->username);
|
||||
}
|
||||
|
||||
if (!empty($this->icon)) {
|
||||
$client->setIcon($this->icon);
|
||||
}
|
||||
|
||||
$client->attach($attachment);
|
||||
|
||||
$success = true;
|
||||
|
||||
$client->send(''); // FIXME: Handle errors
|
||||
|
||||
return $success;
|
||||
}
|
||||
}
|
|
@ -64,6 +64,7 @@
|
|||
"jakub-onderka/php-parallel-lint": "Parallel Linting Tool",
|
||||
"behat/behat": "Behat BDD Testing",
|
||||
"hipchat/hipchat-php": "Hipchat integration",
|
||||
"phptal/phptal": "PHPTAL templating engine"
|
||||
"phptal/phptal": "PHPTAL templating engine",
|
||||
"maknz/slack": "Slack integration"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue