From 19c2994bee871803182a7c9c2a09603b63f0b70d Mon Sep 17 00:00:00 2001 From: Hannes Filip Date: Fri, 6 Mar 2020 15:50:01 +0100 Subject: [PATCH 1/5] List all members of a group or project including inherited members with optional user id filter --- lib/Gitlab/Api/Groups.php | 5 +++-- lib/Gitlab/Api/Projects.php | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/Gitlab/Api/Groups.php b/lib/Gitlab/Api/Groups.php index 38039b787..b771dc579 100644 --- a/lib/Gitlab/Api/Groups.php +++ b/lib/Gitlab/Api/Groups.php @@ -91,15 +91,16 @@ public function transfer($group_id, $project_id) /** * @param integer $id + * @param integer|null $user_id * @param array $parameters * @return mixed */ - public function allMembers($id, array $parameters = []) + public function allMembers($id, $user_id = null, array $parameters = []) { $resolver = $this->createOptionsResolver(); $resolver->setDefined('query'); - return $this->get('groups/'.$this->encodePath($id).'/members/all', $resolver->resolve($parameters)); + return $this->get('groups/'.$this->encodePath($id).'/members/all/'.$this->encodePath($user_id), $resolver->resolve($parameters)); } /** diff --git a/lib/Gitlab/Api/Projects.php b/lib/Gitlab/Api/Projects.php index e5ea9b6cc..2569162e1 100644 --- a/lib/Gitlab/Api/Projects.php +++ b/lib/Gitlab/Api/Projects.php @@ -286,18 +286,19 @@ public function deletePipeline($project_id, $pipeline_id) { return $this->delete($this->getProjectPath($project_id, 'pipelines/'.$this->encodePath($pipeline_id))); } - + /** * @param integer $project_id + * @param integer|null $user_id * @param array $parameters * @return mixed */ - public function allMembers($project_id, $parameters = []) + public function allMembers($project_id, $user_id = null, $parameters = []) { $resolver = $this->createOptionsResolver(); $resolver->setDefined('query'); - return $this->get('projects/'.$this->encodePath($project_id).'/members/all', $resolver->resolve($parameters)); + return $this->get('projects/'.$this->encodePath($project_id).'/members/all/'.$this->encodePath($user_id), $resolver->resolve($parameters)); } /** From 96f9f442da16b2d856174d121768734bf8734773 Mon Sep 17 00:00:00 2001 From: Hannes Filip Date: Fri, 6 Mar 2020 15:53:35 +0100 Subject: [PATCH 2/5] List all members of a group or project including inherited members with optional user id filter and pagination for model --- lib/Gitlab/Model/Group.php | 23 +++++++++++++++++++++++ lib/Gitlab/Model/Project.php | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/Gitlab/Model/Group.php b/lib/Gitlab/Model/Group.php index 97628f3d2..1939dd23e 100644 --- a/lib/Gitlab/Model/Group.php +++ b/lib/Gitlab/Model/Group.php @@ -116,6 +116,29 @@ public function transfer($project_id) return Group::fromArray($this->getClient(), $data); } + /** + * @param integer|null $user_id + * @param bool $all + * @return array|User + */ + public function allMembers($user_id = null, $all = false) + { + if ($all) + $data = (new \Gitlab\ResultPager($this->client))->fetchAll($this->client->groups(),"allMembers", [$this->id,$user_id]); + else + $data = $this->client->groups()->allMembers($this->id, $user_id); + + if ($user_id != null) + return User::fromArray($this->getClient(), $data); + else { + $members = array(); + foreach ($data as $member) { + $members[] = User::fromArray($this->getClient(), $member); + } + return $members; + } + } + /** * @return User[] */ diff --git a/lib/Gitlab/Model/Project.php b/lib/Gitlab/Model/Project.php index 8d08c1165..4a476a3ec 100644 --- a/lib/Gitlab/Model/Project.php +++ b/lib/Gitlab/Model/Project.php @@ -217,6 +217,29 @@ public function remove() return true; } + /** + * @param integer|null $user_id + * @param bool $all + * @return array|User + */ + public function allMembers($user_id = null, $all = false) + { + if ($all) + $data = (new \Gitlab\ResultPager($this->client))->fetchAll($this->client->projects(),"allMembers", [$this->id,$user_id]); + else + $data = $this->client->projects()->allMembers($this->id, $user_id); + + if ($user_id != null) + return User::fromArray($this->getClient(), $data); + else { + $members = array(); + foreach ($data as $member) { + $members[] = User::fromArray($this->getClient(), $member); + } + return $members; + } + } + /** * @param string $username_query * @return User[] From cfefc040c6f1ac1e3959f3df901f88faefbcea70 Mon Sep 17 00:00:00 2001 From: Hannes Filip Date: Fri, 6 Mar 2020 16:19:01 +0100 Subject: [PATCH 3/5] Fix code style --- lib/Gitlab/Model/Group.php | 11 ++++++----- lib/Gitlab/Model/Project.php | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/Gitlab/Model/Group.php b/lib/Gitlab/Model/Group.php index 1939dd23e..89f867ce4 100644 --- a/lib/Gitlab/Model/Group.php +++ b/lib/Gitlab/Model/Group.php @@ -123,14 +123,15 @@ public function transfer($project_id) */ public function allMembers($user_id = null, $all = false) { - if ($all) - $data = (new \Gitlab\ResultPager($this->client))->fetchAll($this->client->groups(),"allMembers", [$this->id,$user_id]); - else + if ($all) { + $data = (new \Gitlab\ResultPager($this->client))->fetchAll($this->client->groups(), "allMembers", [$this->id, $user_id]); + } else { $data = $this->client->groups()->allMembers($this->id, $user_id); + } - if ($user_id != null) + if ($user_id != null) { return User::fromArray($this->getClient(), $data); - else { + } else { $members = array(); foreach ($data as $member) { $members[] = User::fromArray($this->getClient(), $member); diff --git a/lib/Gitlab/Model/Project.php b/lib/Gitlab/Model/Project.php index 4a476a3ec..471bdfa15 100644 --- a/lib/Gitlab/Model/Project.php +++ b/lib/Gitlab/Model/Project.php @@ -224,14 +224,15 @@ public function remove() */ public function allMembers($user_id = null, $all = false) { - if ($all) - $data = (new \Gitlab\ResultPager($this->client))->fetchAll($this->client->projects(),"allMembers", [$this->id,$user_id]); - else + if ($all) { + $data = (new \Gitlab\ResultPager($this->client))->fetchAll($this->client->projects(), "allMembers", [$this->id, $user_id]); + } else { $data = $this->client->projects()->allMembers($this->id, $user_id); + } - if ($user_id != null) + if ($user_id != null) { return User::fromArray($this->getClient(), $data); - else { + } else { $members = array(); foreach ($data as $member) { $members[] = User::fromArray($this->getClient(), $member); From 72e5c9190a19c8e999becd9b483a85ed3d7b8f12 Mon Sep 17 00:00:00 2001 From: Hannes Filip Date: Fri, 6 Mar 2020 16:19:25 +0100 Subject: [PATCH 4/5] Added test units --- test/Gitlab/Tests/Api/GroupsTest.php | 18 +++++++++++++++++- test/Gitlab/Tests/Api/ProjectsTest.php | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/test/Gitlab/Tests/Api/GroupsTest.php b/test/Gitlab/Tests/Api/GroupsTest.php index b4da1de4d..51ff6d5f0 100644 --- a/test/Gitlab/Tests/Api/GroupsTest.php +++ b/test/Gitlab/Tests/Api/GroupsTest.php @@ -199,13 +199,29 @@ public function shouldGetAllMembers() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('groups/1/members/all') + ->with('groups/1/members/all/') ->will($this->returnValue($expectedArray)) ; $this->assertEquals($expectedArray, $api->allMembers(1)); } + /** + * @test + */ + public function shouldGetAllMembersUserID() + { + $expectedArray = array('id' => 2, 'name' => 'Bob'); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('groups/1/members/all/2') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->allMembers(1,2)); + } + /** * @test */ diff --git a/test/Gitlab/Tests/Api/ProjectsTest.php b/test/Gitlab/Tests/Api/ProjectsTest.php index 043c4216b..22a413714 100644 --- a/test/Gitlab/Tests/Api/ProjectsTest.php +++ b/test/Gitlab/Tests/Api/ProjectsTest.php @@ -661,12 +661,28 @@ public function shouldGetAllMembers() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('projects/1/members/all') + ->with('projects/1/members/all/') ->will($this->returnValue($expectedArray)); $this->assertEquals($expectedArray, $api->allMembers(1)); } + /** + * @test + */ + public function shouldGetAllMembersUserID() + { + $expectedArray = array('id' => 2, 'name' => 'Bob'); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('projects/1/members/all/2') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->allMembers(1,2)); + } + /** * @test */ From 2561dacf94ad6abf9b191e32c800166465e0beaf Mon Sep 17 00:00:00 2001 From: Hannes Filip Date: Fri, 6 Mar 2020 16:21:44 +0100 Subject: [PATCH 5/5] Fix style --- test/Gitlab/Tests/Api/GroupsTest.php | 2 +- test/Gitlab/Tests/Api/ProjectsTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Gitlab/Tests/Api/GroupsTest.php b/test/Gitlab/Tests/Api/GroupsTest.php index 51ff6d5f0..571cf3193 100644 --- a/test/Gitlab/Tests/Api/GroupsTest.php +++ b/test/Gitlab/Tests/Api/GroupsTest.php @@ -219,7 +219,7 @@ public function shouldGetAllMembersUserID() ->with('groups/1/members/all/2') ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->allMembers(1,2)); + $this->assertEquals($expectedArray, $api->allMembers(1, 2)); } /** diff --git a/test/Gitlab/Tests/Api/ProjectsTest.php b/test/Gitlab/Tests/Api/ProjectsTest.php index 22a413714..9d85b61a4 100644 --- a/test/Gitlab/Tests/Api/ProjectsTest.php +++ b/test/Gitlab/Tests/Api/ProjectsTest.php @@ -680,7 +680,7 @@ public function shouldGetAllMembersUserID() ->with('projects/1/members/all/2') ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->allMembers(1,2)); + $this->assertEquals($expectedArray, $api->allMembers(1, 2)); } /**