Skip to content

Commit 68ffba1

Browse files
authored
MCLOUD-6375: MC-34668: Introduce environment variable to store a list of quality p… (magento#750)
1 parent a603e4c commit 68ffba1

File tree

11 files changed

+369
-38
lines changed

11 files changed

+369
-38
lines changed

config/schema.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,20 @@ variables:
643643
- stage:
644644
global:
645645
X_FRAME_CONFIGURATION: SAMEORIGIN
646+
QUALITY_PATCHES:
647+
description: Specify a list of Magento quality patches that will be applied during deployment.
648+
type: array
649+
stages:
650+
- build
651+
default:
652+
build: []
653+
examples:
654+
- stage:
655+
build:
656+
QUALITY_PATCHES:
657+
- MC-31387
658+
- MDVA-4567
659+
- MC-45634
646660

647661
# Environment variables
648662
ENV_RELATIONSHIPS:

scenario/deploy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<arguments>
66
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
77
<argument name="steps" xsi:type="array">
8+
<item name="restore-patch-log" xsi:type="object" priority="25">Magento\MagentoCloud\Step\Deploy\PreDeploy\RestorePatchLog</item>
89
<item name="check-state" xsi:type="object" priority="50">Magento\MagentoCloud\Step\Deploy\PreDeploy\CheckState</item>
910
<item name="cache" xsi:type="object" priority="100">Magento\MagentoCloud\Step\Deploy\PreDeploy\ConfigUpdate\Cache</item>
1011
<item name="clean-static-content" xsi:type="object" priority="200">Magento\MagentoCloud\Step\Deploy\PreDeploy\CleanStaticContent</item>

src/Config/Stage/BuildInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ interface BuildInterface extends StageConfigInterface
2222
public const VAR_ERROR_REPORT_DIR_NESTING_LEVEL = 'ERROR_REPORT_DIR_NESTING_LEVEL';
2323

2424
/**
25-
* Perfom Baler JS bundling
25+
* Perform Baler JS bundling
2626
*/
2727
public const VAR_SCD_USE_BALER = 'SCD_USE_BALER';
28+
29+
/**
30+
* Magento quality patches list
31+
*/
32+
public const VAR_QUALITY_PATCHES = 'QUALITY_PATCHES';
2833
}

src/Filesystem/FileList.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ public function getErrorSchema(): string
180180
return $this->directoryList->getRoot() . '/config/schema.error.yaml';
181181
}
182182

183+
/**
184+
* @return string
185+
* @throws UndefinedPackageException
186+
*/
187+
public function getPatchLog(): string
188+
{
189+
return $this->directoryList->getLog() . '/patch.log';
190+
}
191+
192+
/**
193+
* @return string
194+
* @throws UndefinedPackageException
195+
*/
196+
public function getInitPatchLog(): string
197+
{
198+
return $this->directoryList->getInit() . '/var/log/patch.log';
199+
}
200+
183201
/**
184202
* Returns path to doc file generated from schema.error.yaml
185203
*

src/Patch/Manager.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,26 @@ public function __construct(
5151
/**
5252
* Applies all needed patches.
5353
*
54+
* @param array $qualityPatches
5455
* @throws ShellException
5556
* @throws ConfigException
5657
*/
57-
public function apply(): void
58+
public function apply(array $qualityPatches = []): void
5859
{
5960
$this->logger->notice('Applying patches');
6061

6162
$command = 'php ./vendor/bin/ece-patches apply';
6263

64+
if ($qualityPatches) {
65+
$command .= sprintf(" '%s'", implode("' '", $qualityPatches));
66+
}
67+
6368
if ($this->globalSection->get(GlobalSection::VAR_DEPLOYED_MAGENTO_VERSION_FROM_GIT)) {
6469
$command .= ' --git-installation 1';
6570
}
6671

72+
$command .= ' --no-interaction';
73+
6774
try {
6875
$this->shell->execute($command);
6976
} catch (ShellException $exception) {

src/Step/Build/ApplyPatches.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\MagentoCloud\App\Error;
1111
use Magento\MagentoCloud\Config\ConfigException;
12+
use Magento\MagentoCloud\Config\Stage\BuildInterface;
1213
use Magento\MagentoCloud\Patch\Manager;
1314
use Magento\MagentoCloud\Shell\ShellException;
1415
use Magento\MagentoCloud\Step\StepException;
@@ -24,12 +25,21 @@ class ApplyPatches implements StepInterface
2425
*/
2526
private $manager;
2627

28+
/**
29+
* @var BuildInterface
30+
*/
31+
private $stageConfig;
32+
2733
/**
2834
* @param Manager $manager
35+
* @param BuildInterface $stageConfig
2936
*/
30-
public function __construct(Manager $manager)
31-
{
37+
public function __construct(
38+
Manager $manager,
39+
BuildInterface $stageConfig
40+
) {
3241
$this->manager = $manager;
42+
$this->stageConfig = $stageConfig;
3343
}
3444

3545
/**
@@ -38,7 +48,8 @@ public function __construct(Manager $manager)
3848
public function execute(): void
3949
{
4050
try {
41-
$this->manager->apply();
51+
$qualityPatches = $this->stageConfig->get(BuildInterface::VAR_QUALITY_PATCHES);
52+
$this->manager->apply($qualityPatches);
4253
} catch (ConfigException $e) {
4354
throw new StepException($e->getMessage(), $e->getCode(), $e);
4455
} catch (ShellException $e) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Step\Deploy\PreDeploy;
9+
10+
use Magento\MagentoCloud\Filesystem\DirectoryList;
11+
use Magento\MagentoCloud\Filesystem\Driver\File;
12+
use Magento\MagentoCloud\Filesystem\FileList;
13+
use Magento\MagentoCloud\Filesystem\FileSystemException;
14+
use Magento\MagentoCloud\Package\UndefinedPackageException;
15+
use Magento\MagentoCloud\Step\StepException;
16+
use Magento\MagentoCloud\Step\StepInterface;
17+
use Psr\Log\LoggerInterface;
18+
19+
/**
20+
* Restores patch log file.
21+
*/
22+
class RestorePatchLog implements StepInterface
23+
{
24+
/**
25+
* @var DirectoryList
26+
*/
27+
private $directoryList;
28+
29+
/**
30+
* @var LoggerInterface
31+
*/
32+
private $logger;
33+
34+
/**
35+
* @var File
36+
*/
37+
private $file;
38+
39+
/**
40+
* @var FileList
41+
*/
42+
private $fileList;
43+
44+
/**
45+
* @param LoggerInterface $logger
46+
* @param DirectoryList $directoryList
47+
* @param File $file
48+
* @param FileList $fileList
49+
*/
50+
public function __construct(
51+
LoggerInterface $logger,
52+
DirectoryList $directoryList,
53+
File $file,
54+
FileList $fileList
55+
) {
56+
$this->logger = $logger;
57+
$this->directoryList = $directoryList;
58+
$this->file = $file;
59+
$this->fileList = $fileList;
60+
}
61+
62+
/**
63+
* Restores patch log file from build phase if needed.
64+
*
65+
* @return void
66+
* @throws StepException
67+
*/
68+
public function execute()
69+
{
70+
try {
71+
$buildPhaseLogPath = $this->fileList->getInitPatchLog();
72+
if ($this->file->isExists($buildPhaseLogPath)) {
73+
$this->logger->info('Restoring patch log file');
74+
$this->file->createDirectory($this->directoryList->getLog());
75+
$buildPhaseLogContent = $this->file->fileGetContents($buildPhaseLogPath);
76+
$this->file->filePutContents(
77+
$this->fileList->getPatchLog(),
78+
$buildPhaseLogContent,
79+
FILE_APPEND
80+
);
81+
}
82+
} catch (FileSystemException | UndefinedPackageException $exception) {
83+
throw new StepException($exception->getMessage(), $exception->getCode(), $exception);
84+
}
85+
}
86+
}

src/Test/Unit/Config/SchemaTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function testGetDefaultsForBuild(): void
7777
BuildInterface::VAR_SCD_MAX_EXEC_TIME => null,
7878
BuildInterface::VAR_ERROR_REPORT_DIR_NESTING_LEVEL => 1,
7979
BuildInterface::VAR_SCD_USE_BALER => false,
80+
BuildInterface::VAR_QUALITY_PATCHES => []
8081
],
8182
$this->schema->getDefaults(StageConfigInterface::STAGE_BUILD)
8283
);

src/Test/Unit/Patch/ManagerTest.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,65 @@ protected function setUp()
5858
}
5959

6060
/**
61-
* @throws ShellException
61+
* Tests patch applying.
62+
*
63+
* @param bool $deploymentFromGit
64+
* @param string[] $qualityPatches
65+
* @param string $expectedCommand
66+
* @dataProvider applyDataProvider
6267
*/
63-
public function testApply(): void
64-
{
68+
public function testApply(
69+
bool $deploymentFromGit,
70+
array $qualityPatches,
71+
string $expectedCommand
72+
): void {
6573
$this->globalSectionMock->expects($this->once())
6674
->method('get')
6775
->with(GlobalSection::VAR_DEPLOYED_MAGENTO_VERSION_FROM_GIT)
68-
->willReturn(false);
76+
->willReturn($deploymentFromGit);
6977

7078
$processMock = $this->getMockForAbstractClass(ProcessInterface::class);
7179
$this->shellMock->expects($this->once())
7280
->method('execute')
73-
->with('php ./vendor/bin/ece-patches apply')
81+
->with($expectedCommand)
7482
->willReturn($processMock);
7583
$this->loggerMock->method('notice')
7684
->withConsecutive(
7785
['Applying patches'],
7886
['End of applying patches']
7987
);
8088

81-
$this->manager->apply();
89+
$this->manager->apply($qualityPatches);
90+
}
91+
92+
/**
93+
* @return array[]
94+
*/
95+
public function applyDataProvider(): array
96+
{
97+
return [
98+
[
99+
'deploymentFromGit' => false,
100+
'qualityPatches' => [],
101+
'expectedCommand' => 'php ./vendor/bin/ece-patches apply --no-interaction'
102+
],
103+
[
104+
'deploymentFromGit' => true,
105+
'qualityPatches' => [],
106+
'expectedCommand' => 'php ./vendor/bin/ece-patches apply --git-installation 1 --no-interaction'
107+
],
108+
[
109+
'deploymentFromGit' => true,
110+
'qualityPatches' => ['MC-11111', 'MC-22222'],
111+
'expectedCommand' =>
112+
'php ./vendor/bin/ece-patches apply \'MC-11111\' \'MC-22222\' --git-installation 1 --no-interaction'
113+
],
114+
[
115+
'deploymentFromGit' => false,
116+
'qualityPatches' => ['MC-32365'],
117+
'expectedCommand' => 'php ./vendor/bin/ece-patches apply \'MC-32365\' --no-interaction'
118+
],
119+
];
82120
}
83121

84122
/**
@@ -96,7 +134,7 @@ public function testApplyWithException(): void
96134

97135
$this->shellMock->expects($this->once())
98136
->method('execute')
99-
->with('php ./vendor/bin/ece-patches apply')
137+
->with('php ./vendor/bin/ece-patches apply --no-interaction')
100138
->willThrowException(new ShellException('Some error'));
101139
$this->loggerMock->method('notice')
102140
->withConsecutive(
@@ -108,28 +146,4 @@ public function testApplyWithException(): void
108146

109147
$this->manager->apply();
110148
}
111-
112-
/**
113-
* @throws ShellException
114-
*/
115-
public function testApplyDeployedFromGitAndNoCopy(): void
116-
{
117-
$this->globalSectionMock->expects($this->once())
118-
->method('get')
119-
->with(GlobalSection::VAR_DEPLOYED_MAGENTO_VERSION_FROM_GIT)
120-
->willReturn(true);
121-
122-
$processMock = $this->getMockForAbstractClass(ProcessInterface::class);
123-
$this->shellMock->expects($this->once())
124-
->method('execute')
125-
->with('php ./vendor/bin/ece-patches apply --git-installation 1')
126-
->willReturn($processMock);
127-
$this->loggerMock->method('notice')
128-
->withConsecutive(
129-
['Applying patches'],
130-
['End of applying patches']
131-
);
132-
133-
$this->manager->apply();
134-
}
135149
}

0 commit comments

Comments
 (0)