Reducing complexity of Git::runAction() - See #385

This commit is contained in:
Dan Cryer 2014-05-02 15:06:53 +01:00
parent 36134cd2e2
commit 29a9075d4f

View file

@ -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);
}
}