mirror of
https://github.com/joomla-extensions/patchtester.git
synced 2024-12-23 03:19:00 +00:00
Move JGithub and JHttp initialization around
This commit is contained in:
parent
33c1d07b3d
commit
400e931bb7
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* @package PatchTester
|
||||
*
|
||||
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2014 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
/**
|
||||
* Helper class for the patch tester component
|
||||
*
|
||||
* @package PatchTester
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class PatchtesterHelper
|
||||
{
|
||||
/**
|
||||
* Initializes the JGithub object
|
||||
*
|
||||
* @return JGithub
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public static function initializeGithub()
|
||||
{
|
||||
$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', ''));
|
||||
}
|
||||
// Display a message about the lowered API limit without credentials
|
||||
else
|
||||
{
|
||||
JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_CREDENTIALS'), 'notice');
|
||||
}
|
||||
|
||||
return new JGithub($options);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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();
|
||||
|
@ -37,6 +37,7 @@
|
||||
<folder>assets</folder>
|
||||
<folder>backups</folder>
|
||||
<folder>controllers</folder>
|
||||
<folder>helpers</folder>
|
||||
<folder>install</folder>
|
||||
<folder>language</folder>
|
||||
<folder>models</folder>
|
||||
|
Loading…
Reference in New Issue
Block a user