31
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-06-06 23:40:49 +00:00

On page 1 of a request look for a pagination header

This commit is contained in:
Michael Babker 2016-06-25 11:54:03 -05:00
parent 0056aa216b
commit 542e5f2984
3 changed files with 54 additions and 10 deletions

View File

@ -33,10 +33,12 @@ class FetchController extends AbstractController
$this->getApplication()->setHeader('Pragma', 'no-cache');
$this->getApplication()->setHeader('Content-Type', $this->getApplication()->mimeType . '; charset=' . $this->getApplication()->charSet);
$session = \JFactory::getSession();
try
{
// Fetch our page from the session
$page = \JFactory::getSession()->get('com_patchtester_fetcher_page', 1);
$page = $session->get('com_patchtester_fetcher_page', 1);
$model = new PullsModel('com_patchtester.fetch', null, \JFactory::getDbo());
@ -55,18 +57,37 @@ class FetchController extends AbstractController
$this->getApplication()->close(1);
}
// Update the UI and session now
if (isset($status['page']))
// Store the last page to the session if given one
if (isset($status['lastPage']) && $status['lastPage'] !== false)
{
\JFactory::getSession()->set('com_patchtester_fetcher_page', $status['page']);
$message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', $status['page']);
unset($status['page']);
$session->set('com_patchtester_fetcher_last_page', $status['lastPage']);
}
else
// Update the UI and session now
if ($status['complete'] || $page === $session->get('com_patchtester_fetcher_last_page', false))
{
$status['header'] = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL', true);
$status['complete'] = true;
$status['header'] = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL', true);
$message = \JText::_('COM_PATCHTESTER_FETCH_COMPLETE_CLOSE_WINDOW', true);
}
elseif (isset($status['page']))
{
$session->set('com_patchtester_fetcher_page', $status['page']);
if ($session->has('com_patchtester_fetcher_last_page'))
{
$message = \JText::sprintf(
'COM_PATCHTESTER_FETCH_PAGE_NUMBER_OF_TOTAL', $status['page'], $session->get('com_patchtester_fetcher_last_page')
);
}
else
{
$message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', $status['page']);
}
unset($status['page']);
}
$response = new \JResponseJson($status, $message, false, true);

View File

@ -293,8 +293,10 @@ class PullsModel extends \JModelDatabase
try
{
// TODO - Option to configure the batch size
$batchSize = 100;
$pullsResponse = Helper::initializeGithub()->getOpenIssues(
$this->getState()->get('github_user'), $this->getState()->get('github_repo'), $page, 100
$this->getState()->get('github_user'), $this->getState()->get('github_repo'), $page, $batchSize
);
$pulls = json_decode($pullsResponse->body);
@ -304,6 +306,26 @@ class PullsModel extends \JModelDatabase
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $e->getMessage()), $e->getCode(), $e);
}
// If this is page 1, let's check to see if we need to paginate
if ($page === 1)
{
// Default this to being a single page of results
$lastPage = 1;
if (isset($pullsResponse->headers['Link']))
{
preg_match('/(\?page=[0-9]&per_page=' . $batchSize . '+>; rel=\"last\")/', $pullsResponse->headers['Link'], $matches);
if ($matches && isset($matches[0]))
{
$pageSegment = str_replace('&per_page=' . $batchSize, '', $matches[0]);
preg_match('/\d+/', $pageSegment, $pages);
$lastPage = (int) $pages[0];
}
}
}
$count = is_array($pulls) ? count($pulls) : 0;
// If there are no pulls to insert then bail, assume we're finished
@ -361,7 +383,7 @@ class PullsModel extends \JModelDatabase
}
// Need to make another request
return array('complete' => false, 'page' => ($page + 1));
return array('complete' => false, 'page' => ($page + 1), 'lastPage' => isset($lastPage) ? $lastPage : false);
}
/**

View File

@ -30,6 +30,7 @@ COM_PATCHTESTER_FETCH_COMPLETE_CLOSE_WINDOW="All data has been retrieved. Please
COM_PATCHTESTER_FETCH_INITIALIZING="Preparing to fetch GitHub data"
COM_PATCHTESTER_FETCH_INITIALIZING_DESCRIPTION="Making sure all is well to fetch data. Sit tight."
COM_PATCHTESTER_FETCH_PAGE_NUMBER="Processing page %s of GitHub data"
COM_PATCHTESTER_FETCH_PAGE_NUMBER_OF_TOTAL="Processing page %1$s of %2$s pages of GitHub data"
COM_PATCHTESTER_FETCH_PROCESSING="Processing data from GitHub"
COM_PATCHTESTER_FETCH_SUCCESSFUL="Successfully retrieved pull requests"
COM_PATCHTESTER_FIELD_GH_AUTH_DESC="Select 'Credentials' to use authentication through your GitHub Username and Password, or 'Token' for a GitHub API Token"