From e871df80c26b9e211845db0c97085b2b151dbd42 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 8 Oct 2015 16:54:01 +0100 Subject: [PATCH] Adding the ability to run branch-specific stages --- PHPCI/Plugin/Util/Executor.php | 53 +++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/PHPCI/Plugin/Util/Executor.php b/PHPCI/Plugin/Util/Executor.php index 3ca244af..b085ac87 100644 --- a/PHPCI/Plugin/Util/Executor.php +++ b/PHPCI/Plugin/Util/Executor.php @@ -50,12 +50,57 @@ class Executor public function executePlugins(&$config, $stage) { $success = true; - // Ignore any stages for which we don't have plugins set: - if (!array_key_exists($stage, $config) || !is_array($config[$stage])) { - return $success; + + /** @var \PHPCI\Model\Build $build */ + $build = $this->pluginFactory->getResourceFor('PHPCI\Model\Build'); + $branch = $build->getBranch(); + $pluginsToExecute = array(); + + // If we have global plugins to execute for this stage, add them to the list to be executed: + if (array_key_exists($stage, $config) && is_array($config[$stage])) { + $pluginsToExecute[] = $config[$stage]; } - foreach ($config[$stage] as $plugin => $options) { + // If we have branch-specific plugins to execute, add them to the list to be executed: + if (isset($config['branch-' . $branch][$stage]) && is_array($config['branch-' . $branch][$stage])) { + $branchConfig = $config['branch-' . $branch]; + $runOption = isset($branchConfig['run-option']) ? $branchConfig['run-option'] : 'after'; + $plugins = $config['branch-' . $branch][$stage]; + + switch ($runOption) { + case 'replace': + $pluginsToExecute = array(); + $pluginsToExecute[] = $plugins; + break; + + case 'before': + array_unshift($pluginsToExecute, $plugins); + break; + + case 'after': + array_push($pluginsToExecute, $plugins); + break; + + default: + array_push($pluginsToExecute, $plugins); + break; + } + } + + foreach ($pluginsToExecute as $pluginSet) { + if (!$this->doExecutePlugins($pluginSet, $stage)) { + $success = false; + } + } + + return $success; + } + + protected function doExecutePlugins(&$plugins, $stage) + { + $success = true; + + foreach ($plugins as $plugin => $options) { $this->logger->log(Lang::get('running_plugin', $plugin)); $this->setPluginStatus($stage, $plugin, Build::STATUS_RUNNING);