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
|
|
|
*
|
2015-02-22 21:29:43 +00:00
|
|
|
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2015 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
|
|
|
|
2014-05-03 02:02:38 +00:00
|
|
|
use Joomla\Registry\Registry;
|
|
|
|
|
2014-05-03 23:48:08 +00:00
|
|
|
use PatchTester\Helper;
|
|
|
|
|
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
|
|
|
*/
|
2014-05-03 23:48:08 +00:00
|
|
|
class PullModel extends \JModelBase
|
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
|
|
|
|
*/
|
|
|
|
protected $nonProductionFolders = array('build', 'docs', 'installation', 'tests');
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Method to parse a patch and extract the affected files
|
|
|
|
*
|
|
|
|
* @param string $patch Patch file to parse
|
|
|
|
*
|
|
|
|
* @return array Array of files within a patch
|
|
|
|
*
|
2014-05-03 23:48:08 +00:00
|
|
|
* @since 2.0
|
2013-07-13 02:26:21 +00:00
|
|
|
*/
|
2011-10-11 20:51:12 +00:00
|
|
|
protected function parsePatch($patch)
|
|
|
|
{
|
|
|
|
$state = 0;
|
|
|
|
$files = array();
|
|
|
|
|
2011-10-15 01:13:59 +00:00
|
|
|
$lines = explode("\n", $patch);
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
foreach ($lines AS $line)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
switch ($state)
|
|
|
|
{
|
|
|
|
case 0:
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'diff --git') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$state = 1;
|
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2014-05-03 23:48:08 +00:00
|
|
|
$file = new \stdClass;
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->action = 'modified';
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'index') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->index = substr($line, 6);
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, '---') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->old = substr($line, 6);
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, '+++') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->new = substr($line, 6);
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'new file mode') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->action = 'added';
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'deleted file mode') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->action = 'deleted';
|
|
|
|
}
|
|
|
|
|
2015-03-08 17:06:48 +00:00
|
|
|
// Binary files are presently unsupported, use this to reset the parser in the meantime
|
|
|
|
if (strpos($line, 'Binary files') === 0)
|
|
|
|
{
|
|
|
|
$state = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, '@@') === 0)
|
|
|
|
{
|
2015-03-08 17:06:48 +00:00
|
|
|
$state = 0;
|
2013-07-28 17:44:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the patch tester is running in a production environment
|
|
|
|
* If so, do not patch certain files as errors will be thrown
|
|
|
|
*/
|
2015-02-23 13:51:33 +00:00
|
|
|
if (!file_exists(JPATH_ROOT . '/installation/index.php'))
|
2013-07-28 17:44:05 +00:00
|
|
|
{
|
|
|
|
$filePath = explode('/', $file->new);
|
|
|
|
|
|
|
|
if (in_array($filePath[0], $this->nonProductionFolders))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
$files[] = $file;
|
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Patches the code with the supplied pull request
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to apply
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2014-05-03 23:48:08 +00:00
|
|
|
* @since 2.0
|
|
|
|
* @throws \RuntimeException
|
2013-07-13 02:26:21 +00:00
|
|
|
*/
|
2011-10-11 13:02:57 +00:00
|
|
|
public function apply($id)
|
|
|
|
{
|
2014-05-03 01:56:15 +00:00
|
|
|
// Get the Github object
|
2014-05-03 23:48:08 +00:00
|
|
|
$github = Helper::initializeGithub();
|
2015-02-23 00:04:22 +00:00
|
|
|
$rate = $github->authorization->getRateLimit();
|
2014-05-03 01:56:15 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
// If over the API limit, we can't build this list
|
|
|
|
if ($rate->resources->core->remaining == 0)
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2015-02-23 00:04:22 +00:00
|
|
|
throw new \RuntimeException(
|
|
|
|
\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset))
|
|
|
|
);
|
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2015-05-09 16:35:36 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$pull = $github->pulls->get($this->getState()->get('github_user'), $this->getState()->get('github_repo'), $id);
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $e->getMessage()));
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
if (is_null($pull->head->repo))
|
|
|
|
{
|
|
|
|
throw new \RuntimeException(\JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
|
|
|
|
}
|
2014-05-03 01:56:15 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
// Set up the JHttp object
|
|
|
|
$options = new Registry;
|
|
|
|
$options->set('userAgent', 'JPatchTester/2.0');
|
|
|
|
$options->set('timeout', 120);
|
2014-05-03 01:56:15 +00:00
|
|
|
|
2015-05-09 16:39:29 +00:00
|
|
|
$transport = \JHttpFactory::getHttp($options);
|
2014-05-03 01:56:15 +00:00
|
|
|
|
2015-05-09 16:35:36 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$patch = $transport->get($pull->diff_url)->body;
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $e->getMessage()));
|
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
$files = $this->parsePatch($patch);
|
2013-07-13 00:31:00 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
if (!$files)
|
|
|
|
{
|
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
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
|
|
|
|
{
|
2015-05-09 16:35:36 +00:00
|
|
|
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S', $file->old));
|
2013-07-28 17:44:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
if ($file->action == 'added' || $file->action == 'modified')
|
2013-07-13 00:31:00 +00:00
|
|
|
{
|
2015-02-23 00:04:22 +00:00
|
|
|
// If the backup file already exists, we can't apply the patch
|
|
|
|
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2015-05-09 16:35:36 +00:00
|
|
|
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_CONFLICT_S', $file->new));
|
2011-10-11 20:51:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
|
2013-07-13 00:31:00 +00:00
|
|
|
{
|
2015-05-09 16:35:36 +00:00
|
|
|
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S', $file->old));
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2015-05-09 15:52:53 +00:00
|
|
|
$url = 'https://raw.github.com/' . urlencode($pull->head->user->login) . '/' . urlencode($pull->head->repo->name)
|
|
|
|
. '/' . urlencode($pull->head->ref) . '/' . $file->new;
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2015-05-09 16:35:36 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$file->body = $transport->get($url)->body;
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $e->getMessage()));
|
|
|
|
}
|
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
|
|
|
jimport('joomla.filesystem.file');
|
2013-07-13 00:31:00 +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
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
// We only create a backup if the file already exists
|
|
|
|
if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->new) && $file->action == 'modified'))
|
2011-10-11 20:51:12 +00:00
|
|
|
{
|
2015-02-23 00:04:22 +00:00
|
|
|
if (!\JFile::copy(\JPath::clean(JPATH_ROOT . '/' . $file->old), JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
|
2013-07-13 00:31:00 +00:00
|
|
|
{
|
2015-02-23 00:04:22 +00:00
|
|
|
throw new \RuntimeException(
|
|
|
|
sprintf('Can not copy file %s to %s', JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')
|
|
|
|
);
|
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':
|
|
|
|
if (!\JFile::write(\JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body))
|
|
|
|
{
|
|
|
|
throw new \RuntimeException(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new));
|
|
|
|
}
|
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':
|
|
|
|
if (!\JFile::delete(\JPath::clean(JPATH_ROOT . '/' . $file->old)))
|
|
|
|
{
|
|
|
|
throw new \RuntimeException(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
|
|
|
|
}
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
break;
|
2011-10-11 16:04:33 +00:00
|
|
|
}
|
2015-02-23 00:04:22 +00:00
|
|
|
}
|
2011-10-15 01:13:59 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
$table = \JTable::getInstance('TestsTable', '\\PatchTester\\Table\\');
|
|
|
|
$table->pull_id = $pull->number;
|
|
|
|
$table->data = json_encode($files);
|
|
|
|
$table->patched_by = \JFactory::getUser()->id;
|
|
|
|
$table->applied = 1;
|
|
|
|
$table->applied_version = JVERSION;
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2015-02-23 00:04:22 +00:00
|
|
|
if (!$table->store())
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2015-02-23 00:04:22 +00:00
|
|
|
throw new \RuntimeException($table->getError());
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
2011-10-15 01:13:59 +00:00
|
|
|
|
|
|
|
return true;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Reverts the specified pull request
|
|
|
|
*
|
|
|
|
* @param integer $id ID of the pull request to Reverts
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*
|
2014-05-03 23:48:08 +00:00
|
|
|
* @since 2.0
|
|
|
|
* @throws \RuntimeException
|
2013-07-13 02:26:21 +00:00
|
|
|
*/
|
2011-10-11 13:02:57 +00:00
|
|
|
public function revert($id)
|
|
|
|
{
|
2014-05-03 23:48:08 +00:00
|
|
|
$table = \JTable::getInstance('TestsTable', '\\PatchTester\\Table\\');
|
2011-10-11 13:02:57 +00:00
|
|
|
$table->load($id);
|
|
|
|
|
2013-07-13 00:31:00 +00:00
|
|
|
// We don't want to restore files from an older version
|
|
|
|
if ($table->applied_version != JVERSION)
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
|
|
|
$table->delete();
|
2012-06-12 20:07:26 +00:00
|
|
|
|
|
|
|
return $this;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$files = json_decode($table->data);
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!$files)
|
|
|
|
{
|
2015-02-23 13:19:38 +00:00
|
|
|
throw new \RuntimeException(sprintf(\JText::_('%s - Error retrieving table data (%s)'), __METHOD__, htmlentities($table->data)));
|
2012-06-12 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 00:31:00 +00:00
|
|
|
jimport('joomla.filesystem.file');
|
|
|
|
|
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':
|
2014-05-03 23:48:08 +00:00
|
|
|
if (!\JFile::copy(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt', JPATH_ROOT . '/' . $file->old))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2014-05-03 23:48:08 +00:00
|
|
|
throw new \RuntimeException(
|
2013-07-13 02:26:21 +00:00
|
|
|
sprintf(
|
2014-05-03 23:48:08 +00:00
|
|
|
\JText::_('Can not copy file %s to %s'),
|
2013-07-13 02:26:21 +00:00
|
|
|
JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt',
|
|
|
|
JPATH_ROOT . '/' . $file->old
|
|
|
|
)
|
|
|
|
);
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2014-05-03 23:48:08 +00:00
|
|
|
if (!\JFile::delete(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2014-05-03 23:48:08 +00:00
|
|
|
throw new \RuntimeException(sprintf(\JText::_('Can not delete the file: %s'), JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
|
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':
|
2014-05-03 23:48:08 +00:00
|
|
|
if (!\JFile::delete(\JPath::clean(JPATH_ROOT . '/' . $file->new)))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2014-05-03 23:48:08 +00:00
|
|
|
throw new \RuntimeException(sprintf(\JText::_('Can not delete the file: %s'), JPATH_ROOT . '/' . $file->new));
|
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;
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
$table->delete();
|
2011-10-11 13:02:57 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|