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 5dee0ab2..72df7c95 100644 --- a/PHPCI/Controller/SettingsController.php +++ b/PHPCI/Controller/SettingsController.php @@ -72,16 +72,16 @@ class SettingsController extends Controller $emailSettings = $this->settings['phpci']['email_settings']; } - $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->basicSettings = $this->getBasicForm($basicSettings); $this->view->buildSettings = $this->getBuildForm($buildSettings); $this->view->github = $this->getGithubForm(); $this->view->emailSettings = $this->getEmailForm($emailSettings); - $this->view->authenticationSettings = $this->getAuthenticationForm($authenticationSettings); + $this->view->authenticationSettings = $this->getAuthenticationForm($authSettings); $this->view->isWriteable = $this->canWriteConfig(); if (!empty($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/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/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/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/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); diff --git a/PHPCI/View/layout.phtml b/PHPCI/View/layout.phtml index d5b59090..1bd68749 100644 --- a/PHPCI/View/layout.phtml +++ b/PHPCI/View/layout.phtml @@ -136,6 +136,7 @@ +LoginIsDisabled()): ?> + @@ -171,6 +173,8 @@