From c202355a22964ae6261f14f3a3647f407c35fd03 Mon Sep 17 00:00:00 2001 From: Anatoly Pashin Date: Fri, 15 Jan 2021 13:28:31 +1000 Subject: [PATCH 1/4] Add order_by and sort params support to Deployments::all() --- src/Api/Deployments.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Api/Deployments.php b/src/Api/Deployments.php index ac2d9d7a3..97d66e043 100644 --- a/src/Api/Deployments.php +++ b/src/Api/Deployments.php @@ -25,6 +25,12 @@ class Deployments extends AbstractApi public function all($project_id, array $parameters = []) { $resolver = $this->createOptionsResolver(); + $resolver->setDefined('order_by') + ->setAllowedTypes('sort', 'string') + ->setAllowedValues('sort', ['id', 'iid', 'created_at', 'updated_at', 'ref']); + $resolver->setDefined('sort') + ->setAllowedTypes('sort', 'string') + ->setAllowedValues('sort', ['desc', 'asc']); return $this->get($this->getProjectPath($project_id, 'deployments'), $resolver->resolve($parameters)); } From 902d0709ae71d6c442958f6ba6ad8252bf3705a8 Mon Sep 17 00:00:00 2001 From: Anatoly Pashin Date: Fri, 15 Jan 2021 13:34:20 +1000 Subject: [PATCH 2/4] Fix option name --- src/Api/Deployments.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Api/Deployments.php b/src/Api/Deployments.php index 97d66e043..e46a53a73 100644 --- a/src/Api/Deployments.php +++ b/src/Api/Deployments.php @@ -26,8 +26,8 @@ public function all($project_id, array $parameters = []) { $resolver = $this->createOptionsResolver(); $resolver->setDefined('order_by') - ->setAllowedTypes('sort', 'string') - ->setAllowedValues('sort', ['id', 'iid', 'created_at', 'updated_at', 'ref']); + ->setAllowedTypes('order_by', 'string') + ->setAllowedValues('order_by', ['id', 'iid', 'created_at', 'updated_at', 'ref']); $resolver->setDefined('sort') ->setAllowedTypes('sort', 'string') ->setAllowedValues('sort', ['desc', 'asc']); From 0dbf7244f5a9fcb618fedee88b343e7eb2b6ac7e Mon Sep 17 00:00:00 2001 From: Anatoly Pashin Date: Sun, 17 Jan 2021 15:40:27 +1000 Subject: [PATCH 3/4] Add a changelog line and a test --- CHANGELOG.md | 1 + src/Api/Deployments.php | 7 +- tests/Api/DeploymentsTest.php | 128 +++++++++++++++++++++------------- 3 files changed, 87 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb62fb6d7..059983dd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added CI schedule variables endpoints * Added support for triggering a pipeline * Added support for the search_namespaces projects parameter +* Added support for order_by and sort deployments parameters [11.1.0]: https://github.com/GitLabPHP/Client/compare/11.0.0...11.1.0 diff --git a/src/Api/Deployments.php b/src/Api/Deployments.php index e46a53a73..58f1e2ffb 100644 --- a/src/Api/Deployments.php +++ b/src/Api/Deployments.php @@ -18,7 +18,12 @@ class Deployments extends AbstractApi { /** * @param int|string $project_id - * @param array $parameters + * @param array $parameters { + * + * @var string $order_by Return deployments ordered by id, iid, created_at, updated_at, + * or ref fields (default is id) + * @var string $sort Return deployments sorted in asc or desc order (default is desc) + * } * * @return mixed */ diff --git a/tests/Api/DeploymentsTest.php b/tests/Api/DeploymentsTest.php index 5305698aa..512c36352 100644 --- a/tests/Api/DeploymentsTest.php +++ b/tests/Api/DeploymentsTest.php @@ -22,31 +22,43 @@ class DeploymentsTest extends TestCase * @test */ public function shouldGetAllDeployments(): void + { + $expectedArray = $this->getMultipleDeploymentsData(); + + $api = $this->getMultipleDeploymentsRequestMock('projects/1/deployments', $expectedArray); + + $this->assertEquals($expectedArray, $api->all(1)); + } + + /** + * @test + */ + public function shouldShowDeployment(): void { $expectedArray = [ [ - 'created_at' => '2016-08-11T07:36:40.222Z', + 'created_at' => '2016-08-11T11:32:35.444Z', 'deployable' => [ 'commit' => [ 'author_email' => 'admin@example.com', 'author_name' => 'Administrator', - 'created_at' => '2016-08-11T09:36:01.000+02:00', - 'id' => '99d03678b90d914dbb1b109132516d71a4a03ea8', - 'message' => 'Merge branch \'new-title\' into \'master\' + 'created_at' => '2016-08-11T13:28:26.000+02:00', + 'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', + 'message' => 'Merge branch \'rename-readme\' into \'master\' -Update README +Rename README -See merge request !1', - 'short_id' => '99d03678', - 'title' => 'Merge branch \'new-title\' into \'master\' +See merge request !2', + 'short_id' => 'a91957a8', + 'title' => 'Merge branch \'rename-readme\' into \'master\' ', ], 'coverage' => null, - 'created_at' => '2016-08-11T07:36:27.357Z', - 'finished_at' => '2016-08-11T07:36:39.851Z', - 'id' => 657, + 'created_at' => '2016-08-11T11:32:24.456Z', + 'finished_at' => '2016-08-11T11:32:35.145Z', + 'id' => 664, 'name' => 'deploy', 'ref' => 'master', 'runner' => null, @@ -75,10 +87,10 @@ public function shouldGetAllDeployments(): void 'id' => 9, 'name' => 'production', ], - 'id' => 41, - 'iid' => 1, + 'id' => 42, + 'iid' => 2, 'ref' => 'master', - 'sha' => '99d03678b90d914dbb1b109132516d71a4a03ea8', + 'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', 'user' => [ 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'id' => 1, @@ -88,29 +100,47 @@ public function shouldGetAllDeployments(): void 'web_url' => 'http://localhost:3000/root', ], ], + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('projects/1/deployments/42') + ->will($this->returnValue($expectedArray)); + $this->assertEquals($expectedArray, $api->show(1, 42)); + } + + protected function getApiClass() + { + return Deployments::class; + } + + private function getMultipleDeploymentsData(): array + { + return [ [ - 'created_at' => '2016-08-11T11:32:35.444Z', + 'created_at' => '2016-08-11T07:36:40.222Z', 'deployable' => [ 'commit' => [ 'author_email' => 'admin@example.com', 'author_name' => 'Administrator', - 'created_at' => '2016-08-11T13:28:26.000+02:00', - 'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', - 'message' => 'Merge branch \'rename-readme\' into \'master\' + 'created_at' => '2016-08-11T09:36:01.000+02:00', + 'id' => '99d03678b90d914dbb1b109132516d71a4a03ea8', + 'message' => 'Merge branch \'new-title\' into \'master\' -Rename README +Update README -See merge request !2', - 'short_id' => 'a91957a8', - 'title' => 'Merge branch \'rename-readme\' into \'master\' +See merge request !1', + 'short_id' => '99d03678', + 'title' => 'Merge branch \'new-title\' into \'master\' ', ], 'coverage' => null, - 'created_at' => '2016-08-11T11:32:24.456Z', - 'finished_at' => '2016-08-11T11:32:35.145Z', - 'id' => 664, + 'created_at' => '2016-08-11T07:36:27.357Z', + 'finished_at' => '2016-08-11T07:36:39.851Z', + 'id' => 657, 'name' => 'deploy', 'ref' => 'master', 'runner' => null, @@ -139,10 +169,10 @@ public function shouldGetAllDeployments(): void 'id' => 9, 'name' => 'production', ], - 'id' => 42, - 'iid' => 2, + 'id' => 41, + 'iid' => 1, 'ref' => 'master', - 'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', + 'sha' => '99d03678b90d914dbb1b109132516d71a4a03ea8', 'user' => [ 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'id' => 1, @@ -152,22 +182,6 @@ public function shouldGetAllDeployments(): void 'web_url' => 'http://localhost:3000/root', ], ], - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/deployments') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldShowDeployment(): void - { - $expectedArray = [ [ 'created_at' => '2016-08-11T11:32:35.444Z', 'deployable' => [ @@ -233,17 +247,35 @@ public function shouldShowDeployment(): void ], ], ]; + } + protected function getMultipleDeploymentsRequestMock($path, $expectedArray = [], $expectedParameters = []) + { $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('projects/1/deployments/42') + ->with($path, $expectedParameters) ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->show(1, 42)); + + return $api; } - protected function getApiClass() + /** + * @test + */ + public function shouldGetAllDeploymentsSortedByCreatedAt(): void { - return Deployments::class; + $expectedArray = $this->getMultipleDeploymentsData(); + + $api = $this->getMultipleDeploymentsRequestMock( + 'projects/1/deployments', + $expectedArray, + ['page' => 1, 'per_page' => 5, 'order_by' => 'created_at', 'sort' => 'asc'] + ); + + $this->assertEquals( + $expectedArray, + $api->all(1, ['page' => 1, 'per_page' => 5, 'order_by' => 'created_at', 'sort' => 'asc']) + ); } } From c3ae14c4cc46f06867866019d42d5aec63312cd5 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 17 Jan 2021 10:48:17 +0000 Subject: [PATCH 4/4] Update DeploymentsTest.php --- tests/Api/DeploymentsTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Api/DeploymentsTest.php b/tests/Api/DeploymentsTest.php index 512c36352..f7f855187 100644 --- a/tests/Api/DeploymentsTest.php +++ b/tests/Api/DeploymentsTest.php @@ -25,7 +25,7 @@ public function shouldGetAllDeployments(): void { $expectedArray = $this->getMultipleDeploymentsData(); - $api = $this->getMultipleDeploymentsRequestMock('projects/1/deployments', $expectedArray); + $api = $this->getMultipleDeploymentsRequestMock('projects/1/deployments', $expectedArray, []); $this->assertEquals($expectedArray, $api->all(1)); } @@ -110,12 +110,7 @@ public function shouldShowDeployment(): void $this->assertEquals($expectedArray, $api->show(1, 42)); } - protected function getApiClass() - { - return Deployments::class; - } - - private function getMultipleDeploymentsData(): array + private function getMultipleDeploymentsData() { return [ [ @@ -249,7 +244,7 @@ private function getMultipleDeploymentsData(): array ]; } - protected function getMultipleDeploymentsRequestMock($path, $expectedArray = [], $expectedParameters = []) + protected function getMultipleDeploymentsRequestMock(string $path, array $expectedArray, array $expectedParameters) { $api = $this->getApiMock(); $api->expects($this->once()) @@ -278,4 +273,9 @@ public function shouldGetAllDeploymentsSortedByCreatedAt(): void $api->all(1, ['page' => 1, 'per_page' => 5, 'order_by' => 'created_at', 'sort' => 'asc']) ); } + + protected function getApiClass() + { + return Deployments::class; + } }