1
0
Fork 0

Merge remote-tracking branch 'stefangr/implement_bitbucket_api_v2'

pull/6093/merge
Jordi Boggiano 2017-03-07 14:43:16 +01:00
commit 44ea284ab9
9 changed files with 565 additions and 338 deletions

View File

@ -79,7 +79,9 @@ class Config
private $config; private $config;
private $baseDir; private $baseDir;
private $repositories; private $repositories;
/** @var ConfigSourceInterface */
private $configSource; private $configSource;
/** @var ConfigSourceInterface */
private $authConfigSource; private $authConfigSource;
private $useEnvironment; private $useEnvironment;
private $warnedHosts = array(); private $warnedHosts = array();

View File

@ -1,5 +1,15 @@
<?php <?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; namespace Composer\Repository\Vcs;
use Composer\Cache; use Composer\Cache;
@ -18,11 +28,18 @@ abstract class BitbucketDriver extends VcsDriver
protected $tags; protected $tags;
protected $branches; protected $branches;
protected $infoCache = array(); protected $infoCache = array();
protected $branchesUrl = '';
protected $tagsUrl = '';
protected $homeUrl = '';
protected $website = '';
protected $cloneHttpsUrl = '';
/** /**
* @var VcsDriver * @var VcsDriver
*/ */
protected $fallbackDriver; protected $fallbackDriver;
/** @var string|null if set either git or hg */
protected $vcsType;
/** /**
* {@inheritDoc} * {@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} * {@inheritDoc}
*/ */
@ -102,6 +165,9 @@ abstract class BitbucketDriver extends VcsDriver
$this->repository $this->repository
); );
} }
if (!isset($composer['homepage'])) {
$composer['homepage'] = empty($this->website) ? $this->homeUrl : $this->website;
}
$this->infoCache[$identifier] = $composer; $this->infoCache[$identifier] = $composer;
@ -122,14 +188,15 @@ abstract class BitbucketDriver extends VcsDriver
return $this->fallbackDriver->getFileContent($file, $identifier); return $this->fallbackDriver->getFileContent($file, $identifier);
} }
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/' $resource = sprintf(
. $this->owner . '/' . $this->repository . '/src/' . $identifier . '/' . $file; 'https://api.bitbucket.org/1.0/repositories/%s/%s/raw/%s/%s',
$fileData = JsonFile::parseJson($this->getContents($resource), $resource); $this->owner,
if (!is_array($fileData) || ! array_key_exists('data', $fileData)) { $this->repository,
return null; $identifier,
} $file
);
return $fileData['data']; return $this->getContentsWithOAuthCredentials($resource);
} }
/** /**
@ -141,11 +208,131 @@ abstract class BitbucketDriver extends VcsDriver
return $this->fallbackDriver->getChangeDate($identifier); return $this->fallbackDriver->getChangeDate($identifier);
} }
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/' $resource = sprintf(
. $this->owner . '/' . $this->repository . '/changesets/' . $identifier; 'https://api.bitbucket.org/2.0/repositories/%s/%s/commit/%s?fields=date',
$changeset = JsonFile::parseJson($this->getContents($resource), $resource); $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); 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);
}
} }

View File

@ -13,17 +13,13 @@
namespace Composer\Repository\Vcs; namespace Composer\Repository\Vcs;
use Composer\Config; use Composer\Config;
use Composer\Json\JsonFile;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
/** /**
* @author Per Bernhardt <plb@webfactory.de> * @author Per Bernhardt <plb@webfactory.de>
*/ */
class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface class GitBitbucketDriver extends BitbucketDriver
{ {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -34,92 +30,24 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
} }
if (null === $this->rootIdentifier) { if (null === $this->rootIdentifier) {
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository; if (! $this->getRepoData()) {
$repoData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource, true), $resource); return $this->fallbackDriver->getRootIdentifier();
$this->hasIssues = !empty($repoData['has_issues']); }
$this->rootIdentifier = !empty($repoData['main_branch']) ? $repoData['main_branch'] : 'master';
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; 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} * {@inheritDoc}
*/ */
@ -139,7 +67,7 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
} }
/** /**
* @param string $url * {@inheritdoc}
*/ */
protected function setupFallbackDriver($url) protected function setupFallbackDriver($url)
{ {

View File

@ -13,7 +13,6 @@
namespace Composer\Repository\Vcs; namespace Composer\Repository\Vcs;
use Composer\Config; use Composer\Config;
use Composer\Json\JsonFile;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
/** /**
@ -21,86 +20,34 @@ use Composer\IO\IOInterface;
*/ */
class HgBitbucketDriver extends BitbucketDriver class HgBitbucketDriver extends BitbucketDriver
{ {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getRootIdentifier() public function getRootIdentifier()
{ {
if ($this->fallbackDriver) {
return $this->fallbackDriver->getRootIdentifier();
}
if (null === $this->rootIdentifier) { if (null === $this->rootIdentifier) {
$resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'; if (! $this->getRepoData()) {
$repoData = JsonFile::parseJson($this->getContents($resource), $resource); return $this->fallbackDriver->getRootIdentifier();
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');
} }
$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; 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} * {@inheritDoc}
*/ */
@ -119,6 +66,9 @@ class HgBitbucketDriver extends BitbucketDriver
return true; return true;
} }
/**
* {@inheritdoc}
*/
protected function setupFallbackDriver($url) protected function setupFallbackDriver($url)
{ {
$this->fallbackDriver = new HgDriver( $this->fallbackDriver = new HgDriver(
@ -136,6 +86,6 @@ class HgBitbucketDriver extends BitbucketDriver
*/ */
protected function generateSshUrl() protected function generateSshUrl()
{ {
return 'hg@' . $this->originUrl . '/' . $this->owner.'/'.$this->repository; return 'ssh://hg@' . $this->originUrl . '/' . $this->owner.'/'.$this->repository;
} }
} }

View File

@ -27,6 +27,7 @@ class Bitbucket
private $process; private $process;
private $remoteFilesystem; private $remoteFilesystem;
private $token = array(); private $token = array();
private $time;
const OAUTH2_ACCESS_TOKEN_URL = 'https://bitbucket.org/site/oauth2/access_token'; const OAUTH2_ACCESS_TOKEN_URL = 'https://bitbucket.org/site/oauth2/access_token';
@ -37,21 +38,26 @@ class Bitbucket
* @param Config $config The composer configuration * @param Config $config The composer configuration
* @param ProcessExecutor $process Process instance, injectable for mocking * @param ProcessExecutor $process Process instance, injectable for mocking
* @param RemoteFilesystem $remoteFilesystem Remote Filesystem, injectable for mocking * @param RemoteFilesystem $remoteFilesystem Remote Filesystem, injectable for mocking
* @param int $time Timestamp, injectable for mocking
*/ */
public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null) public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null, $time = null)
{ {
$this->io = $io; $this->io = $io;
$this->config = $config; $this->config = $config;
$this->process = $process ?: new ProcessExecutor; $this->process = $process ?: new ProcessExecutor;
$this->remoteFilesystem = $remoteFilesystem ?: Factory::createRemoteFilesystem($this->io, $config); $this->remoteFilesystem = $remoteFilesystem ?: Factory::createRemoteFilesystem($this->io, $config);
$this->time = $time;
} }
/** /**
* @return array * @return string
*/ */
public function getToken() public function getToken()
{ {
return $this->token; if (! isset($this->token['access_token'])) {
return '';
}
return $this->token['access_token'];
} }
/** /**
@ -109,6 +115,8 @@ class Bitbucket
throw $e; throw $e;
} }
return true;
} }
/** /**
@ -151,16 +159,13 @@ class Bitbucket
$this->io->setAuthentication($originUrl, $consumerKey, $consumerSecret); $this->io->setAuthentication($originUrl, $consumerKey, $consumerSecret);
$this->requestAccessToken($originUrl); if (! $this->requestAccessToken($originUrl)) {
return false;
}
// store value in user config // store value in user config
$this->config->getConfigSource()->removeConfigSetting('bitbucket-oauth.'.$originUrl); $this->storeInAuthConfig($originUrl, $consumerKey, $consumerSecret);
$consumer = array(
"consumer-key" => $consumerKey,
"consumer-secret" => $consumerSecret,
);
$this->config->getAuthConfigSource()->addConfigSetting('bitbucket-oauth.'.$originUrl, $consumer);
// Remove conflicting basic auth credentials (if available) // Remove conflicting basic auth credentials (if available)
$this->config->getAuthConfigSource()->removeConfigSetting('http-basic.' . $originUrl); $this->config->getAuthConfigSource()->removeConfigSetting('http-basic.' . $originUrl);
@ -175,17 +180,64 @@ class Bitbucket
* @param string $originUrl * @param string $originUrl
* @param string $consumerKey * @param string $consumerKey
* @param string $consumerSecret * @param string $consumerSecret
* @return array * @return string
*/ */
public function requestToken($originUrl, $consumerKey, $consumerSecret) public function requestToken($originUrl, $consumerKey, $consumerSecret)
{ {
if (!empty($this->token)) { if (!empty($this->token) || $this->getTokenFromConfig($originUrl)) {
return $this->token; return $this->token['access_token'];
} }
$this->io->setAuthentication($originUrl, $consumerKey, $consumerSecret); $this->io->setAuthentication($originUrl, $consumerKey, $consumerSecret);
$this->requestAccessToken($originUrl); if (! $this->requestAccessToken($originUrl)) {
return '';
}
return $this->token; $this->storeInAuthConfig($originUrl, $consumerKey, $consumerSecret);
return $this->token['access_token'];
}
/**
* Store the new/updated credentials to the configuration
* @param string $originUrl
* @param string $consumerKey
* @param string $consumerSecret
*/
private function storeInAuthConfig($originUrl, $consumerKey, $consumerSecret)
{
$this->config->getConfigSource()->removeConfigSetting('bitbucket-oauth.'.$originUrl);
$time = null === $this->time ? time() : $this->time;
$consumer = array(
"consumer-key" => $consumerKey,
"consumer-secret" => $consumerSecret,
"access-token" => $this->token['access_token'],
"access-token-expiration" => $time + $this->token['expires_in']
);
$this->config->getAuthConfigSource()->addConfigSetting('bitbucket-oauth.'.$originUrl, $consumer);
}
/**
* @param string $originUrl
* @return bool
*/
private function getTokenFromConfig($originUrl)
{
$authConfig = $this->config->get('bitbucket-oauth');
if (! isset($authConfig[$originUrl]['access-token']) ||
! isset($authConfig[$originUrl]['access-token-expiration']) ||
time() > $authConfig[$originUrl]['access-token-expiration']
) {
return false;
}
$this->token = array(
'access_token' => $authConfig[$originUrl]['access-token']
);
return true;
} }
} }

View File

@ -122,17 +122,17 @@ class Git
if (!$bitbucketUtil->authorizeOAuth($match[1]) && $this->io->isInteractive()) { if (!$bitbucketUtil->authorizeOAuth($match[1]) && $this->io->isInteractive()) {
$bitbucketUtil->authorizeOAuthInteractively($match[1], $message); $bitbucketUtil->authorizeOAuthInteractively($match[1], $message);
$token = $bitbucketUtil->getToken(); $accessToken = $bitbucketUtil->getToken();
$this->io->setAuthentication($match[1], 'x-token-auth', $token['access_token']); $this->io->setAuthentication($match[1], 'x-token-auth', $accessToken);
} }
} else { //We're authenticating with a locally stored consumer. } else { //We're authenticating with a locally stored consumer.
$auth = $this->io->getAuthentication($match[1]); $auth = $this->io->getAuthentication($match[1]);
//We already have an access_token from a previous request. //We already have an access_token from a previous request.
if ($auth['username'] !== 'x-token-auth') { if ($auth['username'] !== 'x-token-auth') {
$token = $bitbucketUtil->requestToken($match[1], $auth['username'], $auth['password']); $accessToken = $bitbucketUtil->requestToken($match[1], $auth['username'], $auth['password']);
if (!empty($token)) { if (! empty($accessToken)) {
$this->io->setAuthentication($match[1], 'x-token-auth', $token['access_token']); $this->io->setAuthentication($match[1], 'x-token-auth', $accessToken);
} }
} }
} }

View File

@ -245,14 +245,6 @@ class RemoteFilesystem
unset($options['gitlab-token']); unset($options['gitlab-token']);
} }
if (isset($options['bitbucket-token'])) {
// skip using the token for BitBucket downloads as these are not working with auth
if (!$this->isPublicBitBucketDownload($origFileUrl)) {
$fileUrl .= (false === strpos($fileUrl,'?') ? '?' : '&') . 'access_token=' . $options['bitbucket-token'];
}
unset($options['bitbucket-token']);
}
if (isset($options['http'])) { if (isset($options['http'])) {
$options['http']['ignore_errors'] = true; $options['http']['ignore_errors'] = true;
} }
@ -607,9 +599,9 @@ class RemoteFilesystem
$auth = $this->io->getAuthentication($this->originUrl); $auth = $this->io->getAuthentication($this->originUrl);
if ($auth['username'] !== 'x-token-auth') { if ($auth['username'] !== 'x-token-auth') {
$bitbucketUtil = new Bitbucket($this->io, $this->config); $bitbucketUtil = new Bitbucket($this->io, $this->config);
$token = $bitbucketUtil->requestToken($this->originUrl, $auth['username'], $auth['password']); $accessToken = $bitbucketUtil->requestToken($this->originUrl, $auth['username'], $auth['password']);
if (! empty($token)) { if (! empty($accessToken)) {
$this->io->setAuthentication($this->originUrl, 'x-token-auth', $token['access_token']); $this->io->setAuthentication($this->originUrl, 'x-token-auth', $accessToken);
$askForOAuthToken = false; $askForOAuthToken = false;
} }
} else { } else {
@ -736,7 +728,9 @@ class RemoteFilesystem
} elseif ('bitbucket.org' === $originUrl } elseif ('bitbucket.org' === $originUrl
&& $this->fileUrl !== Bitbucket::OAUTH2_ACCESS_TOKEN_URL && 'x-token-auth' === $auth['username'] && $this->fileUrl !== Bitbucket::OAUTH2_ACCESS_TOKEN_URL && 'x-token-auth' === $auth['username']
) { ) {
$options['bitbucket-token'] = $auth['password']; if (!$this->isPublicBitBucketDownload($this->fileUrl)) {
$headers[] = 'Authorization: Bearer ' . $auth['password'];
}
} else { } else {
$authStr = base64_encode($auth['username'] . ':' . $auth['password']); $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
$headers[] = 'Authorization: Basic '.$authStr; $headers[] = 'Authorization: Basic '.$authStr;
@ -1009,6 +1003,13 @@ class RemoteFilesystem
*/ */
private function isPublicBitBucketDownload($urlToBitBucketFile) private function isPublicBitBucketDownload($urlToBitBucketFile)
{ {
$domain = parse_url($urlToBitBucketFile, PHP_URL_HOST);
if (strpos($domain, 'bitbucket.org') === false) {
// Bitbucket downloads are hosted on amazonaws.
// We do not need to authenticate there at all
return true;
}
$path = parse_url($urlToBitBucketFile, PHP_URL_PATH); $path = parse_url($urlToBitBucketFile, PHP_URL_PATH);
// Path for a public download follows this pattern /{user}/{repo}/downloads/{whatever} // Path for a public download follows this pattern /{user}/{repo}/downloads/{whatever}

View File

@ -76,69 +76,96 @@ class GitBitbucketDriverTest extends TestCase
return $driver; 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') ->method('getContents')
->with( ->with(
$this->originUrl, $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 false
) )
->willReturn( ->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( $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
'master',
$driver->getRootIdentifier() $driver->getRootIdentifier();
);
} }
public function testGetParams() public function testDriver()
{
$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')
);
$this->assertEquals(
array('type' => 'git', 'url' => $url, 'reference' => 'reference'),
$driver->getSource('reference')
);
}
public function testGetComposerInformation()
{ {
$driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git')); $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
$this->rfs->expects($this->any()) $this->rfs->expects($this->any())
->method('getContents') ->method('getContents')
->withConsecutive( ->withConsecutive(
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/src/master/composer.json', false), array(
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/changesets/master', false), $this->originUrl,
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/tags', false), 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner',
array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/branches', false) 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&sort=-target.date',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/2.0/repositories/user/repo/refs/branches?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cnext&sort=-target.date',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/1.0/repositories/user/repo/raw/master/composer.json',
false
),
array(
$this->originUrl,
'https://api.bitbucket.org/2.0/repositories/user/repo/commit/master?fields=date',
false
)
) )
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
'{"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}', '{"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}',
'{"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}', '{"name": "master"}',
'{}', '{"values":[{"name":"1.0.1","target":{"hash":"9b78a3932143497c519e49b8241083838c8ff8a1"}},{"name":"1.0.0","target":{"hash":"d3393d514318a9267d2f8ebbf463a9aaa389f8eb"}}]}',
'{"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}}' '{"values":[{"name":"master","target":{"hash":"937992d19d72b5116c3e8c4a04f960e5fa270b22"}}]}',
'{"name": "user/repo","description": "test repo","license": "GPL","authors": [{"name": "Name","email": "local@domain.tld"}],"require": {"creator/package": "^1.0"},"require-dev": {"phpunit/phpunit": "~4.8"}}',
'{"date": "2016-05-17T13:19:52+00:00"}'
); );
$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( $this->assertEquals(
array( array(
'name' => 'user/repo', 'name' => 'user/repo',
@ -159,56 +186,38 @@ class GitBitbucketDriverTest extends TestCase
'time' => '2016-05-17 13:19:52', 'time' => '2016-05-17 13:19:52',
'support' => array( 'support' => array(
'source' => 'https://bitbucket.org/user/repo/src/937992d19d72b5116c3e8c4a04f960e5fa270b22/?at=master' 'source' => 'https://bitbucket.org/user/repo/src/937992d19d72b5116c3e8c4a04f960e5fa270b22/?at=master'
) ),
'homepage' => 'https://bitbucket.org/user/repo'
), ),
$driver->getComposerInformation('master') $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()) $this->assertEquals($url, $driver->getUrl());
->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( $this->assertEquals(
array( array(
'1.0.1' => '9b78a3932143497c519e49b8241083838c8ff8a1', 'type' => 'zip',
'1.0.0' => 'd3393d514318a9267d2f8ebbf463a9aaa389f8eb' '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( $this->assertEquals(
array( array('type' => 'git', 'url' => $url, 'reference' => 'reference'),
'master' => '937992d19d72b5116c3e8c4a04f960e5fa270b22' $driver->getSource('reference')
),
$driver->getBranches()
); );
} }

View File

@ -35,6 +35,8 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
private $config; private $config;
/** @type Bitbucket */ /** @type Bitbucket */
private $bitbucket; private $bitbucket;
/** @var int */
private $time;
protected function setUp() protected function setUp()
{ {
@ -52,7 +54,9 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
$this->config = $this->getMock('Composer\Config'); $this->config = $this->getMock('Composer\Config');
$this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->rfs); $this->time = time();
$this->bitbucket = new Bitbucket($this->io, $this->config, null, $this->rfs, $this->time);
} }
public function testRequestAccessTokenWithValidOAuthConsumer() public function testRequestAccessTokenWithValidOAuthConsumer()
@ -82,14 +86,86 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
) )
); );
$this->config->expects($this->once())
->method('get')
->with('bitbucket-oauth')
->willReturn(null);
$this->setExpectationsForStoringAccessToken();
$this->assertEquals( $this->assertEquals(
array( $this->token,
'access_token' => $this->token, $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
'scopes' => 'repository', );
'expires_in' => 3600, }
'refresh_token' => 'refreshtoken',
'token_type' => 'bearer' public function testRequestAccessTokenWithValidOAuthConsumerAndValidStoredAccessToken()
), {
$this->config->expects($this->once())
->method('get')
->with('bitbucket-oauth')
->willReturn(
array(
$this->origin => array(
'access-token' => $this->token,
'access-token-expiration' => $this->time + 1800,
'consumer-key' => $this->consumer_key,
'consumer-secret' => $this->consumer_secret
)
)
);
$this->assertEquals(
$this->token,
$this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
);
}
public function testRequestAccessTokenWithValidOAuthConsumerAndExpiredAccessToken()
{
$this->config->expects($this->once())
->method('get')
->with('bitbucket-oauth')
->willReturn(
array(
$this->origin => array(
'access-token' => 'randomExpiredToken',
'access-token-expiration' => $this->time - 400,
'consumer-key' => $this->consumer_key,
'consumer-secret' => $this->consumer_secret
)
)
);
$this->io->expects($this->once())
->method('setAuthentication')
->with($this->origin, $this->consumer_key, $this->consumer_secret);
$this->rfs->expects($this->once())
->method('getContents')
->with(
$this->origin,
Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
false,
array(
'retry-auth-failure' => false,
'http' => array(
'method' => 'POST',
'content' => 'grant_type=client_credentials',
)
)
)
->willReturn(
sprintf(
'{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refreshtoken", "token_type": "bearer"}',
$this->token
)
);
$this->setExpectationsForStoringAccessToken();
$this->assertEquals(
$this->token,
$this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret) $this->bitbucket->requestToken($this->origin, $this->consumer_key, $this->consumer_secret)
); );
} }
@ -133,7 +209,12 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
) )
); );
$this->assertEquals(array(), $this->bitbucket->requestToken($this->origin, $this->username, $this->password)); $this->config->expects($this->once())
->method('get')
->with('bitbucket-oauth')
->willReturn(null);
$this->assertEquals('', $this->bitbucket->requestToken($this->origin, $this->username, $this->password));
} }
public function testUsernamePasswordAuthenticationFlow() public function testUsernamePasswordAuthenticationFlow()
@ -161,67 +242,51 @@ class BitbucketTest extends \PHPUnit_Framework_TestCase
$this->isFalse(), $this->isFalse(),
$this->anything() $this->anything()
) )
->willReturn(sprintf('{}', $this->token)) ->willReturn(
; sprintf(
'{"access_token": "%s", "scopes": "repository", "expires_in": 3600, "refresh_token": "refresh_token", "token_type": "bearer"}',
$authJson = $this->getAuthJsonMock(); $this->token
$this->config
->expects($this->exactly(3))
->method('getAuthConfigSource')
->willReturn($authJson)
;
$this->config
->expects($this->once())
->method('getConfigSource')
->willReturn($this->getConfJsonMock())
;
$authJson->expects($this->once())
->method('addConfigSetting')
->with(
'bitbucket-oauth.'.$this->origin,
array(
'consumer-key' => $this->consumer_key,
'consumer-secret' => $this->consumer_secret
) )
); )
;
$authJson->expects($this->once()) $this->setExpectationsForStoringAccessToken(true);
->method('removeConfigSetting')
->with('http-basic.'.$this->origin);
$this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message)); $this->assertTrue($this->bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
} }
private function getAuthJsonMock() private function setExpectationsForStoringAccessToken($removeBasicAuth = false)
{ {
$authjson = $this $configSourceMock = $this->getMock('Composer\Config\ConfigSourceInterface');
->getMockBuilder('Composer\Config\JsonConfigSource') $this->config->expects($this->once())
->disableOriginalConstructor() ->method('getConfigSource')
->getMock() ->willReturn($configSourceMock);
;
$authjson
->expects($this->atLeastOnce())
->method('getName')
->willReturn('auth.json')
;
return $authjson; $configSourceMock->expects($this->once())
}
private function getConfJsonMock()
{
$confjson = $this
->getMockBuilder('Composer\Config\JsonConfigSource')
->disableOriginalConstructor()
->getMock()
;
$confjson
->expects($this->atLeastOnce())
->method('removeConfigSetting') ->method('removeConfigSetting')
->with('bitbucket-oauth.'.$this->origin) ->with('bitbucket-oauth.' . $this->origin);
;
return $confjson; $authConfigSourceMock = $this->getMock('Composer\Config\ConfigSourceInterface');
$this->config->expects($this->atLeastOnce())
->method('getAuthConfigSource')
->willReturn($authConfigSourceMock);
$authConfigSourceMock->expects($this->once())
->method('addConfigSetting')
->with(
'bitbucket-oauth.' . $this->origin,
array(
"consumer-key" => $this->consumer_key,
"consumer-secret" => $this->consumer_secret,
"access-token" => $this->token,
"access-token-expiration" => $this->time + 3600
)
);
if ($removeBasicAuth) {
$authConfigSourceMock->expects($this->once())
->method('removeConfigSetting')
->with('http-basic.' . $this->origin);
}
} }
} }