Skip to content

Commit 62ad597

Browse files
Add top_level_only parameter to Groups::all method (#684)
This PR adds a `top_level_only` parameter to the `GitLab\Api\Groups::all` method, which limits the results to top level groups, excluding all subgroups. Because this parameter is not available for the `subgroups` method, it was necessary to split up the `getGroupSearchResolver` method, which was called by both the `all` and the `subgroups` method.
1 parent 277b8e9 commit 62ad597

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Api/Groups.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Groups extends AbstractApi
3030
* @var bool $statistics include group statistics (admins only)
3131
* @var bool $owned limit by groups owned by the current user
3232
* @var int $min_access_level limit by groups in which the current user has at least this access level
33+
* @var bool $top_level_only limit to top level groups, excluding all subgroups
3334
* }
3435
*
3536
* @return mixed
@@ -314,7 +315,7 @@ public function projects($id, array $parameters = [])
314315
*/
315316
public function subgroups($group_id, array $parameters = [])
316317
{
317-
$resolver = $this->getGroupSearchResolver();
318+
$resolver = $this->getSubgroupSearchResolver();
318319

319320
return $this->get('groups/'.self::encodePath($group_id).'/subgroups', $resolver->resolve($parameters));
320321
}
@@ -500,6 +501,24 @@ public function packages($group_id, array $parameters = [])
500501
* @return OptionsResolver
501502
*/
502503
private function getGroupSearchResolver()
504+
{
505+
$resolver = $this->getSubgroupSearchResolver();
506+
$booleanNormalizer = function (Options $resolver, $value): string {
507+
return $value ? 'true' : 'false';
508+
};
509+
510+
$resolver->setDefined('top_level_only')
511+
->setAllowedTypes('top_level_only', 'bool')
512+
->setNormalizer('top_level_only', $booleanNormalizer)
513+
;
514+
515+
return $resolver;
516+
}
517+
518+
/**
519+
* @return OptionsResolver
520+
*/
521+
private function getSubgroupSearchResolver()
503522
{
504523
$resolver = $this->createOptionsResolver();
505524
$booleanNormalizer = function (Options $resolver, $value): string {

tests/Api/GroupsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ public function shouldGetAllGroupsWithBooleanParam(): void
5858
$this->assertEquals($expectedArray, $api->all(['all_available' => false]));
5959
}
6060

61+
/**
62+
* @test
63+
*/
64+
public function shouldGetAllTopLevelGroupsWithoutSubgroups(): void
65+
{
66+
$expectedArray = [
67+
['id' => 1, 'name' => 'A group'],
68+
['id' => 2, 'name' => 'Another group'],
69+
];
70+
71+
$api = $this->getApiMock();
72+
$api->expects($this->once())
73+
->method('get')
74+
->with('groups', ['top_level_only' => 'true'])
75+
->will($this->returnValue($expectedArray))
76+
;
77+
78+
$this->assertEquals($expectedArray, $api->all(['top_level_only' => true]));
79+
}
80+
6181
/**
6282
* @test
6383
*/

0 commit comments

Comments
 (0)