Skip to content

Commit 2de90ec

Browse files
committed
use OptionsResolver in Issues api
1 parent 42fabca commit 2de90ec

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

UPGRADE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ See [documentation](doc/customize.md) to know how to customize the client timeou
2626

2727
* The second argument of `update`, `remove`, `showComments`, `showComment`, `addComment`, `updateComment`, `removeComment`,
2828
`setTimeEstimate`, `resetTimeEstimate`, `addSpentTime` and `resetSpentTime` methods is now a scoped issue id (iid).
29+
* The `all` method second and subsequent arguments have been replaced by a single associative array of query string parameters.
2930

3031
## `Gitlab\Api\IssueBoards` changes
3132

lib/Gitlab/Api/Issues.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,46 @@ class Issues extends AbstractApi
44
{
55
/**
66
* @param int $project_id
7-
* @param int $page
8-
* @param int $per_page
9-
* @param array $params
7+
* @param array $parameters (
8+
*
9+
* @var string $state Return all issues or just those that are opened or closed.
10+
* @var string $labels Comma-separated list of label names, issues must have all labels to be returned.
11+
* No+Label lists all issues with no labels.
12+
* @var string $milestone The milestone title.
13+
* @var int[] $iids Return only the issues having the given iid.
14+
* @var string $order_by Return requests ordered by created_at or updated_at fields. Default is created_at.
15+
* @var string $sort Return requests sorted in asc or desc order. Default is desc.
16+
* @var string $search Search issues against their title and description.
17+
* )
18+
*
1019
* @return mixed
1120
*/
12-
public function all($project_id = null, $page = 1, $per_page = self::PER_PAGE, array $params = array())
21+
public function all($project_id = null, array $parameters = [])
1322
{
14-
$path = $project_id === null ? 'issues' : $this->getProjectPath($project_id, 'issues');
23+
$resolver = $this->createOptionsResolver();
24+
25+
$resolver->setDefined('state')
26+
->setAllowedValues('state', ['opened', 'closed'])
27+
;
28+
$resolver->setDefined('labels');
29+
$resolver->setDefined('milestone');
30+
$resolver->setDefined('iids')
31+
->setAllowedTypes('iids', 'array')
32+
->setAllowedValues('iids', function (array $value) {
33+
return count($value) == count(array_filter($value, 'is_int'));
34+
})
35+
;
36+
$resolver->setDefined('order_by')
37+
->setAllowedValues('order_by', ['created_at', 'updated_at'])
38+
;
39+
$resolver->setDefined('sort')
40+
->setAllowedValues('sort', ['asc', 'desc'])
41+
;
42+
$resolver->setDefined('search');
1543

16-
$params = array_intersect_key($params, array('labels' => '', 'state' => '', 'sort' => '', 'order_by' => '', 'milestone' => ''));
17-
$params = array_merge(array(
18-
'page' => $page,
19-
'per_page' => $per_page
20-
), $params);
44+
$path = $project_id === null ? 'issues' : $this->getProjectPath($project_id, 'issues');
2145

22-
return $this->get($path, $params);
46+
return $this->get($path, $resolver->resolve($parameters));
2347
}
2448

2549
/**

test/Gitlab/Tests/Api/IssuesTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php namespace Gitlab\Tests\Api;
22

3-
use Gitlab\Api\AbstractApi;
4-
53
class IssuesTest extends TestCase
64
{
75
/**
@@ -17,7 +15,7 @@ public function shouldGetAllIssues()
1715
$api = $this->getApiMock();
1816
$api->expects($this->once())
1917
->method('get')
20-
->with('issues', array('page' => 1, 'per_page' => AbstractApi::PER_PAGE))
18+
->with('issues', array())
2119
->will($this->returnValue($expectedArray))
2220
;
2321

@@ -41,7 +39,7 @@ public function shouldGetProjectIssuesWithPagination()
4139
->will($this->returnValue($expectedArray))
4240
;
4341

44-
$this->assertEquals($expectedArray, $api->all(1, 2, 5));
42+
$this->assertEquals($expectedArray, $api->all(1, ['page' => 2, 'per_page' => 5]));
4543
}
4644

4745
/**
@@ -57,11 +55,11 @@ public function shouldGetProjectIssuesWithParams()
5755
$api = $this->getApiMock();
5856
$api->expects($this->once())
5957
->method('get')
60-
->with('projects/1/issues', array('page' => 2, 'per_page' => 5, 'order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'open'))
58+
->with('projects/1/issues', array('order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'opened'))
6159
->will($this->returnValue($expectedArray))
6260
;
6361

64-
$this->assertEquals($expectedArray, $api->all(1, 2, 5, array('order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'open')));
62+
$this->assertEquals($expectedArray, $api->all(1, array('order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'opened')));
6563
}
6664

6765
/**

0 commit comments

Comments
 (0)