Skip to content
Merged
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,16 @@ n98-magerun2.phar github:pr:patch --mage-os <pr-number>
n98-magerun2.phar github:pr:patch --patch <pr-number>
```

*Directly apply the patch:*

```sh
# Magento 2 Open Source
n98-magerun2.phar github:pr:patch --patch --apply <pr-number>

# for Mage-OS
n98-magerun2.phar github:pr:patch --mage-os --patch --apply <pr-number>
```

Files of the magento2-base and magento2-ee-base and b2b base packages are currently not handled by the command.

**List only the raw diff:**
Expand Down
10 changes: 5 additions & 5 deletions src/N98/Magento/Command/Github/PatchFileContent/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ class Creator
* @param string $diffContent
* @return string
*/
public static function create(string $diffContent): string
public static function create(string $diffContent, string $replaceVendor): string
{
$appDesignProcessor = new AppDesignProcessor();
$diffContent = $appDesignProcessor->process($diffContent);
$diffContent = $appDesignProcessor->process($diffContent, $replaceVendor);

$appCodeProcessor = new AppCodeProcessor();
$diffContent = $appCodeProcessor->process($diffContent);
$diffContent = $appCodeProcessor->process($diffContent, $replaceVendor);

$i18nProcessor = new I18nProcessor();
$diffContent = $i18nProcessor->process($diffContent);
$diffContent = $i18nProcessor->process($diffContent, $replaceVendor);

$libProcessor = new LibProcessor();
$diffContent = $libProcessor->process($diffContent);
$diffContent = $libProcessor->process($diffContent, $replaceVendor);

return $diffContent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

class AppCodeProcessor implements ProcessorInterface
{
public function process(string $diffContent): string
public function process(string $diffContent, string $replaceVendor): string
{
$callback = function ($matches) {
$callback = function ($matches) use ($replaceVendor) {
// camelcase to dash
$matches[1] = preg_replace('/([a-z])([A-Z])/', '$1-$2', $matches[1]);

return 'vendor/magento/module-' . strtolower($matches[1]) . '/';
return 'vendor/' . $replaceVendor . '/module-' . strtolower($matches[1]) . '/';
};

return (string) preg_replace_callback('/app\/code\/Magento\/([a-zA-Z0-9_]+)\//', $callback, $diffContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

class AppDesignProcessor implements ProcessorInterface
{
public function process(string $diffContent): string
public function process(string $diffContent, string $replaceVendor): string
{
// preg_replace app/design/frontend/Magento/<blank>/ with vendor/magento/theme-frontend-<blank>/
$diffContent = preg_replace(
'/app\/design\/frontend\/Magento\/([a-zA-Z0-9_]+)\//',
'vendor/magento/theme-frontend-$1/',
'vendor/' . $replaceVendor . '/theme-frontend-$1/',
$diffContent
);

// preg_replace app/design/adminhtml/Magento/<blank>/ with vendor/magento/theme-adminhtml-<blank>/
$diffContent = preg_replace(
'/app\/design\/adminhtml\/Magento\/([a-zA-Z0-9_]+)\//',
'vendor/magento/theme-adminhtml-$1/',
'vendor/' . $replaceVendor . '/theme-adminhtml-$1/',
$diffContent
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class I18nProcessor implements ProcessorInterface
{
public function process(string $diffContent): string
public function process(string $diffContent, string $replaceVendor): string
{
$diffContent = preg_replace_callback(
'/app\/i18n\/([a-zA-Z0-9_]+)\//',
function ($matches) {
return 'vendor/magento/language-' . strtolower($matches[1]) . '/';
function ($matches) use ($replaceVendor) {
return 'vendor/' . $replaceVendor . '/language-' . strtolower($matches[1]) . '/';
},
$diffContent
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

class LibProcessor implements ProcessorInterface
{
public function process(string $diffContent): string
public function process(string $diffContent, string $replaceVendor): string
{
// edge cases -> Message Queue is part of the framework directory but later on in an own package
$diffContent = preg_replace(
'/lib\/internal\/Magento\/Framework\/MessageQueue\/([a-zA-Z0-9_]+)\//',
'vendor/magento/framework-message-queue/$1/',
'vendor/' . $replaceVendor . '/framework-message-queue/$1/',
$diffContent
);

// Handle the rest of the lib/internal/Magento directory
$callback = function ($matches) {
$callback = function ($matches) use ($replaceVendor) {
// camelcase to dash
$matches[1] = preg_replace('/([a-z])([A-Z])/', '$1-$2', $matches[1]);

return 'vendor/magento/' . strtolower($matches[1]) . '/';
return 'vendor/' . $replaceVendor . '/' . strtolower($matches[1]) . '/';
};

$diffContent = (string) preg_replace_callback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface ProcessorInterface
{
public function process(string $diffContent): string;
public function process(string $diffContent, string $replaceVendor): string;
}
49 changes: 43 additions & 6 deletions src/N98/Magento/Command/Github/PullRequestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Magento\Command\Github\PatchFileContent\Creator as PatchFileContentCreator;
use N98\Util\OperatingSystem;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use WpOrg\Requests\Requests;
use WpOrg\Requests\Response;

class PullRequestCommand extends AbstractMagentoCommand
{
Expand All @@ -29,6 +32,7 @@ protected function configure()
->addOption('repository', 'r', InputOption::VALUE_OPTIONAL, 'Repository to fetch from', 'magento/magento2')
->addOption('mage-os', null, InputOption::VALUE_NONE, 'Shortcut option to use the mage-os/mageos-magento2 repository.')
->addOption('patch', 'd', InputOption::VALUE_NONE, 'Download patch and prepare it for applying')
->addOption('apply', 'a', InputOption::VALUE_NONE, 'Apply patch to current working directory')
->addOption('diff', null, InputOption::VALUE_NONE, 'Raw diff download')
->addOption('json', null, InputOption::VALUE_NONE, 'Show pull request data as json')
->setDescription('Download patch from github merge request <comment>(experimental)</comment>');
Expand Down Expand Up @@ -73,11 +77,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
$table->render();

if ($input->getOption('patch')) {
$this->patchFile($prData, $output);
$replaceVendor = 'magento';
if ($input->getOption('mage-os')) {
$replaceVendor = 'mage-os';
}

$patchFilename = $this->patchFile($prData, $replaceVendor, $output);

if ($input->getOption('apply')) {
$this->applyPatch($output, $patchFilename);
}
}

if (!$input->getOption('patch') && !$input->getOption('diff')) {
$output->writeln('Use <comment>--patch</comment> to download the patch as ready to apply patch file');
$output->writeln('Use <comment>--patch --apply</comment> to download the patch and directly apply it');
$output->writeln('Use <comment>--diff</comment> to see the raw diff');
}

Expand All @@ -100,13 +114,15 @@ protected function fetchDiffContent($diffUrl): string

/**
* @param array $prData
* @param string $replaceVendor
* @param OutputInterface $output
* @return void
* @return string Patch file name
*/
protected function patchFile(array $prData, OutputInterface $output): void
protected function patchFile(array $prData, string $replaceVendor, OutputInterface $output): string
{
$patchFileContent = PatchFileContentCreator::create(
$this->fetchDiffContent($prData['diff_url'])
$this->fetchDiffContent($prData['diff_url']),
$replaceVendor
);

$filename = sprintf(
Expand All @@ -117,17 +133,19 @@ protected function patchFile(array $prData, OutputInterface $output): void

chdir(OperatingSystem::getCwd());
if (file_put_contents($filename, $patchFileContent) === false) {
throw new \RuntimeException('Could not write patch file');
throw new RuntimeException('Could not write patch file');
}

$output->writeln(sprintf('<info>Patch file created:</info> <comment>%s</comment>', $filename));

return $filename;
}

/**
* @param InputInterface $input
* @return \WpOrg\Requests\Response
*/
protected function getPullRequestInfoByApi(InputInterface $input): \WpOrg\Requests\Response
protected function getPullRequestInfoByApi(InputInterface $input): Response
{
$pullRequestDataResponse = Requests::get(
sprintf(
Expand All @@ -140,4 +158,23 @@ protected function getPullRequestInfoByApi(InputInterface $input): \WpOrg\Reques
);
return $pullRequestDataResponse;
}

/**
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param string $patchFilename
* @return void
*/
protected function applyPatch(OutputInterface $output, string $patchFilename): void
{
$output->writeln('<info>Applying patch...</info>');

$process = new Process(['patch', '-p1']);
$process->setInput(file_get_contents($patchFilename));
$process->setTimeout(3600);
$process->setWorkingDirectory(OperatingSystem::getCwd());
$process->start();
$process->wait(function ($type, $buffer) use ($output) {
$output->write('<info>patch > </info><comment>' . $buffer . '</comment>', false);
});
}
}
14 changes: 12 additions & 2 deletions tests/N98/Magento/Command/Github/PatchFileContent/CreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@

class CreatorTest extends TestCase
{
public function testCreate()
public function testCreateWithVendorMagento()
{
$diffContent = 'app/code/Magento/SampleModule/';
$expectedResult = 'vendor/magento/module-sample-module/';

$result = Creator::create($diffContent);
$result = Creator::create($diffContent, 'magento');

$this->assertEquals($expectedResult, $result);
}

public function testCreateWithVendorMageOS()
{
$diffContent = 'app/code/Magento/SampleModule/';
$expectedResult = 'vendor/mage-os/module-sample-module/';

$result = Creator::create($diffContent, 'mage-os');

$this->assertEquals($expectedResult, $result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function testProcess()

$processor = new AppCodeProcessor();

$this->assertEquals($expectedResult, $processor->process($diffContent));
$this->assertEquals($expectedResult, $processor->process($diffContent, 'magento'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testProcess()

$this->assertSame(
$expectedResult,
$processor->process($diffContent)
$processor->process($diffContent, 'magento'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function testProcess()

$processor = new I18nProcessor();

$this->assertEquals($expectedResult, $processor->process($diffContent));
$this->assertEquals($expectedResult, $processor->process($diffContent, 'magento'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function testProcess()

$processor = new I18nProcessor();

$this->assertEquals($expectedResult, $processor->process($diffContent));
$this->assertEquals($expectedResult, $processor->process($diffContent, 'magento'));
}
}