-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experimental github support (refs #24)
- Loading branch information
Showing
9 changed files
with
290 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/nbproject/ | ||
/vendor/ | ||
/satis.json | ||
/web | ||
/web/ | ||
/output/ | ||
|
||
/composer.phar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace MBO\SatisGitlab\Git; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use \GuzzleHttp\Client as GuzzleHttpClient; | ||
|
||
/** | ||
* Helper to create clients | ||
*/ | ||
class ClientFactory { | ||
|
||
/** | ||
* Create a client with options | ||
* | ||
* @param ClientOptions $options | ||
* @param LoggerInterface $logger | ||
* @return ClientInterface | ||
*/ | ||
public static function createClient( | ||
ClientOptions $options, | ||
LoggerInterface $logger | ||
) { | ||
$clientClass = self::detectClientClass($options->getUrl()); | ||
|
||
/* common http options */ | ||
$guzzleOptions = array( | ||
'base_uri' => $options->getUrl(), | ||
'timeout' => 10.0, | ||
'headers' => [] | ||
); | ||
if ( $options->isUnsafeSsl() ){ | ||
$guzzleOptions['verify'] = false; | ||
} | ||
if ( $options->hasToken() ){ | ||
if ( GitlabClient::class === $clientClass ){ | ||
$guzzleOptions['headers']['Private-Token'] = $options->getToken(); | ||
}else if ( GithubClient::class === $clientClass ){ | ||
$guzzleOptions['headers']['Authorization'] = 'token '.$options->getToken(); | ||
} | ||
} | ||
|
||
$httpClient = new GuzzleHttpClient($guzzleOptions); | ||
|
||
/* create gitlab client */ | ||
return new $clientClass($httpClient,$logger); | ||
} | ||
|
||
/** | ||
* Get client class according to URL content | ||
* | ||
* @param string $url | ||
* @return string | ||
*/ | ||
public static function detectClientClass($url){ | ||
$hostname = parse_url($url, PHP_URL_HOST); | ||
if ( 'api.github.com' === $hostname ){ | ||
return GithubClient::class; | ||
}else{ | ||
return GitlabClient::class; | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace MBO\SatisGitlab\Git; | ||
|
||
use \GuzzleHttp\Client as GuzzleHttpClient; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Client implementation for github | ||
*/ | ||
class GithubClient implements ClientInterface { | ||
|
||
const DEFAULT_PER_PAGE = 100; | ||
|
||
/** | ||
* @var GuzzleHttpClient | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* Constructor with an http client and a logger | ||
* @param $httpClient http client | ||
* @param $logger | ||
*/ | ||
public function __construct( | ||
GuzzleHttpClient $httpClient, | ||
LoggerInterface $logger | ||
){ | ||
$this->httpClient = $httpClient ; | ||
$this->logger = $logger ; | ||
} | ||
|
||
/* | ||
* @{inheritDoc} | ||
*/ | ||
public function find(array $options){ | ||
/* https://developer.github.com/v3/#pagination */ | ||
$page = empty($options['page']) ? 1 : $options['page']; | ||
$perPage = empty($options['per_page']) ? self::DEFAULT_PER_PAGE : $options['per_page']; | ||
$uri = '?page='.$page.'&per_page='.$perPage; | ||
|
||
$this->logger->debug('GET '.$uri); | ||
$response = $this->httpClient->get($uri); | ||
$projects = json_decode( (string)$response->getBody(), true ) ; | ||
|
||
$result = array(); | ||
foreach ( $projects as $project ){ | ||
$result[] = new GithubProject($project); | ||
} | ||
return $result; | ||
} | ||
|
||
/* | ||
* @{inheritDoc} | ||
*/ | ||
public function getRawFile( | ||
ProjectInterface $project, | ||
$filePath, | ||
$ref | ||
){ | ||
$metadata = $project->getRawMetadata(); | ||
$uri = str_replace( | ||
'{+path}', | ||
urlencode($filePath), | ||
$metadata['contents_url'] | ||
); | ||
$uri .= '?ref='.$ref; | ||
$this->logger->debug('GET '.$uri); | ||
$response = $this->httpClient->get($uri,[ | ||
'headers' => [ | ||
'Accept' => 'application/vnd.github.v3.raw' | ||
] | ||
]); | ||
return (string)$response->getBody(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace MBO\SatisGitlab\Git; | ||
|
||
/** | ||
* Project implementation for github | ||
*/ | ||
class GithubProject implements ProjectInterface { | ||
|
||
protected $rawMetadata; | ||
|
||
public function __construct($rawMetadata) | ||
{ | ||
$this->rawMetadata = $rawMetadata; | ||
} | ||
|
||
/* | ||
* @{inheritDoc} | ||
*/ | ||
public function getId(){ | ||
return $this->rawMetadata['id']; | ||
} | ||
|
||
/* | ||
* @{inheritDoc} | ||
*/ | ||
public function getName(){ | ||
return $this->rawMetadata['full_name']; | ||
} | ||
|
||
/* | ||
* @{inheritDoc} | ||
*/ | ||
public function getDefaultBranch(){ | ||
return $this->rawMetadata['default_branch']; | ||
} | ||
|
||
/* | ||
* @{inheritDoc} | ||
*/ | ||
public function getHttpUrl(){ | ||
return $this->rawMetadata['clone_url']; | ||
} | ||
|
||
/* | ||
* @{inheritDoc} | ||
*/ | ||
public function getRawMetadata(){ | ||
return $this->rawMetadata; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Tests\SatisGitlab\Git; | ||
|
||
use Tests\SatisGitlab\TestCase; | ||
|
||
use GuzzleHttp\Client as GuzzleHttpClient; | ||
use MBO\SatisGitlab\Git\GitlabClient; | ||
|
||
use Psr\Log\NullLogger; | ||
use MBO\SatisGitlab\Git\ClientOptions; | ||
use MBO\SatisGitlab\Git\ClientFactory; | ||
use MBO\SatisGitlab\Git\GithubClient; | ||
use MBO\SatisGitlab\Git\GithubProject; | ||
|
||
class GithubClientTest extends TestCase { | ||
|
||
/** | ||
* Ensure client can find mborne's projects | ||
*/ | ||
public function testUserRepositories(){ | ||
$clientOptions = new ClientOptions(); | ||
$clientOptions | ||
->setUrl('https://api.github.com/users/mborne/repos') | ||
; | ||
|
||
/* create client */ | ||
$client = ClientFactory::createClient( | ||
$clientOptions, | ||
new NullLogger() | ||
); | ||
$this->assertInstanceOf(GithubClient::class,$client); | ||
|
||
/* search projects */ | ||
$options = array(); | ||
$projects = $client->find($options); | ||
$projectsByName = array(); | ||
foreach ( $projects as $project ){ | ||
$this->assertInstanceOf(GithubProject::class,$project); | ||
$projectsByName[$project->getName()] = $project; | ||
} | ||
|
||
/* check project found */ | ||
$this->assertArrayHasKey( | ||
'mborne/satis-gitlab', | ||
$projectsByName | ||
); | ||
|
||
$project = $projectsByName['mborne/satis-gitlab']; | ||
$composer = $client->getRawFile( | ||
$project, | ||
'composer.json', | ||
$project->getDefaultBranch() | ||
); | ||
$this->assertContains('[email protected]',$composer); | ||
|
||
$testFileInSubdirectory = $client->getRawFile( | ||
$project, | ||
'tests/TestCase.php', | ||
$project->getDefaultBranch() | ||
); | ||
$this->assertContains('class TestCase',$testFileInSubdirectory); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters