2011-10-11 13:02:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-07-13 02:26:21 +00:00
|
|
|
* @package PatchTester
|
|
|
|
*
|
2014-01-03 02:48:28 +00:00
|
|
|
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2014 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
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
/**
|
2013-07-13 02:26:21 +00:00
|
|
|
* Methods supporting a list of pull requests.
|
2011-10-11 13:02:57 +00:00
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @package PatchTester
|
|
|
|
* @since 1.0
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
class PatchtesterModelPulls extends JModelList
|
|
|
|
{
|
2013-07-13 00:31:00 +00:00
|
|
|
/**
|
|
|
|
* Github object
|
|
|
|
*
|
2014-05-01 12:47:10 +00:00
|
|
|
* @var JGithub
|
2013-07-13 02:26:21 +00:00
|
|
|
* @since 2.0
|
2013-07-13 00:31:00 +00:00
|
|
|
*/
|
|
|
|
protected $github;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object containing the rate limit data
|
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @var object
|
|
|
|
* @since 2.0
|
2013-07-13 00:31:00 +00:00
|
|
|
*/
|
|
|
|
protected $rate;
|
2011-10-11 13:02:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2012-06-12 18:42:45 +00:00
|
|
|
* @param array $config An optional associative array of configuration settings.
|
|
|
|
*
|
2014-05-01 14:35:26 +00:00
|
|
|
* @see JControllerLegacy
|
2013-07-13 02:26:21 +00:00
|
|
|
* @since 1.0
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
public function __construct($config = array())
|
|
|
|
{
|
2012-06-12 18:42:45 +00:00
|
|
|
if (empty($config['filter_fields']))
|
|
|
|
{
|
2011-10-11 13:02:57 +00:00
|
|
|
$config['filter_fields'] = array(
|
2014-05-01 14:35:26 +00:00
|
|
|
'id', 'title', 'applied'
|
2011-10-11 13:02:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($config);
|
2013-07-13 00:31:00 +00:00
|
|
|
|
|
|
|
// Set up the Github object
|
2013-07-13 01:06:39 +00:00
|
|
|
$params = JComponentHelper::getParams('com_patchtester');
|
|
|
|
|
|
|
|
$options = new JRegistry;
|
|
|
|
|
2014-05-02 20:24:33 +00:00
|
|
|
// If an API token is set in the params, use it for authentication
|
|
|
|
if ($params->get('gh_token', ''))
|
|
|
|
{
|
|
|
|
$options->set('gh.token', $params->get('gh_token', ''));
|
|
|
|
}
|
2013-07-13 01:06:39 +00:00
|
|
|
// Set the username and password if set in the params
|
2014-05-02 20:24:33 +00:00
|
|
|
elseif ($params->get('gh_user', '') && $params->get('gh_password'))
|
2013-07-13 01:06:39 +00:00
|
|
|
{
|
|
|
|
$options->set('api.username', $params->get('gh_user', ''));
|
|
|
|
$options->set('api.password', $params->get('gh_password', ''));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Display a message about the lowered API limit without credentials
|
|
|
|
JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_CREDENTIALS'), 'notice');
|
|
|
|
}
|
|
|
|
|
2014-05-01 12:47:10 +00:00
|
|
|
$this->github = new JGithub($options);
|
2013-07-13 00:31:00 +00:00
|
|
|
|
|
|
|
// Store the rate data for reuse during this request cycle
|
2014-05-01 12:51:01 +00:00
|
|
|
$this->rate = $this->github->authorization->getRateLimit()->rate;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to auto-populate the model state.
|
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @param string $ordering An optional ordering field.
|
|
|
|
* @param string $direction An optional direction (asc|desc).
|
2011-10-11 13:02:57 +00:00
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @note Calling getState() in this method will result in recursion.
|
|
|
|
* @since 1.0
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
protected function populateState($ordering = null, $direction = null)
|
|
|
|
{
|
2011-10-13 17:58:22 +00:00
|
|
|
// Load the filter state.
|
2013-09-29 00:25:15 +00:00
|
|
|
$search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '');
|
2011-10-13 17:58:22 +00:00
|
|
|
$this->setState('filter.search', $search);
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
// Load the parameters.
|
|
|
|
$params = JComponentHelper::getParams('com_patchtester');
|
2011-10-13 17:58:22 +00:00
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
$this->setState('params', $params);
|
|
|
|
$this->setState('github_user', $params->get('org', 'joomla'));
|
|
|
|
$this->setState('github_repo', $params->get('repo', 'joomla-cms'));
|
2011-10-13 17:58:22 +00:00
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
// List state information.
|
2014-05-01 14:35:26 +00:00
|
|
|
parent::populateState('a.pull_id', 'desc');
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a list of applied patches
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2011-10-11 13:02:57 +00:00
|
|
|
public function getAppliedPatches()
|
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
$db = $this->getDbo();
|
2013-07-13 02:26:21 +00:00
|
|
|
|
2014-05-01 14:35:26 +00:00
|
|
|
$db->setQuery(
|
|
|
|
$db->getQuery(true)
|
|
|
|
->select('*')
|
|
|
|
->from($db->quoteName('#__patchtester_tests'))
|
|
|
|
->where($db->quoteName('applied') . ' = 1')
|
|
|
|
);
|
2013-07-13 02:26:21 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$tests = $db->loadObjectList('pull_id');
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
catch (RuntimeException $e)
|
|
|
|
{
|
|
|
|
$this->setError($e->getMessage());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
2014-05-01 14:35:26 +00:00
|
|
|
* Method to get a JDatabaseQuery object for retrieving the data set from a database.
|
2013-07-13 02:26:21 +00:00
|
|
|
*
|
2014-05-01 14:35:26 +00:00
|
|
|
* @return JDatabaseQuery A JDatabaseQuery object to retrieve the data set.
|
2013-07-13 02:26:21 +00:00
|
|
|
*
|
2014-05-01 14:35:26 +00:00
|
|
|
* @since 2.0
|
2013-07-13 02:26:21 +00:00
|
|
|
*/
|
2014-05-01 14:35:26 +00:00
|
|
|
protected function getListQuery()
|
2011-10-11 13:02:57 +00:00
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
// Create a new query object.
|
|
|
|
$db = $this->getDbo();
|
|
|
|
$query = $db->getQuery(true);
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2014-05-01 14:35:26 +00:00
|
|
|
// Select the required fields from the table.
|
|
|
|
$query->select($this->getState('list.select', 'a.*'));
|
|
|
|
$query->from($db->quoteName('#__patchtester_pulls', 'a'));
|
2012-09-09 01:53:44 +00:00
|
|
|
|
2014-05-02 20:24:33 +00:00
|
|
|
// Join the tests table to get applied patches
|
|
|
|
$query->select($db->quoteName('t.id', 'applied'));
|
|
|
|
$query->join('LEFT', $db->quoteName('#__patchtester_tests', 't') . ' ON t.pull_id = a.pull_id');
|
|
|
|
|
2014-05-01 14:35:26 +00:00
|
|
|
// Filter by search
|
|
|
|
$search = $this->getState('filter.search');
|
2013-07-14 19:34:29 +00:00
|
|
|
|
2014-05-01 14:35:26 +00:00
|
|
|
if (!empty($search))
|
|
|
|
{
|
|
|
|
if (stripos($search, 'id:') === 0)
|
2011-10-15 01:13:59 +00:00
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
$query->where($db->quoteName('a.pull_id') . ' = ' . (int) substr($search, 3));
|
2013-07-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
$search = $db->quote('%' . $db->escape($search, true) . '%');
|
|
|
|
$query->where('(' . $db->quoteName('a.title') . ' LIKE ' . $search . ')');
|
2013-07-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2014-05-01 14:35:26 +00:00
|
|
|
// Handle the list ordering.
|
|
|
|
$ordering = $this->getState('list.ordering');
|
|
|
|
$direction = $this->getState('list.direction');
|
|
|
|
|
|
|
|
if (!empty($ordering))
|
|
|
|
{
|
|
|
|
$query->order($db->escape($ordering) . ' ' . $db->escape($direction));
|
2013-07-14 19:34:29 +00:00
|
|
|
}
|
2014-05-01 14:35:26 +00:00
|
|
|
|
|
|
|
return $query;
|
2013-07-14 19:34:29 +00:00
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2013-07-14 19:34:29 +00:00
|
|
|
/**
|
|
|
|
* Method to request new data from GitHub
|
|
|
|
*
|
2014-05-01 14:35:26 +00:00
|
|
|
* @return void
|
2013-07-14 19:34:29 +00:00
|
|
|
*
|
|
|
|
* @since 2.0
|
2014-05-01 14:35:26 +00:00
|
|
|
* @throws RuntimeException
|
2013-07-14 19:34:29 +00:00
|
|
|
*/
|
2014-05-01 14:35:26 +00:00
|
|
|
public function requestFromGithub()
|
2013-07-14 19:34:29 +00:00
|
|
|
{
|
|
|
|
// If over the API limit, we can't build this list
|
|
|
|
if ($this->rate->remaining > 0)
|
|
|
|
{
|
2014-05-02 19:59:17 +00:00
|
|
|
// Sanity check, ensure there aren't any applied patches
|
|
|
|
if (count($this->getAppliedPatches()) >= 1)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(JText::_('COM_PATCHTESTER_ERROR_APPLIED_PATCHES'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$pulls = array();
|
|
|
|
$page = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
$page++;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$items = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page, 100);
|
|
|
|
}
|
|
|
|
catch (DomainException $e)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(JText::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $e->getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = is_array($items) ? count($items) : 0;
|
|
|
|
|
|
|
|
if ($count)
|
|
|
|
{
|
|
|
|
$pulls = array_merge($pulls, $items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ($count);
|
|
|
|
|
|
|
|
// Dump the old data now
|
|
|
|
$this->getDbo()->truncateTable('#__patchtester_pulls');
|
2013-07-14 19:34:29 +00:00
|
|
|
|
2014-05-02 19:59:17 +00:00
|
|
|
foreach ($pulls as &$pull)
|
2013-07-14 19:34:29 +00:00
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
// Build the data object to store in the database
|
|
|
|
$data = new stdClass;
|
|
|
|
$data->pull_id = $pull->number;
|
|
|
|
$data->title = $pull->title;
|
|
|
|
$data->description = $pull->body;
|
|
|
|
$data->pull_url = $pull->html_url;
|
2013-07-14 19:34:29 +00:00
|
|
|
|
|
|
|
// Try to find a Joomlacode issue number
|
|
|
|
$matches = array();
|
|
|
|
|
|
|
|
preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches);
|
|
|
|
|
|
|
|
if (isset($matches[1]))
|
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
$data->joomlacode_id = (int) $matches[1];
|
2013-07-14 19:34:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches);
|
2012-06-12 18:42:45 +00:00
|
|
|
|
|
|
|
if (isset($matches[1]))
|
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches);
|
2012-06-12 18:42:45 +00:00
|
|
|
|
|
|
|
if (isset($matches[1]))
|
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
$data->joomlacode_id = (int) $matches[1];
|
2012-06-12 18:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-14 19:34:29 +00:00
|
|
|
|
2014-05-01 14:35:26 +00:00
|
|
|
try
|
|
|
|
{
|
2014-05-02 19:59:17 +00:00
|
|
|
$this->getDbo()->insertObject('#__patchtester_pulls', $data, 'id');
|
2014-05-01 14:35:26 +00:00
|
|
|
}
|
|
|
|
catch (RuntimeException $e)
|
|
|
|
{
|
|
|
|
throw new RuntimeException(JText::sprintf('COM_PATCHTESTER_ERROR_INSERT_DATABASE', $e->getMessage()));
|
|
|
|
}
|
2013-07-13 00:31:00 +00:00
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
}
|
2013-07-14 19:34:29 +00:00
|
|
|
else
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2014-05-01 14:35:26 +00:00
|
|
|
throw new RuntimeException(JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', JFactory::getDate($this->rate->reset)));
|
2011-10-13 02:13:19 +00:00
|
|
|
}
|
2014-03-22 15:57:30 +00:00
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|