Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Fix refreshing updated modules #163

Merged
merged 3 commits into from
Nov 12, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions module/ZfModule/src/ZfModule/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public function indexAction()
$sl = $this->getServiceLocator();
$client = $sl->get('EdpGithub\Client');

$repos = $client->api('current_user')->repos(array('type' =>'all', 'per_page' => 100));
$params = array(
'type' => 'all',
'per_page' => 100,
'sort' => 'updated',
'direction' => 'desc',
);

$repos = $client->api('current_user')->repos($params);

$identity = $this->zfcUserAuthentication()->getIdentity();
$cacheKey = 'modules-user-' . $identity->getId();
Expand All @@ -104,7 +111,13 @@ public function organizationAction()
$client = $sl->get('EdpGithub\Client');

$owner = $this->params()->fromRoute('owner', null);
$repos = $client->api('user')->repos($owner);
$params = array(
'type' => 'all',
'per_page' => 100,
'sort' => 'updated',
'direction' => 'desc',
);
$repos = $client->api('user')->repos($owner, $params);

$identity = $this->zfcUserAuthentication()->getIdentity();
$cacheKey = 'modules-organization-' . $identity->getId() . '-' . $owner;
Expand All @@ -116,43 +129,40 @@ public function organizationAction()
return $viewModel;
}

public function fetchModules($repos, $cacheKey)
public function fetchModules ($repos, $cacheKey)
{
$cacheKey .= '-github';
$sl = $this->getServiceLocator();
$mapper = $sl->get('zfmodule_mapper_module');
/* @var $client \EdpGithub\Client */
$client = $sl->get('EdpGithub\Client');
/* @var $cache StorageInterface */
$cache = $sl->get('zfmodule_cache');

$repositories = array();

foreach($repos as $key => $repo) {
foreach ($repos as $repo) {
$isModule = $this->getModuleService()->isModule($repo);
//Verify if repos have been modified
$httpClient = $client->getHttpClient();
/* @var $response \Zend\Http\Response */
$response = $httpClient->getResponse();
if($response->getStatusCode() == 304) {
if($cache->hasItem($cacheKey . '-github')) {
$repositories = $cache->getItem($cacheKey . '-github');
break;
}
} elseif ($cache->hasItem($cacheKey . '-github')) {
$cache->removeItem($cacheKey . '-github');
}

if($repo->fork || !$repo->permissions->push || !$this->getModuleService()->isModule($repo) ) {
continue;
}
$hasCache = $cache->hasItem($cacheKey);

$module = $mapper->findByName($repo->name);
if($module) {
continue;
if ($response->getStatusCode() == 304 && $hasCache) {
$repositories = $cache->getItem($cacheKey);
break;
}

$repositories[] = $repo;
if (!$repo->fork && $repo->permissions->push && $isModule && !$mapper->findByName($repo->name)) {
$repositories[] = $repo;
$cache->removeItem($cacheKey);
}
}

//save list of modules to cache
$cache->setItem($cacheKey . '-github', $repositories);
$cache->setItem($cacheKey, $repositories);

return $repositories;
}
Expand Down