From e9e80c517928497a35e4007fe745451ad0ec5445 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Mon, 12 Oct 2015 17:41:20 +0100 Subject: [PATCH 01/10] Adding a 'broken' and 'fixed' event --- PHPCI/Builder.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 39e8ca41..001dcba5 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -188,6 +188,13 @@ class Builder implements LoggerAwareInterface $this->build->sendStatusPostback(); $success = true; + $previous_build = $this->build->getProject()->getLatestBuild(); + if ($previous_build) { + $previous_state = $previous_build->getStatus(); + } else { + $previous_state = Build::STATUS_NEW; + } + try { // Set up the build: $this->setupBuild(); @@ -210,9 +217,19 @@ class Builder implements LoggerAwareInterface if ($success) { $this->pluginExecutor->executePlugins($this->config, 'success'); + + if ($previous_state == Build::STATUS_FAILED || $previous_status == Build::STATUS_NEW) { + $this->pluginExecutor->executePlugins($this->config, 'fixed'); + } + $this->buildLogger->logSuccess(Lang::get('build_success')); } else { $this->pluginExecutor->executePlugins($this->config, 'failure'); + + if ($previous_state == Build::STATUS_SUCCESS || $previous_status == Build::STATUS_NEW) { + $this->pluginExecutor->executePlugins($this->config, 'broken'); + } + $this->buildLogger->logFailure(Lang::get('build_failed')); } } catch (\Exception $ex) { From f1ca029482d6d8784c757ceda0074f8ad30ab80e Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Mon, 12 Oct 2015 17:42:59 +0100 Subject: [PATCH 02/10] Don't report that the build has been fixed on initial commit --- PHPCI/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 001dcba5..476d1b15 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -218,7 +218,7 @@ class Builder implements LoggerAwareInterface if ($success) { $this->pluginExecutor->executePlugins($this->config, 'success'); - if ($previous_state == Build::STATUS_FAILED || $previous_status == Build::STATUS_NEW) { + if ($previous_state == Build::STATUS_FAILED) { $this->pluginExecutor->executePlugins($this->config, 'fixed'); } From 78dbc592b36dd761a703dd56922bdaca9e12a071 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 11:09:42 +0100 Subject: [PATCH 03/10] Fixing using the wrong variable for the previous state --- PHPCI/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 476d1b15..27001164 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -226,7 +226,7 @@ class Builder implements LoggerAwareInterface } else { $this->pluginExecutor->executePlugins($this->config, 'failure'); - if ($previous_state == Build::STATUS_SUCCESS || $previous_status == Build::STATUS_NEW) { + if ($previous_state == Build::STATUS_SUCCESS || $previous_state == Build::STATUS_NEW) { $this->pluginExecutor->executePlugins($this->config, 'broken'); } From 6fcd73a3567e1d902c24f089be0e231d1e308db8 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 13:09:24 +0100 Subject: [PATCH 04/10] Moving getLatest() before the new build is saved --- PHPCI/Builder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 27001164..d28557c6 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -181,6 +181,8 @@ class Builder implements LoggerAwareInterface */ public function execute() { + $previous_build = $this->build->getProject()->getLatestBuild(); + // Update the build in the database, ping any external services. $this->build->setStatus(Build::STATUS_RUNNING); $this->build->setStarted(new \DateTime()); @@ -188,7 +190,6 @@ class Builder implements LoggerAwareInterface $this->build->sendStatusPostback(); $success = true; - $previous_build = $this->build->getProject()->getLatestBuild(); if ($previous_build) { $previous_state = $previous_build->getStatus(); } else { From 1004b8abf34e9fb78b8c60e83d044d8a2d5fe2af Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 13:31:12 +0100 Subject: [PATCH 05/10] Added a get previous build method --- PHPCI/Builder.php | 4 ++-- PHPCI/Model/Project.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index d28557c6..b3f4ec53 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -181,8 +181,6 @@ class Builder implements LoggerAwareInterface */ public function execute() { - $previous_build = $this->build->getProject()->getLatestBuild(); - // Update the build in the database, ping any external services. $this->build->setStatus(Build::STATUS_RUNNING); $this->build->setStarted(new \DateTime()); @@ -190,6 +188,8 @@ class Builder implements LoggerAwareInterface $this->build->sendStatusPostback(); $success = true; + $previous_build = $this->build->getProject()->getPreviousBuild(); + if ($previous_build) { $previous_state = $previous_build->getStatus(); } else { diff --git a/PHPCI/Model/Project.php b/PHPCI/Model/Project.php index 4a8b3c69..717b0d9a 100644 --- a/PHPCI/Model/Project.php +++ b/PHPCI/Model/Project.php @@ -50,6 +50,29 @@ class Project extends ProjectBase return null; } + /** + * Return the previous build from a specific branch, for this project. + * @param string $branch + * @return mixed|null + */ + public function getPreviousBuild($branch = 'master') + { + $criteria = array('branch' => $branch, 'project_id' => $this->getId()); + + $order = array('id' => 'DESC'); + $builds = Store\Factory::getStore('Build')->getWhere($criteria, 1, 1, array(), $order); + + if (is_array($builds['items']) && count($builds['items'])) { + $previous = array_shift($builds['items']); + + if (isset($previous) && $previous instanceof Build) { + return $previous; + } + } + + return null; + } + /** * Store this project's access_information data * @param string|array $value From 669126267504a2d4bf91bdc4a2aa14a98c037b51 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 13:59:52 +0100 Subject: [PATCH 06/10] Added missing language strings for broken/fixed --- PHPCI/Languages/lang.en.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PHPCI/Languages/lang.en.php b/PHPCI/Languages/lang.en.php index 9729d746..baccdab6 100644 --- a/PHPCI/Languages/lang.en.php +++ b/PHPCI/Languages/lang.en.php @@ -305,6 +305,8 @@ PHPCI', 'stage_complete' => 'Complete', 'stage_success' => 'Success', 'stage_failure' => 'Failure', + 'stage_broken' => 'Broken', + 'stage_fixed' => 'Fixed', // Installer 'installation_url' => 'PHPCI Installation URL', From 7bce04671e5a3746233a7564b6bb0513bf6b2315 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 14:12:08 +0100 Subject: [PATCH 07/10] Tweaked the call to getPreviousBuild() to get the previous build in the same branch --- PHPCI/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index b3f4ec53..708f8f86 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -188,7 +188,7 @@ class Builder implements LoggerAwareInterface $this->build->sendStatusPostback(); $success = true; - $previous_build = $this->build->getProject()->getPreviousBuild(); + $previous_build = $this->build->getProject()->getPreviousBuild($this->build->getBranch()); if ($previous_build) { $previous_state = $previous_build->getStatus(); From f25ec7594863ce59c83ccb07f9e6a902d895ddc4 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 14:28:57 +0100 Subject: [PATCH 08/10] Cleaning up code style --- PHPCI/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 708f8f86..d6d94cae 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -190,10 +190,10 @@ class Builder implements LoggerAwareInterface $previous_build = $this->build->getProject()->getPreviousBuild($this->build->getBranch()); + $previous_state = Build::STATUS_NEW; + if ($previous_build) { $previous_state = $previous_build->getStatus(); - } else { - $previous_state = Build::STATUS_NEW; } try { From faf57284da498a9d6d39ae4bbf0a3e4c22b34d3a Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 15:42:33 +0100 Subject: [PATCH 09/10] Add option to hide slack attachment --- PHPCI/Plugin/SlackNotify.php | 66 +++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/PHPCI/Plugin/SlackNotify.php b/PHPCI/Plugin/SlackNotify.php index 65a190fd..9dae70eb 100644 --- a/PHPCI/Plugin/SlackNotify.php +++ b/PHPCI/Plugin/SlackNotify.php @@ -24,6 +24,7 @@ class SlackNotify implements \PHPCI\Plugin private $username; private $message; private $icon; + private $show_status; /** * Set up the plugin, configure options, etc. @@ -60,6 +61,12 @@ class SlackNotify implements \PHPCI\Plugin $this->username = 'PHPCI'; } + if (isset($options['show_status'])) { + $this->show_status = (bool) $options['show_status']; + } else { + $this->show_status = false; + } + if (isset($options['icon'])) { $this->icon = $options['icon']; } @@ -74,32 +81,10 @@ class SlackNotify implements \PHPCI\Plugin */ public function execute() { - $message = $this->phpci->interpolate($this->message); + $body = $this->phpci->interpolate($this->message); $successfulBuild = $this->build->isSuccessful(); - if ($successfulBuild) { - $status = 'Success'; - $color = 'good'; - } else { - $status = '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' => $status, - 'short' => false - )) - ) - )); - $client = new \Maknz\Slack\Client($this->webHook); $message = $client->createMessage(); @@ -116,12 +101,39 @@ class SlackNotify implements \PHPCI\Plugin $message->setIcon($this->icon); } - $message->attach($attachment); + // Include an attachment which shows the status and hide the message + if ($this->show_status) { - $success = true; + if ($successfulBuild) { + $status = 'Success'; + $color = 'good'; + } else { + $status = 'Failed'; + $color = 'danger'; + } - $message->send(''); + // Build up the attachment data + $attachment = new \Maknz\Slack\Attachment(array( + 'fallback' => $body, + 'pretext' => $body, + 'color' => $color, + 'fields' => array( + new \Maknz\Slack\AttachmentField(array( + 'title' => 'Status', + 'value' => $status, + 'short' => false + )) + ) + )); - return $success; + $message->attach($attachment); + + $body = ''; + } + + + $message->send($body); + + return true; } } From 3d6655c075283f4d7168ac1a9343bd14b5315712 Mon Sep 17 00:00:00 2001 From: Stephen Ball Date: Tue, 13 Oct 2015 15:48:22 +0100 Subject: [PATCH 10/10] Default to showing the slack status attachment --- PHPCI/Plugin/SlackNotify.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PHPCI/Plugin/SlackNotify.php b/PHPCI/Plugin/SlackNotify.php index 9dae70eb..0367cfdb 100644 --- a/PHPCI/Plugin/SlackNotify.php +++ b/PHPCI/Plugin/SlackNotify.php @@ -64,7 +64,7 @@ class SlackNotify implements \PHPCI\Plugin if (isset($options['show_status'])) { $this->show_status = (bool) $options['show_status']; } else { - $this->show_status = false; + $this->show_status = true; } if (isset($options['icon'])) { @@ -131,7 +131,6 @@ class SlackNotify implements \PHPCI\Plugin $body = ''; } - $message->send($body); return true;