From be1f675992ddf9080af7724a094631eecc9cd8a2 Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Mon, 7 Aug 2017 21:17:32 +0200 Subject: [PATCH 1/5] GitlabDriver V4 Paging V4 of gitlab api requires paging in tags/branches --- src/Composer/Repository/Vcs/GitLabDriver.php | 34 +++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitLabDriver.php b/src/Composer/Repository/Vcs/GitLabDriver.php index aea5a253f..49ca44423 100644 --- a/src/Composer/Repository/Vcs/GitLabDriver.php +++ b/src/Composer/Repository/Vcs/GitLabDriver.php @@ -280,17 +280,20 @@ class GitLabDriver extends VcsDriver { $resource = $this->getApiUrl().'/repository/'.$type; - $data = JsonFile::parseJson($this->getContents($resource), $resource); + do { + $data = JsonFile::parseJson($this->getContents($resource), $resource); - $references = array(); + $references = array(); - foreach ($data as $datum) { - $references[$datum['name']] = $datum['commit']['id']; + 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']; - } + // 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']; + } + $resource = $this->getNextPage(); + } while ($resource); return $references; } @@ -444,6 +447,21 @@ class GitLabDriver extends VcsDriver return true; } + protected function getNextPage() + { + $headers = $this->remoteFilesystem->getLastHeaders(); + foreach ($headers as $header) { + if (substr($header, 0, 5) === 'Link:') { + $links = explode(',', substr($header, 5)); + foreach ($links as $link) { + if (preg_match('{<(.+?)>; *rel="next"}', $link, $match)) { + return $match[1]; + } + } + } + } + } + /** * @param array $configuredDomains * @param string $guessedDomain From b486056066599a9a0d36f4bfdcf723effb6c7a2c Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Mon, 7 Aug 2017 21:46:43 +0200 Subject: [PATCH 2/5] add tests for gitlab paging --- .../Test/Repository/Vcs/GitLabDriverTest.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index c663f929d..dbc3cc512 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -216,6 +216,9 @@ JSON; ->willReturn($tagData) ->shouldBeCalledTimes(1) ; + $this->remoteFilesystem->getLastHeaders() + ->willReturn([]); + $driver->setRemoteFilesystem($this->remoteFilesystem->reveal()); $expected = array( @@ -227,6 +230,58 @@ JSON; $this->assertEquals($expected, $driver->getTags(), 'Tags are cached'); } + public function testGetTagsPaginated() { + $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject'); + + $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/branches'; + + // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches + $branchData = <<remoteFilesystem + ->getContents('gitlab.com', $apiUrl, false, array()) + ->willReturn($branchData) + ->shouldBeCalledTimes(1) + ; + + $this->remoteFilesystem + ->getContents('gitlab.com', "http://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/tags?id=mygroup%2Fmyproject&page=2&per_page=20", false, array()) + ->willReturn($branchData) + ->shouldBeCalledTimes(1) + ; + + $this->remoteFilesystem->getLastHeaders() + ->willReturn(['Link: ; rel="next", ; rel="first", ; rel="last"'], ['Link: ; rel="prev", ; rel="first", ; rel="last"']) + ->shouldBeCalledTimes(2); + + $driver->setRemoteFilesystem($this->remoteFilesystem->reveal()); + + $expected = array( + 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e', + 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd', + ); + + $this->assertEquals($expected, $driver->getBranches()); + $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached'); + + } public function testGetBranches() { $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject'); @@ -258,6 +313,9 @@ JSON; ->willReturn($branchData) ->shouldBeCalledTimes(1) ; + $this->remoteFilesystem->getLastHeaders() + ->willReturn([]); + $driver->setRemoteFilesystem($this->remoteFilesystem->reveal()); $expected = array( From b847270d084292582d3f5a4ffb659943b1a2cd5c Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Mon, 7 Aug 2017 21:47:41 +0200 Subject: [PATCH 3/5] rename test method --- tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index dbc3cc512..08211d439 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -230,7 +230,7 @@ JSON; $this->assertEquals($expected, $driver->getTags(), 'Tags are cached'); } - public function testGetTagsPaginated() { + public function testGetPaginatedRefs() { $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject'); $apiUrl = 'https://gitlab.com/api/v4/projects/mygroup%2Fmyproject/repository/branches'; From bc4a7834babbc5b827171c9e1ded9d1c82fc25c1 Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Mon, 7 Aug 2017 21:56:02 +0200 Subject: [PATCH 4/5] php 5 being php5 :/ --- tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index 08211d439..aeb885750 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -217,7 +217,7 @@ JSON; ->shouldBeCalledTimes(1) ; $this->remoteFilesystem->getLastHeaders() - ->willReturn([]); + ->willReturn(array()); $driver->setRemoteFilesystem($this->remoteFilesystem->reveal()); @@ -268,7 +268,7 @@ JSON; ; $this->remoteFilesystem->getLastHeaders() - ->willReturn(['Link: ; rel="next", ; rel="first", ; rel="last"'], ['Link: ; rel="prev", ; rel="first", ; rel="last"']) + ->willReturn(array('Link: ; rel="next", ; rel="first", ; rel="last"'), array('Link: ; rel="prev", ; rel="first", ; rel="last"')) ->shouldBeCalledTimes(2); $driver->setRemoteFilesystem($this->remoteFilesystem->reveal()); From ae42e4f8a3d26f3a4d3a59366fc1f72df11f9ebd Mon Sep 17 00:00:00 2001 From: Helmut Januschka Date: Mon, 7 Aug 2017 22:00:14 +0200 Subject: [PATCH 5/5] php 5 --- tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php index aeb885750..c5d1c8357 100644 --- a/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitLabDriverTest.php @@ -314,7 +314,7 @@ JSON; ->shouldBeCalledTimes(1) ; $this->remoteFilesystem->getLastHeaders() - ->willReturn([]); + ->willReturn(array()); $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());