From a28391da78ad43ecc2bc408ddaea9d15d827796a Mon Sep 17 00:00:00 2001 From: Rolf Offermanns Date: Wed, 3 Oct 2018 22:33:34 +0200 Subject: [PATCH 1/5] Allow adding ssh keys for different user --- lib/gitlab/client/users.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 5d5b0425a..0fbfc8cf4 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -163,9 +163,16 @@ def ssh_key(id) # # @param [String] title The title of an SSH key. # @param [String] key The SSH key body. + # @param [Hash] options A customizable set of options. + # @option options [Integer] :user_id id of the user to associate the key with # @return [Gitlab::ObjectifiedHash] Information about created SSH key. - def create_ssh_key(title, key) - post('/user/keys', body: { title: title, key: key }) + def create_ssh_key(title, key, options = {}) + user_id = options.delete :user_id + if user_id.to_i.zero? + post("/user/keys", body: { title: title, key: key }) + else + post("/users/#{user_id}/keys", body: { title: title, key: key }) + end end # Deletes an SSH key. From 0e6a2c63c9471a2a72ae96072b0ce9ea7831d7e4 Mon Sep 17 00:00:00 2001 From: Rolf Offermanns Date: Wed, 3 Oct 2018 22:33:47 +0200 Subject: [PATCH 2/5] Allow deleting ssh keys for different user --- lib/gitlab/client/users.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 0fbfc8cf4..97a0db87f 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -181,9 +181,16 @@ def create_ssh_key(title, key, options = {}) # Gitlab.delete_ssh_key(1) # # @param [Integer] id The ID of a user's SSH key. + # @param [Hash] options A customizable set of options. + # @option options [Integer] :user_id id of the user to associate the key with # @return [Gitlab::ObjectifiedHash] Information about deleted SSH key. - def delete_ssh_key(id) - delete("/user/keys/#{id}") + def delete_ssh_key(id, options = {}) + user_id = options.delete :user_id + if user_id.to_i.zero? + delete("/user/keys/#{id}") + else + delete("/users/#{user_id}/keys/#{id}") + end end # Gets user emails. From 87389c182a8df572dece4f869bbd917548fabd17 Mon Sep 17 00:00:00 2001 From: Rolf Offermanns Date: Wed, 3 Oct 2018 22:35:02 +0200 Subject: [PATCH 3/5] Add test for adding ssh keys for different user --- spec/gitlab/client/users_spec.rb | 37 ++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 01a37d123..3bcd8e1dd 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -252,18 +252,37 @@ end describe '.create_ssh_key' do - before do - stub_post('/user/keys', 'key') - @key = Gitlab.create_ssh_key('title', 'body') - end + describe 'without user ID' do + before do + stub_post('/user/keys', 'key') + @key = Gitlab.create_ssh_key('title', 'body') + end - it 'gets the correct resource' do - body = { title: 'title', key: 'body' } - expect(a_post('/user/keys').with(body: body)).to have_been_made + it 'gets the correct resource' do + body = { title: 'title', key: 'body' } + expect(a_post('/user/keys').with(body: body)).to have_been_made + end + + it 'returns information about a created SSH key' do + expect(@key.title).to eq('narkoz@helium') + end end - it 'returns information about a created SSH key' do - expect(@key.title).to eq('narkoz@helium') + describe 'with user ID' do + before do + stub_post('/users/1/keys', 'key') + @options = { user_id: 1 } + @key = Gitlab.create_ssh_key('title', 'body', @options) + end + + it 'gets the correct resource' do + body = { title: 'title', key: 'body' } + expect(a_post('/users/1/keys').with(body: body)).to have_been_made + end + + it 'returns information about a created SSH key' do + expect(@key.title).to eq('narkoz@helium') + end end end From b7993f4a0f54589b8dd74ed1dd0f738002f98461 Mon Sep 17 00:00:00 2001 From: Rolf Offermanns Date: Wed, 3 Oct 2018 22:35:18 +0200 Subject: [PATCH 4/5] Add test for deleting ssh keys for different user --- spec/gitlab/client/users_spec.rb | 34 ++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 3bcd8e1dd..eaeb11556 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -287,17 +287,35 @@ end describe '.delete_ssh_key' do - before do - stub_delete('/user/keys/1', 'key') - @key = Gitlab.delete_ssh_key(1) - end + describe 'without user ID' do + before do + stub_delete('/user/keys/1', 'key') + @key = Gitlab.delete_ssh_key(1) + end - it 'gets the correct resource' do - expect(a_delete('/user/keys/1')).to have_been_made + it 'gets the correct resource' do + expect(a_delete('/user/keys/1')).to have_been_made + end + + it 'returns information about a deleted SSH key' do + expect(@key.title).to eq('narkoz@helium') + end end - it 'returns information about a deleted SSH key' do - expect(@key.title).to eq('narkoz@helium') + describe 'with user ID' do + before do + stub_delete('/users/1/keys/1', 'key') + @options = { user_id: 1 } + @key = Gitlab.delete_ssh_key(1, @options) + end + + it 'gets the correct resource' do + expect(a_delete('/users/1/keys/1')).to have_been_made + end + + it 'returns information about a deleted SSH key' do + expect(@key.title).to eq('narkoz@helium') + end end end From 6c777ac8f5d055379aabe6f56976852e9435c13d Mon Sep 17 00:00:00 2001 From: Rolf Offermanns Date: Wed, 3 Oct 2018 22:52:03 +0200 Subject: [PATCH 5/5] Fix rubocop offense single quoted strings when not using interpolation --- lib/gitlab/client/users.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 97a0db87f..bc0c03afb 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -169,7 +169,7 @@ def ssh_key(id) def create_ssh_key(title, key, options = {}) user_id = options.delete :user_id if user_id.to_i.zero? - post("/user/keys", body: { title: title, key: key }) + post('/user/keys', body: { title: title, key: key }) else post("/users/#{user_id}/keys", body: { title: title, key: key }) end