Skip to content
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

[11.11] Add expires_at parameter to Groups::addMember() #746

Merged
merged 7 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 10 additions & 0 deletions src/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ abstract class AbstractApi
*/
private const URI_PREFIX = '/api/v4/';

/**
* The access levels for groups and projects
* as defined in the Gitlab::Access module.
*
* @see https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/access.rb
*
* @var array
*/
protected const ACCESS_LEVELS = [0, 10, 20, 30, 40, 50];

/**
* The client instance.
*
Expand Down
23 changes: 20 additions & 3 deletions src/Api/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,32 @@ public function allMember($group_id, int $user_id)
* @param int|string $group_id
* @param int $user_id
* @param int $access_level
* @param array $parameters
*
* @return mixed
*/
public function addMember($group_id, int $user_id, int $access_level)
public function addMember($group_id, int $user_id, int $access_level, array $parameters = [])
{
return $this->post('groups/'.self::encodePath($group_id).'/members', [
$dateNormalizer = function (OptionsResolver $optionsResolver, \DateTimeInterface $date): string {
return $date->format('Y-m-d');
};

$resolver = $this->createOptionsResolver()
->setRequired(['user_id', 'access_level'])
->setDefined('expires_at')
->setAllowedTypes('user_id', 'int')
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
->setAllowedTypes('access_level', 'int')
->setAllowedTypes('expires_at', \DateTimeInterface::class)
->setAllowedValues('access_level', self::ACCESS_LEVELS)
->setNormalizer('expires_at', $dateNormalizer)
;

$parameters = \array_merge([
'user_id' => $user_id,
'access_level' => $access_level,
]);
], $parameters);

return $this->post('groups/'.self::encodePath($group_id).'/members', $resolver->resolve($parameters));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ public function addShare($project_id, array $parameters = [])

$resolver->setRequired('group_access')
->setAllowedTypes('group_access', 'int')
->setAllowedValues('group_access', [0, 10, 20, 30, 40, 50]);
->setAllowedValues('group_access', self::ACCESS_LEVELS);

$resolver->setDefined('expires_at')
->setAllowedTypes('expires_at', \DateTimeInterface::class)
Expand Down
9 changes: 6 additions & 3 deletions tests/Api/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,19 @@ public function shouldGetMembers(): void
*/
public function shouldAddMember(): void
{
$expectedArray = ['id' => 1, 'name' => 'Matt'];
$tomorrow = (new DateTime('tomorrow'));
$expectedArray = ['id' => 1, 'name' => 'Matt', 'expires_at' => $tomorrow];

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('groups/1/members', ['user_id' => 2, 'access_level' => 3])
->with('groups/1/members', [
'user_id' => 2, 'access_level' => 10, 'expires_at' => $tomorrow->format('Y-m-d'),
])
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->addMember(1, 2, 3));
$this->assertEquals($expectedArray, $api->addMember(1, 2, 10, ['expires_at' => $tomorrow]));
}

/**
Expand Down