Skip to content

Commit dd159b0

Browse files
Bertrand Jaminm1guelpf
authored andcommitted
Add routes to retrieve issues statistics
1 parent 0746ae3 commit dd159b0

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php namespace Gitlab\Api;
2+
3+
use Symfony\Component\OptionsResolver\Options;
4+
use Symfony\Component\OptionsResolver\OptionsResolver;
5+
6+
class IssuesStatistics extends AbstractApi
7+
{
8+
public function all($parameters)
9+
{
10+
return $this->get('issues_statistics', $this->createOptionsResolver()->resolve($parameters));
11+
}
12+
13+
public function project($project_id, $parameters)
14+
{
15+
return $this->get($this->getProjectPath($project_id, 'issues_statistics'), $this->createOptionsResolver()->resolve($parameters));
16+
}
17+
18+
public function group($group_id, $parameters)
19+
{
20+
return $this->get($this->getGroupPath($group_id, 'issues_statistics'), $this->createOptionsResolver()->resolve($parameters));
21+
}
22+
23+
protected function createOptionsResolver()
24+
{
25+
$resolver = new OptionsResolver();
26+
27+
$resolver->setDefined('milestone')
28+
->setAllowedTypes('milestone', 'string');
29+
30+
$resolver->setDefined('labels')
31+
->setAllowedTypes('labels', 'string');
32+
33+
$resolver->setDefined('scope')
34+
->setAllowedValues('scope', ['created-by-me', 'assigned-to-me', 'all']);
35+
36+
$resolver->setDefined('author_id')
37+
->setAllowedTypes('author_id', 'integer');
38+
39+
$resolver->setDefined('author_username')
40+
->setAllowedTypes('author_username', 'string');
41+
42+
$resolver->setDefined('assignee_id')
43+
->setAllowedTypes('assignee_id', 'integer');
44+
45+
$resolver->setDefined('assignee_username')
46+
->setAllowedTypes('assignee_username', 'string');
47+
48+
$resolver->setDefined('my_reaction_emoji')
49+
->setAllowedTypes('my_reaction_emoji', 'string');
50+
51+
$resolver->setDefined('search')
52+
->setAllowedTypes('search', 'string');
53+
54+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
55+
return $value->format('c');
56+
};
57+
58+
$resolver->setDefined('created_after')
59+
->setAllowedTypes('created_after', \DateTimeInterface::class)
60+
->setNormalizer('created_after', $datetimeNormalizer);
61+
62+
$resolver->setDefined('created_before')
63+
->setAllowedTypes('created_before', \DateTimeInterface::class)
64+
->setNormalizer('created_before', $datetimeNormalizer);
65+
66+
$resolver->setDefined('updated_after')
67+
->setAllowedTypes('updated_after', \DateTimeInterface::class)
68+
->setNormalizer('updated_after', $datetimeNormalizer);
69+
70+
$resolver->setDefined('updated_before')
71+
->setAllowedTypes('updated_before', \DateTimeInterface::class)
72+
->setNormalizer('updated_before', $datetimeNormalizer);
73+
74+
$booleanNormalizer = function (Options $resolver, $value) {
75+
return $value ? 'true' : 'false';
76+
};
77+
78+
$resolver->setDefined('confidential')
79+
->setAllowedTypes('confidential', 'bool')
80+
->setNormalizer('confidential', $booleanNormalizer);
81+
82+
return $resolver;
83+
}
84+
}

lib/Gitlab/Client.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ public function schedules()
305305
return new Api\Schedules($this);
306306
}
307307

308+
/**
309+
* @return Api\IssuesStatistics
310+
*/
311+
public function issuesStatistics()
312+
{
313+
return new Api\IssuesStatistics($this);
314+
}
315+
308316
/**
309317
* @param string $name
310318
*
@@ -390,6 +398,10 @@ public function api($name)
390398
case 'schedules':
391399
return $this->schedules();
392400

401+
case 'issues_statistics':
402+
return $this->issuesStatistics();
403+
404+
393405
default:
394406
throw new InvalidArgumentException('Invalid endpoint: "'.$name.'"');
395407
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
use Gitlab\Api\IssuesStatistics;
4+
5+
class IssuesStatisticsTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldGetAll()
11+
{
12+
$expectedArray = array();
13+
14+
$now = new \DateTime();
15+
16+
$api = $this->getApiMock();
17+
$api->expects($this->once())
18+
->method('get')
19+
->with('issues_statistics', [
20+
'milestone' => '',
21+
'labels' => '',
22+
'scope' => 'created-by-me',
23+
'author_id' => 1,
24+
'author_username' => '',
25+
'assignee_id' => 1,
26+
'assignee_username' => '',
27+
'my_reaction_emoji' => '',
28+
'search' => '',
29+
'created_after' => $now->format('c'),
30+
'created_before' => $now->format('c'),
31+
'updated_after' => $now->format('c'),
32+
'updated_before' => $now->format('c'),
33+
'confidential' => 'false'
34+
])
35+
->will($this->returnValue($expectedArray));
36+
37+
$this->assertEquals($expectedArray, $api->all([
38+
'milestone' => '',
39+
'labels' => '',
40+
'scope' => 'created-by-me',
41+
'author_id' => 1,
42+
'author_username' => '',
43+
'assignee_id' => 1,
44+
'assignee_username' => '',
45+
'my_reaction_emoji' => '',
46+
'search' => '',
47+
'created_after' => $now,
48+
'created_before' => $now,
49+
'updated_after' => $now,
50+
'updated_before' => $now,
51+
'confidential' => false
52+
]));
53+
}
54+
55+
/**
56+
* @test
57+
*/
58+
public function shouldGetProject()
59+
{
60+
$expectedArray = array();
61+
62+
$api = $this->getApiMock();
63+
$api->expects($this->once())
64+
->method('get')
65+
->with('projects/1/issues_statistics', [])
66+
->will($this->returnValue($expectedArray));
67+
68+
$this->assertEquals($expectedArray, $api->project(1, []));
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function shouldGetGroup()
75+
{
76+
$expectedArray = array();
77+
78+
$api = $this->getApiMock();
79+
$api->expects($this->once())
80+
->method('get')
81+
->with('groups/1/issues_statistics', [])
82+
->will($this->returnValue($expectedArray));
83+
84+
$this->assertEquals($expectedArray, $api->group(1, []));
85+
}
86+
87+
88+
protected function getApiClass()
89+
{
90+
return IssuesStatistics::class;
91+
}
92+
}

0 commit comments

Comments
 (0)