From 094a84f9b4950faeb956c456125d4879e8893ca0 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Mon, 22 Dec 2014 15:48:35 +0000 Subject: [PATCH 1/4] Hopefully re-greening the build. --- PHPCI/Application.php | 42 ++++++++++++++----------- PHPCI/Controller/SettingsController.php | 6 ++-- PHPCI/Controller/WebhookController.php | 4 +-- PHPCI/Store/BuildStore.php | 8 +++-- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/PHPCI/Application.php b/PHPCI/Application.php index 821c8199..88380c87 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -47,24 +47,7 @@ class Application extends b8\Application return false; }; - // Check settings for disable_authentication enabled and user_id - $skipAuth = function () { - $config = b8\Config::getInstance(); - $state = (bool)$config->get('phpci.authentication_settings.state', false); - $id = $config->get('phpci.authentication_settings.user_id', 0); - - if (false !== $state && 0 != (int)$id) { - $user = b8\Store\Factory::getStore('User') - ->getByPrimaryKey($id); - - if ($user) { - $_SESSION['phpci_user'] = $user; - return true; - } - } - - return false; - }; + $skipAuth = [$this, 'shouldSkipAuth']; // Handler for the route we're about to register, checks for a valid session where necessary: $routeHandler = function (&$route, Response &$response) use (&$request, $validateSession, $skipAuth) { @@ -152,4 +135,27 @@ class Application extends b8\Application $projectStore = b8\Store\Factory::getStore('Project'); $layout->projects = $projectStore->getAll(); } + + /** + * Check whether we should skip auth (because it is disabled) + * @return bool + */ + protected function shouldSkipAuth() + { + $config = b8\Config::getInstance(); + $state = (bool)$config->get('phpci.authentication_settings.state', false); + $userId = $config->get('phpci.authentication_settings.user_id', 0); + + if (false !== $state && 0 != (int)$userId) { + $user = b8\Store\Factory::getStore('User') + ->getByPrimaryKey($userId); + + if ($user) { + $_SESSION['phpci_user'] = $user; + return true; + } + } + + return false; + } } diff --git a/PHPCI/Controller/SettingsController.php b/PHPCI/Controller/SettingsController.php index 3056d203..a764b7e4 100644 --- a/PHPCI/Controller/SettingsController.php +++ b/PHPCI/Controller/SettingsController.php @@ -62,16 +62,16 @@ class SettingsController extends Controller $buildSettings = $this->settings['phpci']['build']; } - $authenticationSettings = array(); + $authSettings = array(); if (isset($this->settings['phpci']['authentication_settings'])) { - $authenticationSettings = $this->settings['phpci']['authentication_settings']; + $authSettings = $this->settings['phpci']['authentication_settings']; } $this->view->github = $this->getGithubForm(); $this->view->emailSettings = $this->getEmailForm($emailSettings); $this->view->buildSettings = $this->getBuildForm($buildSettings); $this->view->isWriteable = $this->canWriteConfig(); - $this->view->authenticationSettings = $this->getAuthenticationForm($authenticationSettings); + $this->view->authenticationSettings = $this->getAuthenticationForm($authSettings); if (!empty($this->settings['phpci']['github']['token'])) { $this->view->githubUser = $this->getGithubUser($this->settings['phpci']['github']['token']); diff --git a/PHPCI/Controller/WebhookController.php b/PHPCI/Controller/WebhookController.php index 56ac0bff..a7652971 100644 --- a/PHPCI/Controller/WebhookController.php +++ b/PHPCI/Controller/WebhookController.php @@ -260,13 +260,13 @@ class WebhookController extends \PHPCI\Controller // build on merge request events if (isset($payload['object_kind']) && $payload['object_kind'] == 'merge_request') { $attributes = $payload['object_attributes']; - if ( $attributes['state'] == 'opened' || $attributes['state'] == 'reopened') { + if ($attributes['state'] == 'opened' || $attributes['state'] == 'reopened') { $branch = $attributes['source_branch']; $commit = $attributes['last_commit']; $committer = $commit['author']['email']; - $this->createBuild($project, $commit['id'], $branch, $committer, $commit['message'] ); + $this->createBuild($project, $commit['id'], $branch, $committer, $commit['message']); } } diff --git a/PHPCI/Store/BuildStore.php b/PHPCI/Store/BuildStore.php index 4f849fa6..39ff7c78 100644 --- a/PHPCI/Store/BuildStore.php +++ b/PHPCI/Store/BuildStore.php @@ -123,11 +123,13 @@ class BuildStore extends BuildStoreBase $select = '`bm`.`build_id`, `bm`.`meta_key`, `bm`.`meta_value`'; $and = $numResults > 1 ? ' AND (`bm`.`build_id` <= :buildId) ' : ' AND (`bm`.`build_id` = :buildId) '; $where = '`bm`.`meta_key` = :key AND `bm`.`project_id` = :projectId ' . $and; - $from = ' `build_meta` AS `bm`'; - if($branch !== null) { + $from = ' `build_meta` AS `bm`'; + + if ($branch !== null) { $where .= ' AND `b`.`branch` = :branch AND `b`.`id`= `bm`.`build_id` '; $from .= ', `build` AS `b`'; - } + } + $query = 'SELECT '.$select.' FROM '.$from.' WHERE '.$where.' ORDER BY `bm`.id DESC LIMIT :numResults'; $stmt = Database::getConnection('read')->prepare($query); From fd8f31840137887e4dbc3ee5849016e443a51377 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Fri, 19 Dec 2014 11:40:23 +0000 Subject: [PATCH 2/4] 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" } } From c2c51e376be2e78b5ea1fb73194e22821f11355e Mon Sep 17 00:00:00 2001 From: Artjom Kurapov Date: Sat, 6 Dec 2014 15:47:06 +0200 Subject: [PATCH 3/4] Logarithmic vertical scale for charts Since we have one diagram for multiple results, we may have PHPCS result giving 12000 errors, while PHPUnit gives 200 tests, and 1 failed test.. those numbers and their variation won't be very visible, unless we use logarithmic scale Closes #686 --- public/assets/js/build-plugins/warnings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/assets/js/build-plugins/warnings.js b/public/assets/js/build-plugins/warnings.js index 0a072d59..ea48d294 100644 --- a/public/assets/js/build-plugins/warnings.js +++ b/public/assets/js/build-plugins/warnings.js @@ -94,7 +94,7 @@ var warningsPlugin = ActiveBuild.UiPlugin.extend({ var data = google.visualization.arrayToDataTable(data); var options = { hAxis: {title: 'Builds'}, - vAxis: {title: 'Warnings / Errors'}, + vAxis: {title: 'Warnings / Errors', logScale:true}, backgroundColor: { fill: 'transparent' }, height: 275, pointSize: 3 From f78b6ae9f99a4aaded5ae15b961fc0001fef925f Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Thu, 11 Dec 2014 14:41:00 +0000 Subject: [PATCH 4/4] Cleanup of the disabled login mode. Hides user menu, the navigation user panel, etc. Closes #718 --- PHPCI/Helper/LoginIsDisabled.php | 35 ++++++++++++++++++++++++++++++++ PHPCI/View/layout.phtml | 5 +++++ 2 files changed, 40 insertions(+) create mode 100644 PHPCI/Helper/LoginIsDisabled.php diff --git a/PHPCI/Helper/LoginIsDisabled.php b/PHPCI/Helper/LoginIsDisabled.php new file mode 100644 index 00000000..208aac23 --- /dev/null +++ b/PHPCI/Helper/LoginIsDisabled.php @@ -0,0 +1,35 @@ + +* @package PHPCI +* @subpackage Web +*/ +class LoginIsDisabled +{ + /** + * Checks if + * @param $method + * @param array $params + * @return mixed|null + */ + public function __call($method, $params = array()) + { + unset($method, $params); + + $config = b8\Config::getInstance(); + $state = (bool) $config->get('phpci.authentication_settings.state', false); + + return (false !== $state); + } +} diff --git a/PHPCI/View/layout.phtml b/PHPCI/View/layout.phtml index b6111646..a3f995d3 100644 --- a/PHPCI/View/layout.phtml +++ b/PHPCI/View/layout.phtml @@ -124,6 +124,7 @@ +LoginIsDisabled()): ?> + @@ -159,6 +161,8 @@