From 0a191c1a9f3af484fe74bb0b1c77a7f9d06d5991 Mon Sep 17 00:00:00 2001 From: Flemming Frandsen Date: Mon, 14 Feb 2022 08:12:01 +0100 Subject: [PATCH 1/3] Fixed the MembershipSourceType enum so it actually works in stead of turning all strings from Gitlab into null --- .../api/models/MembershipSourceType.java | 25 ++++++++++++------- .../api/models/MembershipSourceTypeTest.java | 19 ++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/gitlab4j/api/models/MembershipSourceTypeTest.java diff --git a/src/main/java/org/gitlab4j/api/models/MembershipSourceType.java b/src/main/java/org/gitlab4j/api/models/MembershipSourceType.java index 8f651f29d..e35dbb4f2 100644 --- a/src/main/java/org/gitlab4j/api/models/MembershipSourceType.java +++ b/src/main/java/org/gitlab4j/api/models/MembershipSourceType.java @@ -1,31 +1,38 @@ package org.gitlab4j.api.models; -import org.gitlab4j.api.utils.JacksonJsonEnumHelper; - import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; -public enum MembershipSourceType { +import java.util.Locale; - PROJECT, +public enum MembershipSourceType { + PROJECT("Project"), /** Representing a group */ - NAMESPACE; + NAMESPACE("Namespace"); - private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(MembershipSourceType.class); + public final String name; + + MembershipSourceType(String name) { + this.name = name; + } @JsonCreator public static MembershipSourceType forValue(String value) { - return enumHelper.forValue(value); + if (value == null) { + return null; + } else { + return MembershipSourceType.valueOf(value.toUpperCase(Locale.ROOT)); + } } @JsonValue public String toValue() { - return (enumHelper.toString(this)); + return this.name; } @Override public String toString() { - return (enumHelper.toString(this)); + return this.name; } } diff --git a/src/test/java/org/gitlab4j/api/models/MembershipSourceTypeTest.java b/src/test/java/org/gitlab4j/api/models/MembershipSourceTypeTest.java new file mode 100644 index 000000000..f6cc54c70 --- /dev/null +++ b/src/test/java/org/gitlab4j/api/models/MembershipSourceTypeTest.java @@ -0,0 +1,19 @@ +package org.gitlab4j.api.models; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MembershipSourceTypeTest { + + @Test + public void forValue() { + final MembershipSourceType namespace = MembershipSourceType.forValue(MembershipSourceType.NAMESPACE.name); + Assert.assertEquals(MembershipSourceType.NAMESPACE, namespace); + Assert.assertEquals("Namespace", namespace.name); + final MembershipSourceType project = MembershipSourceType.forValue(MembershipSourceType.PROJECT.name); + Assert.assertEquals(MembershipSourceType.PROJECT, project); + Assert.assertEquals("Project", project.name); + } +} From ef5aad7d7ee9f7acfd59d00328ed98aeac9e195d Mon Sep 17 00:00:00 2001 From: Flemming Frandsen Date: Mon, 14 Feb 2022 08:44:30 +0100 Subject: [PATCH 2/3] Fixed UserApi.getMemberships() so it returns all the memberships of a user, not just the first page of them --- src/main/java/org/gitlab4j/api/UserApi.java | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/UserApi.java b/src/main/java/org/gitlab4j/api/UserApi.java index 8643dbda1..a8c9b59fe 100644 --- a/src/main/java/org/gitlab4j/api/UserApi.java +++ b/src/main/java/org/gitlab4j/api/UserApi.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.ws.rs.core.Form; @@ -1306,9 +1307,24 @@ public void deleteGpgKey(final Integer userId, final Integer keyId) throws GitLa * @throws GitLabApiException if any exception occurs * @since GitLab 12.8 */ - public List getMemberships(Integer userId) throws GitLabApiException { + public List getMemberships(int userId) throws GitLabApiException { + return getMembershipsPager(userId).stream().collect(Collectors.toList()); + } + + /** + * Returns a Pager that lists all projects and groups a user is a member of. (admin only) + * + * This allows lazy-fetching of huge numbers of memberships. + * + *
GitLab Endpoint: GET /users/:id/memberships
+ * + * @param userId the ID of the user to get the memberships for + * @return the list of memberships of the given user + * @throws GitLabApiException if any exception occurs + * @since GitLab 12.8 + */ + public Pager getMembershipsPager(int userId) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm(); - Response response = get(Response.Status.OK, formData.asMap(), "users", userId, "memberships"); - return (response.readEntity(new GenericType>() {})); + return (new Pager<>(this, Membership.class, 100, formData.asMap(), "users", userId, "memberships")); } } From a4dfaf4300016e857b68f05ebba92bed7ed26d91 Mon Sep 17 00:00:00 2001 From: Flemming Frandsen Date: Mon, 14 Feb 2022 08:57:31 +0100 Subject: [PATCH 3/3] Implemented KeysApi --- src/main/java/org/gitlab4j/api/GitLabApi.java | 16 ++++++++++ src/main/java/org/gitlab4j/api/KeysApi.java | 30 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/main/java/org/gitlab4j/api/KeysApi.java diff --git a/src/main/java/org/gitlab4j/api/GitLabApi.java b/src/main/java/org/gitlab4j/api/GitLabApi.java index aa19398f5..7b0fafd85 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApi.java +++ b/src/main/java/org/gitlab4j/api/GitLabApi.java @@ -95,6 +95,7 @@ public String getApiNamespace() { private TodosApi todosApi; private UserApi userApi; private WikisApi wikisApi; + private KeysApi keysApi; /** * Get the GitLab4J shared Logger instance. @@ -1674,6 +1675,21 @@ public WikisApi getWikisApi() { return wikisApi; } + /** + * Gets the KeysApi instance owned by this GitLabApi instance. The KeysApi is used to look up users by their ssh key signatures + * + * @return the KeysApi instance owned by this GitLabApi instance + */ + public KeysApi getKeysAPI() { + synchronized (this) { + if (keysApi == null) { + keysApi = new KeysApi(this); + } + } + return keysApi; + } + + /** * Create and return an Optional instance associated with a GitLabApiException. * diff --git a/src/main/java/org/gitlab4j/api/KeysApi.java b/src/main/java/org/gitlab4j/api/KeysApi.java new file mode 100644 index 000000000..29265ff81 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/KeysApi.java @@ -0,0 +1,30 @@ +package org.gitlab4j.api; + +import org.gitlab4j.api.models.Key; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import java.util.Collections; + +/** + * See: + * https://docs.gitlab.com/ee/api/keys.html#get-user-by-fingerprint-of-ssh-key + */ +public class KeysApi extends AbstractApi { + public KeysApi(GitLabApi gitLabApi) { + super(gitLabApi); + } + + /** + * @param fingerprint The md5 hash of a ssh public key with : separating the bytes Or SHA256:$base64hash + * @return The Key which includes the user who owns the key + * @throws GitLabApiException If anything goes wrong + */ + public Key getUserBySSHKeyFingerprint(String fingerprint) throws GitLabApiException { + MultivaluedMap queryParams = new MultivaluedHashMap<>(); + queryParams.put("fingerprint", Collections.singletonList(fingerprint)); + Response response = get(Response.Status.OK, queryParams, "keys"); + return response.readEntity(Key.class); + } +}