From 2a89d37651a3016c20089165a3f1b56351d3896c Mon Sep 17 00:00:00 2001 From: Mikk Tendermann Date: Thu, 31 Aug 2017 20:14:41 +0300 Subject: [PATCH] fix gitlab not telling visibilty if user is not logged in --- src/Composer/Repository/Vcs/GitLabDriver.php | 7 +++- .../Test/Repository/Vcs/GitLabDriverTest.php | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Composer/Repository/Vcs/GitLabDriver.php b/src/Composer/Repository/Vcs/GitLabDriver.php index f2813db54..45cdac717 100644 --- a/src/Composer/Repository/Vcs/GitLabDriver.php +++ b/src/Composer/Repository/Vcs/GitLabDriver.php @@ -308,7 +308,12 @@ class GitLabDriver extends VcsDriver // we need to fetch the default branch from the api $resource = $this->getApiUrl(); $this->project = JsonFile::parseJson($this->getContents($resource, true), $resource); - $this->isPrivate = $this->project['visibility'] !== 'public'; + 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() diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index 1564412d8..25634138f 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -142,6 +142,44 @@ JSON; return $driver; } + /** + * @dataProvider getInitializeUrls + */ + public function testInitializePublicProjectAsAnonymous($url, $apiUrl) + { + // @link http://doc.gitlab.com/ce/api/projects.html#get-single-project + $projectData = <<remoteFilesystem + ->getContents('gitlab.com', $apiUrl, false, array()) + ->willReturn($projectData) + ->shouldBeCalledTimes(1) + ; + + $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'); + $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab'); + $this->assertEquals('https://gitlab.com/mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default'); + $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl()); + + return $driver; + } + public function testGetDist() { $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject');