2011-10-11 13:02:57 +00:00
|
|
|
<?php
|
2022-09-08 08:54:04 +00:00
|
|
|
|
2011-10-11 13:02:57 +00:00
|
|
|
/**
|
2014-05-03 23:48:08 +00:00
|
|
|
* Patch testing component for the Joomla! CMS
|
2013-07-13 02:26:21 +00:00
|
|
|
*
|
2018-09-01 14:32:23 +00:00
|
|
|
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved.
|
2013-07-13 02:26:21 +00:00
|
|
|
* @license GNU General Public License version 2 or later
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
|
2023-08-17 20:01:47 +00:00
|
|
|
namespace Joomla\Component\Patchtester\Administrator\Model;
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2019-09-10 11:53:40 +00:00
|
|
|
use Joomla\Archive\Zip;
|
2019-09-08 16:02:14 +00:00
|
|
|
use Joomla\CMS\Component\ComponentHelper;
|
2017-08-17 23:22:01 +00:00
|
|
|
use Joomla\CMS\Factory;
|
2019-08-28 01:20:49 +00:00
|
|
|
use Joomla\CMS\Filesystem\Path;
|
2020-03-21 09:58:04 +00:00
|
|
|
use Joomla\CMS\Http\HttpFactory;
|
2019-08-28 01:20:49 +00:00
|
|
|
use Joomla\CMS\Language\Text;
|
2023-08-20 13:54:48 +00:00
|
|
|
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
|
|
|
|
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
|
2017-08-17 23:22:01 +00:00
|
|
|
use Joomla\CMS\Version;
|
2023-08-20 13:54:48 +00:00
|
|
|
use Joomla\Component\Patchtester\Administrator\Github\Exception\UnexpectedResponse;
|
|
|
|
use Joomla\Component\Patchtester\Administrator\GitHub\GitHub;
|
|
|
|
use Joomla\Component\Patchtester\Administrator\GithubCredentialsTrait;
|
2023-08-17 20:01:47 +00:00
|
|
|
use Joomla\Component\Patchtester\Administrator\Helper\Helper;
|
2019-08-28 01:20:49 +00:00
|
|
|
use Joomla\Filesystem\File;
|
2019-09-10 11:53:40 +00:00
|
|
|
use Joomla\Filesystem\Folder;
|
2020-03-21 09:58:04 +00:00
|
|
|
use Joomla\Registry\Registry;
|
2020-03-22 09:37:29 +00:00
|
|
|
use RuntimeException;
|
|
|
|
use stdClass;
|
2014-05-03 23:48:08 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
// phpcs:disable PSR1.Files.SideEffects
|
|
|
|
\defined('_JEXEC') or die;
|
|
|
|
// phpcs:enable PSR1.Files.SideEffects
|
|
|
|
|
2011-10-11 13:02:57 +00:00
|
|
|
/**
|
|
|
|
* Methods supporting pull requests.
|
|
|
|
*
|
2014-05-03 23:48:08 +00:00
|
|
|
* @since 2.0
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
2023-08-20 13:54:48 +00:00
|
|
|
class PullModel extends BaseDatabaseModel
|
2011-10-11 13:02:57 +00:00
|
|
|
{
|
2023-08-20 13:54:48 +00:00
|
|
|
use GithubCredentialsTrait;
|
2023-08-25 20:44:21 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
/**
|
|
|
|
* Array containing top level non-production folders
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
protected $nonProductionFolders
|
2023-08-20 13:54:48 +00:00
|
|
|
= [
|
2022-09-08 08:54:04 +00:00
|
|
|
'build',
|
|
|
|
'docs',
|
|
|
|
'installation',
|
|
|
|
'tests',
|
|
|
|
'.github',
|
2023-08-20 13:54:48 +00:00
|
|
|
];
|
2023-08-25 20:44:21 +00:00
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
/**
|
2022-09-08 08:54:04 +00:00
|
|
|
* Array containing non-production files
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
protected $nonProductionFiles
|
2023-08-20 13:54:48 +00:00
|
|
|
= [
|
2022-09-08 08:54:04 +00:00
|
|
|
'.drone.yml',
|
|
|
|
'.gitignore',
|
|
|
|
'.php_cs',
|
|
|
|
'.travis.yml',
|
|
|
|
'README.md',
|
|
|
|
'build.xml',
|
|
|
|
'composer.json',
|
|
|
|
'composer.lock',
|
|
|
|
'phpunit.xml.dist',
|
|
|
|
'robots.txt.dist',
|
|
|
|
'travisci-phpunit.xml',
|
|
|
|
'LICENSE',
|
|
|
|
'RoboFile.dist.ini',
|
|
|
|
'RoboFile.php',
|
|
|
|
'codeception.yml',
|
|
|
|
'jorobo.dist.ini',
|
|
|
|
'manifest.xml',
|
|
|
|
'crowdin.yaml',
|
2023-08-20 13:54:48 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2022-09-08 08:54:04 +00:00
|
|
|
* The namespace mapper
|
|
|
|
*
|
|
|
|
* @var \JNamespacePsr4Map
|
|
|
|
* @since 4.0.0
|
|
|
|
*/
|
|
|
|
protected $namespaceMapper;
|
2023-08-20 13:54:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
2022-09-08 08:54:04 +00:00
|
|
|
*/
|
2023-08-20 13:54:48 +00:00
|
|
|
public function __construct(
|
|
|
|
$config = [],
|
|
|
|
MVCFactoryInterface $factory = null
|
|
|
|
) {
|
|
|
|
parent::__construct($config, $factory);
|
|
|
|
|
|
|
|
$this->state = $this->getCredentials();
|
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
$this->namespaceMapper = new \JNamespacePsr4Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
|
|
|
* However uses different methods for different repositories.
|
|
|
|
*
|
2023-08-20 13:54:48 +00:00
|
|
|
* @param int $id ID of the pull request to apply
|
2022-09-08 08:54:04 +00:00
|
|
|
*
|
2023-08-20 13:54:48 +00:00
|
|
|
* @return bool
|
2022-09-08 08:54:04 +00:00
|
|
|
*
|
2023-08-20 13:54:48 +00:00
|
|
|
* @throws RuntimeException
|
2022-09-08 08:54:04 +00:00
|
|
|
* @since 3.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function apply(int $id): bool
|
|
|
|
{
|
|
|
|
$params = ComponentHelper::getParams('com_patchtester');
|
2023-08-03 18:49:54 +00:00
|
|
|
// Decide based on repository settings whether patch will be applied through Github or CIServer
|
2023-08-20 13:54:48 +00:00
|
|
|
if ((bool)$params->get('ci_switch', 0)) {
|
2022-09-08 08:54:04 +00:00
|
|
|
return $this->applyWithCIServer($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->applyWithGitHub($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to apply
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2023-08-20 13:54:48 +00:00
|
|
|
* @throws RuntimeException
|
2022-09-08 08:54:04 +00:00
|
|
|
* @since 3.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function applyWithCIServer(int $id): bool
|
|
|
|
{
|
|
|
|
// Get the CIServer Registry
|
|
|
|
$ciSettings = Helper::initializeCISettings();
|
2023-08-21 18:37:40 +00:00
|
|
|
// Get the Github object
|
2022-09-08 08:54:04 +00:00
|
|
|
$github = Helper::initializeGithub();
|
2023-08-21 18:37:40 +00:00
|
|
|
// Retrieve pullData for sha later on.
|
2022-09-08 08:54:04 +00:00
|
|
|
$pull = $this->retrieveGitHubData($github, $id);
|
|
|
|
if ($pull->head->repo === null) {
|
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_REPO_IS_GONE'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$sha = $pull->head->sha;
|
2023-08-21 18:37:40 +00:00
|
|
|
// Create tmp folder if it does not exist
|
2022-09-08 08:54:04 +00:00
|
|
|
if (!file_exists($ciSettings->get('folder.temp'))) {
|
|
|
|
Folder::create($ciSettings->get('folder.temp'));
|
|
|
|
}
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
$tempPath = $ciSettings->get('folder.temp') . '/' . $id;
|
|
|
|
$backupsPath = $ciSettings->get('folder.backups') . '/' . $id;
|
|
|
|
$delLogPath = $tempPath . '/' . $ciSettings->get('zip.log.name');
|
|
|
|
$zipPath = $tempPath . '/' . $ciSettings->get('zip.name');
|
2022-09-08 08:54:04 +00:00
|
|
|
$serverZipPath = sprintf($ciSettings->get('zip.url'), $id);
|
2023-08-21 18:37:40 +00:00
|
|
|
// Patch has already been applied
|
2022-09-08 08:54:04 +00:00
|
|
|
if (file_exists($backupsPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$version = new Version();
|
|
|
|
$httpOption = new Registry();
|
2023-08-20 13:54:48 +00:00
|
|
|
$httpOption->set(
|
|
|
|
'userAgent',
|
|
|
|
$version->getUserAgent('Joomla', true, false)
|
|
|
|
);
|
2023-08-25 20:44:21 +00:00
|
|
|
|
2023-08-21 18:37:40 +00:00
|
|
|
// Try to download the zip file
|
2022-09-08 08:54:04 +00:00
|
|
|
try {
|
|
|
|
$http = HttpFactory::getHttp($httpOption);
|
|
|
|
$result = $http->get($serverZipPath);
|
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
$result = null;
|
|
|
|
}
|
|
|
|
|
2023-08-25 20:48:31 +00:00
|
|
|
if (
|
|
|
|
$result === null
|
2023-08-20 13:54:48 +00:00
|
|
|
|| ($result->getStatusCode() !== 200
|
|
|
|
&& $result->getStatusCode() !== 310)
|
|
|
|
) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
Text::_('COM_PATCHTESTER_SERVER_RESPONDED_NOT_200')
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign to variable to avlod PHP notice "Indirect modification of overloaded property"
|
2023-08-20 13:54:48 +00:00
|
|
|
$content = (string)$result->getBody();
|
2023-08-21 18:37:40 +00:00
|
|
|
// Write the file to disk
|
2022-09-08 08:54:04 +00:00
|
|
|
File::write($zipPath, $content);
|
2023-08-21 18:37:40 +00:00
|
|
|
// Check if zip folder could have been downloaded
|
2022-09-08 08:54:04 +00:00
|
|
|
if (!file_exists($zipPath)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::_('COM_PATCHTESTER_ZIP_DOES_NOT_EXIST')
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Folder::create($tempPath);
|
|
|
|
$zip = new Zip();
|
|
|
|
if (!$zip->extract($zipPath, $tempPath)) {
|
|
|
|
Folder::delete($tempPath);
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::_('COM_PATCHTESTER_ZIP_EXTRACT_FAILED')
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove zip to avoid get listing afterwards
|
|
|
|
File::delete($zipPath);
|
2023-08-21 18:37:40 +00:00
|
|
|
// Verify the composer autoloader for any invalid entries
|
2022-09-08 08:54:04 +00:00
|
|
|
if ($this->verifyAutoloader($tempPath) === false) {
|
2023-08-21 18:37:40 +00:00
|
|
|
// There is something broken in the autoloader, clean up and go back
|
2022-09-08 08:54:04 +00:00
|
|
|
Folder::delete($tempPath);
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::_('COM_PATCHTESTER_PATCH_BREAKS_SITE')
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get files from deleted_logs
|
|
|
|
$deletedFiles = (file_exists($delLogPath) ? file($delLogPath) : []);
|
|
|
|
$deletedFiles = array_map('trim', $deletedFiles);
|
|
|
|
if (file_exists($delLogPath)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
// Remove deleted_logs to avoid get listing afterwards
|
2022-09-08 08:54:04 +00:00
|
|
|
File::delete($delLogPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve all files and merge them into one array
|
|
|
|
$files = Folder::files($tempPath, null, true, true);
|
|
|
|
$files = str_replace(Path::clean($tempPath . '\\'), '', $files);
|
|
|
|
$files = array_merge($files, $deletedFiles);
|
|
|
|
Folder::create($backupsPath);
|
2023-08-21 18:37:40 +00:00
|
|
|
// Moves existent files to backup and replace them or creates new one if they don't exist
|
2022-09-08 08:54:04 +00:00
|
|
|
foreach ($files as $key => $file) {
|
|
|
|
try {
|
|
|
|
$filePath = explode(DIRECTORY_SEPARATOR, Path::clean($file));
|
|
|
|
array_pop($filePath);
|
|
|
|
$filePath = implode(DIRECTORY_SEPARATOR, $filePath);
|
2023-08-20 13:54:48 +00:00
|
|
|
// Deleted_logs returns files as well as folder, if value is folder, unset and skip
|
2022-09-08 08:54:04 +00:00
|
|
|
if (is_dir(JPATH_ROOT . '/' . $file)) {
|
|
|
|
unset($files[$key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(JPATH_ROOT . '/' . $file)) {
|
2023-08-21 18:37:40 +00:00
|
|
|
// Create directories if they don't exist until file
|
2022-09-08 08:54:04 +00:00
|
|
|
if (!file_exists($backupsPath . '/' . $filePath)) {
|
|
|
|
Folder::create($backupsPath . '/' . $filePath);
|
|
|
|
}
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
File::move(
|
|
|
|
JPATH_ROOT . '/' . $file,
|
|
|
|
$backupsPath . '/' . $file
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($tempPath . '/' . $file)) {
|
2023-08-21 18:37:40 +00:00
|
|
|
// Create directories if they don't exist until file
|
2023-08-25 20:48:31 +00:00
|
|
|
if (
|
|
|
|
!file_exists(JPATH_ROOT . '/' . $filePath)
|
2023-08-20 13:54:48 +00:00
|
|
|
|| !is_dir(
|
|
|
|
JPATH_ROOT . '/' . $filePath
|
|
|
|
)
|
|
|
|
) {
|
2022-09-08 08:54:04 +00:00
|
|
|
Folder::create(JPATH_ROOT . '/' . $filePath);
|
|
|
|
}
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
File::copy(
|
|
|
|
$tempPath . '/' . $file,
|
|
|
|
JPATH_ROOT . '/' . $file
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
} catch (RuntimeException $exception) {
|
|
|
|
Folder::delete($tempPath);
|
|
|
|
Folder::move($backupsPath, $backupsPath . '_failed');
|
2023-08-25 20:44:21 +00:00
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_FAILED_APPLYING_PATCH',
|
|
|
|
$file,
|
|
|
|
$exception->getMessage()
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear temp folder and store applied patch in database
|
|
|
|
Folder::delete($tempPath);
|
|
|
|
$this->saveAppliedPatch($id, $files, $sha);
|
2023-08-21 18:37:40 +00:00
|
|
|
// Update the autoloader file
|
2022-09-08 08:54:04 +00:00
|
|
|
$this->namespaceMapper->create();
|
2023-08-21 18:37:40 +00:00
|
|
|
// Change the media version
|
2022-09-08 08:54:04 +00:00
|
|
|
$version = new Version();
|
|
|
|
$version->refreshMediaVersion();
|
2023-08-20 13:54:48 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
|
|
|
*
|
|
|
|
* @param GitHub $github github object
|
|
|
|
* @param integer $id Id of the pull request
|
|
|
|
*
|
|
|
|
* @return stdClass The pull request data
|
|
|
|
*
|
2023-08-20 13:54:48 +00:00
|
|
|
* @throws RuntimeException
|
2022-09-08 08:54:04 +00:00
|
|
|
* @since 2.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function retrieveGitHubData(GitHub $github, int $id): stdClass
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$rateResponse = $github->getRateLimit();
|
|
|
|
$rate = json_decode($rateResponse->body, false);
|
|
|
|
} catch (UnexpectedResponse $exception) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB',
|
|
|
|
$exception->getMessage()
|
2023-08-25 20:48:31 +00:00
|
|
|
),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
2023-08-20 13:54:48 +00:00
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If over the API limit, we can't build this list
|
2023-08-20 13:54:48 +00:00
|
|
|
if ((int)$rate->resources->core->remaining === 0) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_API_LIMIT_LIST',
|
|
|
|
Factory::getDate($rate->resources->core->reset)
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-08-20 13:54:48 +00:00
|
|
|
$pullResponse = $github->getPullRequest(
|
|
|
|
$this->getState()->get('github_user'),
|
|
|
|
$this->getState()->get('github_repo'),
|
|
|
|
$id
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
$pull = json_decode($pullResponse->body, false);
|
|
|
|
} catch (UnexpectedResponse $exception) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB',
|
|
|
|
$exception->getMessage()
|
2023-08-25 20:48:31 +00:00
|
|
|
),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
2023-08-20 13:54:48 +00:00
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pull;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify if the autoload contains any broken entries.
|
|
|
|
*
|
|
|
|
* @param string $path The path to look for the autoloader
|
|
|
|
*
|
|
|
|
* @return boolean True if there are broken dependencies | False otherwise.
|
|
|
|
*
|
|
|
|
* @since 4.0.0
|
|
|
|
*/
|
|
|
|
private function verifyAutoloader(string $path): bool
|
|
|
|
{
|
|
|
|
$result = false;
|
2023-08-20 13:54:48 +00:00
|
|
|
// Check if we have an autoload file
|
2022-09-08 08:54:04 +00:00
|
|
|
if (!file_exists($path . '/libraries/vendor/autoload.php')) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$composerFiles = [
|
|
|
|
'autoload_static.php',
|
|
|
|
'autoload_files.php',
|
|
|
|
'autoload_classmap.php',
|
|
|
|
'autoload_namespaces.php',
|
|
|
|
'autoload_psr4.php',
|
|
|
|
];
|
|
|
|
$filesToCheck = [];
|
2023-08-20 13:54:48 +00:00
|
|
|
array_walk(
|
|
|
|
$composerFiles,
|
|
|
|
static function ($composerFile) use (&$filesToCheck, $path) {
|
2023-08-25 20:48:31 +00:00
|
|
|
if (file_exists($path . '/libraries/vendor/composer/' . $composerFile) === false) {
|
2022-09-08 08:54:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
if ($composerFile === 'autoload_static.php') {
|
|
|
|
// Get the generated token
|
|
|
|
$autoload = file_get_contents(
|
|
|
|
$path . '/libraries/vendor/autoload.php'
|
|
|
|
);
|
|
|
|
$resultMatch = preg_match(
|
|
|
|
'/ComposerAutoloaderInit(.*)::/',
|
|
|
|
$autoload,
|
|
|
|
$match
|
|
|
|
);
|
2023-08-25 20:48:31 +00:00
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
if (!$resultMatch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the class is already defined
|
2023-08-25 20:48:31 +00:00
|
|
|
$autoloadClass = '\Composer\Autoload\ComposerStaticInit' . $match[1];
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
if (class_exists($autoloadClass)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 08:54:04 +00:00
|
|
|
|
2023-08-25 20:48:31 +00:00
|
|
|
require_once $path . '/libraries/vendor/composer/autoload_static.php';
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
$files = $autoloadClass::$files;
|
|
|
|
$filesToCheck = array_merge($filesToCheck, $files);
|
|
|
|
} else {
|
|
|
|
$files = require $path
|
|
|
|
. '/libraries/vendor/composer/' . $composerFile;
|
|
|
|
$filesToCheck = array_merge($filesToCheck, $files);
|
|
|
|
}
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
2023-08-20 13:54:48 +00:00
|
|
|
);
|
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
return $this->checkFilesExist($filesToCheck, $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check a list of files if they exist.
|
|
|
|
*
|
|
|
|
* @param array $files The list of files to check
|
|
|
|
* @param string $path The path where the temporary patch resides
|
|
|
|
*
|
2023-08-25 20:48:31 +00:00
|
|
|
* @return bool True if all files exist | False otherwise.
|
2022-09-08 08:54:04 +00:00
|
|
|
*
|
|
|
|
* @since 4.0.0
|
|
|
|
*/
|
|
|
|
private function checkFilesExist(array $files, string $path): bool
|
|
|
|
{
|
|
|
|
$path = Path::clean($path);
|
2023-08-25 20:48:31 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
foreach ($files as $file) {
|
|
|
|
if (is_array($file)) {
|
|
|
|
$this->checkFilesExist($file, $path);
|
|
|
|
} elseif (!file_exists($file)) {
|
|
|
|
// Check if the file exists in the Joomla filesystem
|
|
|
|
$file = str_ireplace($path, JPATH_SITE, $file);
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the applied patch into database
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the applied pull request
|
|
|
|
* @param array $fileList List of files
|
|
|
|
* @param string $sha sha-key from pull request
|
|
|
|
*
|
|
|
|
* @return integer $id last inserted id
|
|
|
|
*
|
|
|
|
* @since 3.0
|
|
|
|
*/
|
2023-08-20 13:54:48 +00:00
|
|
|
private function saveAppliedPatch(
|
|
|
|
int $id,
|
|
|
|
array $fileList,
|
|
|
|
string $sha = null
|
|
|
|
): int {
|
|
|
|
$record = (object)[
|
2022-09-08 08:54:04 +00:00
|
|
|
'pull_id' => $id,
|
|
|
|
'data' => json_encode($fileList),
|
|
|
|
'patched_by' => Factory::getUser()->id,
|
|
|
|
'applied' => 1,
|
|
|
|
'applied_version' => JVERSION,
|
2023-08-20 13:54:48 +00:00
|
|
|
];
|
|
|
|
$db = $this->getDatabase();
|
2022-09-08 08:54:04 +00:00
|
|
|
$db->insertObject('#__patchtester_tests', $record);
|
|
|
|
$insertId = $db->insertid();
|
|
|
|
if ($sha !== null) {
|
2023-08-20 13:54:48 +00:00
|
|
|
// Insert the retrieved commit SHA into the pulls table for this item
|
|
|
|
$db->setQuery(
|
|
|
|
$db->getQuery(true)
|
2022-09-08 08:54:04 +00:00
|
|
|
->update('#__patchtester_pulls')
|
|
|
|
->set('sha = ' . $db->quote($sha))
|
2023-08-20 13:54:48 +00:00
|
|
|
->where($db->quoteName('pull_id') . ' = ' . $id)
|
|
|
|
)->execute();
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $insertId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to apply
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2023-08-20 13:54:48 +00:00
|
|
|
* @throws RuntimeException
|
2022-09-08 08:54:04 +00:00
|
|
|
* @since 2.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function applyWithGitHub(int $id): bool
|
|
|
|
{
|
|
|
|
$github = Helper::initializeGithub();
|
2023-08-20 13:54:48 +00:00
|
|
|
$pull = $this->retrieveGitHubData($github, $id);
|
2022-09-08 08:54:04 +00:00
|
|
|
if ($pull->head->repo === null) {
|
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_REPO_IS_GONE'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$files = $this->getFiles($id, 1);
|
|
|
|
} catch (UnexpectedResponse $exception) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB',
|
|
|
|
$exception->getMessage()
|
2023-08-25 20:55:48 +00:00
|
|
|
),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
2023-08-20 13:54:48 +00:00
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!count($files)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedFiles = $this->parseFileList($files);
|
|
|
|
if (!count($parsedFiles)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($parsedFiles as $file) {
|
|
|
|
switch ($file->action) {
|
|
|
|
case 'deleted':
|
|
|
|
if (!file_exists(JPATH_ROOT . '/' . $file->filename)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S',
|
|
|
|
$file->filename
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'added':
|
|
|
|
case 'modified':
|
|
|
|
case 'renamed':
|
|
|
|
// If the backup file already exists, we can't apply the patch
|
2023-08-25 20:55:48 +00:00
|
|
|
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->filename) . '.txt')) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_CONFLICT_S',
|
|
|
|
$file->filename
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 20:55:48 +00:00
|
|
|
if (
|
|
|
|
$file->action === 'modified'
|
2023-08-25 20:58:31 +00:00
|
|
|
&& !file_exists(JPATH_ROOT . '/' . $file->filename)
|
|
|
|
) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S',
|
|
|
|
$file->filename
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-08-20 13:54:48 +00:00
|
|
|
$contentsResponse = $github->getFileContents(
|
|
|
|
$pull->head->user->login,
|
|
|
|
$pull->head->repo->name,
|
|
|
|
$file->repofilename,
|
|
|
|
urlencode($pull->head->ref)
|
|
|
|
);
|
|
|
|
$contents = json_decode(
|
|
|
|
$contentsResponse->body,
|
|
|
|
false
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
// In case encoding type ever changes
|
|
|
|
switch ($contents->encoding) {
|
|
|
|
case 'base64':
|
|
|
|
$file->body = base64_decode($contents->content);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::_(
|
|
|
|
'COM_PATCHTESTER_ERROR_UNSUPPORTED_ENCODING'
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
} catch (UnexpectedResponse $exception) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB',
|
|
|
|
$exception->getMessage()
|
2023-08-25 20:55:48 +00:00
|
|
|
),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
2023-08-20 13:54:48 +00:00
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we have ensured that we have all the new files and there are no conflicts
|
|
|
|
foreach ($parsedFiles as $file) {
|
|
|
|
// We only create a backup if the file already exists
|
|
|
|
if (
|
|
|
|
$file->action === 'deleted'
|
2023-08-25 20:55:48 +00:00
|
|
|
|| (file_exists(JPATH_ROOT . '/' . $file->filename) && $file->action === 'modified')
|
|
|
|
|| (file_exists(JPATH_ROOT . '/' . $file->originalFile) && $file->action === 'renamed')
|
2022-09-08 08:54:04 +00:00
|
|
|
) {
|
2023-08-25 20:55:48 +00:00
|
|
|
$filename = $file->action === 'renamed' ? $file->originalFile : $file->filename;
|
2022-09-08 08:54:04 +00:00
|
|
|
$src = JPATH_ROOT . '/' . $filename;
|
2023-08-25 20:55:48 +00:00
|
|
|
$dest = JPATH_COMPONENT . '/backups/' . md5($filename) . '.txt';
|
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
if (!File::copy(Path::clean($src), $dest)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_COPY_FILE',
|
|
|
|
$src,
|
|
|
|
$dest
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($file->action) {
|
|
|
|
case 'modified':
|
|
|
|
case 'added':
|
2023-08-25 20:55:48 +00:00
|
|
|
if (!File::write(Path::clean(JPATH_ROOT . '/' . $file->filename), $file->body)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_WRITE_FILE',
|
|
|
|
JPATH_ROOT . '/' . $file->filename
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'deleted':
|
2023-08-25 20:55:48 +00:00
|
|
|
if (!File::delete(Path::clean(JPATH_ROOT . '/' . $file->filename))) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
JPATH_ROOT . '/' . $file->filename
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'renamed':
|
2023-08-25 20:55:48 +00:00
|
|
|
if (!File::delete(Path::clean(JPATH_ROOT . '/' . $file->originalFile))) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
JPATH_ROOT . '/' . $file->originalFile
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 20:55:48 +00:00
|
|
|
if (!File::write(Path::clean(JPATH_ROOT . '/' . $file->filename), $file->body)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_WRITE_FILE',
|
|
|
|
JPATH_ROOT . '/' . $file->filename
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need the file's body any longer (and it causes issues with binary data when json_encode() is run), so remove it
|
|
|
|
unset($file->body);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->saveAppliedPatch($pull->number, $parsedFiles, $pull->head->sha);
|
|
|
|
// Update the autoloader file
|
|
|
|
$this->namespaceMapper->create();
|
|
|
|
// Change the media version
|
|
|
|
$version = new Version();
|
|
|
|
$version->refreshMediaVersion();
|
2023-08-20 13:54:48 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all files from Github.
|
|
|
|
*
|
|
|
|
* @param int $id The pull ID
|
|
|
|
* @param int $page THhe page umber to process
|
|
|
|
* @param array $files The list of files retrieved from Github
|
|
|
|
*
|
|
|
|
* @return array LIst of files to process.
|
|
|
|
*
|
|
|
|
* @since 4.2.0
|
|
|
|
*/
|
|
|
|
private function getFiles(int $id, int $page, array $files = []): array
|
|
|
|
{
|
2023-08-20 13:54:48 +00:00
|
|
|
$github = Helper::initializeGithub();
|
|
|
|
$filesResponse = $github->getFilesForPullRequest(
|
|
|
|
$this->getState()->get('github_user'),
|
|
|
|
$this->getState()->get('github_repo'),
|
|
|
|
$id,
|
|
|
|
$page
|
|
|
|
);
|
2023-08-25 20:55:48 +00:00
|
|
|
$files = array_merge($files, json_decode($filesResponse->getBody(), false));
|
2023-08-20 13:54:48 +00:00
|
|
|
$lastPage = 1;
|
|
|
|
$headers = $filesResponse->getHeaders();
|
2023-05-12 13:13:46 +00:00
|
|
|
|
|
|
|
if (!isset($headers['link'])) {
|
|
|
|
return $files;
|
|
|
|
}
|
2022-09-08 08:54:04 +00:00
|
|
|
|
|
|
|
preg_match(
|
|
|
|
'/(\?page=[0-9]{1,3}>; rel=\"last\")/',
|
2023-05-12 13:13:46 +00:00
|
|
|
$headers['link'][0],
|
2022-09-08 08:54:04 +00:00
|
|
|
$matches
|
|
|
|
);
|
|
|
|
if ($matches && isset($matches[0])) {
|
|
|
|
preg_match('/\d+/', $matches[0], $pages);
|
2023-08-20 13:54:48 +00:00
|
|
|
$lastPage = (int)$pages[0];
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($page <= $lastPage) {
|
|
|
|
$files = $this->getFiles($id, ++$page, $files);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the list of modified files from a pull request
|
|
|
|
*
|
|
|
|
* @param stdClass $files The modified files to parse
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
private function parseFileList(array $files): array
|
|
|
|
{
|
2023-08-25 20:55:48 +00:00
|
|
|
$parsedFiles = [];
|
2023-08-20 13:54:48 +00:00
|
|
|
/*
|
2023-08-25 20:55:48 +00:00
|
|
|
* Check if the patch tester is running in a development environment
|
|
|
|
* If we are not in development, we'll need to check the exclusion lists
|
|
|
|
*/
|
2022-09-08 08:54:04 +00:00
|
|
|
$isDev = file_exists(JPATH_INSTALLATION . '/index.php');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (!$isDev) {
|
|
|
|
$filePath = explode('/', $file->filename);
|
|
|
|
if (in_array($filePath[0], $this->nonProductionFiles, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($filePath[0], $this->nonProductionFolders, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sometimes the repo filename is not the production file name
|
|
|
|
$prodFileName = $file->filename;
|
|
|
|
$prodRenamedFileName = $file->previous_filename ?? false;
|
|
|
|
$filePath = explode('/', $prodFileName);
|
2023-08-20 13:54:48 +00:00
|
|
|
// Remove the `src` here to match the CMS paths if needed
|
2022-09-08 08:54:04 +00:00
|
|
|
if ($filePath[0] === 'src') {
|
|
|
|
$prodFileName = str_replace('src/', '', $prodFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prodRenamedFileName) {
|
|
|
|
$filePath = explode('/', $prodRenamedFileName);
|
2023-08-20 13:54:48 +00:00
|
|
|
// Remove the `src` here to match the CMS paths if needed
|
2022-09-08 08:54:04 +00:00
|
|
|
if ($filePath[0] === 'src') {
|
2023-08-20 13:54:48 +00:00
|
|
|
$prodRenamedFileName = str_replace(
|
|
|
|
'src/',
|
|
|
|
'',
|
|
|
|
$prodRenamedFileName
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
$parsedFiles[] = (object)array(
|
2022-09-08 08:54:04 +00:00
|
|
|
'action' => $file->status,
|
|
|
|
'filename' => $prodFileName,
|
|
|
|
'repofilename' => $file->filename,
|
|
|
|
'fileurl' => $file->contents_url,
|
|
|
|
'originalFile' => $prodRenamedFileName,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parsedFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverts the specified pull request
|
|
|
|
* However uses different methods for different repositories.
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to revert
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
2023-08-20 13:54:48 +00:00
|
|
|
* @since 3.0
|
2022-09-08 08:54:04 +00:00
|
|
|
*/
|
|
|
|
public function revert(int $id): bool
|
|
|
|
{
|
|
|
|
$params = ComponentHelper::getParams('com_patchtester');
|
|
|
|
// Decide based on repository settings whether patch will be applied through Github or CIServer
|
2023-08-20 13:54:48 +00:00
|
|
|
if ((bool)$params->get('ci_switch', 0)) {
|
2022-09-08 08:54:04 +00:00
|
|
|
return $this->revertWithCIServer($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->revertWithGitHub($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverts the specified pull request with CIServer options
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to revert
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
2023-08-20 13:54:48 +00:00
|
|
|
* @since 3.0
|
2022-09-08 08:54:04 +00:00
|
|
|
*/
|
|
|
|
public function revertWithCIServer(int $id): bool
|
|
|
|
{
|
|
|
|
// Get the CIServer Registry
|
|
|
|
$ciSettings = Helper::initializeCISettings();
|
|
|
|
$testRecord = $this->getTestRecord($id);
|
|
|
|
// We don't want to restore files from an older version
|
|
|
|
if ($testRecord->applied_version !== JVERSION) {
|
|
|
|
return $this->removeTest($testRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = json_decode($testRecord->data, false);
|
|
|
|
if (!$files) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_READING_DATABASE_TABLE',
|
|
|
|
__METHOD__,
|
|
|
|
htmlentities($testRecord->data)
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
$backupsPath = $ciSettings->get('folder.backups')
|
|
|
|
. "/$testRecord->pull_id";
|
2022-09-08 08:54:04 +00:00
|
|
|
foreach ($files as $file) {
|
|
|
|
try {
|
|
|
|
$filePath = explode("\\", $file);
|
|
|
|
array_pop($filePath);
|
|
|
|
$filePath = implode("\\", $filePath);
|
|
|
|
// Delete file from root of it exists
|
|
|
|
if (file_Exists(JPATH_ROOT . "/$file")) {
|
|
|
|
File::delete(JPATH_ROOT . "/$file");
|
|
|
|
// Move from backup, if it exists there
|
|
|
|
if (file_exists($backupsPath . '/' . $file)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
File::move(
|
|
|
|
$backupsPath . '/' . $file,
|
|
|
|
JPATH_ROOT . '/' . $file
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If folder is empty, remove it as well
|
2023-08-25 20:58:31 +00:00
|
|
|
if (count(glob(JPATH_ROOT . '/' . $filePath . '/*')) === 0) {
|
2022-09-08 08:54:04 +00:00
|
|
|
Folder::delete(JPATH_ROOT . '/' . $filePath);
|
|
|
|
}
|
|
|
|
} elseif (file_exists($backupsPath . '/' . $file)) {
|
|
|
|
// Move from backup, if file exists there - got deleted by patch
|
|
|
|
if (!file_exists(JPATH_ROOT . '/' . $filePath)) {
|
|
|
|
Folder::create(JPATH_ROOT . '/' . $filePath);
|
|
|
|
}
|
|
|
|
|
2023-08-20 13:54:48 +00:00
|
|
|
File::move(
|
|
|
|
$backupsPath . '/' . $file,
|
|
|
|
JPATH_ROOT . '/' . $file
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
} catch (RuntimeException $exception) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_FAILED_REVERT_PATCH',
|
|
|
|
$file,
|
|
|
|
$exception->getMessage()
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Folder::delete($backupsPath);
|
|
|
|
// Update the autoloader file
|
|
|
|
$this->namespaceMapper->create();
|
|
|
|
// Change the media version
|
|
|
|
$version = new Version();
|
|
|
|
$version->refreshMediaVersion();
|
2023-08-20 13:54:48 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
return $this->removeTest($testRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves test data from database by specific id
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the record
|
|
|
|
*
|
|
|
|
* @return stdClass $testRecord The record looking for
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
private function getTestRecord(int $id): stdClass
|
|
|
|
{
|
2023-08-20 13:54:48 +00:00
|
|
|
$db = $this->getDatabase();
|
|
|
|
|
|
|
|
return $db->setQuery(
|
|
|
|
$db->getQuery(true)
|
2022-09-08 08:54:04 +00:00
|
|
|
->select('*')
|
|
|
|
->from('#__patchtester_tests')
|
2023-08-20 13:54:48 +00:00
|
|
|
->where('id = ' . (int)$id)
|
|
|
|
)->loadObject();
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the database record for a test
|
|
|
|
*
|
|
|
|
* @param stdClass $testRecord The record being deleted
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
private function removeTest(stdClass $testRecord): bool
|
|
|
|
{
|
2023-08-20 13:54:48 +00:00
|
|
|
$db = $this->getDatabase();
|
|
|
|
// Remove the retrieved commit SHA from the pulls table for this item
|
|
|
|
$db->setQuery(
|
|
|
|
$db->getQuery(true)
|
2022-09-08 08:54:04 +00:00
|
|
|
->update('#__patchtester_pulls')
|
|
|
|
->set('sha = ' . $db->quote(''))
|
2023-08-20 13:54:48 +00:00
|
|
|
->where($db->quoteName('id') . ' = ' . (int)$testRecord->id)
|
|
|
|
)->execute();
|
|
|
|
// And delete the record from the tests table
|
|
|
|
$db->setQuery(
|
|
|
|
$db->getQuery(true)
|
2022-09-08 08:54:04 +00:00
|
|
|
->delete('#__patchtester_tests')
|
2023-08-20 13:54:48 +00:00
|
|
|
->where('id = ' . (int)$testRecord->id)
|
|
|
|
)->execute();
|
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverts the specified pull request with Github Requests
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to revert
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
2023-08-20 13:54:48 +00:00
|
|
|
* @since 2.0
|
2022-09-08 08:54:04 +00:00
|
|
|
*/
|
|
|
|
public function revertWithGitHub(int $id): bool
|
|
|
|
{
|
|
|
|
$testRecord = $this->getTestRecord($id);
|
|
|
|
// We don't want to restore files from an older version
|
|
|
|
if ($testRecord->applied_version != JVERSION) {
|
|
|
|
return $this->removeTest($testRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = json_decode($testRecord->data, false);
|
|
|
|
if (!$files) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_READING_DATABASE_TABLE',
|
|
|
|
__METHOD__,
|
|
|
|
htmlentities($testRecord->data)
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
switch ($file->action) {
|
|
|
|
case 'deleted':
|
|
|
|
case 'modified':
|
2023-08-20 13:54:48 +00:00
|
|
|
$src = JPATH_COMPONENT . '/backups/' . md5($file->filename)
|
|
|
|
. '.txt';
|
2022-09-08 08:54:04 +00:00
|
|
|
$dest = JPATH_ROOT . '/' . $file->filename;
|
|
|
|
if (!File::copy($src, $dest)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_COPY_FILE',
|
|
|
|
$src,
|
|
|
|
$dest
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($src)) {
|
|
|
|
if (!File::delete($src)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
$src
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'added':
|
|
|
|
$src = JPATH_ROOT . '/' . $file->filename;
|
|
|
|
if (file_exists($src)) {
|
|
|
|
if (!File::delete($src)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
$src
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'renamed':
|
2023-08-25 20:58:31 +00:00
|
|
|
$originalSrc = JPATH_COMPONENT . '/backups/' . md5($file->originalFile) . '.txt';
|
2022-09-08 08:54:04 +00:00
|
|
|
$newSrc = JPATH_ROOT . '/' . $file->filename;
|
|
|
|
$dest = JPATH_ROOT . '/' . $file->originalFile;
|
2023-08-25 20:58:31 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
if (!File::copy($originalSrc, $dest)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_COPY_FILE',
|
|
|
|
$originalSrc,
|
|
|
|
$dest
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($originalSrc)) {
|
|
|
|
if (!File::delete($originalSrc)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
$originalSrc
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($newSrc)) {
|
|
|
|
if (!File::delete($newSrc)) {
|
2023-08-20 13:54:48 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
$newSrc
|
|
|
|
)
|
|
|
|
);
|
2022-09-08 08:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the autoloader file
|
|
|
|
$this->namespaceMapper->create();
|
|
|
|
// Change the media version
|
|
|
|
$version = new Version();
|
|
|
|
$version->refreshMediaVersion();
|
2023-08-20 13:54:48 +00:00
|
|
|
|
2022-09-08 08:54:04 +00:00
|
|
|
return $this->removeTest($testRecord);
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|