Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@StingRayZA
Copy link
Contributor

@StingRayZA StingRayZA commented Jun 22, 2023

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
Copy link
Contributor

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

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

Signed-off-by: Raimund Hook <[email protected]>
@StingRayZA StingRayZA force-pushed the stingrayza/add-user-by-username branch from 5c6c34c to 35b88be Compare June 23, 2023 13:26
@StingRayZA
Copy link
Contributor Author

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
Copy link
Contributor Author

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

Thanks in advance 🙏

@github-actions
Copy link

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.

@github-actions github-actions bot added the stale Inactive label Sep 22, 2023
@ltickett
Copy link
Contributor

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

@github-actions github-actions bot removed the stale Inactive label Sep 23, 2023
@github-actions
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Inactive

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants