Skip to content

Commit

Permalink
Add user exists endpoint (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
strangelookingnerd committed Aug 29, 2024
1 parent 7a224e3 commit e5cfb2b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/gitlab4j/api/UserApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gitlab4j.api;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
Expand All @@ -14,6 +15,7 @@
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.CustomAttribute;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.Exists;
import org.gitlab4j.api.models.GpgKey;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
Expand Down Expand Up @@ -1413,4 +1415,24 @@ public void deactivateUser(Long userId) throws GitLabApiException {
}
post(Response.Status.CREATED, (Form) null, "users", userId, "deactivate");
}

/**
* Check if the given user exists.
*
* <pre><code>GitLab Endpoint: POST /users/:username/exists</code></pre>
*
* @param username the name of the user to check
* @throws GitLabApiException if any exception occurs.
*/
public boolean exists(String username) throws GitLabApiException {
if (username == null) {
throw new RuntimeException("username cannot be null");
}
try {
Response response = get(Response.Status.OK, null, getApiClient().getUrlWithBase("users", username, "exists"));
return response.readEntity(Exists.class).getExists();
} catch (IOException e) {
throw new GitLabApiException(e);
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/org/gitlab4j/api/models/Exists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.gitlab4j.api.models;

import org.gitlab4j.api.utils.JacksonJson;

import java.io.Serializable;

public class Exists implements Serializable {
private static final long serialVersionUID = 1L;

private Boolean exists;

public Boolean getExists() {
return exists;
}

public void setExists(Boolean exists) {
this.exists = exists;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
10 changes: 10 additions & 0 deletions src/test/java/org/gitlab4j/api/TestUserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,16 @@ public void testGpgKeysCurrentUser() throws GitLabApiException {
assertNull(found);
}

@Test
public void testUserExists() throws GitLabApiException {
User currentUser = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(currentUser);
assertEquals(TEST_USERNAME, currentUser.getUsername());

assertTrue(gitLabApi.getUserApi().exists(currentUser.getUsername()));
assertFalse(gitLabApi.getUserApi().exists("doesnotexist"));
}

public void testGetMemberships() throws GitLabApiException {
User currentUser = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(currentUser);
Expand Down

0 comments on commit e5cfb2b

Please sign in to comment.