Skip to content
Closed
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
16 changes: 16 additions & 0 deletions src/main/java/org/gitlab4j/api/GitLabApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public String getApiNamespace() {
private TodosApi todosApi;
private UserApi userApi;
private WikisApi wikisApi;
private KeysApi keysApi;

/**
* Get the GitLab4J shared Logger instance.
Expand Down Expand Up @@ -1683,6 +1684,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.
*
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/gitlab4j/api/KeysApi.java
Original file line number Diff line number Diff line change
@@ -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<String, String> queryParams = new MultivaluedHashMap<>();
queryParams.put("fingerprint", Collections.singletonList(fingerprint));
Response response = get(Response.Status.OK, queryParams, "keys");
return response.readEntity(Key.class);
}
}
22 changes: 19 additions & 3 deletions src/main/java/org/gitlab4j/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Membership> getMemberships(Integer userId) throws GitLabApiException {
public List<Membership> 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.
*
* <pre><code>GitLab Endpoint: GET /users/:id/memberships</code></pre>
*
* @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<Membership> 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<List<Membership>>() {}));
return (new Pager<>(this, Membership.class, 100, formData.asMap(), "users", userId, "memberships"));
}
}
25 changes: 16 additions & 9 deletions src/main/java/org/gitlab4j/api/models/MembershipSourceType.java
Original file line number Diff line number Diff line change
@@ -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<MembershipSourceType> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}