Skip to content

Commit 4bb906b

Browse files
committed
Added support for user ID in SshKey class (#116).
1 parent 373f110 commit 4bb906b

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

src/main/java/org/gitlab4j/api/UserApi.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,16 @@ public List<SshKey> getSshKeys(Integer userId) throws GitLabApiException {
414414
}
415415

416416
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "users", userId, "keys");
417-
return (response.readEntity(new GenericType<List<SshKey>>() {}));
417+
List<SshKey> keys = response.readEntity(new GenericType<List<SshKey>>() {});
418+
if (keys == null) {
419+
return (keys);
420+
}
421+
422+
for (SshKey key : keys) {
423+
key.setUserId(userId);
424+
}
425+
426+
return (keys);
418427
}
419428

420429
/**
@@ -466,7 +475,12 @@ public SshKey addSshKey(Integer userId, String title, String key) throws GitLabA
466475

467476
GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key);
468477
Response response = post(Response.Status.CREATED, formData, "users", userId, "keys");
469-
return (response.readEntity(SshKey.class));
478+
SshKey sshKey = response.readEntity(SshKey.class);
479+
if (sshKey != null) {
480+
sshKey.setUserId(userId);
481+
}
482+
483+
return (sshKey);
470484
}
471485

472486
/**

src/main/java/org/gitlab4j/api/models/SshKey.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import javax.xml.bind.annotation.XmlAccessType;
66
import javax.xml.bind.annotation.XmlAccessorType;
77

8+
import com.fasterxml.jackson.annotation.JsonIgnore;
9+
810
@XmlAccessorType(XmlAccessType.FIELD)
911
public class SshKey {
1012

@@ -13,6 +15,8 @@ public class SshKey {
1315
private String key;
1416
private Date createdAt;
1517

18+
private Integer userId;
19+
1620
public Integer getId() {
1721
return id;
1822
}
@@ -44,4 +48,13 @@ public Date getCreatedAt() {
4448
public void setCreatedAt(Date createdAt) {
4549
this.createdAt = createdAt;
4650
}
51+
52+
@JsonIgnore
53+
public Integer getUserId() {
54+
return userId;
55+
}
56+
57+
public void setUserId(Integer userId) {
58+
this.userId = userId;
59+
}
4760
}

src/test/java/org/gitlab4j/api/TestUserApi.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.gitlab4j.api.GitLabApi.ApiVersion;
1515
import org.gitlab4j.api.models.ImpersonationToken;
1616
import org.gitlab4j.api.models.ImpersonationToken.Scope;
17+
import org.gitlab4j.api.models.SshKey;
1718
import org.gitlab4j.api.models.User;
1819
import org.gitlab4j.api.models.Version;
1920
import org.gitlab4j.api.utils.ISO8601;
@@ -32,7 +33,11 @@
3233
*
3334
* TEST_SUDO_AS_USERNAME
3435
*
35-
* If this is null the sudo() tyests will be skipped.
36+
* If this is null the sudo() tests will be skipped.
37+
*
38+
* TEST_SSH_KEY
39+
*
40+
* If this is null the SSH key tests will be skipped.
3641
*
3742
*/
3843
public class TestUserApi {
@@ -42,11 +47,13 @@ public class TestUserApi {
4247
private static final String TEST_PRIVATE_TOKEN;
4348
private static final String TEST_USERNAME;
4449
private static final String TEST_SUDO_AS_USERNAME;
50+
private static final String TEST_SSH_KEY;
4551
static {
4652
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
4753
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
4854
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
4955
TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME");
56+
TEST_SSH_KEY = TestUtils.getProperty("TEST_SSH_KEY");
5057
}
5158

5259
private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1";
@@ -75,6 +82,20 @@ public static void setup() {
7582

7683
if (problems.isEmpty()) {
7784
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
85+
86+
if (TEST_SSH_KEY != null) {
87+
try {
88+
List<SshKey> sshKeys = gitLabApi.getUserApi().getSshKeys();
89+
if (sshKeys != null) {
90+
for (SshKey key : sshKeys) {
91+
if (TEST_SSH_KEY.equals(key.getKey())) {
92+
gitLabApi.getUserApi().deleteSshKey(key.getId());
93+
}
94+
}
95+
}
96+
} catch (Exception ignore) {}
97+
}
98+
7899
} else {
79100
System.err.print(problems);
80101
}
@@ -183,16 +204,32 @@ public void testDeleteImpersonationTokens() throws GitLabApiException, ParseExce
183204
User user = gitLabApi.getUserApi().getCurrentUser();
184205
Scope[] scopes = {Scope.API, Scope.READ_USER};
185206
Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z");
186-
ImpersonationToken createdToken = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes);
207+
ImpersonationToken createdToken = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME + "a", expiresAt, scopes);
187208
assertNotNull(createdToken);
188209

189210
ImpersonationToken token = gitLabApi.getUserApi().getImpersonationToken(user.getId(), createdToken.getId());
190211
assertNotNull(token);
191212
assertEquals(createdToken.getId(), token.getId());
192-
assertTrue(token.getActive());
193213

194214
gitLabApi.getUserApi().revokeImpersonationToken(user.getId(), createdToken.getId());
195215
token = gitLabApi.getUserApi().getImpersonationToken(user.getId(), createdToken.getId());
196216
assertFalse(token.getActive());
197217
}
218+
219+
@Test
220+
public void testGetSshKeys() throws GitLabApiException {
221+
222+
assumeTrue(TEST_SSH_KEY != null);
223+
SshKey sshKey = gitLabApi.getUserApi().addSshKey("Test-Key", TEST_SSH_KEY);
224+
assertNotNull(sshKey);
225+
assertEquals(TEST_SSH_KEY, sshKey.getKey());
226+
gitLabApi.getUserApi().deleteSshKey(sshKey.getId());
227+
228+
User user = gitLabApi.getUserApi().getCurrentUser();
229+
sshKey = gitLabApi.getUserApi().addSshKey(user.getId(), "Test-Key1", TEST_SSH_KEY);
230+
assertNotNull(sshKey);
231+
assertEquals(TEST_SSH_KEY, sshKey.getKey());
232+
assertEquals(user.getId(), sshKey.getUserId());
233+
gitLabApi.getUserApi().deleteSshKey(sshKey.getId());
234+
}
198235
}

0 commit comments

Comments
 (0)