Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
dddded1a
Commit
dddded1a
authored
Jul 17, 2019
by
Greg Messner
Browse files
Added support to get user by email address (#402).
parent
07022cef
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/UserApi.java
View file @
dddded1a
...
@@ -16,6 +16,7 @@ import org.gitlab4j.api.models.CustomAttribute;
...
@@ -16,6 +16,7 @@ import org.gitlab4j.api.models.CustomAttribute;
import
org.gitlab4j.api.models.Email
;
import
org.gitlab4j.api.models.Email
;
import
org.gitlab4j.api.models.ImpersonationToken
;
import
org.gitlab4j.api.models.ImpersonationToken
;
import
org.gitlab4j.api.models.ImpersonationToken.Scope
;
import
org.gitlab4j.api.models.ImpersonationToken.Scope
;
import
org.gitlab4j.api.utils.EmailChecker
;
import
org.gitlab4j.api.models.SshKey
;
import
org.gitlab4j.api.models.SshKey
;
import
org.gitlab4j.api.models.User
;
import
org.gitlab4j.api.models.User
;
...
@@ -307,7 +308,10 @@ public class UserApi extends AbstractApi {
...
@@ -307,7 +308,10 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
User
getUser
(
String
username
)
throws
GitLabApiException
{
public
User
getUser
(
String
username
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
createGitLabApiForm
().
withParam
(
"username"
,
username
,
true
);
GitLabApiForm
formData
=
createGitLabApiForm
()
.
withParam
(
"username"
,
username
,
true
)
.
withParam
(
PAGE_PARAM
,
1
)
.
withParam
(
PER_PAGE_PARAM
,
1
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"users"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"users"
);
List
<
User
>
users
=
response
.
readEntity
(
new
GenericType
<
List
<
User
>>()
{});
List
<
User
>
users
=
response
.
readEntity
(
new
GenericType
<
List
<
User
>>()
{});
return
(
users
.
isEmpty
()
?
null
:
users
.
get
(
0
));
return
(
users
.
isEmpty
()
?
null
:
users
.
get
(
0
));
...
@@ -331,6 +335,42 @@ public class UserApi extends AbstractApi {
...
@@ -331,6 +335,42 @@ public class UserApi extends AbstractApi {
}
}
}
}
/**
* Lookup a user by email address. Returns null if not found.
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param email the email of the user to get
* @return the User instance for the specified email, or null if not found
* @throws GitLabApiException if any exception occurs
* @throws IllegalArgumentException if email is not valid
*/
public
User
getUserByEmail
(
String
email
)
throws
GitLabApiException
{
if
(!
EmailChecker
.
isValidEmail
(
email
))
{
throw
new
IllegalArgumentException
(
"email is not valid"
);
}
List
<
User
>
users
=
findUsers
(
email
,
1
,
1
);
return
(
users
.
isEmpty
()
?
null
:
users
.
get
(
0
));
}
/**
* Lookup a user by email address and returns an Optional with the User instance as the value.
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param email the email of the user to get
* @return the User for the specified email as an Optional instance
*/
public
Optional
<
User
>
getOptionalUserByEmail
(
String
email
)
{
try
{
return
(
Optional
.
ofNullable
(
getUserByEmail
(
email
)));
}
catch
(
GitLabApiException
glae
)
{
return
(
GitLabApi
.
createOptionalFromException
(
glae
));
}
}
/**
/**
* Search users by Email or username
* Search users by Email or username
*
*
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment