Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user_by_username method for single user #688

Merged
merged 2 commits into from
Jun 13, 2024

Conversation

StingRayZA
Copy link
Contributor

(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"]

This allows you to search for a single user by providing their username

Signed-off-by: Raimund Hook <rhook@gitlab.com>
Signed-off-by: Raimund Hook <rhook@gitlab.com>
@StingRayZA
Copy link
Contributor Author

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

Thanks in advance 🙏

@NARKOZ NARKOZ merged commit 192a163 into NARKOZ:master Jun 13, 2024
6 checks passed
@NARKOZ
Copy link
Owner

NARKOZ commented Jun 13, 2024

Sorry for the delay. Thank you ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants