Skip to content

Commit 67043cc

Browse files
committed
Added getUser(String username) and test for same.
1 parent 376d5ac commit 67043cc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/main/java/org/gitlab4j/api/UserApi.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ public User getUser(int userId) throws GitLabApiException {
7474
return (response.readEntity(User.class));
7575
}
7676

77+
/**
78+
* Lookup a user by username.
79+
*
80+
* NOTE: This is for admin users only.
81+
*
82+
* GET /users?username=:username
83+
*
84+
* @param username the username of the user to get
85+
* @return the User instance for the specified username
86+
* @throws GitLabApiException if any exception occurs
87+
*/
88+
public User getUser(String username) throws GitLabApiException {
89+
GitLabApiForm formData = new GitLabApiForm().withParam("username", username, true);
90+
Response response = get(Response.Status.OK, formData.asMap(), "users");
91+
List<User> users = response.readEntity(new GenericType<List<User>>() {});
92+
return (users.isEmpty() ? null : users.get(0));
93+
}
94+
7795
/**
7896
* Search users by Email or username
7997
*

src/test/java/org/gitlab4j/api/TestUserApi.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,11 @@ public void testGetCurrentUser() throws GitLabApiException {
8282
assertNotNull(currentUser);
8383
assertEquals(TEST_USERNAME, currentUser.getUsername());
8484
}
85+
86+
@Test
87+
public void testLookupUser() throws GitLabApiException {
88+
User user = gitLabApi.getUserApi().getUser(TEST_USERNAME);
89+
assertNotNull(user);
90+
assertEquals(TEST_USERNAME, user.getUsername());
91+
}
8592
}

0 commit comments

Comments
 (0)