Skip to content

Commit 9f630a1

Browse files
authored
Add support for Events API (#754)
1 parent 1b9a894 commit 9f630a1

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

src/Api/Events.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Gitlab API library.
7+
*
8+
* (c) Niclas Hoyer <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Gitlab\Api;
15+
16+
use Symfony\Component\OptionsResolver\Options;
17+
18+
class Events extends AbstractApi
19+
{
20+
/**
21+
* @param array $parameters {
22+
*
23+
* @var string $action include only events of a particular action type
24+
* @var string $target_type include only events of a particular target type
25+
* @var \DateTimeInterface $before include only events created before a particular date
26+
* @var \DateTimeInterface $after include only events created after a particular date
27+
* @var string $scope include all events across a user’s projects
28+
* @var string $sort sort events in asc or desc order by created_at
29+
*
30+
* }
31+
*
32+
* @return mixed
33+
*/
34+
public function all(array $parameters = [])
35+
{
36+
$resolver = $this->createOptionsResolver();
37+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
38+
return $value->format('Y-m-d');
39+
};
40+
41+
$resolver->setDefined('action');
42+
$resolver->setDefined('target_type');
43+
$resolver->setDefined('before')
44+
->setAllowedTypes('before', \DateTimeInterface::class)
45+
->setNormalizer('before', $datetimeNormalizer)
46+
;
47+
$resolver->setDefined('after')
48+
->setAllowedTypes('after', \DateTimeInterface::class)
49+
->setNormalizer('after', $datetimeNormalizer)
50+
;
51+
$resolver->setDefined('scope');
52+
$resolver->setDefined('sort');
53+
54+
return $this->get('events', $resolver->resolve($parameters));
55+
}
56+
}

src/Client.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Gitlab\Api\DeployKeys;
1818
use Gitlab\Api\Deployments;
1919
use Gitlab\Api\Environments;
20+
use Gitlab\Api\Events;
2021
use Gitlab\Api\Groups;
2122
use Gitlab\Api\GroupsBoards;
2223
use Gitlab\Api\GroupsEpics;
@@ -174,6 +175,14 @@ public function environments(): Environments
174175
return new Environments($this);
175176
}
176177

178+
/**
179+
* @return Events
180+
*/
181+
public function events(): Events
182+
{
183+
return new Events($this);
184+
}
185+
177186
/**
178187
* @return Groups
179188
*/

tests/Api/EventsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Gitlab API library.
7+
*
8+
* (c) Niclas Hoyer <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Gitlab\Tests\Api;
15+
16+
use Gitlab\Api\Events;
17+
18+
class EventsTest extends TestCase
19+
{
20+
protected function getApiClass()
21+
{
22+
return Events::class;
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function shouldGetAllEvents(): void
29+
{
30+
$expectedArray = [
31+
['id' => 1, 'target_type' => 'Issue'],
32+
['id' => 2, 'target_type' => null],
33+
];
34+
35+
$api = $this->getApiMock();
36+
$api->expects($this->once())
37+
->method('get')
38+
->with('events', [])
39+
->will($this->returnValue($expectedArray))
40+
;
41+
42+
$this->assertEquals($expectedArray, $api->all());
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function shouldGetEventsAfter(): void
49+
{
50+
$expectedArray = [
51+
['id' => 1, 'target_type' => 'Issue'],
52+
['id' => 2, 'target_type' => null],
53+
];
54+
55+
$api = $this->getApiMock();
56+
$api->expects($this->once())
57+
->method('get')
58+
->with('events', ['after' => '1970-01-01'])
59+
->will($this->returnValue($expectedArray))
60+
;
61+
62+
$this->assertEquals($expectedArray, $api->all(['after' => new \DateTime('1970-01-01')]));
63+
}
64+
}

0 commit comments

Comments
 (0)