33
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-12-22 10:58:58 +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

@ -19,13 +19,16 @@ class PatchtesterControllerPull extends JController
{
public function apply()
{
try {
$model = $this->getModel('pull')
try
{
$this->getModel('pull')
->apply(JRequest::getVar('pull_id'));
$msg = 'Patch successfully applied';
$type = 'message';
} catch (Exception $e) {
}
catch (Exception $e)
{
$msg = $e->getMessage();
$type = 'error';
}
@ -35,14 +38,20 @@ class PatchtesterControllerPull extends JController
public function revert()
{
$model = $this->getModel('pull');
if ($model->revert(JRequest::getVar('pull_id'))) {
try
{
$this->getModel('pull')
->revert(JRequest::getVar('pull_id'));
$msg = 'Patch successfully reverted';
$type = 'message';
} else {
$msg = 'Patch did not revert';
}
catch (Exception $e)
{
$msg = $e->getMessage() ?: 'Patch did not revert';
$type = 'error';
}
$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
{
/**
* Method to auto-populate the model state.
*
@ -40,11 +39,13 @@ class PatchtesterModelPull extends JModel
$lines = explode("\n", $patch);
foreach ($lines AS $line) {
foreach ($lines AS $line)
{
switch ($state)
{
case 0:
if (strpos($line, 'diff --git') === 0) {
if (strpos($line, 'diff --git') === 0)
{
$state = 1;
}
$file = new stdClass;
@ -52,27 +53,33 @@ class PatchtesterModelPull extends JModel
break;
case 1:
if (strpos($line, 'index') === 0) {
if (strpos($line, 'index') === 0)
{
$file->index = substr($line, 6);
}
if (strpos($line, '---') === 0) {
if (strpos($line, '---') === 0)
{
$file->old = substr($line, 6);
}
if (strpos($line, '+++') === 0) {
if (strpos($line, '+++') === 0)
{
$file->new = substr($line, 6);
}
if (strpos($line, 'new file mode') === 0) {
if (strpos($line, 'new file mode') === 0)
{
$file->action = 'added';
}
if (strpos($line, 'deleted file mode') === 0) {
if (strpos($line, 'deleted file mode') === 0)
{
$file->action = 'deleted';
}
if (strpos($line, '@@') === 0) {
if (strpos($line, '@@') === 0)
{
$state = 0;
$files[] = $file;
}
@ -84,52 +91,60 @@ class PatchtesterModelPull extends JModel
public function apply($id)
{
jimport('joomla.client.github');
jimport('joomla.client.curl');
//@todo Use the JCurl class
require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/curl.php';
$table = JTable::getInstance('tests', 'PatchTesterTable');
$github = new JGithub;
$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'));
}
$patch = JCurl::getAdapter($pull->diff_url)
$patch = PTCurl::getAdapter($pull->diff_url)
->fetch()->body;
$files = $this->parsePatch($patch);
foreach($files AS $file) {
if ($file->action == 'deleted' && ! file_exists(JPATH_ROOT . '/' . $file->old)) {
foreach ($files as $file)
{
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));
}
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 (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));
}
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));
}
$url = 'https://raw.github.com/' . $pull->head->user->login . '/' . $pull->head->repo->name . '/' .
$pull->head->ref . '/' . $file->new;
$file->body = JCurl::getAdapter($url)
$file->body = PTCurl::getAdapter($url)
->fetch()->body;
}
}
// 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
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 ($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'))
{
throw new Exception(sprintf('Can not copy file %s to %s'
, JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
}
@ -139,13 +154,15 @@ class PatchtesterModelPull extends JModel
{
case 'modified':
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));
}
break;
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));
}
break;
@ -159,7 +176,8 @@ class PatchtesterModelPull extends JModel
$version = new JVersion;
$table->applied_version = $version->getShortVersion();
if ( ! $table->store()) {
if (!$table->store())
{
throw new Exception($table->getError());
}
@ -174,41 +192,64 @@ class PatchtesterModelPull extends JModel
// we don't want to restore files from an older version
$version = new JVersion;
if ($table->applied_version != $version->getShortVersion()) {
if ($table->applied_version != $version->getShortVersion())
{
/*
$table->applied = 0;
$table->applied_version = '';
$table->store();
*/
$table->delete();
return true;
}
$files = json_decode($table->data);
foreach ($files AS $file) {
switch ($file->action) {
if (!$files)
{
throw new Exception(sprintf('%s - Error retrieving table data (%s)'
, __METHOD__, htmlentities($table->data)));
}
foreach ($files as $file)
{
switch ($file->action)
{
case 'deleted':
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'
, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'
, 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'
, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
}
break;
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));
}
break;
}
}
/*
$table->applied_version = '';
$table->applied = 0;
$table->store();
*/
$table->delete();
return true;
}

View File

@ -20,13 +20,15 @@ class PatchtesterModelPulls extends JModelList
/**
* Constructor.
*
* @param array An optional associative array of configuration settings.
* @param array $config An optional associative array of configuration settings.
*
* @see JController
* @since 1.6
* @since 11.1
*/
public function __construct($config = array())
{
if (empty($config['filter_fields'])) {
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'id', 'id',
'title', 'title',
@ -74,6 +76,7 @@ class PatchtesterModelPulls extends JModelList
* ordering requirements.
*
* @param string $id A prefix for the store id.
*
* @return string A store id.
* @since 1.6
*/
@ -86,7 +89,7 @@ class PatchtesterModelPulls extends JModelList
{
$query = $this->_db->getQuery(true);
$query->select('*');
$query->from('#__tests');
$query->from('#__patchtester_tests');
$query->where('applied = 1');
$this->_db->setQuery($query);
@ -98,7 +101,8 @@ class PatchtesterModelPulls extends JModelList
{
jimport('joomla.client.github');
if ($this->getState('github_user') == '' || $this->getState('github_repo') == '') {
if ($this->getState('github_user') == '' || $this->getState('github_repo') == '')
{
return array();
}
@ -107,28 +111,57 @@ class PatchtesterModelPulls extends JModelList
$search = $this->getState('filter.search');
$searchId = $this->getState('filter.searchid');
try {
try
{
$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'));
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]);
continue;
}
if($searchId && $pull->number != $searchId) {
if ($searchId && $pull->number != $searchId)
{
unset($pulls[$i]);
continue;
}
// Try to find a joomlacode issue number
$pulls[$i]->joomlacode_issue = 0;
$matches = array();
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;
} catch (Exception $e) {
}
catch (Exception $e)
{
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return array();

View File

@ -10,7 +10,8 @@
defined('_JEXEC') or die;
// 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'));
}

View File

@ -6,7 +6,8 @@
<copyright>(C) 2011 Ian MacLennan. All rights reserved.
</copyright>
<license>GNU General Public License version 2 or later; see
LICENSE.txt</license>
LICENSE.txt
</license>
<authorEmail>ianlenmac@gmail.com</authorEmail>
<authorUrl>http://github.com/ianmacl</authorUrl>
<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,
`pull_id` int(11) NOT NULL,
`data` text NOT NULL,
`data` longtext NOT NULL,
`patched_by` int(11) NOT NULL,
`applied` int(11) 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
*
* @param JDatabase A database connector object
* @param JDatabase $db A database connector object
*/
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');
// Check for errors.
if (count($errors = $this->get('Errors'))) {
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode("\n", $errors));
return false;
}

View File

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