31
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-05-31 21:00:47 +00:00

Formatting and fixes

This commit is contained in:
Nikolai Plath 2012-06-12 13:42:45 -05:00
parent d1a8e14e64
commit edfce5445f
13 changed files with 269 additions and 179 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<access component="com_patchtester"> <access component="com_patchtester">
<section name="component"> <section name="component">
<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" /> <action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC"/>
<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" /> <action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC"/>
</section> </section>
</access> </access>

View File

@ -29,6 +29,6 @@
filter="rules" filter="rules"
validate="rules" validate="rules"
label="JCONFIG_PERMISSIONS_LABEL" label="JCONFIG_PERMISSIONS_LABEL"
section="component" /> section="component"/>
</fieldset> </fieldset>
</config> </config>

View File

@ -19,13 +19,16 @@ class PatchtesterControllerPull extends JController
{ {
public function apply() public function apply()
{ {
try { try
$model = $this->getModel('pull') {
$this->getModel('pull')
->apply(JRequest::getVar('pull_id')); ->apply(JRequest::getVar('pull_id'));
$msg = 'Patch successfully applied'; $msg = 'Patch successfully applied';
$type = 'message'; $type = 'message';
} catch (Exception $e) { }
catch (Exception $e)
{
$msg = $e->getMessage(); $msg = $e->getMessage();
$type = 'error'; $type = 'error';
} }
@ -35,14 +38,20 @@ class PatchtesterControllerPull extends JController
public function revert() public function revert()
{ {
$model = $this->getModel('pull'); try
if ($model->revert(JRequest::getVar('pull_id'))) { {
$this->getModel('pull')
->revert(JRequest::getVar('pull_id'));
$msg = 'Patch successfully reverted'; $msg = 'Patch successfully reverted';
$type = 'message'; $type = 'message';
} else { }
$msg = 'Patch did not revert'; catch (Exception $e)
{
$msg = $e->getMessage() ?: 'Patch did not revert';
$type = 'error'; $type = 'error';
} }
$this->setRedirect(JRoute::_('index.php?option=com_patchtester&view=pulls', false), $msg, $type); $this->setRedirect(JRoute::_('index.php?option=com_patchtester&view=pulls', false), $msg, $type);
} }

View File

@ -14,7 +14,6 @@ defined('_JEXEC') or die;
*/ */
class PatchtesterModelPull extends JModel class PatchtesterModelPull extends JModel
{ {
/** /**
* Method to auto-populate the model state. * Method to auto-populate the model state.
* *
@ -40,11 +39,13 @@ class PatchtesterModelPull extends JModel
$lines = explode("\n", $patch); $lines = explode("\n", $patch);
foreach ($lines AS $line) { foreach ($lines AS $line)
{
switch ($state) switch ($state)
{ {
case 0: case 0:
if (strpos($line, 'diff --git') === 0) { if (strpos($line, 'diff --git') === 0)
{
$state = 1; $state = 1;
} }
$file = new stdClass; $file = new stdClass;
@ -52,27 +53,33 @@ class PatchtesterModelPull extends JModel
break; break;
case 1: case 1:
if (strpos($line, 'index') === 0) { if (strpos($line, 'index') === 0)
{
$file->index = substr($line, 6); $file->index = substr($line, 6);
} }
if (strpos($line, '---') === 0) { if (strpos($line, '---') === 0)
{
$file->old = substr($line, 6); $file->old = substr($line, 6);
} }
if (strpos($line, '+++') === 0) { if (strpos($line, '+++') === 0)
{
$file->new = substr($line, 6); $file->new = substr($line, 6);
} }
if (strpos($line, 'new file mode') === 0) { if (strpos($line, 'new file mode') === 0)
{
$file->action = 'added'; $file->action = 'added';
} }
if (strpos($line, 'deleted file mode') === 0) { if (strpos($line, 'deleted file mode') === 0)
{
$file->action = 'deleted'; $file->action = 'deleted';
} }
if (strpos($line, '@@') === 0) { if (strpos($line, '@@') === 0)
{
$state = 0; $state = 0;
$files[] = $file; $files[] = $file;
} }
@ -84,52 +91,60 @@ class PatchtesterModelPull extends JModel
public function apply($id) public function apply($id)
{ {
jimport('joomla.client.github'); //@todo Use the JCurl class
jimport('joomla.client.curl'); require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/curl.php';
$table = JTable::getInstance('tests', 'PatchTesterTable'); $table = JTable::getInstance('tests', 'PatchTesterTable');
$github = new JGithub; $github = new JGithub;
$pull = $github->pulls->get($this->getState('github_user'), $this->getState('github_repo'), $id); $pull = $github->pulls->get($this->getState('github_user'), $this->getState('github_repo'), $id);
if (is_null($pull->head->repo)) { if (is_null($pull->head->repo))
{
throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE')); throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
} }
$patch = JCurl::getAdapter($pull->diff_url) $patch = PTCurl::getAdapter($pull->diff_url)
->fetch()->body; ->fetch()->body;
$files = $this->parsePatch($patch); $files = $this->parsePatch($patch);
foreach($files AS $file) { foreach ($files as $file)
if ($file->action == 'deleted' && ! file_exists(JPATH_ROOT . '/' . $file->old)) { {
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
{
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old)); throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old));
} }
if ($file->action == 'added' || $file->action == 'modified') { if ($file->action == 'added' || $file->action == 'modified')
{
// if the backup file already exists, we can't apply the patch // if the backup file already exists, we can't apply the patch
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt')) { if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
{
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new)); throw new Exception(sprintf(JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
} }
if ($file->action == 'modified' && ! file_exists(JPATH_ROOT . '/' . $file->old)) { if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
{
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old)); throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old));
} }
$url = 'https://raw.github.com/' . $pull->head->user->login . '/' . $pull->head->repo->name . '/' . $url = 'https://raw.github.com/' . $pull->head->user->login . '/' . $pull->head->repo->name . '/' .
$pull->head->ref . '/' . $file->new; $pull->head->ref . '/' . $file->new;
$file->body = JCurl::getAdapter($url) $file->body = PTCurl::getAdapter($url)
->fetch()->body; ->fetch()->body;
} }
} }
// at this point, we have ensured that we have all the new files and there are no conflicts // at this point, we have ensured that we have all the new files and there are no conflicts
foreach ($files AS $file) foreach ($files as $file)
{ {
// we only create a backup if the file already exists // we only create a backup if the file already exists
if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->new) && $file->action == 'modified')) { if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->new) && $file->action == 'modified'))
if( ! JFile::copy(JPath::clean(JPATH_ROOT . '/' . $file->old), JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')) { {
if (!JFile::copy(JPath::clean(JPATH_ROOT . '/' . $file->old), JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
{
throw new Exception(sprintf('Can not copy file %s to %s' throw new Exception(sprintf('Can not copy file %s to %s'
, JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')); , JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
} }
@ -139,13 +154,15 @@ class PatchtesterModelPull extends JModel
{ {
case 'modified': case 'modified':
case 'added': case 'added':
if( ! JFile::write(JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body)) { if (!JFile::write(JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body))
{
throw new Exception(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new)); throw new Exception(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new));
} }
break; break;
case 'deleted': case 'deleted':
if( ! JFile::delete(JPATH::clean(JPATH_ROOT . '/' . $file->old))) { if (!JFile::delete(JPATH::clean(JPATH_ROOT . '/' . $file->old)))
{
throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old)); throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
} }
break; break;
@ -159,7 +176,8 @@ class PatchtesterModelPull extends JModel
$version = new JVersion; $version = new JVersion;
$table->applied_version = $version->getShortVersion(); $table->applied_version = $version->getShortVersion();
if ( ! $table->store()) { if (!$table->store())
{
throw new Exception($table->getError()); throw new Exception($table->getError());
} }
@ -174,41 +192,64 @@ class PatchtesterModelPull extends JModel
// we don't want to restore files from an older version // we don't want to restore files from an older version
$version = new JVersion; $version = new JVersion;
if ($table->applied_version != $version->getShortVersion()) {
if ($table->applied_version != $version->getShortVersion())
{
/*
$table->applied = 0; $table->applied = 0;
$table->applied_version = ''; $table->applied_version = '';
$table->store(); $table->store();
*/
$table->delete();
return true; return true;
} }
$files = json_decode($table->data); $files = json_decode($table->data);
foreach ($files AS $file) { if (!$files)
switch ($file->action) { {
throw new Exception(sprintf('%s - Error retrieving table data (%s)'
, __METHOD__, htmlentities($table->data)));
}
foreach ($files as $file)
{
switch ($file->action)
{
case 'deleted': case 'deleted':
case 'modified': case 'modified':
if ( ! JFile::copy(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt', JPATH_ROOT . '/' . $file->old)) { if (!JFile::copy(
JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'
, JPATH_ROOT . '/' . $file->old)
)
{
throw new Exception(sprintf('Can not copy file %s to %s' throw new Exception(sprintf('Can not copy file %s to %s'
, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt' , JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'
, JPATH_ROOT . '/' . $file->old)); , JPATH_ROOT . '/' . $file->old));
} }
if ( ! JFile::delete(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')) {
if (!JFile::delete(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
{
throw new Exception(sprintf('Can not delete the file: %s' throw new Exception(sprintf('Can not delete the file: %s'
, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')); , JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
} }
break; break;
case 'added': case 'added':
if ( ! JFile::delete(JPath::clean(JPATH_ROOT . '/' . $file->new))) { if (!JFile::delete(JPath::clean(JPATH_ROOT . '/' . $file->new)))
{
throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->new)); throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->new));
} }
break; break;
} }
} }
/*
$table->applied_version = ''; $table->applied_version = '';
$table->applied = 0; $table->applied = 0;
$table->store(); $table->store();
*/
$table->delete();
return true; return true;
} }

View File

@ -20,13 +20,15 @@ class PatchtesterModelPulls extends JModelList
/** /**
* Constructor. * Constructor.
* *
* @param array An optional associative array of configuration settings. * @param array $config An optional associative array of configuration settings.
*
* @see JController * @see JController
* @since 1.6 * @since 11.1
*/ */
public function __construct($config = array()) public function __construct($config = array())
{ {
if (empty($config['filter_fields'])) { if (empty($config['filter_fields']))
{
$config['filter_fields'] = array( $config['filter_fields'] = array(
'id', 'id', 'id', 'id',
'title', 'title', 'title', 'title',
@ -49,10 +51,10 @@ class PatchtesterModelPulls extends JModelList
protected function populateState($ordering = null, $direction = null) protected function populateState($ordering = null, $direction = null)
{ {
// Load the filter state. // Load the filter state.
$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search'); $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
$this->setState('filter.search', $search); $this->setState('filter.search', $search);
$searchId = $this->getUserStateFromRequest($this->context.'.filter.searchid', 'filter_searchid'); $searchId = $this->getUserStateFromRequest($this->context . '.filter.searchid', 'filter_searchid');
$this->setState('filter.searchid', $searchId); $this->setState('filter.searchid', $searchId);
// Load the parameters. // Load the parameters.
@ -74,6 +76,7 @@ class PatchtesterModelPulls extends JModelList
* ordering requirements. * ordering requirements.
* *
* @param string $id A prefix for the store id. * @param string $id A prefix for the store id.
*
* @return string A store id. * @return string A store id.
* @since 1.6 * @since 1.6
*/ */
@ -86,7 +89,7 @@ class PatchtesterModelPulls extends JModelList
{ {
$query = $this->_db->getQuery(true); $query = $this->_db->getQuery(true);
$query->select('*'); $query->select('*');
$query->from('#__tests'); $query->from('#__patchtester_tests');
$query->where('applied = 1'); $query->where('applied = 1');
$this->_db->setQuery($query); $this->_db->setQuery($query);
@ -98,7 +101,8 @@ class PatchtesterModelPulls extends JModelList
{ {
jimport('joomla.client.github'); jimport('joomla.client.github');
if ($this->getState('github_user') == '' || $this->getState('github_repo') == '') { if ($this->getState('github_user') == '' || $this->getState('github_repo') == '')
{
return array(); return array();
} }
@ -107,28 +111,57 @@ class PatchtesterModelPulls extends JModelList
$search = $this->getState('filter.search'); $search = $this->getState('filter.search');
$searchId = $this->getState('filter.searchid'); $searchId = $this->getState('filter.searchid');
try { try
{
$github = new JGithub(); $github = new JGithub();
$pulls = $github->pulls->getAll($this->getState('github_user'), $this->getState('github_repo')); $pulls = $github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'));
usort($pulls, array($this, 'sortItems')); usort($pulls, array($this, 'sortItems'));
foreach ($pulls AS $i => &$pull) foreach ($pulls as $i => &$pull)
{
if ($search && false === strpos($pull->title, $search))
{ {
if($search && false === strpos($pull->title, $search)) {
unset($pulls[$i]); unset($pulls[$i]);
continue; continue;
} }
if($searchId && $pull->number != $searchId) {
if ($searchId && $pull->number != $searchId)
{
unset($pulls[$i]); unset($pulls[$i]);
continue; continue;
} }
// Try to find a joomlacode issue number
$pulls[$i]->joomlacode_issue = 0;
$matches = array(); $matches = array();
preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches); preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches);
$pull->joomlacode_issue = isset($matches[1]) ? $matches[1] : 0;
if (isset($matches[1]))
{
$pulls[$i]->joomlacode_issue = (int) $matches[1];
}
else
{
preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches);
if (isset($matches[1]))
{
preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches);
if (isset($matches[1]))
{
$pulls[$i]->joomlacode_issue = (int) $matches[1];
}
}
}
} }
return $pulls; return $pulls;
} catch (Exception $e) { }
catch (Exception $e)
{
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return array(); return array();

View File

@ -10,7 +10,8 @@
defined('_JEXEC') or die; defined('_JEXEC') or die;
// Access check. // Access check.
if (!JFactory::getUser()->authorise('core.manage', 'com_patchtester')) { if (!JFactory::getUser()->authorise('core.manage', 'com_patchtester'))
{
return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
} }

View File

@ -6,7 +6,8 @@
<copyright>(C) 2011 Ian MacLennan. All rights reserved. <copyright>(C) 2011 Ian MacLennan. All rights reserved.
</copyright> </copyright>
<license>GNU General Public License version 2 or later; see <license>GNU General Public License version 2 or later; see
LICENSE.txt</license> LICENSE.txt
</license>
<authorEmail>ianlenmac@gmail.com</authorEmail> <authorEmail>ianlenmac@gmail.com</authorEmail>
<authorUrl>http://github.com/ianmacl</authorUrl> <authorUrl>http://github.com/ianmacl</authorUrl>
<version>1.0alpha</version> <version>1.0alpha</version>

View File

@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS `#__tests` ( CREATE TABLE IF NOT EXISTS `#__patchtester_tests` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`pull_id` int(11) NOT NULL, `pull_id` int(11) NOT NULL,
`data` text NOT NULL, `data` longtext NOT NULL,
`patched_by` int(11) NOT NULL, `patched_by` int(11) NOT NULL,
`applied` int(11) NOT NULL, `applied` int(11) NOT NULL,
`applied_version` varchar(25) NOT NULL, `applied_version` varchar(25) NOT NULL,

View File

@ -1 +1 @@
DROP TABLE IF EXISTS `#__tests` DROP TABLE IF EXISTS `#__patchtester_tests`

View File

@ -18,10 +18,10 @@ class PatchtesterTableTests extends JTable
/** /**
* Constructor * Constructor
* *
* @param JDatabase A database connector object * @param JDatabase $db A database connector object
*/ */
public function __construct(&$db) public function __construct(&$db)
{ {
parent::__construct('#__tests', 'id', $db); parent::__construct('#__patchtester_tests', 'id', $db);
} }
} }

View File

@ -30,7 +30,8 @@ class PatchtesterViewPulls extends JView
$this->patches = $this->get('AppliedPatches'); $this->patches = $this->get('AppliedPatches');
// Check for errors. // Check for errors.
if (count($errors = $this->get('Errors'))) { if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode("\n", $errors)); JError::raiseError(500, implode("\n", $errors));
return false; return false;
} }

View File

@ -1,3 +1,4 @@
#!/bin/sh
rm -rf site rm -rf site
rm -rf admin rm -rf admin
rm -rf ../patchtester.tar.bz2 rm -rf ../patchtester.tar.bz2
@ -6,6 +7,7 @@ cp -r ../components/com_patchtester site
rm -rf admin/backups/*.txt rm -rf admin/backups/*.txt
mv admin/patchtester.xml patchtester.xml mv admin/patchtester.xml patchtester.xml
tar jcf ../com_patchtester.tar.bz2 site admin patchtester.xml tar jcf ../com_patchtester.tar.bz2 site admin patchtester.xml
tar zcf ../com_patchtester.tar.gz site admin patchtester.xml
rm -rf github rm -rf github
mkdir github mkdir github
@ -15,5 +17,7 @@ cp ../libraries/joomla/client/curl.php github
cp -r ../libraries/joomla/client/github github cp -r ../libraries/joomla/client/github github
cp github.xml github cp github.xml github
tar jcf ../file_github.tar.bz2 github/* tar jcf ../file_github.tar.bz2 github/*
tar zcf ../file_github.tar.gz github/*
tar jcf ../pkg_patchtester.tar.bz2 pkg_patchtester.xml ../com_patchtester.tar.bz2 ../file_github.tar.bz2 tar jcf ../pkg_patchtester.tar.bz2 pkg_patchtester.xml ../com_patchtester.tar.bz2 ../file_github.tar.bz2
tar zcf ../pkg_patchtester.tar.gz pkg_patchtester_gz.xml ../com_patchtester.tar.gz ../file_github.tar.gz