Skip to content

[11.1] Add order_by and sort params support to Deployments::all() #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion src/Api/Deployments.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ 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
*/
public function all($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$resolver->setDefined('order_by')
->setAllowedTypes('order_by', 'string')
->setAllowedValues('order_by', ['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));
}
Expand Down
124 changes: 78 additions & 46 deletions tests/Api/DeploymentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -88,29 +100,42 @@ 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));
}

private function getMultipleDeploymentsData()
{
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,
Expand Down Expand Up @@ -139,10 +164,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,
Expand All @@ -152,22 +177,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' => [
Expand Down Expand Up @@ -233,13 +242,36 @@ public function shouldShowDeployment(): void
],
],
];
}

protected function getMultipleDeploymentsRequestMock(string $path, array $expectedArray, array $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;
}

/**
* @test
*/
public function shouldGetAllDeploymentsSortedByCreatedAt(): void
{
$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'])
);
}

protected function getApiClass()
Expand Down