2011-10-11 13:02:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
|
2014-05-03 23:48:08 +00:00
|
|
|
namespace PatchTester\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;
|
2017-08-17 23:22:01 +00:00
|
|
|
use Joomla\CMS\Version;
|
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;
|
2016-06-25 16:34:48 +00:00
|
|
|
use PatchTester\GitHub\Exception\UnexpectedResponse;
|
2019-09-10 13:39:57 +00:00
|
|
|
use PatchTester\GitHub\GitHub;
|
2014-05-03 23:48:08 +00:00
|
|
|
use PatchTester\Helper;
|
2020-03-22 09:37:29 +00:00
|
|
|
use RuntimeException;
|
|
|
|
use stdClass;
|
2014-05-03 23:48:08 +00:00
|
|
|
|
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
|
|
|
*/
|
2019-08-28 00:57:15 +00:00
|
|
|
class PullModel extends AbstractModel
|
2011-10-11 13:02:57 +00:00
|
|
|
{
|
2013-07-28 17:44:05 +00:00
|
|
|
/**
|
|
|
|
* Array containing top level non-production folders
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2016-07-30 20:09:04 +00:00
|
|
|
protected $nonProductionFolders = array(
|
|
|
|
'build',
|
|
|
|
'docs',
|
|
|
|
'installation',
|
|
|
|
'tests',
|
|
|
|
'.github',
|
|
|
|
);
|
2016-03-27 09:59:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array containing non-production files
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
protected $nonProductionFiles = array(
|
2016-10-08 15:44:47 +00:00
|
|
|
'.drone.yml',
|
2016-03-27 09:59:48 +00:00
|
|
|
'.gitignore',
|
2016-10-08 15:44:47 +00:00
|
|
|
'.php_cs',
|
2016-03-27 09:59:48 +00:00
|
|
|
'.travis.yml',
|
|
|
|
'README.md',
|
|
|
|
'build.xml',
|
|
|
|
'composer.json',
|
|
|
|
'composer.lock',
|
|
|
|
'phpunit.xml.dist',
|
|
|
|
'robots.txt.dist',
|
|
|
|
'travisci-phpunit.xml',
|
2016-07-30 20:09:04 +00:00
|
|
|
'LICENSE',
|
|
|
|
'RoboFile.dist.ini',
|
|
|
|
'RoboFile.php',
|
|
|
|
'codeception.yml',
|
|
|
|
'jorobo.dist.ini',
|
|
|
|
'manifest.xml',
|
|
|
|
'crowdin.yaml',
|
2016-03-27 09:59:48 +00:00
|
|
|
);
|
2013-07-28 17:44:05 +00:00
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
2019-09-09 15:32:50 +00:00
|
|
|
* However uses different methods for different repositories.
|
2013-07-13 02:26:21 +00:00
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to apply
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2019-09-09 16:38:25 +00:00
|
|
|
* @since 3.0
|
2019-09-08 16:02:14 +00:00
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @throws RuntimeException
|
2013-07-13 02:26:21 +00:00
|
|
|
*/
|
2020-03-22 09:37:29 +00:00
|
|
|
public function apply(int $id): bool
|
2019-09-08 16:02:14 +00:00
|
|
|
{
|
|
|
|
$params = ComponentHelper::getParams('com_patchtester');
|
|
|
|
|
|
|
|
// Decide based on repository settings whether patch will be applied through Github or CIServer
|
2020-03-22 09:41:28 +00:00
|
|
|
if ((bool) $params->get('ci_switch', 1))
|
2019-09-08 16:02:14 +00:00
|
|
|
{
|
|
|
|
return $this->applyWithCIServer($id);
|
|
|
|
}
|
2020-03-22 09:37:29 +00:00
|
|
|
|
|
|
|
return $this->applyWithGitHub($id);
|
2019-09-08 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to apply
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @since 3.0
|
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @throws RuntimeException
|
2019-09-08 16:02:14 +00:00
|
|
|
*/
|
2020-03-22 09:37:29 +00:00
|
|
|
private function applyWithCIServer(int $id): bool
|
2019-09-08 16:02:14 +00:00
|
|
|
{
|
|
|
|
// Get the CIServer Registry
|
|
|
|
$ciSettings = Helper::initializeCISettings();
|
|
|
|
|
2019-09-10 13:39:57 +00:00
|
|
|
// Get the Github object
|
|
|
|
$github = Helper::initializeGithub();
|
|
|
|
|
2019-09-14 16:54:57 +00:00
|
|
|
// Retrieve pullData for sha later on.
|
2020-03-22 09:37:29 +00:00
|
|
|
$pull = $this->retrieveGitHubData($github, $id);
|
|
|
|
|
|
|
|
if ($pull->head->repo === null)
|
2019-09-11 11:57:14 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_REPO_IS_GONE'));
|
2019-09-11 11:57:14 +00:00
|
|
|
}
|
2019-09-10 13:39:57 +00:00
|
|
|
|
2020-03-31 19:44:32 +00:00
|
|
|
$sha = $pull->head->sha;
|
2020-03-22 09:37:29 +00:00
|
|
|
|
2019-09-09 15:32:50 +00:00
|
|
|
// Create tmp folder if it does not exist
|
|
|
|
if (!file_exists($ciSettings->get('folder.temp')))
|
|
|
|
{
|
2019-09-10 11:53:40 +00:00
|
|
|
Folder::create($ciSettings->get('folder.temp'));
|
2019-09-09 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
$tempPath = $ciSettings->get('folder.temp') . '/' . $id;
|
|
|
|
$backupsPath = $ciSettings->get('folder.backups') . '/' . $id;
|
2019-09-11 09:41:01 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
$delLogPath = $tempPath . '/' . $ciSettings->get('zip.log.name');
|
|
|
|
$zipPath = $tempPath . '/' . $ciSettings->get('zip.name');
|
2019-09-11 09:41:01 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
$serverZipPath = sprintf($ciSettings->get('zip.url'), $id);
|
2019-09-08 16:02:14 +00:00
|
|
|
|
2019-09-09 16:38:25 +00:00
|
|
|
// Patch has already been applied
|
|
|
|
if (file_exists($backupsPath))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-21 09:58:04 +00:00
|
|
|
$version = new Version;
|
|
|
|
$httpOption = new Registry;
|
|
|
|
$httpOption->set('userAgent', $version->getUserAgent('Joomla', true, false));
|
2019-09-08 16:02:14 +00:00
|
|
|
|
2020-03-21 09:58:04 +00:00
|
|
|
// Try to download the zip file
|
|
|
|
try
|
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
$http = HttpFactory::getHttp($httpOption);
|
2020-03-21 09:58:04 +00:00
|
|
|
$result = $http->get($serverZipPath);
|
|
|
|
}
|
2020-03-22 09:37:29 +00:00
|
|
|
catch (RuntimeException $e)
|
2020-03-21 09:58:04 +00:00
|
|
|
{
|
|
|
|
$result = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result === null || ($result->getStatusCode() !== 200 && $result->getStatusCode() !== 310))
|
2019-09-08 16:02:14 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_SERVER_RESPONDED_NOT_200'));
|
2019-09-08 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:58:04 +00:00
|
|
|
// Assign to variable to avlod PHP notice "Indirect modification of overloaded property"
|
|
|
|
$content = (string) $result->getBody();
|
|
|
|
|
|
|
|
// Write the file to disk
|
|
|
|
File::write($zipPath, $content);
|
2019-09-08 16:02:14 +00:00
|
|
|
|
|
|
|
// Check if zip folder could have been downloaded
|
|
|
|
if (!file_exists($zipPath))
|
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_ZIP_DOES_NOT_EXIST'));
|
2019-09-08 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:58:04 +00:00
|
|
|
Folder::create($tempPath);
|
|
|
|
|
2019-09-10 11:53:40 +00:00
|
|
|
$zip = new Zip;
|
2019-09-08 16:02:14 +00:00
|
|
|
|
2019-09-10 11:53:40 +00:00
|
|
|
if (!$zip->extract($zipPath, $tempPath))
|
2019-09-08 16:02:14 +00:00
|
|
|
{
|
2019-09-10 11:53:40 +00:00
|
|
|
Folder::delete($tempPath);
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_ZIP_EXTRACT_FAILED'));
|
2019-09-08 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 16:54:57 +00:00
|
|
|
// Remove zip to avoid get listing afterwards
|
2019-09-10 11:53:40 +00:00
|
|
|
File::delete($zipPath);
|
2019-09-11 09:41:01 +00:00
|
|
|
|
2020-03-31 19:44:32 +00:00
|
|
|
// Verify the composer autoloader for any invalid entries
|
|
|
|
if ($this->verifyAutoloader($tempPath) === false)
|
|
|
|
{
|
|
|
|
// There is something broken in the autoloader, clean up and go back
|
|
|
|
Folder::delete($tempPath);
|
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_PATCH_BREAKS_SITE'));
|
|
|
|
}
|
|
|
|
|
2019-09-14 16:54:57 +00:00
|
|
|
// Get files from deleted_logs
|
2020-03-31 19:44:32 +00:00
|
|
|
$deletedFiles = (file_exists($delLogPath) ? file($delLogPath) : []);
|
2019-09-11 11:57:14 +00:00
|
|
|
$deletedFiles = array_map('trim', $deletedFiles);
|
2019-09-11 09:41:01 +00:00
|
|
|
|
2019-09-14 16:54:57 +00:00
|
|
|
if (file_exists($delLogPath))
|
2019-09-11 12:57:35 +00:00
|
|
|
{
|
2019-09-14 16:54:57 +00:00
|
|
|
// Remove deleted_logs to avoid get listing afterwards
|
2019-09-11 12:57:35 +00:00
|
|
|
File::delete($delLogPath);
|
|
|
|
}
|
2019-09-11 09:41:01 +00:00
|
|
|
|
2019-09-14 16:54:57 +00:00
|
|
|
// Retrieve all files and merge them into one array
|
2019-09-10 11:53:40 +00:00
|
|
|
$files = Folder::files($tempPath, null, true, true);
|
2020-03-22 09:37:29 +00:00
|
|
|
$files = str_replace(Path::clean($tempPath . '\\'), '', $files);
|
2019-09-11 09:41:01 +00:00
|
|
|
$files = array_merge($files, $deletedFiles);
|
2019-09-09 15:32:50 +00:00
|
|
|
|
2019-09-10 11:53:40 +00:00
|
|
|
Folder::create($backupsPath);
|
2019-09-08 16:02:14 +00:00
|
|
|
|
2019-09-09 15:32:50 +00:00
|
|
|
// Moves existent files to backup and replace them or creates new one if they don't exist
|
2019-09-11 09:41:01 +00:00
|
|
|
foreach ($files as $key => $file)
|
2019-09-08 16:02:14 +00:00
|
|
|
{
|
2019-09-11 09:41:01 +00:00
|
|
|
try
|
2019-09-08 16:02:14 +00:00
|
|
|
{
|
2019-10-19 13:47:43 +00:00
|
|
|
$filePath = explode(DIRECTORY_SEPARATOR, Path::clean($file));
|
2019-09-11 09:41:01 +00:00
|
|
|
array_pop($filePath);
|
2019-10-19 13:47:43 +00:00
|
|
|
$filePath = implode(DIRECTORY_SEPARATOR, $filePath);
|
2019-09-11 09:41:01 +00:00
|
|
|
|
2019-09-14 16:54:57 +00:00
|
|
|
// Deleted_logs returns files as well as folder, if value is folder, unset and skip
|
2020-03-22 09:37:29 +00:00
|
|
|
if (is_dir(JPATH_ROOT . '/' . $file))
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
2019-09-11 09:41:01 +00:00
|
|
|
unset($files[$key]);
|
|
|
|
continue;
|
2019-09-09 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
if (file_exists(JPATH_ROOT . '/' . $file))
|
2019-09-11 09:41:01 +00:00
|
|
|
{
|
|
|
|
// Create directories if they don't exist until file
|
2020-03-22 09:37:29 +00:00
|
|
|
if (!file_exists($backupsPath . '/' . $filePath))
|
2019-09-11 09:41:01 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
Folder::create($backupsPath . '/' . $filePath);
|
2019-09-11 09:41:01 +00:00
|
|
|
}
|
2019-09-10 11:53:40 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
File::move(JPATH_ROOT . '/' . $file, $backupsPath . '/' . $file);
|
2019-09-11 09:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
if (file_exists($tempPath . '/' . $file))
|
2019-09-11 09:41:01 +00:00
|
|
|
{
|
2019-09-11 12:57:35 +00:00
|
|
|
// Create directories if they don't exist until file
|
2020-03-22 09:37:29 +00:00
|
|
|
if (!file_exists(JPATH_ROOT . '/' . $filePath) || !is_dir(JPATH_ROOT . '/' . $filePath))
|
2019-09-11 12:57:35 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
Folder::create(JPATH_ROOT . '/' . $filePath);
|
2019-09-11 12:57:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
File::copy($tempPath . '/' . $file, JPATH_ROOT . '/' . $file);
|
2019-09-11 09:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-22 09:37:29 +00:00
|
|
|
catch (RuntimeException $exception)
|
2019-09-10 11:53:40 +00:00
|
|
|
{
|
2019-09-11 09:41:01 +00:00
|
|
|
Folder::delete($tempPath);
|
2019-09-11 12:57:35 +00:00
|
|
|
|
2020-03-31 18:19:05 +00:00
|
|
|
Folder::move($backupsPath, $backupsPath . '_failed');
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_FAILED_APPLYING_PATCH', $file, $exception->getMessage())
|
|
|
|
);
|
2019-09-10 11:53:40 +00:00
|
|
|
}
|
2019-09-08 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:32:50 +00:00
|
|
|
// Clear temp folder and store applied patch in database
|
2019-09-10 11:53:40 +00:00
|
|
|
Folder::delete($tempPath);
|
2019-09-08 16:02:14 +00:00
|
|
|
|
2020-03-31 18:19:05 +00:00
|
|
|
$this->saveAppliedPatch($id, $files, $sha);
|
2019-09-11 20:51:03 +00:00
|
|
|
|
2020-03-28 23:17:31 +00:00
|
|
|
// Remove the autoloader file
|
|
|
|
if (file_exists(JPATH_CACHE . '/autoload_psr4.php'))
|
2020-03-21 15:22:11 +00:00
|
|
|
{
|
2020-03-28 23:17:31 +00:00
|
|
|
File::delete(JPATH_CACHE . '/autoload_psr4.php');
|
2020-03-21 15:22:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 16:02:14 +00:00
|
|
|
// Change the media version
|
|
|
|
$version = new Version;
|
|
|
|
$version->refreshMediaVersion();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
private function retrieveGitHubData(GitHub $github, int $id): stdClass
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$rateResponse = $github->getRateLimit();
|
|
|
|
$rate = json_decode($rateResponse->body, false);
|
|
|
|
}
|
|
|
|
catch (UnexpectedResponse $exception)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $exception->getMessage()),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If over the API limit, we can't build this list
|
|
|
|
if ((int) $rate->resources->core->remaining === 0)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', Factory::getDate($rate->resources->core->reset))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$pullResponse = $github->getPullRequest(
|
|
|
|
$this->getState()->get('github_user'),
|
|
|
|
$this->getState()->get('github_repo'),
|
|
|
|
$id
|
|
|
|
);
|
|
|
|
$pull = json_decode($pullResponse->body, false);
|
|
|
|
}
|
|
|
|
catch (UnexpectedResponse $exception)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $exception->getMessage()),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pull;
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:44:32 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
$autoloadClass = false;
|
|
|
|
|
|
|
|
// Check if we have an autoload file
|
|
|
|
if (!file_exists($path . '/libraries/vendor/autoload.php'))
|
|
|
|
{
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the generated token
|
|
|
|
$autoload = file_get_contents($path . '/libraries/vendor/autoload.php');
|
|
|
|
$resultMatch = preg_match('/ComposerAutoloaderInit(.*)::/', $autoload, $match);
|
|
|
|
|
|
|
|
if (!$resultMatch)
|
|
|
|
{
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the static autoloader
|
|
|
|
require_once $path . '/libraries/vendor/composer/autoload_static.php';
|
|
|
|
$autoloadClass = '\Composer\Autoload\ComposerStaticInit' . $match[1];
|
|
|
|
|
|
|
|
// Get all the files
|
|
|
|
$files = $autoloadClass::$files;
|
|
|
|
|
|
|
|
// Verify all the files exist
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
// Fix the filepath to use the Joomla filesystem
|
|
|
|
$file = str_ireplace($path, JPATH_SITE, $file);
|
|
|
|
|
|
|
|
if (!file_exists($file))
|
|
|
|
{
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the files loader
|
|
|
|
|
|
|
|
// Load the classmap loader
|
|
|
|
|
|
|
|
// Load the namespaces loader
|
|
|
|
|
|
|
|
// Load the PSR-4 loader
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
private function saveAppliedPatch(int $id, array $fileList, string $sha = null): int
|
|
|
|
{
|
|
|
|
$record = (object) array(
|
|
|
|
'pull_id' => $id,
|
|
|
|
'data' => json_encode($fileList),
|
|
|
|
'patched_by' => Factory::getUser()->id,
|
|
|
|
'applied' => 1,
|
|
|
|
'applied_version' => JVERSION,
|
|
|
|
);
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
|
|
$db->insertObject('#__patchtester_tests', $record);
|
|
|
|
$insertId = $db->insertid();
|
|
|
|
|
|
|
|
if ($sha !== null)
|
|
|
|
{
|
|
|
|
// Insert the retrieved commit SHA into the pulls table for this item
|
|
|
|
$db->setQuery(
|
|
|
|
$db->getQuery(true)
|
|
|
|
->update('#__patchtester_pulls')
|
|
|
|
->set('sha = ' . $db->quote($sha))
|
|
|
|
->where($db->quoteName('pull_id') . ' = ' . $id)
|
|
|
|
)->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $insertId;
|
|
|
|
}
|
|
|
|
|
2019-09-08 16:02:14 +00:00
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to apply
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @throws RuntimeException
|
2019-09-08 16:02:14 +00:00
|
|
|
*/
|
2020-03-22 09:37:29 +00:00
|
|
|
private function applyWithGitHub(int $id): bool
|
2011-10-11 13:02:57 +00:00
|
|
|
{
|
2014-05-03 01:56:15 +00:00
|
|
|
// Get the Github object
|
2014-05-03 23:48:08 +00:00
|
|
|
$github = Helper::initializeGithub();
|
2015-05-09 17:00:55 +00:00
|
|
|
|
2019-09-10 13:39:57 +00:00
|
|
|
$pull = $this->retrieveGitHubData($github, $id);
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
if ($pull->head->repo === null)
|
2015-02-23 00:04:22 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_REPO_IS_GONE'));
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2014-05-03 01:56:15 +00:00
|
|
|
|
2015-05-09 16:35:36 +00:00
|
|
|
try
|
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
$filesResponse = $github->getFilesForPullRequest(
|
|
|
|
$this->getState()->get('github_user'),
|
|
|
|
$this->getState()->get('github_repo'),
|
|
|
|
$id
|
|
|
|
);
|
|
|
|
$files = json_decode($filesResponse->body, false);
|
2015-05-09 16:35:36 +00:00
|
|
|
}
|
2020-03-22 09:37:29 +00:00
|
|
|
catch (UnexpectedResponse $exception)
|
2015-05-09 16:35:36 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $exception->getMessage()),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
|
|
|
);
|
2015-05-09 16:35:36 +00:00
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2016-06-25 17:28:29 +00:00
|
|
|
if (!count($files))
|
2015-02-23 00:04:22 +00:00
|
|
|
{
|
2015-02-23 13:50:13 +00:00
|
|
|
return false;
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2013-07-28 17:44:05 +00:00
|
|
|
|
2016-06-25 17:28:29 +00:00
|
|
|
$parsedFiles = $this->parseFileList($files);
|
|
|
|
|
2019-09-23 13:33:46 +00:00
|
|
|
if (!count($parsedFiles))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-25 17:28:29 +00:00
|
|
|
foreach ($parsedFiles as $file)
|
2015-02-23 00:04:22 +00:00
|
|
|
{
|
2016-09-22 13:35:55 +00:00
|
|
|
switch ($file->action)
|
2013-07-13 00:31:00 +00:00
|
|
|
{
|
2016-09-22 13:35:55 +00:00
|
|
|
case 'deleted':
|
|
|
|
if (!file_exists(JPATH_ROOT . '/' . $file->filename))
|
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S', $file->filename)
|
|
|
|
);
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2016-09-22 13:35:55 +00:00
|
|
|
break;
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2016-09-22 13:35:55 +00:00
|
|
|
case 'added':
|
|
|
|
case 'modified':
|
|
|
|
case 'renamed':
|
|
|
|
// If the backup file already exists, we can't apply the patch
|
|
|
|
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->filename) . '.txt'))
|
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::sprintf('COM_PATCHTESTER_CONFLICT_S', $file->filename));
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
2016-06-25 17:28:29 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
if ($file->action === 'modified' && !file_exists(JPATH_ROOT . '/' . $file->filename))
|
2016-09-22 13:35:55 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S', $file->filename)
|
|
|
|
);
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
2016-06-25 17:28:29 +00:00
|
|
|
|
2016-09-22 13:35:55 +00:00
|
|
|
try
|
2016-06-25 17:28:29 +00:00
|
|
|
{
|
2016-09-22 13:35:55 +00:00
|
|
|
$contentsResponse = $github->getFileContents(
|
2020-03-22 09:37:29 +00:00
|
|
|
$pull->head->user->login,
|
|
|
|
$this->getState()->get('github_repo'),
|
|
|
|
$file->repofilename,
|
|
|
|
urlencode($pull->head->ref)
|
2016-09-22 13:35:55 +00:00
|
|
|
);
|
2016-06-25 17:28:29 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
$contents = json_decode($contentsResponse->body, false);
|
2016-06-25 17:28:29 +00:00
|
|
|
|
2016-09-22 13:35:55 +00:00
|
|
|
// In case encoding type ever changes
|
|
|
|
switch ($contents->encoding)
|
|
|
|
{
|
|
|
|
case 'base64':
|
|
|
|
$file->body = base64_decode($contents->content);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::_('COM_PATCHTESTER_ERROR_UNSUPPORTED_ENCODING'));
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
2016-06-25 17:28:29 +00:00
|
|
|
}
|
2020-03-22 09:37:29 +00:00
|
|
|
catch (UnexpectedResponse $exception)
|
2016-09-22 13:35:55 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $exception->getMessage()),
|
|
|
|
$exception->getCode(),
|
|
|
|
$exception
|
2019-10-13 08:12:37 +00:00
|
|
|
);
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
// At this point, we have ensured that we have all the new files and there are no conflicts
|
2016-06-25 17:28:29 +00:00
|
|
|
foreach ($parsedFiles as $file)
|
2015-02-23 00:04:22 +00:00
|
|
|
{
|
|
|
|
// We only create a backup if the file already exists
|
2020-03-22 09:37:29 +00:00
|
|
|
if ($file->action === 'deleted'
|
|
|
|
|| (file_exists(JPATH_ROOT . '/' . $file->filename)
|
2020-03-31 19:44:32 +00:00
|
|
|
&& $file->action === 'modified')
|
2020-03-22 09:37:29 +00:00
|
|
|
|| (file_exists(JPATH_ROOT . '/' . $file->originalFile) && $file->action === 'renamed'))
|
2011-10-11 20:51:12 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
$filename = $file->action === 'renamed' ? $file->originalFile : $file->filename;
|
2016-09-22 13:35:55 +00:00
|
|
|
$src = JPATH_ROOT . '/' . $filename;
|
|
|
|
$dest = JPATH_COMPONENT . '/backups/' . md5($filename) . '.txt';
|
2016-06-25 17:28:29 +00:00
|
|
|
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::copy(Path::clean($src), $dest))
|
2013-07-13 00:31:00 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_COPY_FILE', $src, $dest));
|
2013-07-13 00:31:00 +00:00
|
|
|
}
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
switch ($file->action)
|
|
|
|
{
|
|
|
|
case 'modified':
|
|
|
|
case 'added':
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::write(Path::clean(JPATH_ROOT . '/' . $file->filename), $file->body))
|
2015-02-23 00:04:22 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_WRITE_FILE', JPATH_ROOT . '/' . $file->filename)
|
|
|
|
);
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
break;
|
2013-07-13 00:31:00 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
case 'deleted':
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::delete(Path::clean(JPATH_ROOT . '/' . $file->filename)))
|
2015-02-23 00:04:22 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
JPATH_ROOT . '/' . $file->filename
|
|
|
|
)
|
|
|
|
);
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2016-09-22 13:35:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'renamed':
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::delete(Path::clean(JPATH_ROOT . '/' . $file->originalFile)))
|
2016-09-22 13:35:55 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE',
|
|
|
|
JPATH_ROOT . '/' . $file->originalFile
|
|
|
|
)
|
2019-10-13 08:12:37 +00:00
|
|
|
);
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::write(Path::clean(JPATH_ROOT . '/' . $file->filename), $file->body))
|
2016-09-22 13:35:55 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_WRITE_FILE', JPATH_ROOT . '/' . $file->filename)
|
|
|
|
);
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
break;
|
2011-10-11 16:04:33 +00:00
|
|
|
}
|
2016-06-25 17:39:35 +00:00
|
|
|
|
|
|
|
// 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);
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2011-10-15 01:13:59 +00:00
|
|
|
|
2019-09-09 15:32:50 +00:00
|
|
|
$this->saveAppliedPatch($pull->number, $parsedFiles, $pull->head->sha);
|
|
|
|
|
2020-03-28 23:17:31 +00:00
|
|
|
// Remove the autoloader file
|
|
|
|
if (file_exists(JPATH_CACHE . '/autoload_psr4.php'))
|
2020-03-21 15:22:11 +00:00
|
|
|
{
|
2020-03-28 23:17:31 +00:00
|
|
|
File::delete(JPATH_CACHE . '/autoload_psr4.php');
|
2020-03-21 15:22:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:32:50 +00:00
|
|
|
// Change the media version
|
|
|
|
$version = new Version;
|
|
|
|
$version->refreshMediaVersion();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-10 13:39:57 +00:00
|
|
|
/**
|
2020-03-22 09:37:29 +00:00
|
|
|
* Parse the list of modified files from a pull request
|
2019-09-10 13:39:57 +00:00
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @param stdClass $files The modified files to parse
|
2019-09-10 13:39:57 +00:00
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @return array
|
2019-09-10 13:39:57 +00:00
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @since 3.0.0
|
2019-09-10 13:39:57 +00:00
|
|
|
*/
|
2020-03-22 09:37:29 +00:00
|
|
|
protected function parseFileList(stdClass $files): array
|
2019-09-10 13:39:57 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
$parsedFiles = array();
|
2019-09-10 13:39:57 +00:00
|
|
|
|
2020-03-22 09:37:29 +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
|
|
|
|
*/
|
|
|
|
$isDev = file_exists(JPATH_INSTALLATION . '/index.php');
|
2019-09-10 13:39:57 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
foreach ($files as $file)
|
2019-09-10 13:39:57 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
// Remove the `src` here to match the CMS paths if needed
|
|
|
|
if ($filePath[0] === 'src')
|
|
|
|
{
|
|
|
|
$prodFileName = str_replace('src/', '', $prodFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prodRenamedFileName)
|
|
|
|
{
|
|
|
|
$filePath = explode('/', $prodRenamedFileName);
|
|
|
|
|
|
|
|
// Remove the `src` here to match the CMS paths if needed
|
|
|
|
if ($filePath[0] === 'src')
|
|
|
|
{
|
|
|
|
$prodRenamedFileName = str_replace('src/', '', $prodRenamedFileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedFiles[] = (object) array(
|
|
|
|
'action' => $file->status,
|
|
|
|
'filename' => $prodFileName,
|
|
|
|
'repofilename' => $file->filename,
|
|
|
|
'fileurl' => $file->contents_url,
|
|
|
|
'originalFile' => $prodRenamedFileName,
|
|
|
|
);
|
2019-09-10 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
return $parsedFiles;
|
2019-09-10 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:32:50 +00:00
|
|
|
/**
|
2020-03-22 09:37:29 +00:00
|
|
|
* Reverts the specified pull request
|
|
|
|
* However uses different methods for different repositories.
|
2019-09-09 15:32:50 +00:00
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @param integer $id ID of the pull request to revert
|
2019-09-09 15:32:50 +00:00
|
|
|
*
|
2020-03-22 09:37:29 +00:00
|
|
|
* @return boolean
|
2019-09-09 15:32:50 +00:00
|
|
|
*
|
|
|
|
* @since 3.0
|
2020-03-22 09:37:29 +00:00
|
|
|
* @throws RuntimeException
|
2019-09-09 15:32:50 +00:00
|
|
|
*/
|
2020-03-22 09:37:29 +00:00
|
|
|
public function revert(int $id): bool
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
$params = ComponentHelper::getParams('com_patchtester');
|
2016-06-19 15:06:59 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
// Decide based on repository settings whether patch will be applied through Github or CIServer
|
2020-03-22 09:41:28 +00:00
|
|
|
if (((bool) $params->get('ci_switch', 1) || $id === $this->getPatchChain($id)->insert_id))
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
return $this->revertWithCIServer($id);
|
2019-09-09 15:32:50 +00:00
|
|
|
}
|
2019-09-12 08:52:34 +00:00
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
return $this->revertWithGitHub($id);
|
2019-09-09 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverts the specified pull request with CIServer options
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to revert
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
|
|
|
* @since 3.0
|
2020-03-22 09:37:29 +00:00
|
|
|
* @throws RuntimeException
|
2019-09-09 15:32:50 +00:00
|
|
|
*/
|
2020-03-22 09:37:29 +00:00
|
|
|
public function revertWithCIServer(int $id): bool
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
|
|
|
// Get the CIServer Registry
|
|
|
|
$ciSettings = Helper::initializeCISettings();
|
|
|
|
|
|
|
|
$testRecord = $this->getTestRecord($id);
|
|
|
|
|
|
|
|
// We don't want to restore files from an older version
|
2020-03-22 09:37:29 +00:00
|
|
|
if ($testRecord->applied_version !== JVERSION)
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
|
|
|
return $this->removeTest($testRecord);
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
$files = json_decode($testRecord->data, false);
|
2019-09-09 15:32:50 +00:00
|
|
|
|
|
|
|
if (!$files)
|
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_READING_DATABASE_TABLE',
|
|
|
|
__METHOD__,
|
|
|
|
htmlentities($testRecord->data)
|
|
|
|
)
|
|
|
|
);
|
2019-09-09 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$backupsPath = $ciSettings->get('folder.backups') . "/$testRecord->pull_id";
|
|
|
|
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
2019-09-11 09:41:01 +00:00
|
|
|
try
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
2019-09-10 11:53:40 +00:00
|
|
|
$filePath = explode("\\", $file);
|
2019-09-09 19:38:23 +00:00
|
|
|
array_pop($filePath);
|
2019-09-10 11:53:40 +00:00
|
|
|
$filePath = implode("\\", $filePath);
|
2019-09-09 19:38:23 +00:00
|
|
|
|
2019-09-11 09:41:01 +00:00
|
|
|
// Delete file from root of it exists
|
|
|
|
if (file_Exists(JPATH_ROOT . "/$file"))
|
2019-09-09 19:38:23 +00:00
|
|
|
{
|
2019-09-11 09:41:01 +00:00
|
|
|
File::delete(JPATH_ROOT . "/$file");
|
|
|
|
|
|
|
|
// Move from backup, if it exists there
|
2020-03-22 09:37:29 +00:00
|
|
|
if (file_exists($backupsPath . '/' . $file))
|
2019-09-11 09:41:01 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
File::move($backupsPath . '/' . $file, JPATH_ROOT . '/' . $file);
|
2019-09-11 09:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If folder is empty, remove it as well
|
2020-03-22 09:37:29 +00:00
|
|
|
if (count(glob(JPATH_ROOT . '/' . $filePath . '/*')) === 0)
|
2019-09-11 09:41:01 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
Folder::delete(JPATH_ROOT . '/' . $filePath);
|
2019-09-11 09:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Move from backup, if file exists there - got deleted by patch
|
2020-03-22 09:37:29 +00:00
|
|
|
elseif (file_exists($backupsPath . '/' . $file))
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
if (!file_exists(JPATH_ROOT . '/' . $filePath))
|
2019-09-11 09:41:01 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
Folder::create(JPATH_ROOT . '/' . $filePath);
|
2019-09-11 09:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
File::move($backupsPath . '/' . $file, JPATH_ROOT . '/' . $file);
|
2019-09-09 15:32:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-22 09:37:29 +00:00
|
|
|
catch (RuntimeException $exception)
|
2019-09-09 15:32:50 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_FAILED_REVERT_PATCH', $file, $exception->getMessage())
|
|
|
|
);
|
2019-09-09 15:32:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 11:53:40 +00:00
|
|
|
Folder::delete($backupsPath);
|
2016-03-27 18:01:31 +00:00
|
|
|
|
2020-03-22 09:41:28 +00:00
|
|
|
// Remove the autoloader file
|
2020-03-28 23:17:31 +00:00
|
|
|
if (file_exists(JPATH_CACHE . '/autoload_psr4.php'))
|
2020-03-21 15:22:11 +00:00
|
|
|
{
|
2020-03-28 23:17:31 +00:00
|
|
|
File::delete(JPATH_CACHE . '/autoload_psr4.php');
|
2020-03-21 15:22:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 11:58:13 +00:00
|
|
|
// Change the media version
|
2017-08-17 23:22:01 +00:00
|
|
|
$version = new Version;
|
2016-11-12 11:58:13 +00:00
|
|
|
$version->refreshMediaVersion();
|
|
|
|
|
2019-09-09 15:32:50 +00:00
|
|
|
return $this->removeTest($testRecord);
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
|
|
return $db->setQuery(
|
|
|
|
$db->getQuery(true)
|
|
|
|
->select('*')
|
|
|
|
->from('#__patchtester_tests')
|
|
|
|
->where('id = ' . (int) $id)
|
|
|
|
)->loadObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
|
|
// Remove the retrieved commit SHA from the pulls table for this item
|
|
|
|
$db->setQuery(
|
|
|
|
$db->getQuery(true)
|
|
|
|
->update('#__patchtester_pulls')
|
|
|
|
->set('sha = ' . $db->quote(''))
|
|
|
|
->where($db->quoteName('id') . ' = ' . (int) $testRecord->id)
|
|
|
|
)->execute();
|
|
|
|
|
|
|
|
// And delete the record from the tests table
|
|
|
|
$db->setQuery(
|
|
|
|
$db->getQuery(true)
|
|
|
|
->delete('#__patchtester_tests')
|
|
|
|
->where('id = ' . (int) $testRecord->id)
|
|
|
|
)->execute();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
2019-09-09 15:32:50 +00:00
|
|
|
* Reverts the specified pull request with Github Requests
|
2013-07-13 02:26:21 +00:00
|
|
|
*
|
2016-03-11 16:36:53 +00:00
|
|
|
* @param integer $id ID of the pull request to revert
|
2013-07-13 02:26:21 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2014-05-03 23:48:08 +00:00
|
|
|
* @since 2.0
|
2020-03-22 09:37:29 +00:00
|
|
|
* @throws RuntimeException
|
2013-07-13 02:26:21 +00:00
|
|
|
*/
|
2020-03-22 09:37:29 +00:00
|
|
|
public function revertWithGitHub(int $id): bool
|
2011-10-11 13:02:57 +00:00
|
|
|
{
|
2019-09-09 15:32:50 +00:00
|
|
|
$testRecord = $this->getTestRecord($id);
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2013-07-13 00:31:00 +00:00
|
|
|
// We don't want to restore files from an older version
|
2016-06-19 15:06:59 +00:00
|
|
|
if ($testRecord->applied_version != JVERSION)
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2016-06-19 15:06:59 +00:00
|
|
|
return $this->removeTest($testRecord);
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:37:29 +00:00
|
|
|
$files = json_decode($testRecord->data, false);
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!$files)
|
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf(
|
|
|
|
'COM_PATCHTESTER_ERROR_READING_DATABASE_TABLE',
|
|
|
|
__METHOD__,
|
|
|
|
htmlentities($testRecord->data)
|
|
|
|
)
|
|
|
|
);
|
2012-06-12 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
switch ($file->action)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
case 'deleted':
|
|
|
|
case 'modified':
|
2016-06-25 17:28:29 +00:00
|
|
|
$src = JPATH_COMPONENT . '/backups/' . md5($file->filename) . '.txt';
|
|
|
|
$dest = JPATH_ROOT . '/' . $file->filename;
|
|
|
|
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::copy($src, $dest))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_COPY_FILE', $src, $dest)
|
|
|
|
);
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2016-06-25 17:28:29 +00:00
|
|
|
if (file_exists($src))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::delete($src))
|
2015-05-09 17:24:57 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
2019-08-28 01:20:49 +00:00
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', $src)
|
2015-05-09 17:24:57 +00:00
|
|
|
);
|
|
|
|
}
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'added':
|
2016-06-25 17:28:29 +00:00
|
|
|
$src = JPATH_ROOT . '/' . $file->filename;
|
|
|
|
|
|
|
|
if (file_exists($src))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::delete($src))
|
2015-05-09 17:24:57 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
2019-08-28 01:20:49 +00:00
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', $src)
|
2015-05-09 17:24:57 +00:00
|
|
|
);
|
|
|
|
}
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2016-09-22 13:35:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'renamed':
|
|
|
|
$originalSrc = JPATH_COMPONENT . '/backups/' . md5($file->originalFile) . '.txt';
|
|
|
|
$newSrc = JPATH_ROOT . '/' . $file->filename;
|
|
|
|
$dest = JPATH_ROOT . '/' . $file->originalFile;
|
|
|
|
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::copy($originalSrc, $dest))
|
2016-09-22 13:35:55 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_COPY_FILE', $originalSrc, $dest)
|
|
|
|
);
|
2016-09-22 13:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($originalSrc))
|
|
|
|
{
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::delete($originalSrc))
|
2016-09-22 13:35:55 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
2019-08-28 01:20:49 +00:00
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', $originalSrc)
|
2016-09-22 13:35:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($newSrc))
|
|
|
|
{
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::delete($newSrc))
|
2016-09-22 13:35:55 +00:00
|
|
|
{
|
2020-03-22 09:37:29 +00:00
|
|
|
throw new RuntimeException(
|
2019-08-28 01:20:49 +00:00
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', $newSrc)
|
2016-09-22 13:35:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:41:28 +00:00
|
|
|
// Remove the autoloader file
|
2020-03-28 23:17:31 +00:00
|
|
|
if (file_exists(JPATH_CACHE . '/autoload_psr4.php'))
|
2020-03-21 15:22:11 +00:00
|
|
|
{
|
2020-03-28 23:17:31 +00:00
|
|
|
File::delete(JPATH_CACHE . '/autoload_psr4.php');
|
2020-03-21 15:22:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 11:58:13 +00:00
|
|
|
// Change the media version
|
2017-08-17 23:22:01 +00:00
|
|
|
$version = new Version;
|
2016-11-12 11:58:13 +00:00
|
|
|
$version->refreshMediaVersion();
|
|
|
|
|
2016-06-19 15:06:59 +00:00
|
|
|
return $this->removeTest($testRecord);
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|