Skip to content

Commit

Permalink
add support for --orgs and --users for gitlab (refs #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mborne committed Nov 24, 2018
1 parent b8eecc9 commit b267843
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 5 deletions.
86 changes: 86 additions & 0 deletions src/MBO/SatisGitlab/Git/GitlabClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use \GuzzleHttp\Client as GuzzleHttpClient;
use Psr\Log\LoggerInterface;

use MBO\SatisGitlab\Filter\ProjectFilterInterface;

/**
* Find gitlab projects
*
Expand Down Expand Up @@ -43,6 +45,90 @@ public function __construct(
* @{inheritDoc}
*/
public function find(FindOptions $options){
if ( empty($options->getUsers()) && empty($options->getOrganizations()) ){
return $this->findBySearch($options);
}

$result = array();
foreach ( $options->getUsers() as $user ){
$result = array_merge($result,$this->findByUser(
$user,
$options->getFilterCollection()
));
}
foreach ( $options->getOrganizations() as $org ){
$result = array_merge($result,$this->findByGroup(
$org,
$options->getFilterCollection()
));
}
return $result;
}

/**
* Find projects by username
*
* @return void
*/
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;
}

/**
* Find projects by group
*
* @return void
*/
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;
}

/**
* Find all projects using option search
*/
protected function findBySearch(FindOptions $options){
/*
* refs :
* https://docs.gitlab.com/ee/api/projects.html#list-all-projects
Expand Down
48 changes: 43 additions & 5 deletions tests/Git/GitlabClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
class GitlabClientTest extends TestCase {

/**
* Ensure client can find mborne/sample-composer
* @return GitlabClient
*/
public function testGitlabDotComAuthenticated(){
protected function createGitlabClient(){
$gitlabToken = getenv('SATIS_GITLAB_TOKEN');
if ( empty($gitlabToken) ){
$this->markTestSkipped("Missing SATIS_GITLAB_TOKEN for gitlab.com");
return;
}

$clientOptions = new ClientOptions();
Expand All @@ -31,12 +30,51 @@ public function testGitlabDotComAuthenticated(){
->setToken($gitlabToken)
;


/* create client */
$client = ClientFactory::createClient(
return ClientFactory::createClient(
$clientOptions,
new NullLogger()
);
}

/**
* Ensure client can find mborne/sample-composer by username
*/
public function testGitlabDotComByUser(){
/* create client */
$client = $this->createGitlabClient();
$this->assertInstanceOf(GitlabClient::class,$client);

/* search projects */
$findOptions = new FindOptions();
$findOptions->setUsers(array('mborne'));
$projects = $client->find($findOptions);
$projectsByName = array();
foreach ( $projects as $project ){
$projectsByName[$project->getName()] = $project;
}
/* check project found */
$this->assertArrayHasKey(
'mborne/sample-composer',
$projectsByName
);

$project = $projectsByName['mborne/sample-composer'];
$composer = $client->getRawFile(
$project,
'composer.json',
$project->getDefaultBranch()
);
$this->assertContains('[email protected]',$composer);
}


/**
* Ensure client can find mborne/sample-composer with search
*/
public function testGitlabDotComSearch(){
/* create client */
$client = $this->createGitlabClient();
$this->assertInstanceOf(GitlabClient::class,$client);

/* search projects */
Expand Down

0 comments on commit b267843

Please sign in to comment.