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

Display GitHub error message when module sync fails #348

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions module/Application/src/Application/Service/RepositoryRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ public function getUserRepositoryMetadata($user, $module)
* @param string $user
* @param array $params
*
* @return RepositoryCollection
* @return RepositoryCollection|bool
*/
public function getUserRepositories($user, $params = [])
{
return $this->githubClient->api('user')->repos($user, $params);
try {
return $this->githubClient->api('user')->repos($user, $params);
} catch (RuntimeException $e) {
return false;
}
}

/**
Expand Down Expand Up @@ -140,10 +144,14 @@ public function getRepositoryFileMetadata($user, $module, $filePath)
*
* @param array $params
*
* @return RepositoryCollection
* @return RepositoryCollection|bool
*/
public function getAuthenticatedUserRepositories($params = [])
{
return $this->githubClient->api('current_user')->repos($params);
try {
return $this->githubClient->api('current_user')->repos($params);
} catch (RuntimeException $e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public function testCanRetrieveUserRepositories()
$this->assertEquals(count($payload), $count);
}

public function testGetUserRepositoriesReturnsFalseOnRemoteFailure()
{
$client = $this->getClientMock(
new Api\User(),
[]
);
$client->expects($this->any())
->method('api')
->willThrowException(new Exception\RuntimeException());

$service = new RepositoryRetriever($client);

$repositories = $service->getUserRepositories('foo');
$this->assertFalse($repositories);
}

public function testCanRetrieveUserRepositoryMetadata()
{
$payload = [
Expand Down Expand Up @@ -202,6 +218,22 @@ public function testCanRetrieveAuthenticatedUserRepositories()
$this->assertEquals(count($payload), $count);
}

public function testGetAuthenticatedUserRepositoriesReturnsFalseOnRemoteFailure()
{
$client = $this->getClientMock(
new Api\CurrentUser(),
[]
);
$client->expects($this->any())
->method('api')
->willThrowException(new Exception\RuntimeException());

$service = new RepositoryRetriever($client);

$repositories = $service->getAuthenticatedUserRepositories();
$this->assertFalse($repositories);
}

public function testRepositoryFileContentFails()
{
$clientMock = $this->getMock('EdpGithub\Client');
Expand Down
12 changes: 9 additions & 3 deletions module/User/view/user/helper/user-organizations.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@

<?php
$jsTemplate = <<<'JAVASCRIPT'
$('#org-%s').on("show.bs.collapse", function () {
$('#org-content-%s').load("%s");
$('#org-%1$s').on("show.bs.collapse", function () {
$.ajax({
url: "%2$s",
}).done(function(data) {
$('#org-content-%1$s').html(data);
}).fail(function(xhr) {
$('#org-content-%1$s').html(xhr.responseText);
});
});
JAVASCRIPT;

$this->inlineScript()->appendScript(sprintf($jsTemplate, $org->login, $org->login, $this->url('zf-module/list', ['owner' => $org->login])));
$this->inlineScript()->appendScript(sprintf($jsTemplate, $org->login, $this->url('zf-module/list', ['owner' => $org->login])));
?>
<?php endforeach; ?>
</div>
11 changes: 10 additions & 1 deletion module/User/view/zfc-user/user/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@
</div>
</div>
</div>
<?php $this->inlineScript()->appendScript('$("#repositories").load("' . $this->escapeJs($this->url('zf-module')) . '");'); ?>
<?php $this->inlineScript()->appendScript(<<<EOB
$.ajax({
url: "{$this->escapeJs($this->url('zf-module'))}",
}).done(function(data) {
$('#repositories').html(data);
}).fail(function(xhr) {
$('#repositories').html(xhr.responseText);
});
EOB
);
28 changes: 21 additions & 7 deletions module/ZfModule/src/ZfModule/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,24 @@ public function indexAction()
return $this->redirect()->toRoute('zfcuser/login');
}

$viewModel = new ViewModel();
$viewModel->setTerminal(true);

$currentUserRepositories = $this->repositoryRetriever->getAuthenticatedUserRepositories([
'type' => 'all',
'per_page' => 100,
'sort' => 'updated',
'direction' => 'desc',
]);
if ($currentUserRepositories === false) {
$this->getResponse()->setStatusCode(503);
$viewModel->setVariable('errorMessage', 'module_fetch_failed');

$repositories = $this->unregisteredRepositories($currentUserRepositories);
return $viewModel;
}

$viewModel = new ViewModel(['repositories' => $repositories]);
$viewModel->setTerminal(true);
$repositories = $this->unregisteredRepositories($currentUserRepositories);
$viewModel->setVariable('repositories', $repositories);

return $viewModel;
}
Expand All @@ -106,17 +113,24 @@ public function listAction()

$owner = $this->params()->fromRoute('owner', null);

$viewModel = new ViewModel();
$viewModel->setTerminal(true);
$viewModel->setTemplate('zf-module/module/index.phtml');

$userRepositories = $this->repositoryRetriever->getUserRepositories($owner, [
'per_page' => 100,
'sort' => 'updated',
'direction' => 'desc',
]);
if ($userRepositories === false) {
$this->getResponse()->setStatusCode(503);
$viewModel->setVariable('errorMessage', 'module_fetch_failed');

$repositories = $this->unregisteredRepositories($userRepositories);
return $viewModel;
}

$viewModel = new ViewModel(['repositories' => $repositories]);
$viewModel->setTerminal(true);
$viewModel->setTemplate('zf-module/module/index.phtml');
$repositories = $this->unregisteredRepositories($userRepositories);
$viewModel->setVariable('repositories', $repositories);

return $viewModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,46 @@ public function testIndexActionRendersUnregisteredModulesOnly()
$this->assertSame($unregisteredModule, $viewVariable[0]);
}

public function testIndexActionRendersErrorMessageWhenModuleFetchReturnsFalse()
{
$this->authenticatedAs(new User());

$repositoryRetriever = $this->getMockBuilder(RepositoryRetriever::class)
->disableOriginalConstructor()
->getMock()
;

$repositoryRetriever
->expects($this->once())
->method('getAuthenticatedUserRepositories')
->willReturn(false)
;

$this->getApplicationServiceLocator()
->setAllowOverride(true)
->setService(
RepositoryRetriever::class,
$repositoryRetriever
)
;

$this->dispatch('/module');

$this->assertMatchedRouteName('zf-module');

$this->assertControllerName(Controller\ModuleController::class);
$this->assertActionName('index');
$this->assertResponseStatusCode(Http\Response::STATUS_CODE_503);

$viewModel = $this->getApplication()->getMvcEvent()->getViewModel();

$this->assertTrue($viewModel->terminate());
$this->assertSame('zf-module/module/index', $viewModel->getTemplate());

$this->assertEquals(null, $viewModel->getVariable('repositories'));
$this->assertEquals('module_fetch_failed', $viewModel->getVariable('errorMessage'));
}

public function testListActionRedirectsIfNotAuthenticated()
{
$this->notAuthenticated();
Expand Down Expand Up @@ -440,6 +480,53 @@ public function testListActionRendersUnregisteredModulesOnly()
$this->assertSame($unregisteredModule, $viewVariable[0]);
}

public function testListActionRendersErrorMessageWhenModuleFetchReturnsFalse()
{
$this->authenticatedAs(new User());

$repositoryRetriever = $this->getMockBuilder(RepositoryRetriever::class)
->disableOriginalConstructor()
->getMock()
;

$vendor = 'suzie';

$repositoryRetriever
->expects($this->once())
->method('getUserRepositories')
->willReturn(false)
;

$this->getApplicationServiceLocator()
->setAllowOverride(true)
->setService(
RepositoryRetriever::class,
$repositoryRetriever
)
;

$url = sprintf(
'/module/list/%s',
$vendor
);

$this->dispatch($url);

$this->assertMatchedRouteName('zf-module/list');

$this->assertControllerName(Controller\ModuleController::class);
$this->assertActionName('list');
$this->assertResponseStatusCode(Http\Response::STATUS_CODE_503);

$viewModel = $this->getApplication()->getMvcEvent()->getViewModel();

$this->assertTrue($viewModel->terminate());
$this->assertSame('zf-module/module/index.phtml', $viewModel->getTemplate());

$this->assertEquals(null, $viewModel->getVariable('repositories'));
$this->assertEquals('module_fetch_failed', $viewModel->getVariable('errorMessage'));
}

public function testAddActionRedirectsIfNotAuthenticated()
{
$this->notAuthenticated();
Expand Down
4 changes: 3 additions & 1 deletion module/ZfModule/view/zf-module/module/index.phtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php /* @var stdClass[] $repositories */ ?>
<?php if (!count($repositories)): ?>
<?php if (isset($errorMessage)): ?>
<div class="alert alert-block alert-danger">An error occurred while fetching modules</div>
<?php elseif (!count($repositories)): ?>
<div class="alert alert-block">No modules found</div>
<?php else: ?>
<?php foreach ($repositories as $repository): ?>
Expand Down