Skip to content

Commit

Permalink
dry-coded rollback command
Browse files Browse the repository at this point in the history
  • Loading branch information
krtek4 committed Nov 16, 2015
1 parent 8dbd1db commit a04adb7
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Liip/RMT/Action/BaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public function __construct($options = array())
*/
abstract public function execute();

/**
* Rollback the effect of this action
*/
public function rollback()
{
// assume there is nothing to do by default
}

/**
* Return the name of the action as it will be display to the user
*
Expand Down
16 changes: 13 additions & 3 deletions src/Liip/RMT/Action/VcsCommitAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ public function execute()

return;
}
$vcs->saveWorkingCopy(
str_replace('%version%', Context::getParam('new-version'), $this->options['commit-message'])
);
$vcs->saveWorkingCopy($this->getCommitMessage(Context::getParam('new-version')));
$this->confirmSuccess();
}

public function rollback()
{
$version = Context::get('version-persister')->getCurrentVersion();
Context::get('vcs')->revertLastCommit($this->getCommitMessage($version));
$this->confirmSuccess();
}

protected function getCommitMessage($version)
{
return str_replace('%version%', $version, $this->options['commit-message']);
}
}
8 changes: 8 additions & 0 deletions src/Liip/RMT/Action/VcsTagAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public function execute()
);
$this->confirmSuccess();
}

public function rollback()
{
$tag = Context::get('version-persister')->getCurrentVersionTag();
Context::get('vcs')->deleteTag($tag);
$this->confirmSuccess();
}

}
2 changes: 2 additions & 0 deletions src/Liip/RMT/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Liip\RMT\Command\CurrentCommand;
use Liip\RMT\Command\ConfigCommand;
use Liip\RMT\Command\InitCommand;
use Liip\RMT\Command\RollbackCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application as BaseApplication;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function __construct()
// Add command that require the config file
if (file_exists($this->getConfigFilePath())) {
$this->add(new ReleaseCommand());
$this->add(new RollbackCommand());
$this->add(new CurrentCommand());
$this->add(new ChangesCommand());
$this->add(new ConfigCommand());
Expand Down
60 changes: 60 additions & 0 deletions src/Liip/RMT/Command/RollbackCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the project RMT
*
* Copyright (c) 2013, Liip AG, http://www.liip.ch
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Liip\RMT\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Liip\RMT\Context;

/**
* Rollback the last release
*/
class RollbackCommand extends BaseCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('rollback');
$this->setDescription('Rollback the last release if there was no change since.');
$this->setHelp('The <comment>rollback</comment> should be used to cancel a previously done release.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if (count(Context::get('vcs')->getLocalModifications()) > 0) {
Context::get('output')->writeln('<error>Local modifications found. Aborting.</error>');
return;
}

$tag = Context::get('version-persister')->getCurrentVersionTag();
$modifications = Context::get('vcs')->getAllModificationsSince($tag, false, false);
if (count($modifications) > 0) {
Context::get('output')->writeln('<error>There were commits since the last release. Aborting.</error>');
return;
}

$this->rollbackActionListIfExist('post-release-actions');
$this->rollbackActionListIfExist('pre-release-actions');
}

protected function rollbackActionListIfExist($name)
{
$actions = Context::getInstance()->getList($name);
$actions = array_reverse($actions);
foreach ($actions as $num => $action) {
$this->getOutput()->write(++$num.') '.$action->getTitle().' : ');
$action->rollback();
}
}
}
17 changes: 17 additions & 0 deletions src/Liip/RMT/VCS/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function createTag($tagName)
return $this->executeGitCommand("tag $tagName");
}

public function deleteTag($tagName)
{
return $this->executeGitCommand("tag -d $tagName");
}

public function publishTag($tagName, $remote = null)
{
$remote = $remote == null ? 'origin' : $remote;
Expand All @@ -68,6 +73,18 @@ public function saveWorkingCopy($commitMsg = '')
$this->executeGitCommand("commit -m \"$commitMsg\"");
}

public function revertLastCommit($commitMsg = null)
{
if (! is_null($commitMsg)) {
$msg = $this->executeGitCommand('log -1 --pretty=%B');
if (count($msg) != 1 || $msg[0] !== $commitMsg) {
return;
}
}

$this->executeGitCommand('reset --hard HEAD~1');
}

public function getCurrentBranch()
{
$branches = $this->executeGitCommand('branch');
Expand Down
16 changes: 16 additions & 0 deletions src/Liip/RMT/VCS/Hg.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function createTag($tagName)
return $this->executeHgCommand("tag $tagName");
}

public function deleteTag($tagName)
{
return $this->executeHgCommand("tag --remove $tagName");
}

public function publishTag($tagName, $remote = null)
{
// nothing to do, tags are published with other changes
Expand All @@ -75,6 +80,17 @@ public function saveWorkingCopy($commitMsg = '')
$this->executeHgCommand("commit -m \"$commitMsg\"");
}

public function revertLastCommit($commitMsg = null)
{
if (! is_null($commitMsg)) {
$msg = $this->executeHgCommand('log -l 1 -T "{desc}"');
if (count($msg) != 1 || $msg[0] !== $commitMsg) {
return;
}
}

$this->executeHgCommand('reset --hard HEAD~1'); }

public function getCurrentBranch()
{
$data = $this->executeHgCommand('branch');
Expand Down
15 changes: 15 additions & 0 deletions src/Liip/RMT/VCS/VCSInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function getTags();
*/
public function createTag($tagName);

/**
* Delete a tag
*
* @param string $tagName
*/
public function deleteTag($tagName);

/**
* Publish a new created tag
*
Expand Down Expand Up @@ -78,6 +85,14 @@ public function getLocalModifications();
*/
public function saveWorkingCopy($commitMsg = '');

/**
* Revert the last commit. If a message is given, only revert
* if the commit message matches.
*
* @param string|null $commitMsg
*/
public function revertLastCommit($commitMsg = null);

/**
* Publish local modification
*
Expand Down

0 comments on commit a04adb7

Please sign in to comment.