mirror of
https://github.com/composer/composer
synced 2025-05-09 00:22:53 +00:00
Merge remote-tracking branch 'stefangr/implement_bitbucket_api_v2'
This commit is contained in:
commit
44ea284ab9
9 changed files with 565 additions and 338 deletions
|
@ -1,5 +1,15 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer\Repository\Vcs;
|
||||
|
||||
use Composer\Cache;
|
||||
|
@ -18,11 +28,18 @@ abstract class BitbucketDriver extends VcsDriver
|
|||
protected $tags;
|
||||
protected $branches;
|
||||
protected $infoCache = array();
|
||||
protected $branchesUrl = '';
|
||||
protected $tagsUrl = '';
|
||||
protected $homeUrl = '';
|
||||
protected $website = '';
|
||||
protected $cloneHttpsUrl = '';
|
||||
|
||||
/**
|
||||
* @var VcsDriver
|
||||
*/
|
||||
protected $fallbackDriver;
|
||||
/** @var string|null if set either git or hg */
|
||||
protected $vcsType;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
|
@ -44,6 +61,52 @@ abstract class BitbucketDriver extends VcsDriver
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->getUrl();
|
||||
}
|
||||
|
||||
return $this->cloneHttpsUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to fetch the repository data via the BitBucket API and
|
||||
* sets some parameters which are used in other methods
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
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'),
|
||||
null,
|
||||
'&'
|
||||
)
|
||||
);
|
||||
|
||||
$repoData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource, true), $resource);
|
||||
if ($this->fallbackDriver) {
|
||||
return false;
|
||||
}
|
||||
$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'];
|
||||
$this->vcsType = $repoData['scm'];
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -102,6 +165,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,14 +188,15 @@ 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;
|
||||
$fileData = JsonFile::parseJson($this->getContents($resource), $resource);
|
||||
if (!is_array($fileData) || ! array_key_exists('data', $fileData)) {
|
||||
return null;
|
||||
}
|
||||
$resource = sprintf(
|
||||
'https://api.bitbucket.org/1.0/repositories/%s/%s/raw/%s/%s',
|
||||
$this->owner,
|
||||
$this->repository,
|
||||
$identifier,
|
||||
$file
|
||||
);
|
||||
|
||||
return $fileData['data'];
|
||||
return $this->getContentsWithOAuthCredentials($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -141,11 +208,131 @@ abstract class BitbucketDriver extends VcsDriver
|
|||
return $this->fallbackDriver->getChangeDate($identifier);
|
||||
}
|
||||
|
||||
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'
|
||||
. $this->owner . '/' . $this->repository . '/changesets/' . $identifier;
|
||||
$changeset = JsonFile::parseJson($this->getContents($resource), $resource);
|
||||
$resource = sprintf(
|
||||
'https://api.bitbucket.org/2.0/repositories/%s/%s/commit/%s?fields=date',
|
||||
$this->owner,
|
||||
$this->repository,
|
||||
$identifier
|
||||
);
|
||||
$commit = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
|
||||
|
||||
return new \DateTime($changeset['timestamp']);
|
||||
return new \DateTime($commit['date']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getSource($identifier)
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->getSource($identifier);
|
||||
}
|
||||
|
||||
return array('type' => $this->vcsType, 'url' => $this->getUrl(), 'reference' => $identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getDist($identifier)
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->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 ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->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',
|
||||
'sort' => '-target.date'
|
||||
),
|
||||
null,
|
||||
'&'
|
||||
)
|
||||
);
|
||||
$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'];
|
||||
}
|
||||
}
|
||||
if ($this->vcsType === 'hg') {
|
||||
unset($this->tags['tip']);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getBranches()
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->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',
|
||||
'sort' => '-target.date'
|
||||
),
|
||||
null,
|
||||
'&'
|
||||
)
|
||||
);
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -201,5 +388,38 @@ abstract class BitbucketDriver extends VcsDriver
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return void
|
||||
*/
|
||||
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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getMainBranchData()
|
||||
{
|
||||
$resource = sprintf(
|
||||
'https://api.bitbucket.org/1.0/repositories/%s/%s/main-branch',
|
||||
$this->owner,
|
||||
$this->repository
|
||||
);
|
||||
|
||||
return JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,17 +13,13 @@
|
|||
namespace Composer\Repository\Vcs;
|
||||
|
||||
use Composer\Config;
|
||||
use Composer\Json\JsonFile;
|
||||
use Composer\IO\IOInterface;
|
||||
|
||||
/**
|
||||
* @author Per Bernhardt <plb@webfactory.de>
|
||||
*/
|
||||
class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
|
||||
class GitBitbucketDriver extends BitbucketDriver
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -34,92 +30,24 @@ 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';
|
||||
if (! $this->getRepoData()) {
|
||||
return $this->fallbackDriver->getRootIdentifier();
|
||||
}
|
||||
|
||||
if ($this->vcsType !== 'git') {
|
||||
throw new \RuntimeException(
|
||||
$this->url.' does not appear to be a git repository, use '.
|
||||
$this->cloneHttpsUrl.' if this is a mercurial bitbucket repository'
|
||||
);
|
||||
}
|
||||
|
||||
$mainBranchData = $this->getMainBranchData();
|
||||
$this->rootIdentifier = !empty($mainBranchData['name']) ? $mainBranchData['name'] : 'master';
|
||||
}
|
||||
|
||||
return $this->rootIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->getUrl();
|
||||
}
|
||||
|
||||
return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getSource($identifier)
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->getSource($identifier);
|
||||
}
|
||||
|
||||
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}
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getBranches()
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -139,7 +67,7 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setupFallbackDriver($url)
|
||||
{
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
namespace Composer\Repository\Vcs;
|
||||
|
||||
use Composer\Config;
|
||||
use Composer\Json\JsonFile;
|
||||
use Composer\IO\IOInterface;
|
||||
|
||||
/**
|
||||
|
@ -21,86 +20,34 @@ use Composer\IO\IOInterface;
|
|||
*/
|
||||
class HgBitbucketDriver extends BitbucketDriver
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getRootIdentifier()
|
||||
{
|
||||
if ($this->fallbackDriver) {
|
||||
return $this->fallbackDriver->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');
|
||||
if (! $this->getRepoData()) {
|
||||
return $this->fallbackDriver->getRootIdentifier();
|
||||
}
|
||||
$this->hasIssues = !empty($repoData['has_issues']);
|
||||
$this->rootIdentifier = $repoData['tip']['raw_node'];
|
||||
|
||||
if ($this->vcsType !== 'hg') {
|
||||
throw new \RuntimeException(
|
||||
$this->url.' does not appear to be a mercurial repository, use '.
|
||||
$this->cloneHttpsUrl.' if this is a git bitbucket repository'
|
||||
);
|
||||
}
|
||||
|
||||
$mainBranchData = $this->getMainBranchData();
|
||||
$this->rootIdentifier = !empty($mainBranchData['name']) ? $mainBranchData['name'] : 'default';
|
||||
}
|
||||
|
||||
return $this->rootIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getSource($identifier)
|
||||
{
|
||||
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'];
|
||||
}
|
||||
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}
|
||||
*/
|
||||
|
@ -119,6 +66,9 @@ class HgBitbucketDriver extends BitbucketDriver
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setupFallbackDriver($url)
|
||||
{
|
||||
$this->fallbackDriver = new HgDriver(
|
||||
|
@ -136,6 +86,6 @@ class HgBitbucketDriver extends BitbucketDriver
|
|||
*/
|
||||
protected function generateSshUrl()
|
||||
{
|
||||
return 'hg@' . $this->originUrl . '/' . $this->owner.'/'.$this->repository;
|
||||
return 'ssh://hg@' . $this->originUrl . '/' . $this->owner.'/'.$this->repository;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue