From e78499d28de4fd72d422fab3c3d6661cc4c88906 Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Mon, 28 Oct 2013 12:29:37 -0400 Subject: [PATCH] First working version of GitHub Enterprise API. --- src/Composer/Repository/Vcs/GitHubDriver.php | 36 ++++++++++++-------- src/Composer/Util/GitHub.php | 8 ++--- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index 6345fa473..f6cdd16f8 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -45,10 +45,10 @@ class GitHubDriver extends VcsDriver */ public function initialize() { - preg_match('#^(?:(?:https?|git)://github\.com/|git@github\.com:)([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match); - $this->owner = $match[1]; - $this->repository = $match[2]; - $this->originUrl = 'github.com'; + preg_match('#^(?:(?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match); + $this->owner = $match[3]; + $this->repository = $match[4]; + $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->fetchRootIdentifier(); @@ -75,7 +75,7 @@ class GitHubDriver extends VcsDriver 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) { 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' => ''); } @@ -127,7 +128,8 @@ class GitHubDriver extends VcsDriver $notFoundRetries = 2; while ($notFoundRetries) { 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)); if (empty($composer['content']) || $composer['encoding'] !== 'base64' || !($composer = base64_decode($composer['content']))) { throw new \RuntimeException('Could not retrieve composer.json from '.$resource); @@ -149,16 +151,17 @@ class GitHubDriver extends VcsDriver $composer = JsonFile::parseJson($composer, $resource); 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); $composer['time'] = $commit['commit']['committer']['date']; } if (!isset($composer['support']['source'])) { $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) { - $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(); } 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); $this->tags = array(); foreach ($tagsData as $tag) { @@ -201,7 +205,8 @@ class GitHubDriver extends VcsDriver return $this->gitDriver->getBranches(); } 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); $this->branches = array(); foreach ($branchData as $branch) { @@ -218,7 +223,7 @@ class GitHubDriver extends VcsDriver */ 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; } @@ -240,7 +245,7 @@ class GitHubDriver extends VcsDriver */ 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() { - $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); if (null === $repoData && null !== $this->gitDriver) { diff --git a/src/Composer/Util/GitHub.php b/src/Composer/Util/GitHub.php index 93894cc76..ed988894b 100644 --- a/src/Composer/Util/GitHub.php +++ b/src/Composer/Util/GitHub.php @@ -51,10 +51,6 @@ class GitHub */ public function authorizeOAuth($originUrl) { - if ('github.com' !== $originUrl) { - return false; - } - // if available use token from git config if (0 === $this->process->execute('git config github.accesstoken', $output)) { $this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic'); @@ -78,6 +74,8 @@ class GitHub { $attemptCounter = 0; + $apiUrl = ('github.com' === $originUrl) ? 'api.github.com' : $originUrl . '/api/v3'; + if ($message) { $this->io->write($message); } @@ -95,7 +93,7 @@ class GitHub $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( 'method' => 'POST', 'follow_location' => false,