Skip to content
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
83 changes: 82 additions & 1 deletion src/main/java/org/gitlab4j/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.CustomAttribute;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.GpgKey;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.Membership;
Expand Down Expand Up @@ -1214,6 +1215,87 @@ public void deleteEmail(final Object userIdOrUsername, final Long emailId) throw
delete(Response.Status.NO_CONTENT, null, "users", getUserIdOrUsername(userIdOrUsername), "emails", emailId);
}

/**
* Get all GPG keys for the current user.
*
* <pre><code>GitLab Endpoint: GET /user/gpg_keys</code></pre>
*
* @throws GitLabApiException if any exception occurs
*/
public List<GpgKey> listGpgKeys() throws GitLabApiException {
Response response = get(Response.Status.OK, null, "user", "gpg_keys");
return (response.readEntity(new GenericType<List<GpgKey>>() {}));
}

/**
* Add a GPG key for the current user
*
* <pre><code>GitLab Endpoint: POST /user/gpg_keys</code></pre>
*
* @param key the ASCII-armored exported public GPG key to add
* @throws GitLabApiException if any exception occurs
*/
public GpgKey addGpgKey(final String key) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("key", key, true);
Response response = post(Response.Status.CREATED, formData, "user", "gpg_keys");
return (response.readEntity(GpgKey.class));
}

/**
* Remove a specific GPG key for the current user
*
* <pre><code>GitLab Endpoint: DELETE /user/gpg_keys/:keyId</code></pre>
*
* @param keyId the key ID in the form if an Integer(ID)
* @throws GitLabApiException if any exception occurs
*/
public void deleteGpgKey(final Integer keyId) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "user", "gpg_keys", keyId);
}

/**
* Get all GPG keys for a given user.
*
* <pre><code>GitLab Endpoint: GET /users/:id/gpg_keys</code></pre>
*
* @param userId the user in the form of an Integer(ID)
* @throws GitLabApiException if any exception occurs
*/
public List<GpgKey> listGpgKeys(final Integer userId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "users", userId, "gpg_keys");
return (response.readEntity(new GenericType<List<GpgKey>>() {}));
}

/**
* Add a GPG key for a specific user
*
* <pre><code>GitLab Endpoint: POST /users/:id/gpg_keys</code></pre>
*
* @param userId the user in the form of an Integer(ID)
* @param key the ASCII-armored exported public GPG key to add
* @throws GitLabApiException if any exception occurs
*/
public GpgKey addGpgKey(final Integer userId, final String key) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("key", key, true);
Response response = post(Response.Status.CREATED, formData, "users", userId, "gpg_keys");
return (response.readEntity(GpgKey.class));
}

/**
* Remove a specific GPG key for a specific user
*
* <pre><code>GitLab Endpoint: DELETE /users/:id/gpg_keys/:keyId</code></pre>
*
* @param userId the user in the form of an Integer(ID)
* @param keyId the key ID in the form if an Integer(ID)
* @throws GitLabApiException if any exception occurs
*/
public void deleteGpgKey(final Integer userId, final Integer keyId) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "users", userId, "gpg_keys", keyId);
}

/**
* Lists all projects and groups a user is a member of. (admin only)
*
Expand All @@ -1229,5 +1311,4 @@ public List<Membership> getMemberships(Integer userId) throws GitLabApiException
Response response = get(Response.Status.OK, formData.asMap(), "users", userId, "memberships");
return (response.readEntity(new GenericType<List<Membership>>() {}));
}

}
34 changes: 34 additions & 0 deletions src/main/java/org/gitlab4j/api/models/GpgKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.gitlab4j.api.models;

import java.util.Date;

public class GpgKey {

private Integer id;
private String key;
private Date created_at;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public Date getCreated_at() {
return created_at;
}

public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
}
100 changes: 96 additions & 4 deletions src/test/java/org/gitlab4j/api/TestUserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.GpgKey;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.Membership;
Expand Down Expand Up @@ -60,14 +61,66 @@ public class TestUserApi extends AbstractIntegrationTest {
private static final String TEST_SUDO_AS_USERNAME = HelperUtils.getProperty(SUDO_AS_USERNAME_KEY);

private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1";
private static final String TEST_SSH_KEY =
private static final String TEST_SSH_KEY =
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3rWzl/oPAD+Em2iGTmR81HcYZsopvnKp7jelI4XS91fT1NjCRrGsxf5Mw/" +
"KnmtBjhk+kQjkhIrnsBDcs6DZWtNcHJtyWJZrYsfxMTqWCaQv+OTRwVboqS2pmPcbK3gizUd5GCLFTKbg4OMpdywTwi6NAPwQ" +
"rtn3xwiVnGGCfBSyRFppcYP81otALctrlAW57V5+bQwFIJteJ+NWe1UmPxrqQ0N/a+dEEoJHzwX8RtVSkULafrRw8avn6Zp2x" +
"1OlD2aIEMQWvepNTRW6UDMSmWFc61ycy1pF5sCT5rij+b/fN4qCEvQs6R7GmCzaaZzbWuAqaxLRdITm/WUxdG6rjh";

private static final String TEST_USER_EMAIL = "[email protected]";

// Key for [email protected] - set to never expire
private static final String TEST_GPG_KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n" +
"mQINBGBHltIBEADW8zSGAs/XEkwWI36xOusuOqSINhTDVuqq3n50Oazb+a9Ai/MM\n" +
"8GTm900ZZghGBVUXGm8PkWuSmabcpbDbjYmJx8aIY71UipXxXrfBzr8S2yOx7IjU\n" +
"m6AEw0HwNUF6ayBwsklUFFWMyCeCV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
"RdiHTVHrUdiLl+6TIK90cm8CzNoiF+UGvdD4HObWbySh8O8n1nno+lX9EwSoq20o\n" +
"0WobXesDjNIrzJHow0WGGbx5gTxlZq0WwmgXgwYM0PbqlfjxFjct+98AEQEAAYkC\n" +
"NgQYAQgAIBYhBITn8nLtdg7yuIFwX9GGnCExgkynBQJgR5bSAhsMAAoJENGGnCEx\n" +
"gkynbOkQAI+N/wFxOTbewuTiy0P11saqqYr7Jwc7NLhqOVZ1tHKaTB6ZDbIrlWjN\n" +
"u2tFk7PqsA4/zI6KO9JoKiQYbopZ+xjd1nCJUjkUKI/wi4rl0t7ELQKhlSlUC11f\n" +
"Nz0C6Q+9cwRQCCT4sX/ZkzVQbGWx9fkAYVHzezDqh43xorXJ2ix5y63pr1NGuUgx\n" +
"EujMlUbXFzXpUrCmdUVWujlp4gSEfd6tLW0WMw0tYJe6UY7xx4EmWsT+kAGj7QLH\n" +
"L06yFigDQ0eUkGQ1T7Z0AjG4EXGETbX6lSLwzIBnmaZXQxdx4LiRy9TcaNtcowH4\n" +
"U2yxCoG0o0kS7sS/rI77TV6WZ46DPCJmlNJ+MP8lt0j/nsDA3AECB1AA+8SNepbA\n" +
"LSZY7MJmh4nsqJ+iy/XMosipluZx2u6ZwlXAHxAzHhs7FBsvdMtq/gLNAlZzVyeH\n" +
"UqzRaMJps7xIbap5d5jZT5jaZwFeGi+63YVRx3Jm6dkiBCPFffLyWdrzkFTZdWqZ\n" +
"PkiRbJ64wYPIWQgAN/RhmCcRBhxJE8f7kgo/nBkZ5dwmfXgnXpheEaaCSzvJ4nMh\n" +
"TUdg6ZLna12QndjI5gy5aenrr5H/HmDKKSNkuWZv0+NS4GhwnL8NFs+MRk6znpLN\n" +
"aEjPdfYxINCMz+uotKJV9NieDWIbEJLlfZUf2hJwuwwjQGAyVf7b\n" +
"=ryCD\n" +
"-----END PGP PUBLIC KEY BLOCK-----";

private static final String TEST_EXTERNAL_USERNAME = HelperUtils.getProperty(EXTERNAL_USERNAME_KEY);
private static final String TEST_EXTERNAL_PROVIDER = HelperUtils.getProperty(EXTERNAL_PROVIDER_KEY);
private static final String TEST_EXTERNAL_UID = HelperUtils.getProperty(EXTERNAL_UID_KEY);
Expand Down Expand Up @@ -153,7 +206,7 @@ public void testGetVersion() throws GitLabApiException {
assertNotNull(version.getVersion());
assertNotNull(version.getRevision());
}

@Test
public void testGetCurrentUser() throws GitLabApiException {
User currentUser = gitLabApi.getUserApi().getCurrentUser();
Expand Down Expand Up @@ -271,7 +324,7 @@ public void testCreateImpersonationToken() throws GitLabApiException, ParseExcep

User user = gitLabApi.getUserApi().getCurrentUser();

// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
// registry configured and the test would thus fail.
Scope[] scopes = {Scope.API, Scope.READ_USER, Scope.READ_REPOSITORY, Scope.WRITE_REPOSITORY, Scope.SUDO};
Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z");
Expand Down Expand Up @@ -443,7 +496,7 @@ public void testEmails() throws GitLabApiException {
assertTrue(emails.size() == currentSize + 1);
Email found = emails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
assertNotNull(found);

gitLabApi.getUserApi().deleteEmail(currentUser, email.getId());
emails = gitLabApi.getUserApi().getEmails(currentUser);
assertEquals(currentSize, emails.size());
Expand All @@ -452,6 +505,45 @@ public void testEmails() throws GitLabApiException {
}

@Test
public void testGpgKeys() throws GitLabApiException {
User currentUser = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(currentUser);
List<GpgKey> keys = gitLabApi.getUserApi().listGpgKeys(currentUser.getId());
assertNotNull(keys);
int currentSize = keys.size();

GpgKey key = gitLabApi.getUserApi().addGpgKey(currentUser.getId(), TEST_GPG_KEY);
keys = gitLabApi.getUserApi().listGpgKeys(currentUser.getId());
assertTrue(keys.size() == currentSize + 1);
GpgKey found = keys.stream().filter(e -> e.getKey().equals(TEST_GPG_KEY)).findAny().orElse(null);
assertNotNull(found);

gitLabApi.getUserApi().deleteGpgKey(currentUser.getId(), key.getId());
keys = gitLabApi.getUserApi().listGpgKeys(currentUser.getId());
assertEquals(currentSize, keys.size());
found = keys.stream().filter(e -> e.getKey().equals(TEST_GPG_KEY)).findAny().orElse(null);
assertNull(found);
}

@Test
public void testGpgKeysCurrentUser() throws GitLabApiException {
List<GpgKey> keys = gitLabApi.getUserApi().listGpgKeys();
assertNotNull(keys);
int currentSize = keys.size();

GpgKey key = gitLabApi.getUserApi().addGpgKey(TEST_GPG_KEY);
keys = gitLabApi.getUserApi().listGpgKeys();
assertTrue(keys.size() == currentSize + 1);
GpgKey found = keys.stream().filter(e -> e.getKey().equals(TEST_GPG_KEY)).findAny().orElse(null);
assertNotNull(found);

gitLabApi.getUserApi().deleteGpgKey(key.getId());
keys = gitLabApi.getUserApi().listGpgKeys();
assertEquals(currentSize, keys.size());
found = keys.stream().filter(e -> e.getKey().equals(TEST_GPG_KEY)).findAny().orElse(null);
assertNull(found);
}

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