|
@@ -14,14 +14,19 @@ namespace Composer\Repository\Vcs;
|
|
|
|
|
|
use Composer\Cache;
|
|
use Composer\Cache;
|
|
use Composer\Config;
|
|
use Composer\Config;
|
|
|
|
+use Composer\Downloader\TransportException;
|
|
use Composer\Json\JsonFile;
|
|
use Composer\Json\JsonFile;
|
|
use Composer\IO\IOInterface;
|
|
use Composer\IO\IOInterface;
|
|
|
|
+use Composer\Util\Bitbucket;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author Per Bernhardt <plb@webfactory.de>
|
|
* @author Per Bernhardt <plb@webfactory.de>
|
|
*/
|
|
*/
|
|
class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
{
|
|
{
|
|
|
|
+ /**
|
|
|
|
+ * @var Cache
|
|
|
|
+ */
|
|
protected $cache;
|
|
protected $cache;
|
|
protected $owner;
|
|
protected $owner;
|
|
protected $repository;
|
|
protected $repository;
|
|
@@ -29,6 +34,11 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
protected $branches;
|
|
protected $branches;
|
|
protected $rootIdentifier;
|
|
protected $rootIdentifier;
|
|
protected $infoCache = array();
|
|
protected $infoCache = array();
|
|
|
|
+ private $hasIssues;
|
|
|
|
+ /**
|
|
|
|
+ * @var GitDriver
|
|
|
|
+ */
|
|
|
|
+ private $gitDriver;
|
|
|
|
|
|
/**
|
|
/**
|
|
* {@inheritDoc}
|
|
* {@inheritDoc}
|
|
@@ -47,9 +57,14 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
*/
|
|
*/
|
|
public function getRootIdentifier()
|
|
public function getRootIdentifier()
|
|
{
|
|
{
|
|
|
|
+ if ($this->gitDriver) {
|
|
|
|
+ return $this->gitDriver->getRootIdentifier();
|
|
|
|
+ }
|
|
|
|
+
|
|
if (null === $this->rootIdentifier) {
|
|
if (null === $this->rootIdentifier) {
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository;
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository;
|
|
- $repoData = JsonFile::parseJson($this->getContents($resource), $resource);
|
|
|
|
|
|
+ $repoData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource, true), $resource);
|
|
|
|
+ $this->hasIssues = !empty($repoData['has_issues']);
|
|
$this->rootIdentifier = !empty($repoData['main_branch']) ? $repoData['main_branch'] : 'master';
|
|
$this->rootIdentifier = !empty($repoData['main_branch']) ? $repoData['main_branch'] : 'master';
|
|
}
|
|
}
|
|
|
|
|
|
@@ -61,7 +76,11 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
*/
|
|
*/
|
|
public function getUrl()
|
|
public function getUrl()
|
|
{
|
|
{
|
|
- return $this->url;
|
|
|
|
|
|
+ if ($this->gitDriver) {
|
|
|
|
+ return $this->gitDriver->getUrl();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -69,6 +88,10 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
*/
|
|
*/
|
|
public function getSource($identifier)
|
|
public function getSource($identifier)
|
|
{
|
|
{
|
|
|
|
+ if ($this->gitDriver) {
|
|
|
|
+ return $this->gitDriver->getSource($identifier);
|
|
|
|
+ }
|
|
|
|
+
|
|
return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
|
|
return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -87,24 +110,53 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
*/
|
|
*/
|
|
public function getComposerInformation($identifier)
|
|
public function getComposerInformation($identifier)
|
|
{
|
|
{
|
|
|
|
+ if ($this->gitDriver) {
|
|
|
|
+ return $this->gitDriver->getComposerInformation($identifier);
|
|
|
|
+ }
|
|
|
|
+
|
|
if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
|
|
if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
|
|
$this->infoCache[$identifier] = JsonFile::parseJson($res);
|
|
$this->infoCache[$identifier] = JsonFile::parseJson($res);
|
|
}
|
|
}
|
|
|
|
|
|
if (!isset($this->infoCache[$identifier])) {
|
|
if (!isset($this->infoCache[$identifier])) {
|
|
- $resource = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json';
|
|
|
|
- $composer = $this->getContents($resource);
|
|
|
|
- if (!$composer) {
|
|
|
|
- return;
|
|
|
|
|
|
+ $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/src/'.$identifier.'/composer.json';
|
|
|
|
+ $file = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
|
|
|
|
+ if (!is_array($file) || ! array_key_exists('data', $file)) {
|
|
|
|
+ return array();
|
|
}
|
|
}
|
|
|
|
|
|
- $composer = JsonFile::parseJson($composer, $resource);
|
|
|
|
|
|
+ $composer = JsonFile::parseJson($file['data'], $resource);
|
|
|
|
|
|
if (empty($composer['time'])) {
|
|
if (empty($composer['time'])) {
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier;
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier;
|
|
- $changeset = JsonFile::parseJson($this->getContents($resource), $resource);
|
|
|
|
|
|
+ $changeset = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
|
|
$composer['time'] = $changeset['timestamp'];
|
|
$composer['time'] = $changeset['timestamp'];
|
|
}
|
|
}
|
|
|
|
+ if (!isset($composer['support']['source'])) {
|
|
|
|
+ $label = array_search($identifier, $this->getTags()) ?: array_search($identifier, $this->getBranches()) ?: $identifier;
|
|
|
|
+
|
|
|
|
+ if (array_key_exists($label, $tags = $this->getTags())) {
|
|
|
|
+ $hash = $tags[$label];
|
|
|
|
+ } elseif (array_key_exists($label, $branches = $this->getBranches())) {
|
|
|
|
+ $hash = $branches[$label];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (! isset($hash)) {
|
|
|
|
+ $composer['support']['source'] = sprintf('https://%s/%s/%s/src', $this->originUrl, $this->owner, $this->repository);
|
|
|
|
+ } else {
|
|
|
|
+ $composer['support']['source'] = sprintf(
|
|
|
|
+ 'https://%s/%s/%s/src/%s/?at=%s',
|
|
|
|
+ $this->originUrl,
|
|
|
|
+ $this->owner,
|
|
|
|
+ $this->repository,
|
|
|
|
+ $hash,
|
|
|
|
+ $label
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!isset($composer['support']['issues']) && $this->hasIssues) {
|
|
|
|
+ $composer['support']['issues'] = sprintf('https://%s/%s/%s/issues', $this->originUrl, $this->owner, $this->repository);
|
|
|
|
+ }
|
|
|
|
|
|
if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
|
|
if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
|
|
$this->cache->write($identifier, json_encode($composer));
|
|
$this->cache->write($identifier, json_encode($composer));
|
|
@@ -121,9 +173,13 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
*/
|
|
*/
|
|
public function getTags()
|
|
public function getTags()
|
|
{
|
|
{
|
|
|
|
+ if ($this->gitDriver) {
|
|
|
|
+ return $this->gitDriver->getTags();
|
|
|
|
+ }
|
|
|
|
+
|
|
if (null === $this->tags) {
|
|
if (null === $this->tags) {
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
|
|
- $tagsData = JsonFile::parseJson($this->getContents($resource), $resource);
|
|
|
|
|
|
+ $tagsData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
|
|
$this->tags = array();
|
|
$this->tags = array();
|
|
foreach ($tagsData as $tag => $data) {
|
|
foreach ($tagsData as $tag => $data) {
|
|
$this->tags[$tag] = $data['raw_node'];
|
|
$this->tags[$tag] = $data['raw_node'];
|
|
@@ -138,9 +194,13 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
*/
|
|
*/
|
|
public function getBranches()
|
|
public function getBranches()
|
|
{
|
|
{
|
|
|
|
+ if ($this->gitDriver) {
|
|
|
|
+ return $this->gitDriver->getBranches();
|
|
|
|
+ }
|
|
|
|
+
|
|
if (null === $this->branches) {
|
|
if (null === $this->branches) {
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches';
|
|
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches';
|
|
- $branchData = JsonFile::parseJson($this->getContents($resource), $resource);
|
|
|
|
|
|
+ $branchData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
|
|
$this->branches = array();
|
|
$this->branches = array();
|
|
foreach ($branchData as $branch => $data) {
|
|
foreach ($branchData as $branch => $data) {
|
|
$this->branches[$branch] = $data['raw_node'];
|
|
$this->branches[$branch] = $data['raw_node'];
|
|
@@ -167,4 +227,76 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ protected function attemptCloneFallback()
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ $this->setupGitDriver($this->generateSshUrl());
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ } catch (\RuntimeException $e) {
|
|
|
|
+ $this->gitDriver = null;
|
|
|
|
+
|
|
|
|
+ $this->io->writeError('<error>Failed to clone the '.$this->generateSshUrl().' repository, try running in interactive mode so that you can enter your Bitbucket OAuth consumer credentials</error>');
|
|
|
|
+ throw $e;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Generate an SSH URL
|
|
|
|
+ *
|
|
|
|
+ * @return string
|
|
|
|
+ */
|
|
|
|
+ private function generateSshUrl()
|
|
|
|
+ {
|
|
|
|
+ return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Get the remote content.
|
|
|
|
+ *
|
|
|
|
+ * @param string $url The URL of content
|
|
|
|
+ * @param bool $fetchingRepoData
|
|
|
|
+ *
|
|
|
|
+ * @return mixed The result
|
|
|
|
+ */
|
|
|
|
+ protected function getContentsWithOAuthCredentials($url, $fetchingRepoData = false)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ return parent::getContents($url);
|
|
|
|
+ } catch (TransportException $e) {
|
|
|
|
+ $bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process, $this->remoteFilesystem);
|
|
|
|
+
|
|
|
|
+ switch ($e->getCode()) {
|
|
|
|
+ case 403:
|
|
|
|
+ if (!$this->io->hasAuthentication($this->originUrl) && $bitbucketUtil->authorizeOAuth($this->originUrl)) {
|
|
|
|
+ return parent::getContents($url);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!$this->io->isInteractive() && $fetchingRepoData) {
|
|
|
|
+ return $this->attemptCloneFallback();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ throw $e;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ throw $e;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param string $url
|
|
|
|
+ */
|
|
|
|
+ private function setupGitDriver($url)
|
|
|
|
+ {
|
|
|
|
+ $this->gitDriver = new GitDriver(
|
|
|
|
+ array('url' => $url),
|
|
|
|
+ $this->io,
|
|
|
|
+ $this->config,
|
|
|
|
+ $this->process,
|
|
|
|
+ $this->remoteFilesystem
|
|
|
|
+ );
|
|
|
|
+ $this->gitDriver->initialize();
|
|
|
|
+ }
|
|
}
|
|
}
|