From fd8f31840137887e4dbc3ee5849016e443a51377 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Fri, 19 Dec 2014 11:40:23 +0000 Subject: [PATCH] Add a plugin to handle sending notifications to Slack (https://slack.com/) Closes #720 --- PHPCI/Helper/BuildInterpolator.php | 12 +++ PHPCI/Plugin/Lint.php | 4 - PHPCI/Plugin/SlackNotify.php | 127 +++++++++++++++++++++++++++++ composer.json | 3 +- 4 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 PHPCI/Plugin/SlackNotify.php diff --git a/PHPCI/Helper/BuildInterpolator.php b/PHPCI/Helper/BuildInterpolator.php index c2b625d6..fbadbc30 100644 --- a/PHPCI/Helper/BuildInterpolator.php +++ b/PHPCI/Helper/BuildInterpolator.php @@ -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%']); diff --git a/PHPCI/Plugin/Lint.php b/PHPCI/Plugin/Lint.php index 65a3a2e3..90436d70 100644 --- a/PHPCI/Plugin/Lint.php +++ b/PHPCI/Plugin/Lint.php @@ -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; } diff --git a/PHPCI/Plugin/SlackNotify.php b/PHPCI/Plugin/SlackNotify.php new file mode 100644 index 00000000..cff30e5a --- /dev/null +++ b/PHPCI/Plugin/SlackNotify.php @@ -0,0 +1,127 @@ + + * @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; + } +} diff --git a/composer.json b/composer.json index f7d1115d..2d3930e3 100644 --- a/composer.json +++ b/composer.json @@ -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" } }