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

Adding user_by_username method for single user #671

Closed StingRayZA closed 8 months ago

StingRayZA commented 1 year ago

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"]
ltickett commented 1 year ago

I think we may need to handle the scenario where no match is found?

I suspect .first will throw an error.

We can add a spec too.

Great work @StingRayZA

StingRayZA commented 1 year ago

I think we may need to handle the scenario where no match is found?

I suspect .first will throw an error.

We can add a spec too.

I had a look at this specifically, and in the case where a 'plain' api returns no results, you get an empty array back. I decided to make this the behaviour for this method, too.

❯ curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "http://gdk.test:3000/api/v4/users?username=reported_user_11234"                                  
[]

I fleshed the specs out, too, in order to cater for this particular occurence.

StingRayZA commented 1 year ago

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

Thanks in advance 🙏

github-actions[bot] commented 11 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ltickett commented 11 months ago

I think we're still looking for a review here please 🙏

github-actions[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.