From 2f473763a71a04604e85bb03ae5e675921762b4a Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Fri, 2 May 2014 15:06:53 +0100 Subject: [PATCH] Reducing complexity of Git::runAction() - See #385 --- PHPCI/Plugin/Git.php | 129 +++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 59 deletions(-) diff --git a/PHPCI/Plugin/Git.php b/PHPCI/Plugin/Git.php index 54f1f160..00c1f78f 100644 --- a/PHPCI/Plugin/Git.php +++ b/PHPCI/Plugin/Git.php @@ -31,7 +31,6 @@ class Git implements \PHPCI\Plugin $this->actions = $options; } - public function execute() { $buildPath = $this->phpci->buildPath; @@ -58,68 +57,80 @@ class Git implements \PHPCI\Plugin return $success; } - protected function runAction($action, $options = array()) + protected function runAction($action, array $options = array()) { - // If options isn't an array, it should be. - if (!is_array($options)) { - $options = array(); + switch ($action) { + case 'merge': + return $this->runMergeAction($options); + + case 'tag': + return $this->runTagAction($options); + + case 'pull': + return $this->runPullAction($options); + + case 'push': + return $this->runPushAction($options); } - // Handle git merges. - if ($action == 'merge' && array_key_exists('branch', $options)) { - $cmd = 'git checkout %s && git merge ' . $this->build->getBranch(); - return $this->phpci->executeCommand($cmd, $this->directory, $options['branch']); - } - - // Handle tagging: - if ($action == 'tag') { - $tagName = date('Ymd-His'); - $message = 'Tag created by PHPCI: ' . date('Y-m-d H:i:s'); - - if (array_key_exists('name', $options)) { - $tagName = $this->phpci->interpolate($options['name']); - } - - if (array_key_exists('message', $options)) { - $message = $this->phpci->interpolate($options['message']); - } - - $cmd = 'git tag %s -m "%s"'; - return $this->phpci->executeCommand($cmd, $tagName, $message); - } - - // Handle pull: - if ($action == 'pull') { - $branch = $this->build->getBranch(); - $remote = 'origin'; - - if (array_key_exists('branch', $options)) { - $branch = $this->phpci->interpolate($options['branch']); - } - - if (array_key_exists('remote', $options)) { - $remote = $this->phpci->interpolate($options['remote']); - } - - return $this->phpci->executeCommand('git pull %s %s', $remote, $branch); - } - - // Handle push: - if ($action == 'push') { - $branch = $this->build->getBranch(); - $remote = 'origin'; - - if (array_key_exists('branch', $options)) { - $branch = $this->phpci->interpolate($options['branch']); - } - - if (array_key_exists('remote', $options)) { - $remote = $this->phpci->interpolate($options['remote']); - } - - return $this->phpci->executeCommand('git push %s %s', $remote, $branch); - } return false; } + + protected function runMergeAction($options) + { + if (array_key_exists('branch', $options)) { + $cmd = 'git checkout %s && git merge ' . $this->build->getBranch(); + return $this->phpci->executeCommand($cmd, $this->directory, $options['branch']); + } + } + + protected function runTagAction($options) + { + $tagName = date('Ymd-His'); + $message = 'Tag created by PHPCI: ' . date('Y-m-d H:i:s'); + + if (array_key_exists('name', $options)) { + $tagName = $this->phpci->interpolate($options['name']); + } + + if (array_key_exists('message', $options)) { + $message = $this->phpci->interpolate($options['message']); + } + + $cmd = 'git tag %s -m "%s"'; + return $this->phpci->executeCommand($cmd, $tagName, $message); + } + + protected function runPullAction($options) + { + $branch = $this->build->getBranch(); + $remote = 'origin'; + + if (array_key_exists('branch', $options)) { + $branch = $this->phpci->interpolate($options['branch']); + } + + if (array_key_exists('remote', $options)) { + $remote = $this->phpci->interpolate($options['remote']); + } + + return $this->phpci->executeCommand('git pull %s %s', $remote, $branch); + } + + protected function runPushAction($options) + { + $branch = $this->build->getBranch(); + $remote = 'origin'; + + if (array_key_exists('branch', $options)) { + $branch = $this->phpci->interpolate($options['branch']); + } + + if (array_key_exists('remote', $options)) { + $remote = $this->phpci->interpolate($options['remote']); + } + + return $this->phpci->executeCommand('git push %s %s', $remote, $branch); + } }