Skip to content

Commit cd7d0c3

Browse files
Add user exists endpoint (#1157)
1 parent 050eaf5 commit cd7d0c3

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.util.Date;
56
import java.util.List;
67
import java.util.Objects;
@@ -14,6 +15,7 @@
1415
import org.gitlab4j.api.GitLabApi.ApiVersion;
1516
import org.gitlab4j.api.models.CustomAttribute;
1617
import org.gitlab4j.api.models.Email;
18+
import org.gitlab4j.api.models.Exists;
1719
import org.gitlab4j.api.models.GpgKey;
1820
import org.gitlab4j.api.models.ImpersonationToken;
1921
import org.gitlab4j.api.models.ImpersonationToken.Scope;
@@ -1413,4 +1415,24 @@ public void deactivateUser(Long userId) throws GitLabApiException {
14131415
}
14141416
post(Response.Status.CREATED, (Form) null, "users", userId, "deactivate");
14151417
}
1418+
1419+
/**
1420+
* Check if the given user exists.
1421+
*
1422+
* <pre><code>GitLab Endpoint: POST /users/:username/exists</code></pre>
1423+
*
1424+
* @param username the name of the user to check
1425+
* @throws GitLabApiException if any exception occurs.
1426+
*/
1427+
public boolean exists(String username) throws GitLabApiException {
1428+
if (username == null) {
1429+
throw new RuntimeException("username cannot be null");
1430+
}
1431+
try {
1432+
Response response = get(Response.Status.OK, null, getApiClient().getUrlWithBase("users", username, "exists"));
1433+
return response.readEntity(Exists.class).getExists();
1434+
} catch (IOException e) {
1435+
throw new GitLabApiException(e);
1436+
}
1437+
}
14161438
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
import java.io.Serializable;
6+
7+
public class Exists implements Serializable {
8+
private static final long serialVersionUID = 1L;
9+
10+
private Boolean exists;
11+
12+
public Boolean getExists() {
13+
return exists;
14+
}
15+
16+
public void setExists(Boolean exists) {
17+
this.exists = exists;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return (JacksonJson.toJsonString(this));
23+
}
24+
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,16 @@ public void testGpgKeysCurrentUser() throws GitLabApiException {
607607
assertNull(found);
608608
}
609609

610+
@Test
611+
public void testUserExists() throws GitLabApiException {
612+
User currentUser = gitLabApi.getUserApi().getCurrentUser();
613+
assertNotNull(currentUser);
614+
assertEquals(TEST_USERNAME, currentUser.getUsername());
615+
616+
assertTrue(gitLabApi.getUserApi().exists(currentUser.getUsername()));
617+
assertFalse(gitLabApi.getUserApi().exists("doesnotexist"));
618+
}
619+
610620
public void testGetMemberships() throws GitLabApiException {
611621
User currentUser = gitLabApi.getUserApi().getCurrentUser();
612622
assertNotNull(currentUser);

0 commit comments

Comments
 (0)