Enhanced GitHub API/Private Repository support

main
Beau Simensen 12 years ago
parent edf93f1fcc
commit 6efbc7d531

@ -2,8 +2,10 @@
namespace Composer\Repository\Vcs; namespace Composer\Repository\Vcs;
use Composer\Downloader\TransportException;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Util\ProcessExecutor;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -16,14 +18,30 @@ class GitHubDriver extends VcsDriver
protected $branches; protected $branches;
protected $rootIdentifier; protected $rootIdentifier;
protected $infoCache = array(); protected $infoCache = array();
protected $isPrivate = false;
public function __construct($url, IOInterface $io) /**
* Git Driver
*
* @var GitDriver
*/
protected $gitDriver;
/**
* Constructor
*
* @param string $url
* @param IOInterface $io
* @param ProcessExecutor $process
* @param callback $remoteFilesystemGenerator
*/
public function __construct($url, IOInterface $io, ProcessExecutor $process = null, $remoteFilesystemGenerator = null)
{ {
preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match); preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
$this->owner = $match[1]; $this->owner = $match[1];
$this->repository = $match[2]; $this->repository = $match[2];
parent::__construct($url, $io); parent::__construct($url, $io, $process, $remoteFilesystemGenerator);
} }
/** /**
@ -31,6 +49,7 @@ class GitHubDriver extends VcsDriver
*/ */
public function initialize() public function initialize()
{ {
$this->fetchRootIdentifier();
} }
/** /**
@ -38,11 +57,9 @@ class GitHubDriver extends VcsDriver
*/ */
public function getRootIdentifier() public function getRootIdentifier()
{ {
if (null === $this->rootIdentifier) { if ($this->gitDriver) {
$repoData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository)); return $this->gitDriver->getRootIdentifier();
$this->rootIdentifier = $repoData['master_branch'] ?: 'master';
} }
return $this->rootIdentifier; return $this->rootIdentifier;
} }
@ -51,6 +68,9 @@ class GitHubDriver extends VcsDriver
*/ */
public function getUrl() public function getUrl()
{ {
if ($this->gitDriver) {
return $this->gitDriver->getUrl();
}
return $this->url; return $this->url;
} }
@ -59,9 +79,19 @@ class GitHubDriver extends VcsDriver
*/ */
public function getSource($identifier) public function getSource($identifier)
{ {
if ($this->gitDriver) {
return $this->gitDriver->getSource($identifier);
}
$label = array_search($identifier, $this->getTags()) ?: $identifier; $label = array_search($identifier, $this->getTags()) ?: $identifier;
if ($this->isPrivate) {
// Private GitHub repositories should be accessed using the
// SSH version of the URL.
$url = $this->generateSshUrl();
} else {
$url = $this->getUrl();
}
return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $label); return array('type' => 'git', 'url' => $url, 'reference' => $label);
} }
/** /**
@ -69,6 +99,9 @@ class GitHubDriver extends VcsDriver
*/ */
public function getDist($identifier) public function getDist($identifier)
{ {
if ($this->gitDriver) {
return $this->gitDriver->getDist($identifier);
}
$label = array_search($identifier, $this->getTags()) ?: $identifier; $label = array_search($identifier, $this->getTags()) ?: $identifier;
$url = $this->getScheme() . '://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label; $url = $this->getScheme() . '://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
@ -80,6 +113,9 @@ class GitHubDriver extends VcsDriver
*/ */
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if ($this->gitDriver) {
return $this->gitDriver->getComposerInformation($identifier);
}
if (!isset($this->infoCache[$identifier])) { if (!isset($this->infoCache[$identifier])) {
$composer = $this->getContents($this->getScheme() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json'); $composer = $this->getContents($this->getScheme() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
if (!$composer) { if (!$composer) {
@ -103,6 +139,9 @@ class GitHubDriver extends VcsDriver
*/ */
public function getTags() public function getTags()
{ {
if ($this->gitDriver) {
return $this->gitDriver->getTags();
}
if (null === $this->tags) { if (null === $this->tags) {
$tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags')); $tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'));
$this->tags = array(); $this->tags = array();
@ -119,6 +158,9 @@ class GitHubDriver extends VcsDriver
*/ */
public function getBranches() public function getBranches()
{ {
if ($this->gitDriver) {
return $this->gitDriver->getBranches();
}
if (null === $this->branches) { if (null === $this->branches) {
$branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/branches')); $branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/branches'));
$this->branches = array(); $this->branches = array();
@ -137,4 +179,59 @@ class GitHubDriver extends VcsDriver
{ {
return extension_loaded('openssl') && preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url); return extension_loaded('openssl') && preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url);
} }
/**
* Generate an SSH URL
*
* @return string
*/
protected function generateSshUrl()
{
return 'git@github.com:'.$this->owner.'/'.$this->repository.'.git';
}
/**
* Fetch root identifier from GitHub
*
* @throws TransportException
*/
protected function fetchRootIdentifier()
{
$repoDataUrl = $this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository;
$attemptCounter = 0;
while (null === $this->rootIdentifier) {
if (5 == $attemptCounter++) {
throw new \RuntimeException("Either you have entered invalid credentials or this GitHub repository does not exists (404)");
}
try {
$repoData = JsonFile::parseJson($this->getContents($repoDataUrl));
$this->rootIdentifier = $repoData['master_branch'] ?: 'master';
} catch (TransportException $e) {
switch($e->getCode()) {
case 401:
case 404:
$this->isPrivate = true;
if (!$this->io->isInteractive()) {
$this->gitDriver = new GitDriver(
$this->generateSshUrl(),
$this->io,
$this->process,
$this->remoteFilesystemGenerator
);
$this->gitDriver->initialize();
return;
}
$this->io->write('Authentication required (<info>'.$this->url.'</info>):');
$username = $this->io->ask('Username: ');
$password = $this->io->askAndHideAnswer('Password: ');
$this->io->setAuthorization($this->url, $username, $password);
break;
default:
throw $e;
break;
}
}
}
}
} }

@ -27,6 +27,7 @@ abstract class VcsDriver implements VcsDriverInterface
protected $url; protected $url;
protected $io; protected $io;
protected $process; protected $process;
protected $remoteFilesystemGenerator;
/** /**
* Constructor. * Constructor.
@ -34,12 +35,16 @@ abstract class VcsDriver implements VcsDriverInterface
* @param string $url The URL * @param string $url The URL
* @param IOInterface $io The IO instance * @param IOInterface $io The IO instance
* @param ProcessExecutor $process Process instance, injectable for mocking * @param ProcessExecutor $process Process instance, injectable for mocking
* @param callback $remoteFilesystemGenerator Generates Remote Filesystem, injectable for mocking
*/ */
public function __construct($url, IOInterface $io, ProcessExecutor $process = null) public function __construct($url, IOInterface $io, ProcessExecutor $process = null, $remoteFilesystemGenerator = null)
{ {
$this->url = $url; $this->url = $url;
$this->io = $io; $this->io = $io;
$this->process = $process ?: new ProcessExecutor; $this->process = $process ?: new ProcessExecutor;
$this->remoteFilesystemGenerator = $remoteFilesystemGenerator ?: function() use ($io) {
return new RemoteFilesystem($io);
};
} }
/** /**
@ -80,7 +85,7 @@ abstract class VcsDriver implements VcsDriverInterface
*/ */
protected function getContents($url) protected function getContents($url)
{ {
$rfs = new RemoteFilesystem($this->io); $rfs = call_user_func($this->remoteFilesystemGenerator);
return $rfs->getContents($this->url, $url, false); return $rfs->getContents($this->url, $url, false);
} }

@ -80,13 +80,11 @@ class RemoteFilesystem
* @param string $fileUrl The file URL * @param string $fileUrl The file URL
* @param string $fileName the local filename * @param string $fileName the local filename
* @param boolean $progress Display the progression * @param boolean $progress Display the progression
* @param boolean $firstCall Whether this is the first attempt at fetching this resource
* *
* @throws TransportException When the file could not be downloaded * @throws TransportException When the file could not be downloaded
*/ */
protected function get($originUrl, $fileUrl, $fileName = null, $progress = true, $firstCall = true) protected function get($originUrl, $fileUrl, $fileName = null, $progress = true)
{ {
$this->firstCall = $firstCall;
$this->bytesMax = 0; $this->bytesMax = 0;
$this->result = null; $this->result = null;
$this->originUrl = $originUrl; $this->originUrl = $originUrl;
@ -140,20 +138,12 @@ class RemoteFilesystem
protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
{ {
switch ($notificationCode) { switch ($notificationCode) {
case STREAM_NOTIFY_AUTH_REQUIRED:
case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_FAILURE:
if (404 === $messageCode && !$this->firstCall) { throw new TransportException(trim($message), $messageCode);
throw new TransportException("The '" . $this->fileUrl . "' URL not found", 404); break;
}
// for private repository returning 404 error when the authorization is incorrect
$auth = $this->io->getAuthorization($this->originUrl);
$attemptAuthentication = $this->firstCall && 404 === $messageCode && null === $auth['username'];
$this->firstCall = false;
// get authorization informations case STREAM_NOTIFY_AUTH_REQUIRED:
if (401 === $messageCode || $attemptAuthentication) { if (401 === $messageCode) {
if (!$this->io->isInteractive()) { if (!$this->io->isInteractive()) {
$message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console"; $message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console";
@ -165,7 +155,7 @@ class RemoteFilesystem
$password = $this->io->askAndHideAnswer(' Password: '); $password = $this->io->askAndHideAnswer(' Password: ');
$this->io->setAuthorization($this->originUrl, $username, $password); $this->io->setAuthorization($this->originUrl, $username, $password);
$this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress, false); $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress);
} }
break; break;

@ -0,0 +1,227 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Repository\Vcs;
use Composer\Downloader\TransportException;
use Composer\Repository\Vcs\GitHubDriver;
/**
* @author Beau Simensen <beau@dflydev.com>
*/
class GitHubDriverTest extends \PHPUnit_Framework_TestCase
{
public function testPrivateRepository()
{
$repoUrl = 'http://github.com/composer/packagist';
$repoApiUrl = 'https://api.github.com/repos/composer/packagist';
$repoSshUrl = 'git@github.com:composer/packagist.git';
$identifier = 'v0.0.0';
$sha = 'SOMESHA';
$io = $this->getMock('Composer\IO\IOInterface');
$io->expects($this->any())
->method('isInteractive')
->will($this->returnValue(true));
$remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
->setConstructorArgs(array($io))
->getMock();
$remoteFilesystem->expects($this->at(0))
->method('getContents')
->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
$io->expects($this->once())
->method('ask')
->with($this->equalTo('Username: '))
->will($this->returnValue('someuser'));
$io->expects($this->once())
->method('askAndHideAnswer')
->with($this->equalTo('Password: '))
->will($this->returnValue('somepassword'));
$io->expects($this->once())
->method('setAuthorization')
->with($this->equalTo($repoUrl), 'someuser', 'somepassword');
$remoteFilesystem->expects($this->at(1))
->method('getContents')
->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
->will($this->returnValue('{"master_branch": "test_master"}'));
$gitHubDriver = new GitHubDriver($repoUrl, $io, null, function() use ($remoteFilesystem) { return $remoteFilesystem; });
$gitHubDriver->initialize();
$this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
$this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
$dist = $gitHubDriver->getDist($identifier);
$this->assertEquals('zip', $dist['type']);
$this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
$this->assertEquals('v0.0.0', $dist['reference']);
$source = $gitHubDriver->getSource($identifier);
$this->assertEquals('git', $source['type']);
$this->assertEquals($repoSshUrl, $source['url']);
$this->assertEquals('v0.0.0', $source['reference']);
$dist = $gitHubDriver->getDist($sha);
$this->assertEquals('zip', $dist['type']);
$this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
$this->assertEquals('v0.0.0', $dist['reference']);
$source = $gitHubDriver->getSource($sha);
$this->assertEquals('git', $source['type']);
$this->assertEquals($repoSshUrl, $source['url']);
$this->assertEquals('v0.0.0', $source['reference']);
}
public function testPublicRepository()
{
$repoUrl = 'http://github.com/composer/packagist';
$repoApiUrl = 'https://api.github.com/repos/composer/packagist';
$identifier = 'v0.0.0';
$sha = 'SOMESHA';
$io = $this->getMock('Composer\IO\IOInterface');
$io->expects($this->any())
->method('isInteractive')
->will($this->returnValue(true));
$remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
->setConstructorArgs(array($io))
->getMock();
$remoteFilesystem->expects($this->at(0))
->method('getContents')
->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
->will($this->returnValue('{"master_branch": "test_master"}'));
$gitHubDriver = new GitHubDriver($repoUrl, $io, null, function() use ($remoteFilesystem) {
return $remoteFilesystem;
});
$gitHubDriver->initialize();
$this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
$this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
$dist = $gitHubDriver->getDist($identifier);
$this->assertEquals('zip', $dist['type']);
$this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
$this->assertEquals($identifier, $dist['reference']);
$source = $gitHubDriver->getSource($identifier);
$this->assertEquals('git', $source['type']);
$this->assertEquals($repoUrl, $source['url']);
$this->assertEquals($identifier, $source['reference']);
$dist = $gitHubDriver->getDist($sha);
$this->assertEquals('zip', $dist['type']);
$this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
$this->assertEquals($identifier, $dist['reference']);
$source = $gitHubDriver->getSource($sha);
$this->assertEquals('git', $source['type']);
$this->assertEquals($repoUrl, $source['url']);
$this->assertEquals($identifier, $source['reference']);
}
public function testPrivateRepositoryNoInteraction()
{
$repoUrl = 'http://github.com/composer/packagist';
$repoApiUrl = 'https://api.github.com/repos/composer/packagist';
$repoSshUrl = 'git@github.com:composer/packagist.git';
$identifier = 'v0.0.0';
$sha = 'SOMESHA';
$process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
->disableOriginalConstructor()
->getMock();
$io = $this->getMock('Composer\IO\IOInterface');
$io->expects($this->any())
->method('isInteractive')
->will($this->returnValue(false));
$remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
->setConstructorArgs(array($io))
->getMock();
$remoteFilesystem->expects($this->at(0))
->method('getContents')
->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
$process->expects($this->at(0))
->method('execute')
->with($this->stringContains('git fetch origin'));
$process->expects($this->at(1))
->method('execute')
->with($this->stringContains('git tag'));
$process->expects($this->at(2))
->method('splitLines')
->will($this->returnValue(array($identifier)));
$process->expects($this->at(3))
->method('execute')
->with($this->stringContains('git branch'));
$process->expects($this->at(4))
->method('splitLines')
->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
$process->expects($this->at(5))
->method('execute')
->with($this->stringContains('git branch'));
$process->expects($this->at(6))
->method('splitLines')
->will($this->returnValue(array(' upstream/HEAD -> upstream/test_master')));
$gitHubDriver = new GitHubDriver($repoUrl, $io, $process, function() use ($remoteFilesystem) {
return $remoteFilesystem;
});
$gitHubDriver->initialize();
$this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
// Dist is not available for GitDriver
$dist = $gitHubDriver->getDist($identifier);
$this->assertNull($dist);
$source = $gitHubDriver->getSource($identifier);
$this->assertEquals('git', $source['type']);
$this->assertEquals($repoSshUrl, $source['url']);
$this->assertEquals($identifier, $source['reference']);
// Dist is not available for GitDriver
$dist = $gitHubDriver->getDist($sha);
$this->assertNull($dist);
$source = $gitHubDriver->getSource($sha);
$this->assertEquals('git', $source['type']);
$this->assertEquals($repoSshUrl, $source['url']);
$this->assertEquals($sha, $source['reference']);
}
protected function setAttribute($object, $attribute, $value)
{
$attr = new \ReflectionProperty($object, $attribute);
$attr->setAccessible(true);
$attr->setValue($object, $value);
}
}

@ -105,41 +105,14 @@ class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase
public function testCallbackGetNotifyFailure404() public function testCallbackGetNotifyFailure404()
{ {
$fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface')); $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
$this->setAttribute($fs, 'firstCall', false);
try { try {
$this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, '', 404, 0, 0); $this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0);
$this->fail(); $this->fail();
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertInstanceOf('Composer\Downloader\TransportException', $e); $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
$this->assertContains('URL not found', $e->getMessage()); $this->assertEquals(404, $e->getCode());
} $this->assertContains('HTTP/1.1 404 Not Found', $e->getMessage());
}
public function testCallbackGetNotifyFailure404FirstCall()
{
$io = $this->getMock('Composer\IO\IOInterface');
$io
->expects($this->once())
->method('getAuthorization')
->will($this->returnValue(array('username' => null)))
;
$io
->expects($this->once())
->method('isInteractive')
->will($this->returnValue(false))
;
$fs = new RemoteFilesystem($io);
$this->setAttribute($fs, 'firstCall', true);
try {
$this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, '', 404, 0, 0);
$this->fail();
} catch (\Exception $e) {
$this->assertInstanceOf('Composer\Downloader\TransportException', $e);
$this->assertContains('URL required authentication', $e->getMessage());
$this->assertAttributeEquals(false, 'firstCall', $fs);
} }
} }

Loading…
Cancel
Save