From db751c843bbb020a96d17950930d02071aa6d998 Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sat, 19 Aug 2017 17:19:31 +0200 Subject: [PATCH 1/8] Allow creating ssh keys for different users --- lib/gitlab/client/users.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index 6953f4650..ab649483e 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -161,9 +161,14 @@ def ssh_key(id) # # @param [String] title The title of an SSH key. # @param [String] key The SSH key body. + # @param [Integer] id optional 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, user_id) + if user_id.to_i.zero? + post("/user/keys", body: { title: title, key: key }) + else + post("/user/#{user_id}/keys", body: { title: title, key: key }) + end end # Deletes an SSH key. From 60b4b28898e9c5247a85eb46062eabef3ea07c8b Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sat, 19 Aug 2017 17:22:32 +0200 Subject: [PATCH 2/8] Fix parameter handling --- lib/gitlab/client/users.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index ab649483e..be978187e 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -161,9 +161,11 @@ def ssh_key(id) # # @param [String] title The title of an SSH key. # @param [String] key The SSH key body. - # @param [Integer] id optional id of the user to associate the key with + # @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, user_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 }) else From 61ee15459bbdb69c30b32e5f1da1f682b8f799df Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sat, 19 Aug 2017 17:27:16 +0200 Subject: [PATCH 3/8] Fix typo in url --- 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 be978187e..bfe3ed214 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -169,7 +169,7 @@ def create_ssh_key(title, key, options={}) if user_id.to_i.zero? post("/user/keys", body: { title: title, key: key }) else - post("/user/#{user_id}/keys", body: { title: title, key: key }) + post("/users/#{user_id}/keys", body: { title: title, key: key }) end end From a0790fa36aa8aa269680269f228357c5f2e296d3 Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sat, 19 Aug 2017 17:42:13 +0200 Subject: [PATCH 4/8] Allow deleting ssh keys for given user --- lib/gitlab/client/users.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index bfe3ed214..f27a99c63 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -180,7 +180,11 @@ def create_ssh_key(title, key, options={}) # # @param [Integer] id The ID of a user's SSH key. # @return [Gitlab::ObjectifiedHash] Information about deleted SSH key. - def delete_ssh_key(id) + def delete_ssh_key(id, options={}) + user_id = options.delete :user_id + if user_id.to_i.zero? + delete("/users/#{user_id}/keys/#{id}") + end delete("/user/keys/#{id}") end From 3737a53f79018d2e644f8ce17e6a57374f5dc80d Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sat, 19 Aug 2017 17:59:51 +0200 Subject: [PATCH 5/8] Fix conditional check --- lib/gitlab/client/users.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index f27a99c63..f578a970b 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -184,8 +184,9 @@ def delete_ssh_key(id, options={}) user_id = options.delete :user_id if user_id.to_i.zero? delete("/users/#{user_id}/keys/#{id}") + else + delete("/user/keys/#{id}") end - delete("/user/keys/#{id}") end # Gets user emails. From efc46a759caa2a78cb6a9a3bf01989afff731079 Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sat, 19 Aug 2017 18:07:23 +0200 Subject: [PATCH 6/8] Fix logic for delete_ssh_key --- lib/gitlab/client/users.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index f578a970b..caf49e300 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -183,9 +183,9 @@ def create_ssh_key(title, key, options={}) def delete_ssh_key(id, options={}) user_id = options.delete :user_id if user_id.to_i.zero? - delete("/users/#{user_id}/keys/#{id}") - else delete("/user/keys/#{id}") + else + delete("/users/#{user_id}/keys/#{id}") end end From 853f6f17d642aec86a353f5b88cdc028d40014fa Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sun, 20 Aug 2017 11:17:11 +0200 Subject: [PATCH 7/8] Add function to search for usernames --- lib/gitlab/client/users.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/gitlab/client/users.rb b/lib/gitlab/client/users.rb index caf49e300..1ddec6176 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -258,5 +258,21 @@ def user_search(search, options={}) options[:search] = search get("/users", query: options) end + + + # Search for user by username + # + # @example + # Gitlab.user_search_username('gitlab') + # + # @param [String] search A username to search + # @param [Hash] options A customizable set of options. + # @option options [String] :per_page Number of user to return per page + # @option options [String] :page The page to retrieve + # @return [Array] + def user_search(username, options={}) + options[:username] = username + get("/users", query: options) + end end end From 36f48909ee50995a4551353dd76c66aa2a389b5f Mon Sep 17 00:00:00 2001 From: Alexander Wirt Date: Sun, 20 Aug 2017 11:18:04 +0200 Subject: [PATCH 8/8] Fix function name --- 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 1ddec6176..9960f510e 100644 --- a/lib/gitlab/client/users.rb +++ b/lib/gitlab/client/users.rb @@ -270,7 +270,7 @@ def user_search(search, options={}) # @option options [String] :per_page Number of user to return per page # @option options [String] :page The page to retrieve # @return [Array] - def user_search(username, options={}) + def user_search_username(username, options={}) options[:username] = username get("/users", query: options) end