@ -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\Config;
use Composer\Config;
@ -7,41 +17,76 @@ use Composer\Cache;
use Composer\IO\IOInterface;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Json\JsonFile;
use Composer\Downloader\TransportException;
use Composer\Downloader\TransportException;
use Composer\Util\RemoteFilesystem;
/**
/**
* Simplistic driver for GitLab currently only supports the api, not local checkouts.
* 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
class GitLabDriver extends VcsDriver
{
{
protected $owner;
private $scheme;
protected $repository;
private $owner;
protected $originUrl;
private $repository;
protected $cache;
protected $infoCache = array();
private $cache;
private $infoCache = array();
protected $project;
/**
protected $commits = array();
* @var array Project data returned by GitLab API
protected $tags;
*/
protected $branches;
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;
/**
/**
* Extracts information from the repository url.
* Extracts information from the repository url.
* SSH urls are not supported in order to know the HTTP sheme to use.
*
*
* {@inheritDoc}
* {@inheritDoc}
*/
*/
public function initialize()
public function initialize()
{
{
preg_match('#^(?:(https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match);
if (!preg_match('#^(https?)://([^/]+)/([^/]+)/([^/]+)(?:\.git|/)?$#', $this->url, $match)) {
throw new \InvalidArgumentException('The URL provided is invalid. It must be the HTTP URL of a GitLab project.');
}
$this->scheme = $match[1];
$this->scheme = $match[1];
$this->owner = $match[4];
$this->originUrl = $match[2];
$this->repository = $match[5];
$this->owner = $match[3];
$this->originUrl = !empty($match[2]) ? $match[2] : $match[3];
$this->repository = preg_replace('#(\.git)$#', '', $match[4]);
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository);
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository);
$this->fetchProject();
$this->fetchProject();
}
}
/**
* Updates the RemoteFilesystem instance.
* Mainly useful for tests.
*
* @internal
*/
public function setRemoteFilesystem(RemoteFilesystem $remoteFilesystem)
{
$this->remoteFilesystem = $remoteFilesystem;
}
/**
/**
* Fetches the composer.json file from the project by a identifier.
* Fetches the composer.json file from the project by a identifier.
*
*
@ -53,10 +98,9 @@ class GitLabDriver extends VcsDriver
{
{
// Convert the root identifier to a cachable commit id
// Convert the root identifier to a cachable commit id
if (!preg_match('{[a-f0-9]{40}}i', $identifier)) {
if (!preg_match('{[a-f0-9]{40}}i', $identifier)) {
foreach ($this->getBranches() as $ref => $id) {
$branches = $this->getBranches();
if ($ref === $identifier) {
if (isset($branches[$identifier])) {
$identifier = $id;
$identifier = $branches[$identifier];
}
}
}
}
}
@ -88,24 +132,6 @@ class GitLabDriver extends VcsDriver
return $this->infoCache[$identifier] = $composer;
return $this->infoCache[$identifier] = $composer;
}
}
/**
* {@inheritDoc}
*/
public function hasComposerFile($identifier)
{
try {
$this->getComposerInformation($identifier);
return true;
} catch (TransportException $e) {
if ($e->getCode() !== 404) {
throw $e;
}
}
return false;
}
/**
/**
* {@inheritDoc}
* {@inheritDoc}
*/
*/
@ -173,9 +199,10 @@ class GitLabDriver extends VcsDriver
}
}
/**
/**
* Fetches composer.json file from the repository through api
* Fetches composer.json file from the repository through api.
*
*
* @param string $identifier
* @param string $identifier
*
* @return array
* @return array
*/
*/
protected function fetchComposerFile($identifier)
protected function fetchComposerFile($identifier)
@ -186,18 +213,16 @@ class GitLabDriver extends VcsDriver
}
}
/**
/**
* Root url
* @return string Base URL for GitLab API v3
*
* {@inheritDoc}
*/
*/
protected function getApiUrl()
public function getApiUrl()
{
{
// this needs to be https, but our install is running http
return $this->scheme.'://'.$this->originUrl.'/api/v3/projects/'.$this->owner.'%2F'.$this->repository;
return 'http://'.$this->originUrl.'/api/v3/projects/'.$this->owner.'%2F'.$this->repository;
}
}
/**
/**
* @param string $type
* @param string $type
*
* @return string[] where keys are named references like tags or branches and the value a sha
* @return string[] where keys are named references like tags or branches and the value a sha
*/
*/
protected function getReferences($type)
protected function getReferences($type)
@ -212,7 +237,7 @@ class GitLabDriver extends VcsDriver
$references[$datum['name']] = $datum['commit']['id'];
$references[$datum['name']] = $datum['commit']['id'];
// Keep the last commit date of a reference to avoid
// Keep the last commit date of a reference to avoid
// unnecessary API call when retre iving the composer file.
// unnecessary API call when retrie ving the composer file.
$this->commits[$datum['commit']['id']] = $datum['commit'];
$this->commits[$datum['commit']['id']] = $datum['commit'];
}
}
@ -235,17 +260,18 @@ class GitLabDriver extends VcsDriver
*/
*/
public static function supports(IOInterface $io, Config $config, $url, $deep = false)
public static function supports(IOInterface $io, Config $config, $url, $deep = false)
{
{
if (!preg_match('#^((?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $url, $matches )) {
if (!preg_match('#^(https?)://([^/]+)/([^/]+)/([^/]+)(?:\.git|/)?$#', $url, $match )) {
return false;
return false;
}
}
$originUrl = empty($matches[2]) ? $matches[3] : $matches[2];
$scheme = $match[1];
$originUrl = $match[2];
if (!in_array($originUrl, (array) $config->get('gitlab-domains'))) {
if (!in_array($originUrl, (array) $config->get('gitlab-domains'))) {
return false;
return false;
}
}
if (!extension_loaded('openssl')) {
if ('https' === $scheme & & !extension_loaded('openssl')) {
if ($io->isVerbose()) {
if ($io->isVerbose()) {
$io->write('Skipping GitLab driver for '.$url.' because the OpenSSL PHP extension is missing.');
$io->write('Skipping GitLab driver for '.$url.' because the OpenSSL PHP extension is missing.');
}
}