123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- <?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\Config;
- use Composer\Cache;
- use Composer\IO\IOInterface;
- use Composer\Json\JsonFile;
- use Composer\Downloader\TransportException;
- use Composer\Util\HttpDownloader;
- use Composer\Util\GitLab;
- use Composer\Util\Http\Response;
- /**
- * Driver for GitLab API, use the Git driver for local checkouts.
- *
- * @author Henrik Bjørnskov <henrik@bjrnskov.dk>
- * @author Jérôme Tamarelle <jerome@tamarelle.net>
- */
- class GitLabDriver extends VcsDriver
- {
- private $scheme;
- private $namespace;
- private $repository;
- /**
- * @var array Project data returned by GitLab API
- */
- private $project;
- /**
- * @var array Keeps commits returned by GitLab API
- */
- private $commits = array();
- /**
- * @var array List of tag => reference
- */
- private $tags;
- /**
- * @var array List of branch => reference
- */
- private $branches;
- /**
- * Git Driver
- *
- * @var GitDriver
- */
- protected $gitDriver;
- /**
- * Defaults to true unless we can make sure it is public
- *
- * @var bool defines whether the repo is private or not
- */
- private $isPrivate = true;
- /**
- * @var int port number
- */
- protected $portNumber;
- const URL_REGEX = '#^(?:(?P<scheme>https?)://(?P<domain>.+?)(?::(?P<port>[0-9]+))?/|git@(?P<domain2>[^:]+):)(?P<parts>.+)/(?P<repo>[^/]+?)(?:\.git|/)?$#';
- /**
- * Extracts information from the repository url.
- *
- * SSH urls use https by default. Set "secure-http": false on the repository config to use http instead.
- *
- * {@inheritDoc}
- */
- public function initialize()
- {
- if (!preg_match(self::URL_REGEX, $this->url, $match)) {
- throw new \InvalidArgumentException('The URL provided is invalid. It must be the HTTP URL of a GitLab project.');
- }
- $guessedDomain = !empty($match['domain']) ? $match['domain'] : $match['domain2'];
- $configuredDomains = $this->config->get('gitlab-domains');
- $urlParts = explode('/', $match['parts']);
- $this->scheme = !empty($match['scheme'])
- ? $match['scheme']
- : (isset($this->repoConfig['secure-http']) && $this->repoConfig['secure-http'] === false ? 'http' : 'https')
- ;
- $this->originUrl = $this->determineOrigin($configuredDomains, $guessedDomain, $urlParts);
- if (!empty($match['port']) && true === is_numeric($match['port'])) {
- // If it is an HTTP based URL, and it has a port
- $this->portNumber = (int) $match['port'];
- }
- $this->namespace = implode('/', $urlParts);
- $this->repository = preg_replace('#(\.git)$#', '', $match['repo']);
- $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->namespace.'/'.$this->repository);
- $this->fetchProject();
- }
- /**
- * Updates the HttpDownloader instance.
- * Mainly useful for tests.
- *
- * @internal
- */
- public function setHttpDownloader(HttpDownloader $httpDownloader)
- {
- $this->httpDownloader = $httpDownloader;
- }
- /**
- * {@inheritdoc}
- */
- public function getFileContent($file, $identifier)
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getFileContent($file, $identifier);
- }
- // Convert the root identifier to a cacheable commit id
- if (!preg_match('{[a-f0-9]{40}}i', $identifier)) {
- $branches = $this->getBranches();
- if (isset($branches[$identifier])) {
- $identifier = $branches[$identifier];
- }
- }
- $resource = $this->getApiUrl().'/repository/files/'.$this->urlEncodeAll($file).'/raw?ref='.$identifier;
- try {
- $content = $this->getContents($resource)->getBody();
- } catch (TransportException $e) {
- if ($e->getCode() !== 404) {
- throw $e;
- }
- return null;
- }
- return $content;
- }
- /**
- * {@inheritdoc}
- */
- public function getChangeDate($identifier)
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getChangeDate($identifier);
- }
- if (isset($this->commits[$identifier])) {
- return new \DateTime($this->commits[$identifier]['committed_date']);
- }
- return new \DateTime();
- }
- /**
- * {@inheritDoc}
- */
- public function getRepositoryUrl()
- {
- return $this->isPrivate ? $this->project['ssh_url_to_repo'] : $this->project['http_url_to_repo'];
- }
- /**
- * {@inheritDoc}
- */
- public function getUrl()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getUrl();
- }
- return $this->project['web_url'];
- }
- /**
- * {@inheritDoc}
- */
- public function getDist($identifier)
- {
- $url = $this->getApiUrl().'/repository/archive.zip?sha='.$identifier;
- return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => '');
- }
- /**
- * {@inheritDoc}
- */
- public function getSource($identifier)
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getSource($identifier);
- }
- return array('type' => 'git', 'url' => $this->getRepositoryUrl(), 'reference' => $identifier);
- }
- /**
- * {@inheritDoc}
- */
- public function getRootIdentifier()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getRootIdentifier();
- }
- return $this->project['default_branch'];
- }
- /**
- * {@inheritDoc}
- */
- public function getBranches()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getBranches();
- }
- if (!$this->branches) {
- $this->branches = $this->getReferences('branches');
- }
- return $this->branches;
- }
- /**
- * {@inheritDoc}
- */
- public function getTags()
- {
- if ($this->gitDriver) {
- return $this->gitDriver->getTags();
- }
- if (!$this->tags) {
- $this->tags = $this->getReferences('tags');
- }
- return $this->tags;
- }
- /**
- * @return string Base URL for GitLab API v3
- */
- public function getApiUrl()
- {
- $domainName = $this->originUrl;
- $portNumber = (true === is_numeric($this->portNumber)) ? sprintf(':%s', $this->portNumber) : '';
- return $this->scheme.'://'.$domainName.$portNumber.'/api/v4/projects/'.$this->urlEncodeAll($this->namespace).'%2F'.$this->urlEncodeAll($this->repository);
- }
- /**
- * Urlencode all non alphanumeric characters. rawurlencode() can not be used as it does not encode `.`
- *
- * @param string $string
- * @return string
- */
- private function urlEncodeAll($string)
- {
- $encoded = '';
- for ($i = 0; isset($string[$i]); $i++) {
- $character = $string[$i];
- if (!ctype_alnum($character) && !in_array($character, array('-', '_'), true)) {
- $character = '%' . sprintf('%02X', ord($character));
- }
- $encoded .= $character;
- }
- return $encoded;
- }
- /**
- * @param string $type
- *
- * @return string[] where keys are named references like tags or branches and the value a sha
- */
- protected function getReferences($type)
- {
- $perPage = 100;
- $resource = $this->getApiUrl().'/repository/'.$type.'?per_page='.$perPage;
- $references = array();
- do {
- $response = $this->getContents($resource);
- $data = $response->decodeJson();
- foreach ($data as $datum) {
- $references[$datum['name']] = $datum['commit']['id'];
- // Keep the last commit date of a reference to avoid
- // unnecessary API call when retrieving the composer file.
- $this->commits[$datum['commit']['id']] = $datum['commit'];
- }
- if (count($data) >= $perPage) {
- $resource = $this->getNextPage($response);
- } else {
- $resource = false;
- }
- } while ($resource);
- return $references;
- }
- protected function fetchProject()
- {
- // we need to fetch the default branch from the api
- $resource = $this->getApiUrl();
- $this->project = $this->getContents($resource, true)->decodeJson();
- if (isset($this->project['visibility'])) {
- $this->isPrivate = $this->project['visibility'] !== 'public';
- } else {
- // client is not authendicated, therefore repository has to be public
- $this->isPrivate = false;
- }
- }
- protected function attemptCloneFallback()
- {
- try {
- if ($this->isPrivate === false) {
- $url = $this->generatePublicUrl();
- } else {
- $url = $this->generateSshUrl();
- }
- // If this repository may be private and we
- // cannot ask for authentication credentials (because we
- // are not interactive) then we fallback to GitDriver.
- $this->setupGitDriver($url);
- return true;
- } catch (\RuntimeException $e) {
- $this->gitDriver = null;
- $this->io->writeError('<error>Failed to clone the '.$url.' repository, try running in interactive mode so that you can enter your credentials</error>');
- throw $e;
- }
- }
- /**
- * Generate an SSH URL
- *
- * @return string
- */
- protected function generateSshUrl()
- {
- return 'git@' . $this->originUrl . ':'.$this->namespace.'/'.$this->repository.'.git';
- }
- protected function generatePublicUrl()
- {
- return $this->scheme . '://' . $this->originUrl . '/'.$this->namespace.'/'.$this->repository.'.git';
- }
- protected function setupGitDriver($url)
- {
- $this->gitDriver = new GitDriver(
- array('url' => $url),
- $this->io,
- $this->config,
- $this->process,
- $this->httpDownloader
- );
- $this->gitDriver->initialize();
- }
- /**
- * {@inheritDoc}
- */
- protected function getContents($url, $fetchingRepoData = false)
- {
- try {
- $response = parent::getContents($url);
- if ($fetchingRepoData) {
- $json = $response->decodeJson();
- // force auth as the unauthenticated version of the API is broken
- if (!isset($json['default_branch'])) {
- if (!empty($json['id'])) {
- $this->isPrivate = false;
- }
- throw new TransportException('GitLab API seems to not be authenticated as it did not return a default_branch', 401);
- }
- }
- return $response;
- } catch (TransportException $e) {
- $gitLabUtil = new GitLab($this->io, $this->config, $this->process, $this->httpDownloader);
- switch ($e->getCode()) {
- case 401:
- case 404:
- // try to authorize only if we are fetching the main /repos/foo/bar data, otherwise it must be a real 404
- if (!$fetchingRepoData) {
- throw $e;
- }
- if ($gitLabUtil->authorizeOAuth($this->originUrl)) {
- return parent::getContents($url);
- }
- if (!$this->io->isInteractive()) {
- if ($this->attemptCloneFallback()) {
- return new Response(array('url' => 'dummy'), 200, array(), 'null');
- }
- }
- $this->io->writeError('<warning>Failed to download ' . $this->namespace . '/' . $this->repository . ':' . $e->getMessage() . '</warning>');
- $gitLabUtil->authorizeOAuthInteractively($this->scheme, $this->originUrl, 'Your credentials are required to fetch private repository metadata (<info>'.$this->url.'</info>)');
- return parent::getContents($url);
- case 403:
- if (!$this->io->hasAuthentication($this->originUrl) && $gitLabUtil->authorizeOAuth($this->originUrl)) {
- return parent::getContents($url);
- }
- if (!$this->io->isInteractive() && $fetchingRepoData) {
- if ($this->attemptCloneFallback()) {
- return new Response(array('url' => 'dummy'), 200, array(), 'null');
- }
- }
- throw $e;
- default:
- throw $e;
- }
- }
- }
- /**
- * Uses the config `gitlab-domains` to see if the driver supports the url for the
- * repository given.
- *
- * {@inheritDoc}
- */
- public static function supports(IOInterface $io, Config $config, $url, $deep = false)
- {
- if (!preg_match(self::URL_REGEX, $url, $match)) {
- return false;
- }
- $scheme = !empty($match['scheme']) ? $match['scheme'] : null;
- $guessedDomain = !empty($match['domain']) ? $match['domain'] : $match['domain2'];
- $urlParts = explode('/', $match['parts']);
- if (false === self::determineOrigin((array) $config->get('gitlab-domains'), $guessedDomain, $urlParts)) {
- return false;
- }
- if ('https' === $scheme && !extension_loaded('openssl')) {
- $io->writeError('Skipping GitLab driver for '.$url.' because the OpenSSL PHP extension is missing.', true, IOInterface::VERBOSE);
- return false;
- }
- return true;
- }
- protected function getNextPage(Response $response)
- {
- $header = $response->getHeader('link');
- $links = explode(',', $header);
- foreach ($links as $link) {
- if (preg_match('{<(.+?)>; *rel="next"}', $link, $match)) {
- return $match[1];
- }
- }
- }
- /**
- * @param array $configuredDomains
- * @param string $guessedDomain
- * @param array $urlParts
- * @return bool|string
- */
- private static function determineOrigin(array $configuredDomains, $guessedDomain, array &$urlParts)
- {
- if (in_array($guessedDomain, $configuredDomains)) {
- return $guessedDomain;
- }
- while (null !== ($part = array_shift($urlParts))) {
- $guessedDomain .= '/' . $part;
- if (in_array($guessedDomain, $configuredDomains)) {
- return $guessedDomain;
- }
- }
- return false;
- }
- }
|