Skip to content

Commit 542e5f2

Browse files
author
Michael Babker
committed
On page 1 of a request look for a pagination header
1 parent 0056aa2 commit 542e5f2

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

administrator/components/com_patchtester/PatchTester/Controller/FetchController.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ public function execute()
3333
$this->getApplication()->setHeader('Pragma', 'no-cache');
3434
$this->getApplication()->setHeader('Content-Type', $this->getApplication()->mimeType . '; charset=' . $this->getApplication()->charSet);
3535

36+
$session = \JFactory::getSession();
37+
3638
try
3739
{
3840
// Fetch our page from the session
39-
$page = \JFactory::getSession()->get('com_patchtester_fetcher_page', 1);
41+
$page = $session->get('com_patchtester_fetcher_page', 1);
4042

4143
$model = new PullsModel('com_patchtester.fetch', null, \JFactory::getDbo());
4244

@@ -55,18 +57,37 @@ public function execute()
5557
$this->getApplication()->close(1);
5658
}
5759

58-
// Update the UI and session now
59-
if (isset($status['page']))
60+
// Store the last page to the session if given one
61+
if (isset($status['lastPage']) && $status['lastPage'] !== false)
6062
{
61-
\JFactory::getSession()->set('com_patchtester_fetcher_page', $status['page']);
62-
$message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', $status['page']);
63-
unset($status['page']);
63+
$session->set('com_patchtester_fetcher_last_page', $status['lastPage']);
6464
}
65-
else
65+
66+
// Update the UI and session now
67+
if ($status['complete'] || $page === $session->get('com_patchtester_fetcher_last_page', false))
6668
{
67-
$status['header'] = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL', true);
69+
$status['complete'] = true;
70+
$status['header'] = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL', true);
71+
6872
$message = \JText::_('COM_PATCHTESTER_FETCH_COMPLETE_CLOSE_WINDOW', true);
6973
}
74+
elseif (isset($status['page']))
75+
{
76+
$session->set('com_patchtester_fetcher_page', $status['page']);
77+
78+
if ($session->has('com_patchtester_fetcher_last_page'))
79+
{
80+
$message = \JText::sprintf(
81+
'COM_PATCHTESTER_FETCH_PAGE_NUMBER_OF_TOTAL', $status['page'], $session->get('com_patchtester_fetcher_last_page')
82+
);
83+
}
84+
else
85+
{
86+
$message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', $status['page']);
87+
}
88+
89+
unset($status['page']);
90+
}
7091

7192
$response = new \JResponseJson($status, $message, false, true);
7293

administrator/components/com_patchtester/PatchTester/Model/PullsModel.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ public function requestFromGithub($page)
293293
try
294294
{
295295
// TODO - Option to configure the batch size
296+
$batchSize = 100;
297+
296298
$pullsResponse = Helper::initializeGithub()->getOpenIssues(
297-
$this->getState()->get('github_user'), $this->getState()->get('github_repo'), $page, 100
299+
$this->getState()->get('github_user'), $this->getState()->get('github_repo'), $page, $batchSize
298300
);
299301

300302
$pulls = json_decode($pullsResponse->body);
@@ -304,6 +306,26 @@ public function requestFromGithub($page)
304306
throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $e->getMessage()), $e->getCode(), $e);
305307
}
306308

309+
// If this is page 1, let's check to see if we need to paginate
310+
if ($page === 1)
311+
{
312+
// Default this to being a single page of results
313+
$lastPage = 1;
314+
315+
if (isset($pullsResponse->headers['Link']))
316+
{
317+
preg_match('/(\?page=[0-9]&per_page=' . $batchSize . '+>; rel=\"last\")/', $pullsResponse->headers['Link'], $matches);
318+
319+
if ($matches && isset($matches[0]))
320+
{
321+
$pageSegment = str_replace('&per_page=' . $batchSize, '', $matches[0]);
322+
323+
preg_match('/\d+/', $pageSegment, $pages);
324+
$lastPage = (int) $pages[0];
325+
}
326+
}
327+
}
328+
307329
$count = is_array($pulls) ? count($pulls) : 0;
308330

309331
// If there are no pulls to insert then bail, assume we're finished
@@ -361,7 +383,7 @@ public function requestFromGithub($page)
361383
}
362384

363385
// Need to make another request
364-
return array('complete' => false, 'page' => ($page + 1));
386+
return array('complete' => false, 'page' => ($page + 1), 'lastPage' => isset($lastPage) ? $lastPage : false);
365387
}
366388

367389
/**

administrator/components/com_patchtester/language/en-GB/en-GB.com_patchtester.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ COM_PATCHTESTER_FETCH_COMPLETE_CLOSE_WINDOW="All data has been retrieved. Please
3030
COM_PATCHTESTER_FETCH_INITIALIZING="Preparing to fetch GitHub data"
3131
COM_PATCHTESTER_FETCH_INITIALIZING_DESCRIPTION="Making sure all is well to fetch data. Sit tight."
3232
COM_PATCHTESTER_FETCH_PAGE_NUMBER="Processing page %s of GitHub data"
33+
COM_PATCHTESTER_FETCH_PAGE_NUMBER_OF_TOTAL="Processing page %1$s of %2$s pages of GitHub data"
3334
COM_PATCHTESTER_FETCH_PROCESSING="Processing data from GitHub"
3435
COM_PATCHTESTER_FETCH_SUCCESSFUL="Successfully retrieved pull requests"
3536
COM_PATCHTESTER_FIELD_GH_AUTH_DESC="Select 'Credentials' to use authentication through your GitHub Username and Password, or 'Token' for a GitHub API Token"

0 commit comments

Comments
 (0)