123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?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;
- use Composer\Downloader\TransportException;
- use Composer\Json\JsonFile;
- use Composer\Util\Bitbucket;
- abstract class BitbucketDriver extends VcsDriver
- {
- /** @var Cache */
- protected $cache;
- protected $owner;
- protected $repository;
- protected $hasIssues;
- protected $rootIdentifier;
- 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}
- */
- public function initialize()
- {
- preg_match('#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)$#', $this->url, $match);
- $this->owner = $match[1];
- $this->repository = $match[2];
- $this->originUrl = 'bitbucket.org';
- $this->cache = new Cache(
- $this->io,
- implode('/', array(
- $this->config->get('cache-repo-dir'),
- $this->originUrl,
- $this->owner,
- $this->repository,
- ))
- );
- }
- /**
- * {@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}
- */
- public function getComposerInformation($identifier)
- {
- if ($this->fallbackDriver) {
- return $this->fallbackDriver->getComposerInformation($identifier);
- }
- if (!isset($this->infoCache[$identifier])) {
- if ($this->shouldCache($identifier) && $res = $this->cache->read($identifier)) {
- return $this->infoCache[$identifier] = JsonFile::parseJson($res);
- }
- $composer = $this->getBaseComposerInformation($identifier);
- // specials for bitbucket
- 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 (!isset($composer['homepage'])) {
- $composer['homepage'] = empty($this->website) ? $this->homeUrl : $this->website;
- }
- $this->infoCache[$identifier] = $composer;
- if ($this->shouldCache($identifier)) {
- $this->cache->write($identifier, json_encode($composer));
- }
- }
- return $this->infoCache[$identifier];
- }
- /**
- * {@inheritdoc}
- */
- public function getFileContent($file, $identifier)
- {
- if ($this->fallbackDriver) {
- return $this->fallbackDriver->getFileContent($file, $identifier);
- }
- if (strpos($identifier, '/') !== false) {
- $branches = $this->getBranches();
- if (isset($branches[$identifier])) {
- $identifier = $branches[$identifier];
- }
- }
- $resource = sprintf(
- 'https://api.bitbucket.org/1.0/repositories/%s/%s/raw/%s/%s',
- $this->owner,
- $this->repository,
- $identifier,
- $file
- );
- return $this->getContentsWithOAuthCredentials($resource);
- }
- /**
- * {@inheritdoc}
- */
- public function getChangeDate($identifier)
- {
- if ($this->fallbackDriver) {
- return $this->fallbackDriver->getChangeDate($identifier);
- }
- $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($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,values.heads,next',
- 'sort' => '-target.date',
- ),
- null,
- '&'
- )
- );
- $hasNext = true;
- while ($hasNext) {
- $branchData = JsonFile::parseJson($this->getContentsWithOAuthCredentials($resource), $resource);
- foreach ($branchData['values'] as $data) {
- // skip headless branches which seem to be deleted branches that bitbucket nevertheless returns in the API
- if ($this->vcsType === 'hg' && empty($data['heads'])) {
- continue;
- }
- $this->branches[$data['name']] = $data['target']['hash'];
- }
- if (empty($branchData['next'])) {
- $hasNext = false;
- } else {
- $resource = $branchData['next'];
- }
- }
- }
- return $this->branches;
- }
- /**
- * 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);
- if (403 === $e->getCode() || (401 === $e->getCode() && strpos($e->getMessage(), 'Could not authenticate against') === 0)) {
- if (!$this->io->hasAuthentication($this->originUrl)
- && $bitbucketUtil->authorizeOAuth($this->originUrl)
- ) {
- return parent::getContents($url);
- }
- if (!$this->io->isInteractive() && $fetchingRepoData) {
- return $this->attemptCloneFallback();
- }
- }
- throw $e;
- }
- }
- /**
- * Generate an SSH URL
- *
- * @return string
- */
- abstract protected function generateSshUrl();
- protected function attemptCloneFallback()
- {
- try {
- $this->setupFallbackDriver($this->generateSshUrl());
- } catch (\RuntimeException $e) {
- $this->fallbackDriver = 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;
- }
- }
- /**
- * @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);
- }
- }
|