File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed
main/java/org/gitlab4j/api
test/java/org/gitlab4j/api Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments