Implement Bitbucket API version 2.0 (where applicable).

main
Stefan Grootscholten 8 years ago
parent b3b05949bb
commit d25c483231

@ -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);
}
}
}
}

@ -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';
}
}

@ -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;
}
}

@ -76,69 +76,96 @@ class GitBitbucketDriverTest extends TestCase
return $driver;
}
public function testGetRootIdentifier()
public function testGetRootIdentifierWrongScmType()
{
$driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
$this->setExpectedException(
'\RuntimeException',
'https://bitbucket.org/user/repo.git does not appear to be a git repository, use https://bitbucket.org/user/repo if this is a mercurial bitbucket repository'
);
$this->rfs->expects($this->any())
$this->rfs->expects($this->once())
->method('getContents')
->with(
$this->originUrl,
'https://api.bitbucket.org/1.0/repositories/user/repo',
'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner',
false
)
->willReturn(
'{"scm": "git", "has_wiki": false, "last_updated": "2016-05-17T13:20:21.993", "no_forks": true, "forks_count": 0, "created_on": "2015-02-18T16:22:24.688", "owner": "user", "logo": "https://bitbucket.org/user/repo/avatar/32/?ts=1463484021", "email_mailinglist": "", "is_mq": false, "size": 9975494, "read_only": false, "fork_of": null, "mq_of": null, "followers_count": 0, "state": "available", "utc_created_on": "2015-02-18 15:22:24+00:00", "website": "", "description": "", "has_issues": false, "is_fork": false, "slug": "repo", "is_private": true, "name": "repo", "language": "php", "utc_last_updated": "2016-05-17 11:20:21+00:00", "no_public_forks": true, "creator": null, "resource_uri": "/1.0/repositories/user/repo"}'
'{"scm":"hg","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo","name":"https"},{"href":"ssh:\/\/hg@bitbucket.org\/user\/repo","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}'
);
$this->assertEquals(
'master',
$driver->getRootIdentifier()
);
}
public function testGetParams()
{
$url = 'https://bitbucket.org/user/repo.git';
$driver = $this->getDriver(array('url' => $url));
$this->assertEquals($url, $driver->getUrl());
$this->assertEquals(
array(
'type' => 'zip',
'url' => 'https://bitbucket.org/user/repo/get/reference.zip',
'reference' => 'reference',
'shasum' => ''
),
$driver->getDist('reference')
);
$driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
$this->assertEquals(
array('type' => 'git', 'url' => $url, 'reference' => 'reference'),
$driver->getSource('reference')
);
$driver->getRootIdentifier();
}
public function testGetComposerInformation()
public function testDriver()
{
$driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
$this->rfs->expects($this->any())
->method('getContents')
->withConsecutive(
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/src/master/composer.json', false),
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/changesets/master', false),
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/tags', false),
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/branches', false)
array(
$this->originUrl,
'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/1.0/repositories/user/repo/main-branch',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/2.0/repositories/user/repo/refs/tags?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cnext',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/2.0/repositories/user/repo/refs/branches?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cnext',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/1.0/repositories/user/repo/src/master/composer.json',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/1.0/repositories/user/repo/changesets/master',
false
)
)
->willReturnOnConsecutiveCalls(
'{"scm":"git","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo.git","name":"https"},{"href":"ssh:\/\/git@bitbucket.org\/user\/repo.git","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}',
'{"name": "master"}',
'{"values":[{"name":"1.0.1","target":{"hash":"9b78a3932143497c519e49b8241083838c8ff8a1"}},{"name":"1.0.0","target":{"hash":"d3393d514318a9267d2f8ebbf463a9aaa389f8eb"}}]}',
'{"values":[{"name":"master","target":{"hash":"937992d19d72b5116c3e8c4a04f960e5fa270b22"}}]}',
'{"node": "937992d19d72", "path": "composer.json", "data": "{\n \"name\": \"user/repo\",\n \"description\": \"test repo\",\n \"license\": \"GPL\",\n \"authors\": [\n {\n \"name\": \"Name\",\n \"email\": \"local@domain.tld\"\n }\n ],\n \"require\": {\n \"creator/package\": \"^1.0\"\n },\n \"require-dev\": {\n \"phpunit/phpunit\": \"~4.8\"\n }\n}\n", "size": 269}',
'{"node": "937992d19d72", "files": [{"type": "modified", "file": "path/to/file"}], "raw_author": "User <local@domain.tld>", "utctimestamp": "2016-05-17 11:19:52+00:00", "author": "user", "timestamp": "2016-05-17 13:19:52", "raw_node": "937992d19d72b5116c3e8c4a04f960e5fa270b22", "parents": ["71e195a33361"], "branch": "master", "message": "Commit message\n", "revision": null, "size": -1}',
'{}',
'{"master": {"node": "937992d19d72", "files": [{"type": "modified", "file": "path/to/file"}], "raw_author": "User <local@domain.tld>", "utctimestamp": "2016-05-17 11:19:52+00:00", "author": "user", "timestamp": "2016-05-17 13:19:52", "raw_node": "937992d19d72b5116c3e8c4a04f960e5fa270b22", "parents": ["71e195a33361"], "branch": "master", "message": "Commit message\n", "revision": null, "size": -1}}'
'{"node": "937992d19d72", "files": [{"type": "modified", "file": "path/to/file"}], "raw_author": "User <local@domain.tld>", "utctimestamp": "2016-05-17 11:19:52+00:00", "author": "user", "timestamp": "2016-05-17 13:19:52", "raw_node": "937992d19d72b5116c3e8c4a04f960e5fa270b22", "parents": ["71e195a33361"], "branch": "master", "message": "Commit message\n", "revision": null, "size": -1}'
);
$this->assertEquals(
'master',
$driver->getRootIdentifier()
);
$this->assertEquals(
array(
'1.0.1' => '9b78a3932143497c519e49b8241083838c8ff8a1',
'1.0.0' => 'd3393d514318a9267d2f8ebbf463a9aaa389f8eb'
),
$driver->getTags()
);
$this->assertEquals(
array(
'master' => '937992d19d72b5116c3e8c4a04f960e5fa270b22'
),
$driver->getBranches()
);
$this->assertEquals(
array(
'name' => 'user/repo',
@ -159,56 +186,38 @@ class GitBitbucketDriverTest extends TestCase
'time' => '2016-05-17 13:19:52',
'support' => array(
'source' => 'https://bitbucket.org/user/repo/src/937992d19d72b5116c3e8c4a04f960e5fa270b22/?at=master'
)
),
'homepage' => 'https://bitbucket.org/user/repo'
),
$driver->getComposerInformation('master')
);
return $driver;
}
public function testGetTags()
/**
* @depends testDriver
* @param \Composer\Repository\Vcs\VcsDriverInterface $driver
*/
public function testGetParams($driver)
{
$driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
$url = 'https://bitbucket.org/user/repo.git';
$this->rfs->expects($this->once())
->method('getContents')
->with(
'bitbucket.org',
'https://api.bitbucket.org/1.0/repositories/user/repo/tags',
false
)
->willReturn(
'{"1.0.1": {"node": "9b78a3932143", "files": [{"type": "modified", "file": "path/to/file"}], "branches": [], "raw_author": "User <local@domain.tld>", "utctimestamp": "2015-04-16 14:50:40+00:00", "author": "user", "timestamp": "2015-04-16 16:50:40", "raw_node": "9b78a3932143497c519e49b8241083838c8ff8a1", "parents": ["84531c04dbfc", "50c2a4635ad0"], "branch": null, "message": "Commit message\n", "revision": null, "size": -1}, "1.0.0": {"node": "d3393d514318", "files": [{"type": "modified", "file": "path/to/file2"}], "branches": [], "raw_author": "User <local@domain.tld>", "utctimestamp": "2015-04-16 09:31:45+00:00", "author": "user", "timestamp": "2015-04-16 11:31:45", "raw_node": "d3393d514318a9267d2f8ebbf463a9aaa389f8eb", "parents": ["5a29a73cd1a0"], "branch": null, "message": "Commit message\n", "revision": null, "size": -1}}'
);
$this->assertEquals($url, $driver->getUrl());
$this->assertEquals(
array(
'1.0.1' => '9b78a3932143497c519e49b8241083838c8ff8a1',
'1.0.0' => 'd3393d514318a9267d2f8ebbf463a9aaa389f8eb'
'type' => 'zip',
'url' => 'https://bitbucket.org/user/repo/get/reference.zip',
'reference' => 'reference',
'shasum' => ''
),
$driver->getTags()
$driver->getDist('reference')
);
}
public function testGetBranches()
{
$driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
$this->rfs->expects($this->once())
->method('getContents')
->with(
'bitbucket.org',
'https://api.bitbucket.org/1.0/repositories/user/repo/branches',
false
)
->willReturn(
'{"master": {"node": "937992d19d72", "files": [{"type": "modified", "file": "path/to/file"}], "raw_author": "User <local@domain.tld>", "utctimestamp": "2016-05-17 11:19:52+00:00", "author": "user", "timestamp": "2016-05-17 13:19:52", "raw_node": "937992d19d72b5116c3e8c4a04f960e5fa270b22", "parents": ["71e195a33361"], "branch": "master", "message": "Commit message\n", "revision": null, "size": -1}}'
);
$this->assertEquals(
array(
'master' => '937992d19d72b5116c3e8c4a04f960e5fa270b22'
),
$driver->getBranches()
array('type' => 'git', 'url' => $url, 'reference' => 'reference'),
$driver->getSource('reference')
);
}

Loading…
Cancel
Save