From 6a53bc3c56a47b32c591b785722d4f67840f9b35 Mon Sep 17 00:00:00 2001 From: Raimund Hook Date: Thu, 22 Jun 2023 17:21:28 +0100 Subject: [PATCH 1/2] Adding user_by_username method for single user This allows you to search for a single user by providing their username Signed-off-by: Raimund Hook --- lib/gitlab/client/users.rb | 15 ++++++++++++++- spec/gitlab/client/users_spec.rb | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 31dd23886..4b61d5d89 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -298,7 +298,7 @@ def delete_email(id, user_id = nil) delete(url) end - # Search for groups by name + # Search for users by name # # @example # Gitlab.user_search('gitlab') @@ -313,6 +313,19 @@ def user_search(search, options = {}) get('/users', query: options) end + # Get user by username + # + # @example + # Gitlab.user_by_username('gitlab') + # + # @param [String] username A username to get. + # @param [Hash] options A customizable set of options. + # @return [Gitlab::ObjectifiedHash] + def user_by_username(username, options = {}) + options[:username] = username + get('/users', query: options).first + end + # Gets user custom_attributes. # # @example diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index b680518c1..8927e0667 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -524,6 +524,21 @@ end end + describe '.user_by_username' do + before do + stub_get('/users?username=User', 'user') + @user = Gitlab.user_by_username('User') + end + + it 'gets the correct resource' do + expect(a_get('/users?username=User')).to have_been_made + end + + it 'returns information about a user' do + expect(@user.email).to eq('john@example.com') + end + end + describe '.user_custom_attributes' do before do stub_get('/users/2/custom_attributes', 'user_custom_attributes') From 09577a925cf9150a005db40839239c6a0fb12392 Mon Sep 17 00:00:00 2001 From: Raimund Hook Date: Fri, 23 Jun 2023 12:41:11 +0100 Subject: [PATCH 2/2] Changed output to array, added empty spec Signed-off-by: Raimund Hook --- lib/gitlab/client/users.rb | 4 ++-- spec/fixtures/empty_array.json | 1 + spec/fixtures/user_by_username.json | 1 + spec/gitlab/client/users_spec.rb | 29 +++++++++++++++++++++-------- 4 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 spec/fixtures/empty_array.json create mode 100644 spec/fixtures/user_by_username.json diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 4b61d5d89..80bf9d80f 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -320,10 +320,10 @@ def user_search(search, options = {}) # # @param [String] username A username to get. # @param [Hash] options A customizable set of options. - # @return [Gitlab::ObjectifiedHash] + # @return [Array] def user_by_username(username, options = {}) options[:username] = username - get('/users', query: options).first + get('/users', query: options) end # Gets user custom_attributes. diff --git a/spec/fixtures/empty_array.json b/spec/fixtures/empty_array.json new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/spec/fixtures/empty_array.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/spec/fixtures/user_by_username.json b/spec/fixtures/user_by_username.json new file mode 100644 index 000000000..1aa5ceb5f --- /dev/null +++ b/spec/fixtures/user_by_username.json @@ -0,0 +1 @@ +[{"id":1,"email":"john@example.com","name":"John Smith","username":"john.smith","bio":null,"skype":"","linkedin":"","twitter":"john","dark_scheme":false,"theme_id":1,"blocked":false,"created_at":"2012-09-17T09:41:56Z"}] \ No newline at end of file diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 8927e0667..e6cf48e9c 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -525,17 +525,30 @@ end describe '.user_by_username' do - before do - stub_get('/users?username=User', 'user') - @user = Gitlab.user_by_username('User') - end + context 'with a valid username' do + before do + stub_get('/users?username=User', 'user_by_username') + @user = Gitlab.user_by_username('User') + end - it 'gets the correct resource' do - expect(a_get('/users?username=User')).to have_been_made + it 'gets the correct resource' do + expect(a_get('/users?username=User')).to have_been_made + end + + it 'returns information about a user' do + expect(@user.first.email).to eq('john@example.com') + end end - it 'returns information about a user' do - expect(@user.email).to eq('john@example.com') + context 'with an invalid username' do + before do + stub_get('/users?username=InvalidUser', 'empty_array') + @user = Gitlab.user_by_username('InvalidUser') + end + + it 'returns an empty array' do + expect(@user).to eq([]) + end end end