From e5cfb2bfa9a49db734ca0a33bddeb678f09b42c4 Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Thu, 29 Aug 2024 10:04:13 +0200 Subject: [PATCH] Add user exists endpoint (#1135) --- src/main/java/org/gitlab4j/api/UserApi.java | 22 +++++++++++++++++ .../java/org/gitlab4j/api/models/Exists.java | 24 +++++++++++++++++++ .../java/org/gitlab4j/api/TestUserApi.java | 10 ++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/main/java/org/gitlab4j/api/models/Exists.java diff --git a/src/main/java/org/gitlab4j/api/UserApi.java b/src/main/java/org/gitlab4j/api/UserApi.java index 190cd6c9..94de754e 100644 --- a/src/main/java/org/gitlab4j/api/UserApi.java +++ b/src/main/java/org/gitlab4j/api/UserApi.java @@ -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; @@ -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; @@ -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. + * + *
GitLab Endpoint: POST /users/:username/exists
+ *
+ * @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);
+ }
+ }
}
diff --git a/src/main/java/org/gitlab4j/api/models/Exists.java b/src/main/java/org/gitlab4j/api/models/Exists.java
new file mode 100644
index 00000000..5e68a581
--- /dev/null
+++ b/src/main/java/org/gitlab4j/api/models/Exists.java
@@ -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));
+ }
+}
diff --git a/src/test/java/org/gitlab4j/api/TestUserApi.java b/src/test/java/org/gitlab4j/api/TestUserApi.java
index 1ac9ceb4..4ebbafee 100644
--- a/src/test/java/org/gitlab4j/api/TestUserApi.java
+++ b/src/test/java/org/gitlab4j/api/TestUserApi.java
@@ -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);