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

Mc 37324: Add fallback to 'patch' command when 'git' command is not available #2

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
11 changes: 11 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
@@ -27,6 +27,11 @@
<service id="Magento\CloudPatches\Patch\Pool\RequiredPool" lazy="true"/>
<service id="Magento\CloudPatches\Patch\Pool\LocalPool" lazy="true"/>
<service id="Magento\CloudPatches\Patch\Status\StatusPool" autowire="false"/>
<service id="Magento\CloudPatches\Patch\PatchCommandNotFound" autowire="false"/>
<service id="Magento\CloudPatches\Patch\PatchCommandException" autowire="false"/>
<service id="Magento\CloudPatches\Shell\Command\DriverException" autowire="false"/>
<service id="Magento\CloudPatches\Patch\PatchCommand" autowire="false"/>
<service id="Magento\CloudPatches\Patch\PatchCommandInterface" alias="Magento\CloudPatches\Patch\PatchCommand"/>
<service id="statusPool" class="Magento\CloudPatches\Patch\Status\StatusPool" lazy="true">
<argument key="$resolvers" type="collection">
<argument type="service" id="Magento\CloudPatches\Patch\Status\LocalResolver"/>
@@ -76,5 +81,11 @@
<argument key="$actionPool" type="service" id="ApplyOptionalActionPool"/>
</service>
<service id="Magento\CloudPatches\Patch\PatchBuilder" shared="false"/>
<service id="Magento\CloudPatches\Patch\PatchCommand">
<argument key="$drivers" type="collection">
<argument type="service" id="Magento\CloudPatches\Shell\Command\GitDriver"/>
<argument type="service" id="Magento\CloudPatches\Shell\Command\PatchDriver"/>
</argument>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion src/Command/Process/Action/RevertAction.php
Original file line number Diff line number Diff line change
@@ -179,7 +179,7 @@ private function printPatchRevertingFailed(OutputInterface $output, PatchInterfa
'Reverting patch %s (%s) failed.%s',
$patch->getId(),
$patch->getPath(),
$this->renderer->formatErrorOutput($errorOutput)
PHP_EOL . $errorOutput
);

$this->logger->error($errorMessage);
4 changes: 2 additions & 2 deletions src/Command/Process/ApplyLocal.php
Original file line number Diff line number Diff line change
@@ -89,13 +89,13 @@ public function run(InputInterface $input, OutputInterface $output)
$this->printInfo($output, $message);
array_push($appliedPatches, $patch);
} catch (ApplierException $exception) {
$this->printError($output, 'Error: patch conflict happened');
$this->printError($output, 'Error: patch can\'t be applied');
$messages = $this->rollbackProcessor->process($appliedPatches);
$output->writeln($messages);
$errorMessage = sprintf(
'Applying patch %s failed.%s',
$patch->getPath(),
$this->renderer->formatErrorOutput($exception->getMessage())
PHP_EOL . $exception->getMessage()
);

throw new RuntimeException($errorMessage, $exception->getCode());
2 changes: 1 addition & 1 deletion src/Command/Process/Ece/Revert.php
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ function ($patch) {
$errorMessage = sprintf(
'Reverting patch %s failed.%s',
$patch->getPath(),
$this->renderer->formatErrorOutput($exception->getMessage())
PHP_EOL . $exception->getMessage()
);
$this->printError($output, $errorMessage);
}
15 changes: 0 additions & 15 deletions src/Command/Process/Renderer.php
Original file line number Diff line number Diff line change
@@ -140,21 +140,6 @@ public function printPatchInfo(
$output->writeln('');
}

/**
* Format error output.
*
* @param string $errorOutput
* @return string
*/
public function formatErrorOutput(string $errorOutput): string
{
if (preg_match('#^.*?Error Output:(?<errors>.*?)$#is', $errorOutput, $matches)) {
$errorOutput = PHP_EOL . 'Error Output:' . $matches['errors'];
}

return $errorOutput;
}

/**
* Asks a confirmation question to the user.
*
9 changes: 7 additions & 2 deletions src/Command/Process/ShowStatus.php
Original file line number Diff line number Diff line change
@@ -111,9 +111,14 @@ function ($patch) {
*/
private function printDetailsInfo(OutputInterface $output)
{
$supportUrl = 'https://support.magento.com';
$releaseNotesUrl = 'https://devdocs.magento.com/quality-patches/release-notes.html';

$output->writeln(
'<info>More detailed information about patches you can find on </info>' .
'<href=https://support.magento.com>https://support.magento.com</>'
'<info>Patch details you can find on </info>' .
sprintf('<href=%1$s>%1$s</> <info>(search for patch id, ex. MDVA-30265)</info>', $supportUrl) .
PHP_EOL .
sprintf('<info>Release notes</info> <href=%1$s>%1$s</>', $releaseNotesUrl)
);
}

47 changes: 19 additions & 28 deletions src/Patch/Applier.php
Original file line number Diff line number Diff line change
@@ -10,18 +10,16 @@
use Magento\CloudPatches\Composer\MagentoVersion;
use Magento\CloudPatches\Filesystem\Filesystem;
use Magento\CloudPatches\Patch\Status\StatusPool;
use Magento\CloudPatches\Shell\ProcessFactory;
use Symfony\Component\Process\Exception\ProcessFailedException;

/**
* Applies and reverts patches.
*/
class Applier
{
/**
* @var ProcessFactory
* @var PatchCommandInterface
*/
private $processFactory;
private $patchCommand;

/**
* @var GitConverter
@@ -39,18 +37,18 @@ class Applier
private $filesystem;

/**
* @param ProcessFactory $processFactory
* @param PatchCommandInterface $patchCommand
* @param GitConverter $gitConverter
* @param MagentoVersion $magentoVersion
* @param Filesystem $filesystem
*/
public function __construct(
ProcessFactory $processFactory,
PatchCommandInterface $patchCommand,
GitConverter $gitConverter,
MagentoVersion $magentoVersion,
Filesystem $filesystem
) {
$this->processFactory = $processFactory;
$this->patchCommand = $patchCommand;
$this->gitConverter = $gitConverter;
$this->magentoVersion = $magentoVersion;
$this->filesystem = $filesystem;
@@ -69,13 +67,11 @@ public function apply(string $path, string $id): string
{
$content = $this->readContent($path);
try {
$this->processFactory->create(['git', 'apply'], $content)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->apply($content);
} catch (PatchCommandException $exception) {
try {
$this->processFactory->create(['git', 'apply', '--check', '--reverse'], $content)
->mustRun();
} catch (ProcessFailedException $reverseException) {
$this->patchCommand->revertCheck($content);
} catch (PatchCommandException $reverseException) {
throw new ApplierException($exception->getMessage(), $exception->getCode());
}

@@ -98,13 +94,11 @@ public function revert(string $path, string $id): string
{
$content = $this->readContent($path);
try {
$this->processFactory->create(['git', 'apply', '--reverse'], $content)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->revert($content);
} catch (PatchCommandException $exception) {
try {
$this->processFactory->create(['git', 'apply', '--check'], $content)
->mustRun();
} catch (ProcessFailedException $applyException) {
$this->patchCommand->applyCheck($content);
} catch (PatchCommandException $applyException) {
throw new ApplierException($exception->getMessage(), $exception->getCode());
}

@@ -124,13 +118,11 @@ public function status(string $patchContent): string
{
$patchContent = $this->prepareContent($patchContent);
try {
$this->processFactory->create(['git', 'apply', '--check'], $patchContent)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->applyCheck($patchContent);
} catch (PatchCommandException $exception) {
try {
$this->processFactory->create(['git', 'apply', '--check', '--reverse'], $patchContent)
->mustRun();
} catch (ProcessFailedException $reverseException) {
$this->patchCommand->revertCheck($patchContent);
} catch (PatchCommandException $reverseException) {
return StatusPool::NA;
}

@@ -150,9 +142,8 @@ public function checkApply(string $patchContent): bool
{
$patchContent = $this->prepareContent($patchContent);
try {
$this->processFactory->create(['git', 'apply', '--check'], $patchContent)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->applyCheck($patchContent);
} catch (PatchCommandException $exception) {
return false;
}

4 changes: 2 additions & 2 deletions src/Patch/Conflict/Processor.php
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ public function process(
array $appliedPatches,
string $exceptionMessage
) {
$errorMessage = 'Error: patch conflict happened';
$errorMessage = 'Error: patch can\'t be applied';
$this->logger->error($errorMessage);
$output->writeln('<error>' . $errorMessage . '</error>');

@@ -84,7 +84,7 @@ public function process(
'Applying patch %s (%s) failed.%s%s',
$patch->getId(),
$patch->getPath(),
$this->renderer->formatErrorOutput($exceptionMessage),
PHP_EOL . $exceptionMessage,
$conflictDetails ? PHP_EOL . $conflictDetails : ''
);

89 changes: 89 additions & 0 deletions src/Patch/PatchCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Patch;

use Magento\CloudPatches\Shell\Command\DriverInterface;

/**
* Patch command selector
*/
class PatchCommand implements PatchCommandInterface
{
/**
* @var DriverInterface[]
*/
private $drivers;

/**
* @var DriverInterface
*/
private $driver;

/**
* @param DriverInterface[] $drivers
*/
public function __construct(
array $drivers
) {
$this->drivers = $drivers;
}

/**
* @inheritDoc
*/
public function apply(string $patch)
{
$this->getDriver()->apply($patch);
}

/**
* @inheritDoc
*/
public function revert(string $patch)
{
$this->getDriver()->revert($patch);
}

/**
* @inheritDoc
*/
public function applyCheck(string $patch)
{
$this->getDriver()->applyCheck($patch);
}

/**
* @inheritDoc
*/
public function revertCheck(string $patch)
{
$this->getDriver()->revertCheck($patch);
}

/**
* Returns first available driver
*
* @return DriverInterface
* @throws PatchCommandNotFound
*/
private function getDriver(): DriverInterface
{
if ($this->driver === null) {
foreach ($this->drivers as $driver) {
if ($driver->isInstalled()) {
$this->driver = $driver;
break;
}
}
if ($this->driver === null) {
throw new PatchCommandNotFound();
}
}
return $this->driver;
}
}
17 changes: 17 additions & 0 deletions src/Patch/PatchCommandException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Patch;

use Magento\CloudPatches\App\GenericException;

/**
* Generic patch command exception
*/
class PatchCommandException extends GenericException
{
}
50 changes: 50 additions & 0 deletions src/Patch/PatchCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Patch;

/**
* Patch command interface
*/
interface PatchCommandInterface
{
/**
* Applies patch
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function apply(string $patch);

/**
* Reverts patch
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function revert(string $patch);

/**
* Checks if patch can be applied.
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function applyCheck(string $patch);

/**
* Checks if patch can be reverted
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function revertCheck(string $patch);
}
Loading