diff --git a/administrator/components/com_patchtester/helpers/patchtester.php b/administrator/components/com_patchtester/helpers/patchtester.php new file mode 100644 index 0000000..62c8589 --- /dev/null +++ b/administrator/components/com_patchtester/helpers/patchtester.php @@ -0,0 +1,51 @@ +get('gh_token', '')) + { + $options->set('gh.token', $params->get('gh_token', '')); + } + // Set the username and password if set in the params + elseif ($params->get('gh_user', '') && $params->get('gh_password')) + { + $options->set('api.username', $params->get('gh_user', '')); + $options->set('api.password', $params->get('gh_password', '')); + } + // Display a message about the lowered API limit without credentials + else + { + JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_CREDENTIALS'), 'notice'); + } + + return new JGithub($options); + } +} diff --git a/administrator/components/com_patchtester/models/pull.php b/administrator/components/com_patchtester/models/pull.php index ae0adcc..2bf6b37 100644 --- a/administrator/components/com_patchtester/models/pull.php +++ b/administrator/components/com_patchtester/models/pull.php @@ -16,20 +16,6 @@ defined('_JEXEC') or die; */ class PatchtesterModelPull extends JModelLegacy { - /** - * @var JHttp - * @since 2.0 - */ - protected $transport; - - /** - * Github object - * - * @var JGithub - * @since 2.0 - */ - protected $github; - /** * Array containing top level non-production folders * @@ -38,64 +24,6 @@ class PatchtesterModelPull extends JModelLegacy */ protected $nonProductionFolders = array('build', 'docs', 'installation', 'tests'); - /** - * Object containing the rate limit data - * - * @var object - * @since 2.0 - */ - protected $rate; - - /** - * Constructor - * - * @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request). - * - * @since 2.0 - * @throws RuntimeException - */ - public function __construct($config = array()) - { - parent::__construct($config); - - // Set up the JHttp object - $options = new JRegistry; - $options->set('userAgent', 'JPatchTester/2.0'); - $options->set('timeout', 120); - - // Make sure we can use the cURL driver - $driver = JHttpFactory::getAvailableDriver($options, 'curl'); - - if (!($driver instanceof JHttpTransportCurl)) - { - throw new RuntimeException('Cannot use the PHP cURL adapter in this environment, cannot use patchtester', 500); - } - - $this->transport = new JHttp($options, $driver); - - // Set up the Github object - $params = JComponentHelper::getParams('com_patchtester'); - - $options = new JRegistry; - - // 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', '')); - } - // Set the username and password if set in the params - elseif ($params->get('gh_user', '') && $params->get('gh_password')) - { - $options->set('api.username', $params->get('gh_user', '')); - $options->set('api.password', $params->get('gh_password', '')); - } - - $this->github = new JGithub($options); - - // Store the rate data for reuse during this request cycle - $this->rate = $this->github->authorization->getRateLimit()->rate; - } - /** * Method to auto-populate the model state. * @@ -212,17 +140,35 @@ class PatchtesterModelPull extends JModelLegacy */ public function apply($id) { + // Get the Github object + $github = PatchtesterHelper::initializeGithub(); + // Only act if there are API hits remaining - if ($this->rate->remaining > 0) + if ($github->authorization->getRateLimit()->rate->remaining > 0) { - $pull = $this->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)) { throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE')); } - $patch = $this->transport->get($pull->diff_url)->body; + // Set up the JHttp object + $options = new JRegistry; + $options->set('userAgent', 'JPatchTester/2.0'); + $options->set('timeout', 120); + + // Make sure we can use the cURL driver + $driver = JHttpFactory::getAvailableDriver($options, 'curl'); + + if (!($driver instanceof JHttpTransportCurl)) + { + throw new RuntimeException('Cannot use the PHP cURL adapter in this environment, cannot use patchtester', 500); + } + + $transport = new JHttp($options, $driver); + + $patch = $transport->get($pull->diff_url)->body; $files = $this->parsePatch($patch); @@ -255,7 +201,7 @@ class PatchtesterModelPull extends JModelLegacy $url = 'https://raw.github.com/' . $pull->head->user->login . '/' . $pull->head->repo->name . '/' . $pull->head->ref . '/' . $file->new; - $file->body = $this->transport->get($url)->body; + $file->body = $transport->get($url)->body; } } diff --git a/administrator/components/com_patchtester/models/pulls.php b/administrator/components/com_patchtester/models/pulls.php index 41a212b..34517a6 100644 --- a/administrator/components/com_patchtester/models/pulls.php +++ b/administrator/components/com_patchtester/models/pulls.php @@ -16,22 +16,6 @@ defined('_JEXEC') or die; */ class PatchtesterModelPulls extends JModelList { - /** - * Github object - * - * @var JGithub - * @since 2.0 - */ - protected $github; - - /** - * Object containing the rate limit data - * - * @var object - * @since 2.0 - */ - protected $rate; - /** * Constructor. * @@ -50,33 +34,6 @@ class PatchtesterModelPulls extends JModelList } parent::__construct($config); - - // Set up the Github object - $params = JComponentHelper::getParams('com_patchtester'); - - $options = new JRegistry; - - // 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', '')); - } - // Set the username and password if set in the params - elseif ($params->get('gh_user', '') && $params->get('gh_password')) - { - $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'); - } - - $this->github = new JGithub($options); - - // Store the rate data for reuse during this request cycle - $this->rate = $this->github->authorization->getRateLimit()->rate; } /** @@ -198,8 +155,11 @@ class PatchtesterModelPulls extends JModelList */ public function requestFromGithub() { + // Get the Github object + $github = PatchtesterHelper::initializeGithub(); + // If over the API limit, we can't build this list - if ($this->rate->remaining > 0) + if ($github->authorization->getRateLimit()->rate->remaining > 0) { // Sanity check, ensure there aren't any applied patches if (count($this->getAppliedPatches()) >= 1) @@ -216,7 +176,7 @@ class PatchtesterModelPulls extends JModelList try { - $items = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page, 100); + $items = $github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page, 100); } catch (DomainException $e) { diff --git a/administrator/components/com_patchtester/patchtester.php b/administrator/components/com_patchtester/patchtester.php index b82a8ed..06be1eb 100644 --- a/administrator/components/com_patchtester/patchtester.php +++ b/administrator/components/com_patchtester/patchtester.php @@ -14,6 +14,8 @@ if (!JFactory::getUser()->authorise('core.manage', 'com_patchtester')) return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); } +JLoader::register('PatchtesterHelper', __DIR__ . '/helpers/patchtester.php'); + $controller = JControllerLegacy::getInstance('PatchTester'); $controller->execute(JFactory::getApplication()->input->getCmd('task')); $controller->redirect(); diff --git a/administrator/components/com_patchtester/patchtester.xml b/administrator/components/com_patchtester/patchtester.xml index 02ec2d5..22c47c1 100644 --- a/administrator/components/com_patchtester/patchtester.xml +++ b/administrator/components/com_patchtester/patchtester.xml @@ -37,6 +37,7 @@ assets backups controllers + helpers install language models