31
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-06-10 17:22:21 +00:00

Finish updating rate limit checks

This commit is contained in:
Michael Babker 2015-02-22 19:04:22 -05:00
parent 4a555d1b68
commit 003b9c5791
2 changed files with 105 additions and 101 deletions

View File

@ -126,122 +126,123 @@ class PullModel extends \JModelBase
{
// Get the Github object
$github = Helper::initializeGithub();
$rate = $github->authorization->getRateLimit();
// Only act if there are API hits remaining
if ($github->authorization->getRateLimit()->rate->remaining > 0)
// If over the API limit, we can't build this list
if ($rate->resources->core->remaining == 0)
{
$pull = $github->pulls->get($this->getState()->get('github_user'), $this->getState()->get('github_repo'), $id);
throw new \RuntimeException(
\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset))
);
}
if (is_null($pull->head->repo))
$pull = $github->pulls->get($this->getState()->get('github_user'), $this->getState()->get('github_repo'), $id);
if (is_null($pull->head->repo))
{
throw new \RuntimeException(\JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
}
// Set up the JHttp object
$options = new Registry;
$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);
if (!$files)
{
// TODO - Should be a better way to enqueue messages without needing the application...
\JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_FILES_TO_PATCH', 'message'));
return true;
}
foreach ($files as $file)
{
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
{
throw new \RuntimeException(\JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old));
}
// Set up the JHttp object
$options = new Registry;
$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))
if ($file->action == 'added' || $file->action == 'modified')
{
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);
if (!$files)
{
// TODO - Should be a better way to enqueue messages without needing the application...
\JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_FILES_TO_PATCH', 'message'));
return true;
}
foreach ($files as $file)
{
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
// If the backup file already exists, we can't apply the patch
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
{
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old));
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
}
if ($file->action == 'added' || $file->action == 'modified')
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
{
// If the backup file already exists, we can't apply the patch
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
{
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
}
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
{
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old));
}
$url = 'https://raw.github.com/' . urlencode($pull->head->user->login) . '/' . urlencode($pull->head->repo->name) . '/' . urlencode($pull->head->ref) . '/' . $file->new;
$file->body = $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 \RuntimeException(
sprintf('Can not copy file %s to %s', JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')
);
}
throw new \RuntimeException(sprintf(\JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old));
}
switch ($file->action)
{
case 'modified':
case 'added':
if (!\JFile::write(\JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body))
{
throw new \RuntimeException(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new));
}
$url = 'https://raw.github.com/' . urlencode($pull->head->user->login) . '/' . urlencode($pull->head->repo->name) . '/' . urlencode($pull->head->ref) . '/' . $file->new;
break;
case 'deleted':
if (!\JFile::delete(\JPath::clean(JPATH_ROOT . '/' . $file->old)))
{
throw new \RuntimeException(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
}
break;
}
}
$table = \JTable::getInstance('TestsTable', '\\PatchTester\\Table\\');
$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 \RuntimeException($table->getError());
$file->body = $transport->get($url)->body;
}
}
else
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)
{
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_API_LIMIT_ACTION', \JFactory::getDate($this->rate->reset)));
// 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 \RuntimeException(
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 \RuntimeException(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 \RuntimeException(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
}
break;
}
}
$table = \JTable::getInstance('TestsTable', '\\PatchTester\\Table\\');
$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 \RuntimeException($table->getError());
}
return true;

View File

@ -308,11 +308,14 @@ class PullsModel extends \JModelDatabase
{
// Get the Github object
$github = Helper::initializeGithub();
$rate = $github->authorization->getRateLimit();
// If over the API limit, we can't build this list
if ($github->authorization->getRateLimit()->resources->core->remaining == 0)
if ($rate->resources->core->remaining == 0)
{
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($this->rate->reset)));
throw new \RuntimeException(
\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset))
);
}
// Sanity check, ensure there aren't any applied patches