1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-09 00:22:53 +00:00

Implement Bitbucket API version 2.0 (where applicable).

This commit is contained in:
Stefan Grootscholten 2016-12-27 22:16:32 +01:00
parent b3b05949bb
commit d25c483231
4 changed files with 280 additions and 183 deletions

View file

@ -18,6 +18,12 @@ abstract class BitbucketDriver extends VcsDriver
protected $tags;
protected $branches;
protected $infoCache = array();
protected $branchesUrl = '';
protected $tagsUrl = '';
protected $homeUrl = '';
protected $website = '';
protected $cloneHttpsUrl = '';
protected $cloneSshUrl = '';
/**
* @var VcsDriver
@ -44,6 +50,40 @@ abstract class BitbucketDriver extends VcsDriver
);
}
/**
* {@inheritDoc}
*/
public function getUrl()
{
return $this->cloneHttpsUrl;
}
/**
* @return array
*/
protected function getRepoData()
{
$resource = sprintf(
'https://api.bitbucket.org/2.0/repositories/%s/%s?%s',
$this->owner,
$this->repository,
http_build_query(
array('fields' => '-project,-owner')
)
);
$repoData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource, true), $resource);
$this->parseCloneUrls($repoData['links']['clone']);
$this->hasIssues = !empty($repoData['has_issues']);
$this->branchesUrl = $repoData['links']['branches']['href'];
$this->tagsUrl = $repoData['links']['tags']['href'];
$this->homeUrl = $repoData['links']['html']['href'];
$this->website = $repoData['website'];
return $repoData;
}
/**
* {@inheritDoc}
*/
@ -102,6 +142,9 @@ abstract class BitbucketDriver extends VcsDriver
$this->repository
);
}
if (!isset($composer['homepage'])) {
$composer['homepage'] = empty($this->website) ? $this->homeUrl : $this->website;
}
$this->infoCache[$identifier] = $composer;
@ -122,8 +165,13 @@ abstract class BitbucketDriver extends VcsDriver
return $this->fallbackDriver->getFileContent($file, $identifier);
}
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'
. $this->owner . '/' . $this->repository . '/src/' . $identifier . '/' . $file;
$resource = sprintf(
'https://api.bitbucket.org/1.0/repositories/%s/%s/src/%s/%s',
$this->owner,
$this->repository,
$identifier,
$file
);
$fileData = JsonFile::parseJson($this->getContents($resource), $resource);
if (!is_array($fileData) || ! array_key_exists('data', $fileData)) {
return null;
@ -148,6 +196,89 @@ abstract class BitbucketDriver extends VcsDriver
return new \DateTime($changeset['timestamp']);
}
/**
* {@inheritDoc}
*/
public function getDist($identifier)
{
$url = sprintf(
'https://bitbucket.org/%s/%s/get/%s.zip',
$this->owner,
$this->repository,
$identifier
);
return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
}
/**
* {@inheritDoc}
*/
public function getTags()
{
if (null === $this->tags) {
$this->tags = array();
$resource = sprintf(
'%s?%s',
$this->tagsUrl,
http_build_query(
array(
'pagelen' => 100,
'fields' => 'values.name,values.target.hash,next'
)
)
);
$hasNext = true;
while ($hasNext) {
$tagsData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
foreach ($tagsData['values'] as $data) {
$this->tags[$data['name']] = $data['target']['hash'];
}
if (empty($tagsData['next'])) {
$hasNext = false;
} else {
$resource = $tagsData['next'];
}
}
}
return $this->tags;
}
/**
* {@inheritDoc}
*/
public function getBranches()
{
if (null === $this->branches) {
$this->branches = array();
$resource = sprintf(
'%s?%s',
$this->branchesUrl,
http_build_query(
array(
'pagelen' => 100,
'fields' => 'values.name,values.target.hash,next'
)
)
);
$hasNext = true;
while ($hasNext) {
$branchData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
foreach ($branchData['values'] as $data) {
$this->branches[$data['name']] = $data['target']['hash'];
}
if (empty($branchData['next'])) {
$hasNext = false;
} else {
$resource = $branchData['next'];
}
}
}
return $this->branches;
}
/**
* Get the remote content.
*
@ -184,7 +315,10 @@ abstract class BitbucketDriver extends VcsDriver
*
* @return string
*/
abstract protected function generateSshUrl();
protected function generateSshUrl()
{
return $this->cloneSshUrl;
}
protected function attemptCloneFallback()
{
@ -202,4 +336,25 @@ abstract class BitbucketDriver extends VcsDriver
}
abstract protected function setupFallbackDriver($url);
/**
* @param array $cloneLinks
* @return void
*/
protected function parseCloneUrls(array $cloneLinks)
{
foreach ($cloneLinks as $cloneLink) {
if ($cloneLink['name'] === 'https') {
// Format: https://(user@)bitbucket.org/{user}/{repo}
// Strip username from URL (only present in clone URL's for private repositories)
$this->cloneHttpsUrl = preg_replace('/https:\/\/([^@]+@)?/', 'https://', $cloneLink['href']);
} elseif ($cloneLink['name'] === 'ssh') {
// Format: ssh://{git or hg}@bitbucket.org/{user}/{repo}
// Replace for git URL's
$href = preg_replace('/ssh:\/\/git@bitbucket\.org\//', 'git@bitbucket.org:', $cloneLink['href']);
// Replace for hg URL's
$this->cloneSshUrl = str_replace('ssh://', '', $href);
}
}
}
}

View file

@ -21,9 +21,6 @@ use Composer\IO\IOInterface;
*/
class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
{
/**
* {@inheritDoc}
*/
@ -34,10 +31,22 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
}
if (null === $this->rootIdentifier) {
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository;
$repoData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource, true), $resource);
$this->hasIssues = !empty($repoData['has_issues']);
$this->rootIdentifier = !empty($repoData['main_branch']) ? $repoData['main_branch'] : 'master';
$repoData = $this->getRepoData();
if ($repoData['scm'] !== 'git') {
throw new \RuntimeException(
$this->url.' does not appear to be a git repository, use '.
$this->cloneHttpsUrl.' if this is a mercurial bitbucket repository'
);
}
$resource = sprintf(
'https://api.bitbucket.org/1.0/repositories/%s/%s/main-branch',
$this->owner,
$this->repository
);
$main_branch_data = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource, true), $resource);
$this->rootIdentifier = !empty($main_branch_data['name']) ? $main_branch_data['name'] : 'master';
}
return $this->rootIdentifier;
@ -52,7 +61,7 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
return $this->fallbackDriver->getUrl();
}
return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
return parent::getUrl();
}
/**
@ -67,17 +76,6 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
}
/**
* {@inheritDoc}
*/
public function getDist($identifier)
{
$url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$identifier.'.zip';
return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
}
/**
* {@inheritDoc}
*/
@ -87,16 +85,7 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
return $this->fallbackDriver->getTags();
}
if (null === $this->tags) {
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
$tagsData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
$this->tags = array();
foreach ($tagsData as $tag => $data) {
$this->tags[$tag] = $data['raw_node'];
}
}
return $this->tags;
return parent::getTags();
}
/**
@ -108,16 +97,7 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
return $this->fallbackDriver->getBranches();
}
if (null === $this->branches) {
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches';
$branchData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
$this->branches = array();
foreach ($branchData as $branch => $data) {
$this->branches[$branch] = $data['raw_node'];
}
}
return $this->branches;
return parent::getBranches();
}
/**
@ -152,12 +132,4 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
);
$this->fallbackDriver->initialize();
}
/**
* {@inheritdoc}
*/
protected function generateSshUrl()
{
return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
}
}

View file

@ -21,33 +21,33 @@ use Composer\IO\IOInterface;
*/
class HgBitbucketDriver extends BitbucketDriver
{
/**
* {@inheritDoc}
*/
public function getRootIdentifier()
{
if (null === $this->rootIdentifier) {
$resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
$repoData = JsonFile::parseJson($this->getContents($resource), $resource);
if (array() === $repoData || !isset($repoData['tip'])) {
throw new \RuntimeException($this->url.' does not appear to be a mercurial repository, use '.$this->url.'.git if this is a git bitbucket repository');
$repoData = $this->getRepoData();
if ($repoData['scm'] !== 'hg') {
throw new \RuntimeException(
$this->url.' does not appear to be a mercurial repository, use '.
$this->cloneHttpsUrl.' if this is a git bitbucket repository'
);
}
$this->hasIssues = !empty($repoData['has_issues']);
$this->rootIdentifier = $repoData['tip']['raw_node'];
$resource = sprintf(
'https://api.bitbucket.org/1.0/repositories/%s/%s/main-branch',
$this->owner,
$this->repository
);
$main_branch_data = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource, true), $resource);
$this->rootIdentifier = !empty($main_branch_data['name']) ? $main_branch_data['name'] : 'default';
}
return $this->rootIdentifier;
}
/**
* {@inheritDoc}
*/
public function getUrl()
{
return $this->url;
}
/**
* {@inheritDoc}
*/
@ -56,51 +56,20 @@ class HgBitbucketDriver extends BitbucketDriver
return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $identifier);
}
/**
* {@inheritDoc}
*/
public function getDist($identifier)
{
$url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$identifier.'.zip';
return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
}
/**
* {@inheritDoc}
*/
public function getTags()
{
if (null === $this->tags) {
$resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags';
$tagsData = JsonFile::parseJson($this->getContents($resource), $resource);
$this->tags = array();
foreach ($tagsData as $tag => $data) {
$this->tags[$tag] = $data['raw_node'];
}
parent::getTags();
if (isset($this->tags['tip'])) {
unset($this->tags['tip']);
}
return $this->tags;
}
/**
* {@inheritDoc}
*/
public function getBranches()
{
if (null === $this->branches) {
$resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches';
$branchData = JsonFile::parseJson($this->getContents($resource), $resource);
$this->branches = array();
foreach ($branchData as $branch => $data) {
$this->branches[$branch] = $data['raw_node'];
}
}
return $this->branches;
}
/**
* {@inheritDoc}
*/
@ -130,12 +99,4 @@ class HgBitbucketDriver extends BitbucketDriver
);
$this->fallbackDriver->initialize();
}
/**
* {@inheritdoc}
*/
protected function generateSshUrl()
{
return 'hg@' . $this->originUrl . '/' . $this->owner.'/'.$this->repository;
}
}