1
0
Fork 0

First working version of GitHub Enterprise API.

pull/2375/head
Gennady Feldman 2013-10-28 12:29:37 -04:00
parent f8376a5b34
commit e78499d28d
2 changed files with 24 additions and 20 deletions

View File

@ -45,10 +45,10 @@ class GitHubDriver extends VcsDriver
*/ */
public function initialize() public function initialize()
{ {
preg_match('#^(?:(?:https?|git)://github\.com/|git@github\.com:)([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match); preg_match('#^(?:(?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match);
$this->owner = $match[1]; $this->owner = $match[3];
$this->repository = $match[2]; $this->repository = $match[4];
$this->originUrl = 'github.com'; $this->originUrl = isset($match[1]) ? $match[1] : $match[2];
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository); $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository);
$this->fetchRootIdentifier(); $this->fetchRootIdentifier();
@ -75,7 +75,7 @@ class GitHubDriver extends VcsDriver
return $this->gitDriver->getUrl(); return $this->gitDriver->getUrl();
} }
return 'https://github.com/'.$this->owner.'/'.$this->repository.'.git'; return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
} }
/** /**
@ -105,7 +105,8 @@ class GitHubDriver extends VcsDriver
if ($this->gitDriver) { if ($this->gitDriver) {
return $this->gitDriver->getDist($identifier); return $this->gitDriver->getDist($identifier);
} }
$url = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/zipball/'.$identifier; $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3';
$url = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/zipball/'.$identifier;
return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => ''); return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
} }
@ -127,7 +128,8 @@ class GitHubDriver extends VcsDriver
$notFoundRetries = 2; $notFoundRetries = 2;
while ($notFoundRetries) { while ($notFoundRetries) {
try { try {
$resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/contents/composer.json?ref='.urlencode($identifier); $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3';
$resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/contents/composer.json?ref='.urlencode($identifier);
$composer = JsonFile::parseJson($this->getContents($resource)); $composer = JsonFile::parseJson($this->getContents($resource));
if (empty($composer['content']) || $composer['encoding'] !== 'base64' || !($composer = base64_decode($composer['content']))) { if (empty($composer['content']) || $composer['encoding'] !== 'base64' || !($composer = base64_decode($composer['content']))) {
throw new \RuntimeException('Could not retrieve composer.json from '.$resource); throw new \RuntimeException('Could not retrieve composer.json from '.$resource);
@ -149,16 +151,17 @@ class GitHubDriver extends VcsDriver
$composer = JsonFile::parseJson($composer, $resource); $composer = JsonFile::parseJson($composer, $resource);
if (!isset($composer['time'])) { if (!isset($composer['time'])) {
$resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.urlencode($identifier); $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3';
$resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/commits/'.urlencode($identifier);
$commit = JsonFile::parseJson($this->getContents($resource), $resource); $commit = JsonFile::parseJson($this->getContents($resource), $resource);
$composer['time'] = $commit['commit']['committer']['date']; $composer['time'] = $commit['commit']['committer']['date'];
} }
if (!isset($composer['support']['source'])) { if (!isset($composer['support']['source'])) {
$label = array_search($identifier, $this->getTags()) ?: array_search($identifier, $this->getBranches()) ?: $identifier; $label = array_search($identifier, $this->getTags()) ?: array_search($identifier, $this->getBranches()) ?: $identifier;
$composer['support']['source'] = sprintf('https://github.com/%s/%s/tree/%s', $this->owner, $this->repository, $label); $composer['support']['source'] = sprintf('https://%s/%s/%s/tree/%s', $this->originUrl, $this->owner, $this->repository, $label);
} }
if (!isset($composer['support']['issues']) && $this->hasIssues) { if (!isset($composer['support']['issues']) && $this->hasIssues) {
$composer['support']['issues'] = sprintf('https://github.com/%s/%s/issues', $this->owner, $this->repository); $composer['support']['issues'] = sprintf('https://%s/%s/%s/issues', $this->originUrl, $this->owner, $this->repository);
} }
} }
@ -181,7 +184,8 @@ class GitHubDriver extends VcsDriver
return $this->gitDriver->getTags(); return $this->gitDriver->getTags();
} }
if (null === $this->tags) { if (null === $this->tags) {
$resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'; $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3';
$resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/tags';
$tagsData = JsonFile::parseJson($this->getContents($resource), $resource); $tagsData = JsonFile::parseJson($this->getContents($resource), $resource);
$this->tags = array(); $this->tags = array();
foreach ($tagsData as $tag) { foreach ($tagsData as $tag) {
@ -201,7 +205,8 @@ class GitHubDriver extends VcsDriver
return $this->gitDriver->getBranches(); return $this->gitDriver->getBranches();
} }
if (null === $this->branches) { if (null === $this->branches) {
$resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads'; $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3';
$resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads';
$branchData = JsonFile::parseJson($this->getContents($resource), $resource); $branchData = JsonFile::parseJson($this->getContents($resource), $resource);
$this->branches = array(); $this->branches = array();
foreach ($branchData as $branch) { foreach ($branchData as $branch) {
@ -218,7 +223,7 @@ class GitHubDriver extends VcsDriver
*/ */
public static function supports(IOInterface $io, $url, $deep = false) public static function supports(IOInterface $io, $url, $deep = false)
{ {
if (!preg_match('#^((?:https?|git)://github\.com/|git@github\.com:)([^/]+)/(.+?)(?:\.git)?$#', $url)) { if (!preg_match('#^((?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $url)) {
return false; return false;
} }
@ -240,7 +245,7 @@ class GitHubDriver extends VcsDriver
*/ */
protected function generateSshUrl() protected function generateSshUrl()
{ {
return 'git@github.com:'.$this->owner.'/'.$this->repository.'.git'; return 'git@' . $this->originUrl . ':'.$this->owner.'/'.$this->repository.'.git';
} }
/** /**
@ -357,7 +362,8 @@ class GitHubDriver extends VcsDriver
*/ */
protected function fetchRootIdentifier() protected function fetchRootIdentifier()
{ {
$repoDataUrl = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository; $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3';
$repoDataUrl = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository;
$repoData = JsonFile::parseJson($this->getContents($repoDataUrl, true), $repoDataUrl); $repoData = JsonFile::parseJson($this->getContents($repoDataUrl, true), $repoDataUrl);
if (null === $repoData && null !== $this->gitDriver) { if (null === $repoData && null !== $this->gitDriver) {

View File

@ -51,10 +51,6 @@ class GitHub
*/ */
public function authorizeOAuth($originUrl) public function authorizeOAuth($originUrl)
{ {
if ('github.com' !== $originUrl) {
return false;
}
// if available use token from git config // if available use token from git config
if (0 === $this->process->execute('git config github.accesstoken', $output)) { if (0 === $this->process->execute('git config github.accesstoken', $output)) {
$this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic'); $this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic');
@ -78,6 +74,8 @@ class GitHub
{ {
$attemptCounter = 0; $attemptCounter = 0;
$apiUrl = ('github.com' === $originUrl) ? 'api.github.com' : $originUrl . '/api/v3';
if ($message) { if ($message) {
$this->io->write($message); $this->io->write($message);
} }
@ -95,7 +93,7 @@ class GitHub
$appName .= ' on ' . trim($output); $appName .= ' on ' . trim($output);
} }
$contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://api.github.com/authorizations', false, array( $contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://'. $apiUrl . '/authorizations', false, array(
'http' => array( 'http' => array(
'method' => 'POST', 'method' => 'POST',
'follow_location' => false, 'follow_location' => false,