From b471440ea0bcf74dcad608ae689e67c5ef2a4700 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Mon, 23 Nov 2015 21:53:35 +0100 Subject: [PATCH 1/2] Adds support for gitlab install in subfolder Since gitlab can be hosted on any url also subdirectories should be supported. (e.g https://mycompany.com/gitlab). This supports only http and https protocols since the gitlab api url is derived from the package repository url. And the ssh protocol doesn't support folders this way. --- src/Composer/Repository/Vcs/GitLabDriver.php | 2 +- .../Composer/Test/Repository/Vcs/GitLabDriverTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Composer/Repository/Vcs/GitLabDriver.php b/src/Composer/Repository/Vcs/GitLabDriver.php index 00f656f30..0879a4c2c 100644 --- a/src/Composer/Repository/Vcs/GitLabDriver.php +++ b/src/Composer/Repository/Vcs/GitLabDriver.php @@ -70,7 +70,7 @@ class GitLabDriver extends VcsDriver */ public function initialize() { - if (!preg_match('#^((https?)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match)) { + if (!preg_match('#^((https?)://([0-9a-zA-Z\./]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match)) { throw new \InvalidArgumentException('The URL provided is invalid. It must be the HTTP URL of a GitLab project.'); } diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index d5d6ed832..687a0f6c8 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -217,4 +217,14 @@ JSON; array('http://example.com/foo/bar', false), ); } + + public function testGitlabSubDirectory() + { + $url = 'https://mycompany.com/gitlab/mygroup/myproject'; + $apiUrl = 'https://mycompany.com/gitlab/api/v3/projects/mygroup%2Fmyproject'; + + $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal()); + $driver->initialize(); + $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL'); + } } From df92cb09abe29bfb00c1d419466be3fb157288a6 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Sun, 29 Nov 2015 13:59:54 +0100 Subject: [PATCH 2/2] extract regex to const. To be able to reuse it --- src/Composer/Repository/Vcs/GitLabDriver.php | 7 +++++-- tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitLabDriver.php b/src/Composer/Repository/Vcs/GitLabDriver.php index 0879a4c2c..a4db638dc 100644 --- a/src/Composer/Repository/Vcs/GitLabDriver.php +++ b/src/Composer/Repository/Vcs/GitLabDriver.php @@ -62,6 +62,9 @@ class GitLabDriver extends VcsDriver */ protected $gitDriver; + const URL_REGEX = '#^((https?)://(.*)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#'; + + /** * Extracts information from the repository url. * SSH urls uses https by default. @@ -70,7 +73,7 @@ class GitLabDriver extends VcsDriver */ public function initialize() { - if (!preg_match('#^((https?)://([0-9a-zA-Z\./]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match)) { + if (!preg_match(static::URL_REGEX, $this->url, $match)) { throw new \InvalidArgumentException('The URL provided is invalid. It must be the HTTP URL of a GitLab project.'); } @@ -343,7 +346,7 @@ class GitLabDriver extends VcsDriver */ public static function supports(IOInterface $io, Config $config, $url, $deep = false) { - if (!preg_match('#^((https?)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git|/)?$#', $url, $match)) { + if (!preg_match(static::URL_REGEX, $url, $match)) { return false; } diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index 687a0f6c8..0b8441514 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -26,6 +26,7 @@ class GitLabDriverTest extends \PHPUnit_Framework_TestCase $this->config->merge(array( 'config' => array( 'home' => sys_get_temp_dir().'/composer-test', + 'gitlab-domains' => array('mycompany.com/gitlab', 'gitlab.com') ), )); @@ -215,6 +216,7 @@ JSON; array('git@gitlab.com:foo/bar.git', extension_loaded('openssl')), array('git@example.com:foo/bar.git', false), array('http://example.com/foo/bar', false), + array('https://mycompany.com/gitlab/mygroup/myproject', true), ); }