Skip to content

Commit 9008641

Browse files
committed
MC-38191: Implement check for patch availability in MQP
1 parent e5e70dd commit 9008641

13 files changed

+567
-61
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\PatchChecker\Patch\MQP\Data;
9+
10+
/**
11+
* Aggregated patch data class.
12+
*/
13+
class AggregatedPatch
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private $config;
19+
/**
20+
* @var array
21+
*/
22+
private $patches;
23+
24+
/**
25+
* @param array $config
26+
*/
27+
public function __construct(array $config)
28+
{
29+
$this->config = $config;
30+
}
31+
32+
/**
33+
* Get patch items
34+
*
35+
* @return Patch[]
36+
*/
37+
public function getPatches(): array
38+
{
39+
if ($this->patches === null) {
40+
$this->patches = [];
41+
foreach ($this->config as $packageName => $packageConfiguration) {
42+
foreach ($packageConfiguration as $patchTitle => $patchInfo) {
43+
foreach ($patchInfo as $packageConstraint => $patchData) {
44+
$this->patches[] = new Patch($packageName, $packageConstraint, $patchData['file']);
45+
}
46+
}
47+
}
48+
}
49+
50+
return $this->patches;
51+
}
52+
}

app/code/Patch/MQP/Data/Patch.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\PatchChecker\Patch\MQP\Data;
9+
10+
/**
11+
* Patch data
12+
*/
13+
class Patch
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $packageName;
19+
/**
20+
* @var string
21+
*/
22+
private $packageConstraint;
23+
/**
24+
* @var string
25+
*/
26+
private $filename;
27+
28+
/**
29+
* @param string $packageName
30+
* @param string $packageConstraint
31+
* @param string $filename
32+
*/
33+
public function __construct(
34+
string $packageName,
35+
string $packageConstraint,
36+
string $filename
37+
) {
38+
$this->packageName = $packageName;
39+
$this->packageConstraint = $packageConstraint;
40+
$this->filename = $filename;
41+
}
42+
43+
/**
44+
* Get package name
45+
*
46+
* @return string
47+
*/
48+
public function getPackageName(): string
49+
{
50+
return $this->packageName;
51+
}
52+
53+
/**
54+
* Get package constraints
55+
*
56+
* @return string
57+
*/
58+
public function getPackageConstraint(): string
59+
{
60+
return $this->packageConstraint;
61+
}
62+
63+
/**
64+
* Get patch filename
65+
*
66+
* @return string
67+
*/
68+
public function getFilename(): string
69+
{
70+
return $this->filename;
71+
}
72+
}

app/code/Patch/MQP/PatchChecker.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\PatchChecker\Patch\MQP;
9+
10+
use Composer\Semver\Semver;
11+
use Magento\PatchChecker\Deploy\Instance;
12+
use Magento\PatchChecker\Deploy\InstanceManager;
13+
use Magento\PatchChecker\Patch\Check\StrategyManager;
14+
use Magento\PatchChecker\Patch\Checker;
15+
use Magento\QualityPatches\Info;
16+
17+
/**
18+
* MQP patch versions checker
19+
*/
20+
class PatchChecker
21+
{
22+
/**
23+
* @var InstanceManager
24+
*/
25+
private $instanceManager;
26+
/**
27+
* @var StrategyManager
28+
*/
29+
private $strategyManager;
30+
/**
31+
* @var VersionsManager
32+
*/
33+
private $versionsManager;
34+
35+
public function __construct(
36+
InstanceManager $instanceManager,
37+
StrategyManager $strategyManager,
38+
VersionsManager $versionsManager
39+
) {
40+
$this->instanceManager = $instanceManager;
41+
$this->strategyManager = $strategyManager;
42+
$this->versionsManager = $versionsManager;
43+
}
44+
45+
public function checkPatchForAllReleases(string $patchId)
46+
{
47+
$patchRepository = new PatchRepository(new Info());
48+
$aggregatedPatch = $patchRepository->findOne($patchId);
49+
50+
$result = [];
51+
foreach ($this->instanceManager->getInstanceList() as $groupName => $groupInstanceList) {
52+
foreach ($groupInstanceList as $instance) {
53+
if (is_int($instance)) {
54+
for ($i = 0; $i < $instance; $i++) {
55+
$result[$groupName][] = ['instance_name' => 'n/a', 'check_strategy' => 'n/a'];
56+
}
57+
continue;
58+
}
59+
if ($instance->getInstanceType() == Instance::INSTANCE_TYPE_INVALID) {
60+
$result[$groupName][] = ['instance_name' => $instance->getInstanceName(), 'check_strategy' => 'n/a'];
61+
continue;
62+
}
63+
64+
$checkResult = [];
65+
$status = Checker::PATCH_APPLY_RESULT_FAILED;
66+
$instanceVersion = $instance->getInstanceName();
67+
foreach ($aggregatedPatch->getPatches() as $patch) {
68+
$packageVersion = $this->versionsManager->getPackageVersion(
69+
$instanceVersion,
70+
$patch->getPackageName()
71+
);
72+
if ($packageVersion && Semver::satisfies($packageVersion, $patch->getPackageConstraint())) {
73+
$status = Checker::PATCH_APPLY_RESULT_SUCCESSFUL;
74+
break;
75+
}
76+
}
77+
foreach ($this->strategyManager->getStrategyList() as $strategy) {
78+
$checkResult[$strategy->getStrategyName()] = $status;
79+
}
80+
81+
$result[$groupName][] = [
82+
'instance_name' => $instance->getInstanceName(),
83+
'check_strategy' => $checkResult
84+
];
85+
}
86+
}
87+
88+
return $result;
89+
}
90+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\PatchChecker\Patch\MQP;
9+
10+
use Magento\PatchChecker\Util;
11+
use Magento\PatchChecker\Patch\MQP\Data\AggregatedPatch;
12+
use Magento\QualityPatches\Info;
13+
14+
/**
15+
* MQP patches repository
16+
*/
17+
class PatchRepository
18+
{
19+
/**
20+
* @var Info
21+
*/
22+
private $info;
23+
24+
/**
25+
* @param Info $info
26+
*/
27+
public function __construct(Info $info)
28+
{
29+
$this->info = $info;
30+
}
31+
32+
33+
/**
34+
* Find patch by ID
35+
*
36+
* @param string $id
37+
* @return AggregatedPatch
38+
* @throws \Exception
39+
*/
40+
public function findOne(string $id): AggregatedPatch
41+
{
42+
$config = $this->getConfiguration();
43+
if (isset($config[$id])) {
44+
return new AggregatedPatch($config[$id]);
45+
}
46+
throw new \Exception("Patch '$id' cannot be found.");
47+
}
48+
49+
/**
50+
* Get patches configuration
51+
*
52+
* @return array
53+
* @throws \Exception
54+
*/
55+
private function getConfiguration(): array
56+
{
57+
$result = [];
58+
$configPath = $this->info->getPatchesConfig();
59+
if (file_exists($configPath)) {
60+
$result = Util::getJsonFile($configPath);
61+
}
62+
63+
return $result;
64+
}
65+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\PatchChecker\Patch\MQP;
9+
10+
use Magento\PatchChecker\Util;
11+
12+
/**
13+
* Versions manager class
14+
*/
15+
class VersionsManager
16+
{
17+
/**
18+
* @var array
19+
*/
20+
private $configuration;
21+
22+
/**
23+
* @param string $coreVersion
24+
* @param string $packageName
25+
* @return string|null
26+
*/
27+
public function getPackageVersion(string $coreVersion, string $packageName)
28+
{
29+
return $this->getConfiguration()[$coreVersion][$packageName] ?? null;
30+
}
31+
32+
/**
33+
* Get versions configuration
34+
*
35+
* @return array
36+
*/
37+
private function getConfiguration(): array
38+
{
39+
if ($this->configuration === null) {
40+
$this->configuration = [];
41+
$configPath = $this->getConfigurationPath();
42+
if (file_exists($configPath)) {
43+
$this->configuration = Util::getJsonFile($configPath);
44+
}
45+
}
46+
47+
return $this->configuration;
48+
}
49+
50+
/**
51+
* Get versions configuration path
52+
*
53+
* @return string
54+
*/
55+
private function getConfigurationPath(): string
56+
{
57+
return BP . 'app/config/magento_package_versions.json';
58+
}
59+
}

app/code/Util.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\PatchChecker;
9+
10+
/**
11+
* Util class
12+
*/
13+
class Util
14+
{
15+
/**
16+
* @param string $path
17+
* @return array
18+
* @throws \Exception
19+
*/
20+
public static function getJsonFile(string $path): array
21+
{
22+
$content = file_get_contents($path);
23+
$result = json_decode($content, true);
24+
if (json_last_error() !== JSON_ERROR_NONE) {
25+
throw new \Exception(
26+
"Unable to unserialize json file '{$path}'. Error: " . json_last_error_msg()
27+
);
28+
}
29+
return $result;
30+
}
31+
}

0 commit comments

Comments
 (0)