Skip to content

Commit

Permalink
Move GitHub API failure management to Github\Request
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jul 13, 2010
1 parent d5503f6 commit df35979
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/Application/S2bBundle/Command/S2bPopulateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$github = new \phpGitHubApi();
$github->setRequest(new Github\Request());
$githubSearch = new Github\Search($github, new \Goutte\Client(), $output);
$githubUser = new Github\User($github, $output);
$githubRepo = new Github\Repo($github, $output);
Expand Down Expand Up @@ -70,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if(isset($repos[$repo->getFullName()])) {
continue;
}
$output->write(sprintf('Discover %s:', $repo->getName()));
$output->write(sprintf('Discover %s:', $repo->getFullName()));
if(!$githubRepo->validateFiles($repo)) {
$output->writeLn(' IGNORED');
continue;
Expand Down
4 changes: 3 additions & 1 deletion src/Application/S2bBundle/Controller/RepoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ protected function addRepo($username, $name)
if($repo) {
return $repo;
}
$githubRepo = new Github\Repo(new \phpGithubApi(), new Output());
$github = new \phpGithubApi();
$github->setRequest(new Github\Request());
$githubRepo = new Github\Repo($github, new Output());

if(!$repo = $githubRepo->update(Repo::create($username.'/'.$name))) {
return false;
Expand Down
24 changes: 6 additions & 18 deletions src/Application/S2bBundle/Github/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public function updateInfos(Entities\Repo $repo)
if(404 == $e->getCode()) {
return false;
}
$this->output->write(sprintf('{%s}', $e->getCode()));
sleep(5);
return $this->updateInfos($repo);
throw $e;
}

$repo->setDescription($data['description']);
Expand All @@ -86,9 +84,7 @@ public function updateCommits(Entities\Repo $repo)
if(404 == $e->getCode()) {
return false;
}
$this->output->write(sprintf('{%s}', $e->getCode()));
sleep(5);
return $this->updateCommits($repo);
throw $e;
}
$repo->setLastCommits(array_slice($commits, 0, 10));

Expand All @@ -105,9 +101,7 @@ public function updateFiles(Entities\Repo $repo)
if(404 == $e->getCode()) {
return false;
}
$this->output->write(sprintf('{%s}', $e->getCode()));
sleep(5);
return $this->updateFiles($repo);
throw $e;
}
if($repo instanceof Entities\Project && !isset($blobs['src/autoload.php'])) {
return false;
Expand Down Expand Up @@ -141,9 +135,7 @@ public function validateFiles(Entities\Repo $repo)
if(404 == $e->getCode()) {
return false;
}
$this->output->write(sprintf('{%s}', $e->getCode()));
sleep(5);
return $this->validateRepoFiles($repo);
throw $e;
}

return isset($blobs['src/autoload.php']);
Expand All @@ -159,9 +151,7 @@ public function updateTags(Entities\Repo $repo)
if(404 == $e->getCode()) {
return false;
}
$this->output->write(sprintf('{%s}', $e->getCode()));
sleep(5);
return $this->updateTags($repo);
throw $e;
}
$repo->setTags(array_keys($tags));

Expand All @@ -177,9 +167,7 @@ public function getContributorNames(Entities\Repo $repo)
if(404 == $e->getCode()) {
return array();
}
$this->output->write(sprintf('{%s}', $e->getCode()));
sleep(5);
return $this->getContributorNames($repo);
throw $e;
}
$names = array();
foreach($contributors as $contributor) {
Expand Down
40 changes: 40 additions & 0 deletions src/Application/S2bBundle/Github/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Application\S2bBundle\Github;

require_once __DIR__.'/../../../vendor/php-github-api/lib/request/phpGitHubApiRequest.php';

class Request extends \phpGitHubApiRequest
{
/**
* How many times retry to communicate with GitHub before giving up
*
* @var int
*/
protected $maxTries = 2;

/**
* Send a request to the server, receive a response
*
* @param string $apiPath Request API path
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
*
* @return string HTTP response
*/
public function doSend($apiPath, array $parameters = array(), $httpMethod = 'GET')
{
for($tries = 1; $tries <= $this->maxTries; $tries++) {
try {
return parent::doSend($apiPath, $parameters, $httpMethod);
}
catch(\phpGithubApiRequestException $e) {
if(404 == $e->getCode()) {
throw $e;
}
}
}

throw $e;
}
}
6 changes: 0 additions & 6 deletions src/Application/S2bBundle/Github/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ protected function searchReposOnGitHub($query, array $repos, $limit)
catch(\Exception $e) {
$this->output->write(' - '.$e->getMessage());
}

if(empty($repos)) {
$this->output->writeLn(' - Failed, will retry');
sleep(3);
return $this->searchReposOnGitHub($query, $repos, $limit);
}
$this->output->writeLn('... DONE');
return array_slice($repos, 0, $limit);
}
Expand Down
11 changes: 1 addition & 10 deletions src/Application/S2bBundle/Github/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ public function import($name)

public function update(Entities\User $user)
{
try {
$data = $this->github->getUserApi()->show($user->getName());
}
catch(\phpGitHubApiRequestException $e) {
if(404 == $e->getCode()) {
return false;
}
sleep(5);
return $this->update($user);
}
$data = $this->github->getUserApi()->show($user->getName());

$user->setEmail(isset($data['email']) ? $data['email'] : null);
$user->setFullName(isset($data['name']) ? $data['name'] : null);
Expand Down

0 comments on commit df35979

Please sign in to comment.