From 07e3a69e499dad69541dbf0516113580143a9b10 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Thu, 28 Nov 2024 20:09:45 +0100 Subject: [PATCH] Fixes --- .../gitlab4j/api/PersonalAccessTokenApi.java | 2 +- src/main/java/org/gitlab4j/api/UserApi.java | 34 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java b/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java index dd80de427..4f92004d9 100644 --- a/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java +++ b/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java @@ -106,7 +106,7 @@ public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException { throw new RuntimeException("tokenId cannot be null"); } Response.Status expectedStatus = - (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "personal_access_tokens", tokenId); } } diff --git a/src/main/java/org/gitlab4j/api/UserApi.java b/src/main/java/org/gitlab4j/api/UserApi.java index d72decaa4..4256a90c0 100644 --- a/src/main/java/org/gitlab4j/api/UserApi.java +++ b/src/main/java/org/gitlab4j/api/UserApi.java @@ -786,9 +786,30 @@ public Optional getOptionalSshKey(Long keyId) { * @param key the new SSH key * @return an SshKey instance with info on the added SSH key * @throws GitLabApiException if any exception occurs + * @deprecated use {@link #addSshKey(String, String, Date)} instead */ + @Deprecated public SshKey addSshKey(String title, String key) throws GitLabApiException { - GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key); + return addSshKey(title, key, null); + } + + /** + * Creates a new key owned by the currently authenticated user. + * + *
GitLab Endpoint: POST /user/keys
+ * + * @param title the new SSH Key's title + * @param key the new SSH key + * @param expiresAt the expiration date of the ssh key, optional + * @return an SshKey instance with info on the added SSH key + * @throws GitLabApiException if any exception occurs + */ + public SshKey addSshKey(String title, String key, Date expiresAt) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("title", title) + .withParam("key", key) + .withParam("expires_at", expiresAt); + Response response = post(Response.Status.CREATED, formData, "user", "keys"); return (response.readEntity(SshKey.class)); } @@ -803,7 +824,9 @@ public SshKey addSshKey(String title, String key) throws GitLabApiException { * @param key the new SSH key * @return an SshKey instance with info on the added SSH key * @throws GitLabApiException if any exception occurs + * @deprecated use {@link #addSshKey(Long, String, String, Date)} instead */ + @Deprecated public SshKey addSshKey(Long userId, String title, String key) throws GitLabApiException { return addSshKey(userId, title, key, null); } @@ -825,10 +848,11 @@ public SshKey addSshKey(Long userId, String title, String key, Date expiresAt) t throw new RuntimeException("userId cannot be null"); } - GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key); - if (expiresAt != null) { - formData.withParam("expires_at", expiresAt); - } + GitLabApiForm formData = new GitLabApiForm() + .withParam("title", title) + .withParam("key", key) + .withParam("expires_at", expiresAt); + Response response = post(Response.Status.CREATED, formData, "users", userId, "keys"); SshKey sshKey = response.readEntity(SshKey.class); if (sshKey != null) {