Skip to content

Commit

Permalink
GithubClient and GitlabClient : avoid some code duplication (refs #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mborne committed Nov 24, 2018
1 parent 8845f1d commit 1da046d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 79 deletions.
59 changes: 30 additions & 29 deletions src/MBO/RemoteGit/GithubClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace MBO\RemoteGit;

use \GuzzleHttp\Client as GuzzleHttpClient;
use Psr\Log\LoggerInterface;
use \GuzzleHttp\Client as GuzzleHttpClient;

use MBO\RemoteGit\Filter\ProjectFilterInterface;

/**
* Client implementation for github
*
* See following github docs :
*
* https://developer.github.com/v3/repos/#list-organization-repositories
* https://developer.github.com/v3/repos/#list-user-repositories
* https://developer.github.com/v3/#pagination
*
*/
class GithubClient implements ClientInterface {

Expand Down Expand Up @@ -69,29 +77,10 @@ protected function findByUser(
$user,
ProjectFilterInterface $projectFilter
){
$result = array();
for ($page = 1; $page <= self::MAX_PAGES; $page++) {
/*
* https://developer.github.com/v3/repos/#list-user-repositories
* https://developer.github.com/v3/#pagination
*/
$uri = '/users/'.$user.'/repos?page='.$page.'&per_page='.self::DEFAULT_PER_PAGE;

$this->logger->debug('GET '.$uri);
$response = $this->httpClient->get($uri);
$rawProjects = json_decode( (string)$response->getBody(), true ) ;
if ( empty($rawProjects) ){
break;
}
foreach ( $rawProjects as $rawProject ){
$project = new GithubProject($rawProject);
if ( ! $projectFilter->isAccepted($project) ){
continue;
}
$result[] = $project;
}
}
return $result;
return $this->fetchAllPages(
'/users/'.$user.'/repos',
$projectFilter
);
}

/**
Expand All @@ -102,14 +91,26 @@ protected function findByUser(
protected function findByOrg(
$org,
ProjectFilterInterface $projectFilter
){
return $this->fetchAllPages(
'/orgs/'.$org.'/repos',
$projectFilter
);
}

/**
* Fetch all pages for a given URI
*
* @param string $path such as '/orgs/IGNF/repos' or '/users/mborne/repos'
* @return ProjectInterface[]
*/
private function fetchAllPages(
$path,
ProjectFilterInterface $projectFilter
){
$result = array();
for ($page = 1; $page <= self::MAX_PAGES; $page++) {
/*
* https://developer.github.com/v3/repos/#list-organization-repositories
* https://developer.github.com/v3/#pagination
*/
$uri = '/orgs/'.$org.'/repos?page='.$page.'&per_page='.self::DEFAULT_PER_PAGE;
$uri = $path.'?page='.$page.'&per_page='.self::DEFAULT_PER_PAGE;

$this->logger->debug('GET '.$uri);
$response = $this->httpClient->get($uri);
Expand Down
90 changes: 40 additions & 50 deletions src/MBO/RemoteGit/GitlabClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
/**
* Find gitlab projects
*
* TODO add ClientInterface and RepositoryInterface to allow github, gogs and local repositories & co?
* See following gitlab docs :
*
* https://docs.gitlab.com/ee/api/projects.html#list-all-projects
* https://docs.gitlab.com/ee/api/projects.html#search-for-projects-by-name
*
*/
class GitlabClient implements ClientInterface {

Expand Down Expand Up @@ -45,6 +48,7 @@ public function __construct(
* @{inheritDoc}
*/
public function find(FindOptions $options){
/* find all projects applying optional search */
if ( empty($options->getUsers()) && empty($options->getOrganizations()) ){
return $this->findBySearch($options);
}
Expand Down Expand Up @@ -74,25 +78,10 @@ protected function findByUser(
$user,
ProjectFilterInterface $projectFilter
){
$result = array();
for ($page = 1; $page <= self::MAX_PAGES; $page++) {
$uri = '/api/v4/users/'.$user.'/projects?page='.$page.'&per_page='.self::DEFAULT_PER_PAGE;

$this->logger->debug('GET '.$uri);
$response = $this->httpClient->get($uri);
$rawProjects = json_decode( (string)$response->getBody(), true ) ;
if ( empty($rawProjects) ){
break;
}
foreach ( $rawProjects as $rawProject ){
$project = new GitlabProject($rawProject);
if ( ! $projectFilter->isAccepted($project) ){
continue;
}
$result[] = $project;
}
}
return $result;
return $this->fetchAllPages(
'/api/v4/users/'.urlencode($user).'/projects',
$projectFilter
);
}

/**
Expand All @@ -104,43 +93,44 @@ protected function findByGroup(
$group,
ProjectFilterInterface $projectFilter
){
$result = array();
for ($page = 1; $page <= self::MAX_PAGES; $page++) {
$uri = '/api/v4/groups/'.urlencode($group).'/projects?page='.$page.'&per_page='.self::DEFAULT_PER_PAGE;

$this->logger->debug('GET '.$uri);
$response = $this->httpClient->get($uri);
$rawProjects = json_decode( (string)$response->getBody(), true ) ;
if ( empty($rawProjects) ){
break;
}
foreach ( $rawProjects as $rawProject ){
$project = new GitlabProject($rawProject);
if ( ! $projectFilter->isAccepted($project) ){
continue;
}
$result[] = $project;
}
}
return $result;
return $this->fetchAllPages(
'/api/v4/groups/'.urlencode($group).'/projects',
$projectFilter
);
}

/**
* Find all projects using option search
*/
protected function findBySearch(FindOptions $options){
/*
* refs :
* https://docs.gitlab.com/ee/api/projects.html#list-all-projects
* https://docs.gitlab.com/ee/api/projects.html#search-for-projects-by-name
*/
$path = '/api/v4/projects';
if ( $options->hasSearch() ){
$path .= '?search='.$options->getSearch();
}
return $this->fetchAllPages(
$path,
$options->getFilterCollection()
);
}


/**
* Fetch all pages for a given path
*
* @param string $path "/api/v4/projects?search=something", "/api/v4/projects"
* @param ProjectFilterInterface $projectFilter
* @return void
*/
private function fetchAllPages(
$path,
ProjectFilterInterface $projectFilter
){
$result = array();

if ( strpos($path,'?') === false ){
$path .= '?';
}
for ($page = 1; $page <= self::MAX_PAGES; $page++) {
$uri = '/api/v4/projects?page='.$page.'&per_page='.self::DEFAULT_PER_PAGE;
if ( $options->hasSearch() ){
$uri .= '&search='.$options->getSearch();
}
$uri = $path.'page='.$page.'&per_page='.self::DEFAULT_PER_PAGE;
$this->logger->debug('GET '.$uri);
$response = $this->httpClient->get($uri);
$rawProjects = json_decode( (string)$response->getBody(), true ) ;
Expand All @@ -149,7 +139,7 @@ protected function findBySearch(FindOptions $options){
}
foreach ( $rawProjects as $rawProject ){
$project = new GitlabProject($rawProject);
if ( ! $options->getFilterCollection()->isAccepted($project) ){
if ( ! $projectFilter->isAccepted($project) ){
continue;
}
$result[] = $project;
Expand Down

0 comments on commit 1da046d

Please sign in to comment.