33
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2025-02-02 11:58:26 +00:00

Add an API rate limit check

This commit is contained in:
Michael Babker 2013-07-12 19:31:00 -05:00
parent 7697095b26
commit 4aef2140c5
4 changed files with 173 additions and 135 deletions

View File

@ -13,11 +13,6 @@ class PTGithub extends JGithub
*/
protected $repos;
public static function getInstance(JRegistry $options = null, JGithubHttp $client = null)
{
return new PTGithub($options, $client);
}
public function __get($name)
{
if ($name == 'repos')

View File

@ -20,6 +20,8 @@ COM_PATCHTESTER_REPO_IS_GONE="The patch could not be applied because the reposit
COM_PATCHTESTER_CONFLICT_S="The patch could not be applied because it conflicts with a previously applied patch: %s"
COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S="The file marked for deletion does not exist: %s"
COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S="The file marked for modification does not exist: %s"
COM_PATCHTESTER_API_LIMIT_ACTION="The GitHub API rate limit has been reached for this resource, could not connect to GitHub to perform the requested action. The rate limit will reset at %s"
COM_PATCHTESTER_API_LIMIT_LIST="The GitHub API rate limit has been reached for this resource, could not connect to GitHub for updated data. The rate limit will reset at %s"
COM_PATCHTESTER_OPEN_IN_GITHUB="Open in GitHub"
COM_PATCHTESTER_OPEN_IN_JOOMLACODE="Open in JoomlaCode"

View File

@ -19,6 +19,20 @@ class PatchtesterModelPull extends JModelLegacy
*/
protected $transport;
/**
* Github object
*
* @var PTGithub
*/
protected $github;
/**
* Object containing the rate limit data
*
* @var object
*/
protected $rate;
/**
* Constructor
*
@ -37,6 +51,12 @@ class PatchtesterModelPull extends JModelLegacy
$options->set('timeout', 120);
$this->transport = JHttpFactory::getHttp($options, 'curl');
// Set up the Github object
$this->github = new PTGithub;
// Store the rate data for reuse during this request cycle
$this->rate = $this->github->account->getRateLimit()->rate;
}
/**
@ -116,91 +136,96 @@ class PatchtesterModelPull extends JModelLegacy
public function apply($id)
{
jimport('joomla.filesystem.file');
$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))
// Only act if there are API hits remaining
if ($this->rate->remaining > 0)
{
throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
}
$pull = $this->github->pulls->get($this->getState('github_user'), $this->getState('github_repo'), $id);
$patch = $this->transport->get($pull->diff_url)->body;
$files = $this->parsePatch($patch);
foreach ($files as $file)
{
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
if (is_null($pull->head->repo))
{
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old));
}
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'))
{
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
}
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 = $this->transport->get($url)->body;
}
}
// at this point, we have ensured that we have all the new files and there are no conflicts
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'))
{
throw new Exception(sprintf('Can not copy file %s to %s'
, JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
}
throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
}
switch ($file->action)
$patch = $this->transport->get($pull->diff_url)->body;
$files = $this->parsePatch($patch);
foreach ($files as $file)
{
case 'modified':
case 'added':
if (!JFile::write(JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body))
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 the backup file already exists, we can't apply the patch
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
{
throw new Exception(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new));
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
}
break;
case 'deleted':
if (!JFile::delete(JPATH::clean(JPATH_ROOT . '/' . $file->old)))
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
{
throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old));
}
break;
$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;
}
}
jimport('joomla.filesystem.file');
// At this point, we have ensured that we have all the new files and there are no conflicts
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'))
{
throw new Exception(sprintf('Can not copy file %s to %s'
, JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
}
}
switch ($file->action)
{
case 'modified':
case 'added':
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)))
{
throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
}
break;
}
}
$table = JTable::getInstance('tests', 'PatchTesterTable');
$table->pull_id = $pull->number;
$table->data = json_encode($files);
$table->patched_by = JFactory::getUser()->id;
$table->applied = 1;
$table->applied_version = JVERSION;
if (!$table->store())
{
throw new Exception($table->getError());
}
}
$table->pull_id = $pull->number;
$table->data = json_encode($files);
$table->patched_by = JFactory::getUser()->id;
$table->applied = 1;
$version = new JVersion;
$table->applied_version = $version->getShortVersion();
if (!$table->store())
else
{
throw new Exception($table->getError());
throw new Exception(JText::sprintf('COM_PATCHTESTER_API_LIMIT_ACTION', JFactory::getDate($this->rate->reset)));
}
return true;
@ -208,16 +233,11 @@ class PatchtesterModelPull extends JModelLegacy
public function revert($id)
{
jimport('joomla.filesystem.file');
$table = JTable::getInstance('tests', 'PatchTesterTable');
$table->load($id);
// we don't want to restore files from an older version
$version = new JVersion;
if ($table->applied_version != $version->getShortVersion())
// We don't want to restore files from an older version
if ($table->applied_version != JVERSION)
{
$table->delete();
@ -232,6 +252,8 @@ class PatchtesterModelPull extends JModelLegacy
, __METHOD__, htmlentities($table->data)));
}
jimport('joomla.filesystem.file');
foreach ($files as $file)
{
switch ($file->action)

View File

@ -16,6 +16,19 @@ jimport('joomla.application.component.modellist');
*/
class PatchtesterModelPulls extends JModelList
{
/**
* Github object
*
* @var PTGithub
*/
protected $github;
/**
* Object containing the rate limit data
*
* @var object
*/
protected $rate;
/**
* Constructor.
@ -38,6 +51,18 @@ class PatchtesterModelPulls extends JModelList
}
parent::__construct($config);
// Set up the Github object
$this->github = new PTGithub;
// Store the rate data for reuse during this request cycle
$this->rate = $this->github->account->getRateLimit()->rate;
// Check the API rate limit, display a message if over
if ($this->rate->remaining == 0)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', JFactory::getDate($this->rate->reset)));
}
}
@ -67,25 +92,8 @@ class PatchtesterModelPulls extends JModelList
// List state information.
parent::populateState('number', 'desc');
//-- GitHubs default list limit is 30
$this->state->set('list.limit', 30);
}
/**
* Method to get a store id based on model configuration state.
*
* This is necessary because the model is used by the component and
* different modules that might need different sets of data or different
* ordering requirements.
*
* @param string $id A prefix for the store id.
*
* @return string A store id.
* @since 1.6
*/
protected function getStoreId($id = '')
{
return parent::getStoreId($id);
// GitHub's default list limit is 30
$this->setState('list.limit', 30);
}
public function getAppliedPatches()
@ -102,8 +110,6 @@ class PatchtesterModelPulls extends JModelList
public function getItems()
{
jimport('joomla.client.github');
if ($this->getState('github_user') == '' || $this->getState('github_repo') == '')
{
return array();
@ -118,50 +124,58 @@ class PatchtesterModelPulls extends JModelList
try
{
$github = new JGithub;
$pulls = $github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page);
usort($pulls, array($this, 'sortItems'));
foreach ($pulls as $i => &$pull)
// If over the API limit, we can't build this list
// TODO - Cache the request data in case of API limiting
if ($this->rate->remaining > 0)
{
if ($search && false === strpos($pull->title, $search))
$pulls = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page);
usort($pulls, array($this, 'sortItems'));
foreach ($pulls as $i => &$pull)
{
unset($pulls[$i]);
continue;
}
if ($search && false === strpos($pull->title, $search))
{
unset($pulls[$i]);
continue;
}
if ($searchId && $pull->number != $searchId)
{
unset($pulls[$i]);
continue;
}
if ($searchId && $pull->number != $searchId)
{
unset($pulls[$i]);
continue;
}
// Try to find a joomlacode issue number
$pulls[$i]->joomlacode_issue = 0;
// Try to find a joomlacode issue number
$pulls[$i]->joomlacode_issue = 0;
$matches = array();
$matches = array();
preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches);
if (isset($matches[1]))
{
$pulls[$i]->joomlacode_issue = (int) $matches[1];
}
else
{
preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches);
preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches);
if (isset($matches[1]))
{
preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches);
$pulls[$i]->joomlacode_issue = (int) $matches[1];
}
else
{
preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches);
if (isset($matches[1]))
{
$pulls[$i]->joomlacode_issue = (int) $matches[1];
preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches);
if (isset($matches[1]))
{
$pulls[$i]->joomlacode_issue = (int) $matches[1];
}
}
}
}
}
else
{
$pulls = array();
}
return $pulls;
}
@ -188,8 +202,13 @@ class PatchtesterModelPulls extends JModelList
public function getTotal()
{
return PTGithub::getInstance()
->repos->get('joomla', 'joomla-cms')
->open_issues_count;
if ($this->rate->remaining > 0)
{
return $this->github->repos->get('joomla', 'joomla-cms')->open_issues_count;
}
else
{
return 0;
}
}
}