Skip to content

MC-38191: Implement check for patch availability in MQP #1

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

Open
wants to merge 7 commits into
base: master
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
11 changes: 11 additions & 0 deletions app/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$vendorAutoloadPath = __DIR__ . '/../vendor/autoload.php';
if (!file_exists($vendorAutoloadPath)) {
throw new RuntimeException('Required file \'autoload.php\' was not found.');
}
require $vendorAutoloadPath;
2 changes: 2 additions & 0 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
define('VAR_DIR_NAME', 'var');
define('UPLOAD_PATH', 'var/uploads/');
define('STATS_PATH', 'var/stats/');

require_once __DIR__ . '/autoload.php';
11 changes: 9 additions & 2 deletions app/code/Cache/File.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

class Cache_File
namespace Magento\PatchChecker\Cache;

class File
{
const CACHE_DIR_NAME = 'cache';

Expand All @@ -24,7 +31,7 @@ public function __construct()
} elseif (!is_dir($cachePath) || !is_writable($cachePath)) {
$cachePath = '/tmp';
if (!$this->validateWritablePath($cachePath)) {
throw new Exception('Cache dir is not writable.');
throw new \Exception('Cache dir is not writable.');
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion app/code/Deploy/Instance.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

class Deploy_Instance
namespace Magento\PatchChecker\Deploy;

class Instance
{
const INSTANCE_TYPE_GIT = 'git';
const INSTANCE_TYPE_COMPOSER = 'composer';
Expand Down
11 changes: 8 additions & 3 deletions app/code/Deploy/InstanceManager.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

require_once 'app/code/Deploy/Instance.php';
namespace Magento\PatchChecker\Deploy;

class Deploy_InstanceManager
class InstanceManager
{
private $instanceList = [];

Expand All @@ -24,7 +29,7 @@ private function generateInstanceList()
foreach ($groupInstanceList as $instanceName => $instancePath) {
$this->instanceList[$groupName][] = is_int($instancePath)
? $instancePath
: new Deploy_Instance($instanceName, $instancePath);
: new Instance($instanceName, $instancePath);
}
}

Expand Down
11 changes: 9 additions & 2 deletions app/code/Design.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

require_once 'app/code/Cache/File.php';
namespace Magento\PatchChecker;

use Magento\PatchChecker\Cache\File;

class Design
{
Expand Down Expand Up @@ -32,7 +39,7 @@ public function generateStaticHash()

public function getStaticHash()
{
$cache = new Cache_File();
$cache = new File();
$content = $cache->loadCache(self::STATIC_HASH_CACHE_ID);
if (!$content) {
$content = $this->generateStaticHash();
Expand Down
7 changes: 7 additions & 0 deletions app/code/File/Filesystem.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PatchChecker\File;

class Filesystem
{
Expand Down
14 changes: 11 additions & 3 deletions app/code/File/Uploader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

class File_Uploader
namespace Magento\PatchChecker\File;

use Magento\PatchChecker\Patch\Converter;

class Uploader
{
protected $_maximumFileSize = 15728640; // 15Mb

Expand Down Expand Up @@ -94,8 +103,7 @@ public function upload()
$newFileName[$fileId] = $this->getHashedFileName($name) . '.patch';
$patchPath = $this->_uploadPath . $newFileName[$fileId];
if (move_uploaded_file($_FILES[$fileElementName]['tmp_name'][$fileId], $patchPath)) {
require_once 'app/code/Patch/Converter.php';
$converter = new Patch_Converter();
$converter = new Converter();
$converter->preparePatch($patchPath);
} else {
$error[$fileId][] = 'Unable to move uploaded file from tmp folder.';
Expand Down
73 changes: 73 additions & 0 deletions app/code/Patch/AbstractChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PatchChecker\Patch;

use Magento\PatchChecker\Deploy\Instance;
use Magento\PatchChecker\Deploy\InstanceManager;

/**
* Abstract patch checker
*/
abstract class AbstractChecker
{
const PATCH_APPLY_RESULT_FAILED = 0;
const PATCH_APPLY_RESULT_SUCCESSFUL = 1;
const PATCH_APPLY_RESULT_MERGED = 2;

/**
* @var InstanceManager
*/
private $instanceManager;

/**
* @param InstanceManager $instanceManager
*/
public function __construct(
InstanceManager $instanceManager
) {
$this->instanceManager = $instanceManager;
}

/**
* Return patch status for each configured version.
*
* @param string $patch
* @return array
*/
public function check(string $patch)
{
$result = [];
foreach ($this->instanceManager->getInstanceList() as $groupName => $groupInstanceList) {
foreach ($groupInstanceList as $instance) {
if (is_int($instance)) {
for ($i = 0; $i < $instance; $i++) {
$result[$groupName][] = ['instance_name' => 'n/a', 'result' => 'n/a'];
}
} elseif ($instance->getInstanceType() == Instance::INSTANCE_TYPE_INVALID) {
$result[$groupName][] = ['instance_name' => $instance->getInstanceName(), 'result' => 'n/a'];
} else {
$result[$groupName][] = [
'instance_name' => $instance->getInstanceName(),
'result' => $this->getResult($instance, $patch)
];
}
}
}

return $result;
}

/**
* Get status of the patch
*
* @param Instance $instance
* @param string $patch
* @return array|string
*/
abstract public function getResult(Instance $instance, string $patch);
}
17 changes: 14 additions & 3 deletions app/code/Patch/Check/Strategy/AbstractStrategy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PatchChecker\Patch\Check\Strategy;

use Magento\PatchChecker\Patch\Checker;

abstract class AbstractStrategy implements StrategyInterface
{
Expand Down Expand Up @@ -47,17 +56,19 @@ protected function executeCommand($command)
public function check($patchPath, $instancePath)
{
if (!$patchPath) {
return false;
return Checker::PATCH_APPLY_RESULT_FAILED;
}

$result = $this->executeCommand($this->getCommand($patchPath, $instancePath));
if (!$result) {
return Patch_Checker::PATCH_APPLY_RESULT_SUCCESSFUL;
return Checker::PATCH_APPLY_RESULT_SUCCESSFUL;
}

$result = $this->executeCommand($this->getCommand($patchPath, $instancePath, true));
if (!$result) {
return Patch_Checker::PATCH_APPLY_RESULT_MERGED;
return Checker::PATCH_APPLY_RESULT_MERGED;
}

return Checker::PATCH_APPLY_RESULT_FAILED;
}
}
7 changes: 7 additions & 0 deletions app/code/Patch/Check/Strategy/GitApplyStrategy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PatchChecker\Patch\Check\Strategy;

class GitApplyStrategy extends AbstractStrategy
{
Expand Down
7 changes: 7 additions & 0 deletions app/code/Patch/Check/Strategy/PatchStrategy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PatchChecker\Patch\Check\Strategy;

class PatchStrategy extends AbstractStrategy
{
Expand Down
7 changes: 7 additions & 0 deletions app/code/Patch/Check/Strategy/StrategyInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PatchChecker\Patch\Check\Strategy;

interface StrategyInterface
{
Expand Down
14 changes: 10 additions & 4 deletions app/code/Patch/Check/StrategyManager.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

require_once 'app/code/Patch/Check/Strategy/StrategyInterface.php';
require_once 'app/code/Patch/Check/Strategy/AbstractStrategy.php';
require_once 'app/code/Patch/Check/Strategy/PatchStrategy.php';
require_once 'app/code/Patch/Check/Strategy/GitApplyStrategy.php';
namespace Magento\PatchChecker\Patch\Check;

use Magento\PatchChecker\Patch\Check\Strategy\GitApplyStrategy;
use Magento\PatchChecker\Patch\Check\Strategy\PatchStrategy;
use Magento\PatchChecker\Patch\Check\Strategy\StrategyInterface;

class StrategyManager
{
Expand Down
Loading