From a03cb9149aa807fc24e165a5e569a80d3a72f615 Mon Sep 17 00:00:00 2001 From: chenzw <656469722@qq.com> Date: Fri, 22 Nov 2024 09:57:50 +0800 Subject: [PATCH] reactor --- .../gitlab4j/api/PersonalAccessTokenApi.java | 16 +++++++++ src/main/java/org/gitlab4j/api/UserApi.java | 35 +++++++++---------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java b/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java index 2b77cc401..dd80de427 100644 --- a/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java +++ b/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java @@ -93,4 +93,20 @@ public PersonalAccessToken getPersonalAccessToken(String id) throws GitLabApiExc Response response = get(Response.Status.OK, null, "personal_access_tokens", id); return (response.readEntity(PersonalAccessToken.class)); } + + /** + * Revokes a personal access token. Available only for admin users. + * + *
GitLab Endpoint: DELETE /personal_access_tokens/:token_id
+ * @param tokenId the personal access token ID to revoke
+ * @throws GitLabApiException if any exception occurs
+ */
+ public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException {
+ if (tokenId == null) {
+ throw new RuntimeException("tokenId cannot be null");
+ }
+ Response.Status expectedStatus =
+ (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 0fbdf69ea..d72decaa4 100644
--- a/src/main/java/org/gitlab4j/api/UserApi.java
+++ b/src/main/java/org/gitlab4j/api/UserApi.java
@@ -805,19 +805,7 @@ public SshKey addSshKey(String title, String key) throws GitLabApiException {
* @throws GitLabApiException if any exception occurs
*/
public SshKey addSshKey(Long userId, String title, String key) throws GitLabApiException {
-
- if (userId == null) {
- throw new RuntimeException("userId cannot be null");
- }
-
- GitLabApiForm formData = new GitLabApiForm().withParam("title", title).withParam("key", key);
- Response response = post(Response.Status.CREATED, formData, "users", userId, "keys");
- SshKey sshKey = response.readEntity(SshKey.class);
- if (sshKey != null) {
- sshKey.setUserId(userId);
- }
-
- return (sshKey);
+ return addSshKey(userId, title, key, null);
}
/**
@@ -833,12 +821,20 @@ public SshKey addSshKey(Long userId, String title, String key) throws GitLabApiE
* @throws GitLabApiException if any exception occurs
*/
public SshKey addSshKey(Long userId, 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));
+ if (userId == null) {
+ 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);
+ }
+ Response response = post(Response.Status.CREATED, formData, "users", userId, "keys");
+ SshKey sshKey = response.readEntity(SshKey.class);
+ if (sshKey != null) {
+ sshKey.setUserId(userId);
+ }
+ return (sshKey);
}
/**
@@ -1030,6 +1026,7 @@ public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException {
(isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, null, "personal_access_tokens", tokenId);
}
+
// as per https://docs.gitlab.com/ee/api/README.html#impersonation-tokens, impersonation tokens are a type of
// personal access token
private ImpersonationToken createPersonalAccessTokenOrImpersonationToken(