From b85248709258f47c7c146f6b41cbd2617ae6a079 Mon Sep 17 00:00:00 2001 From: Maciej Kozak Date: Thu, 18 Aug 2022 14:10:51 +0200 Subject: [PATCH] add memberships endpoint --- lib/gitlab/client/users.rb | 10 ++++++++++ spec/fixtures/memberships.json | 1 + spec/gitlab/client/users_spec.rb | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 spec/fixtures/memberships.json diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 39eb701a5..c042cb156 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -393,5 +393,15 @@ def create_user_impersonation_token(user_id, name, scopes, expires_at = nil) def revoke_user_impersonation_token(user_id, impersonation_token_id) delete("/users/#{user_id}/impersonation_tokens/#{impersonation_token_id}") end + + # Lists all projects and groups a user is a member of + # + # @example + # Gitlab.memberships(2) + # + # @param [Integer] user_id The ID of the user. + def memberships(user_id) + get("/users/#{user_id}/memberships") + end end end diff --git a/spec/fixtures/memberships.json b/spec/fixtures/memberships.json new file mode 100644 index 000000000..a6de14197 --- /dev/null +++ b/spec/fixtures/memberships.json @@ -0,0 +1 @@ +[{"source_id": 1,"source_name": "Project one","source_type": "Project","access_level": "20"},{"source_id": 3,"source_name": "Group three","source_type": "Namespace","access_level": "20"}] diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 715360738..202844019 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -632,4 +632,22 @@ expect(@token.to_hash).to be_empty end end + + describe '.memberships' do + before do + stub_get('/user/2/memberships', 'memberships') + @memberships = Gitlab.memberships(2) + end + + it 'gets the correct resource' do + expect(a_get('/user/2/memberships')).to have_been_made + end + + it 'returns an information about all project and groups of user' do + expect(@memberships.first.source_id).to eq 1 + expect(@memberships.first.source_name).to eq 'Project one' + expect(@memberships.first.source_type).to eq 'Project' + expect(@memberships.first.access_level).to eq 20 + end + end end