NARKOZ / gitlab

Ruby wrapper and CLI for the GitLab REST API
https://narkoz.github.io/gitlab
BSD 2-Clause "Simplified" License
1.06k stars 394 forks source link

Add user_by_username method for single user #688

Closed StingRayZA closed 3 months ago

StingRayZA commented 4 months ago

(Originally logged as #671 )

Change

This change allows you to run a username search to find a single user.

Context

The GitLab api search parameter searches through more than just username. According to the docs, it searches name, username and public_email.

You can see this behaviour here:

❯ curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "http://gdk.test:3000/api/v4/users?search=reported" | jq '.[] | .username + ", " + .name'  
"mrrobot, Mr Roboto (reported)"
"reported_user_22, Stacy Orn"
"reported_user_21, Ocie Nader"
"reported_user_20, Elke Langworth"
"reported_user_19, Stephani Herman"
"reported_user_18, Deloris Block"
"reported_user_17, Ute Gutmann"
"reported_user_16, Maisha Zieme"
"reported_user_15, Willodean Murray"
"reported_user_14, Darrell Haley"
"reported_user_13, Beverly Rath"
"reported_user_12, Lucinda Haag"
"reported_user_11, Linh Kertzmann"
"reported_user_10, Bella Gibson"
"reported_user_9, Onie Schmidt"
"reported_user_8, Heike Bins"
"reported_user_7, Ilene Schuster"
"reported_user_6, Rozanne Hoppe"
"reported_user_5, Gaylene Schiller"
"reported_user_4, Holli Willms"

If I wanted to get a specific user by username, I need to use the username parameter:

❯ curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "http://gdk.test:3000/api/v4/users?username=reported_user_1" | jq '.[] | .username + ", " + .name'
"reported_user_1, Olevia Schuppe"

You can see this doesn't also return users starting with reported_user_1 (like reported_user_10, etc)

Current behaviour

The current user_search endpoint would require the following invocation to return a single user:

irb(main):004:0> gl.user_search(nil, username: 'reported_user_1').map{ |user| user.username}
=> ["reported_user_1"]

Invoking the same with 'simply' user_search returns multiple matches:

irb(main):005:0> gl.user_search('reported_user_1').map{ |user| user.username}
=> 
["reported_user_19",                            
 "reported_user_18",                            
 "reported_user_17",                            
 "reported_user_16",                            
 "reported_user_15",                            
 "reported_user_14",
 "reported_user_13",
 "reported_user_12",
 "reported_user_11",
 "reported_user_10",
 "reported_user_1"]
StingRayZA commented 4 months ago

Hi @NARKOZ Would you be able to take a look at this, please?

Thanks in advance 🙏

NARKOZ commented 3 months ago

Sorry for the delay. Thank you ❤️