Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollback ! #103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing $this->rollbackReleaseAction in the middle, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the release action do that has to be rollbacked ?

Or asked with other words, what won't be rollbacked by the fact that we undo the last commit ?

}

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that the revertLastCommit is based on the commit message. Since some months I'm thinking about writing more explicit commit message. Currently we have:
zebra__git_
It's a bit sad, especially if you think that most of the time, we have a message at disposable for the Changelog. So I was think about to write nicer messages. But if we use it in this context, this will block us to do so...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if we start to use user input this could be an error.

But I was not confident enough to just delete the last commit without any check. Can we guarantee that a commit will be done each time ?


/**
* Publish local modification
*
Expand Down