Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user exists endpoint (#1135) #1157

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading