From 7dfc97d366185c15713f219b3e93cce4ff8e7bd2 Mon Sep 17 00:00:00 2001 From: chenzw <656469722@qq.com> Date: Thu, 21 Nov 2024 17:56:25 +0800 Subject: [PATCH 1/5] feat: add revokePersonalAccessToken(tokenId) and addSshKey expiresAt field --- src/main/java/org/gitlab4j/api/UserApi.java | 302 ++++++++++++-------- 1 file changed, 175 insertions(+), 127 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/UserApi.java b/src/main/java/org/gitlab4j/api/UserApi.java index c95514130..790fba921 100644 --- a/src/main/java/org/gitlab4j/api/UserApi.java +++ b/src/main/java/org/gitlab4j/api/UserApi.java @@ -68,8 +68,8 @@ public List getUsers() throws GitLabApiException { String url = this.gitLabApi.getGitLabServerUrl(); if (url.startsWith("https://gitlab.com")) { GitLabApi.getLogger() - .warning("Fetching all users from " + url - + " may take many minutes to complete, use Pager getUsers(int) instead."); + .warning("Fetching all users from " + url + + " may take many minutes to complete, use Pager getUsers(int) instead."); } return (getUsers(getDefaultPerPage()).all()); @@ -87,8 +87,9 @@ public List getUsers() throws GitLabApiException { */ public List getUsers(int page, int perPage) throws GitLabApiException { Response response = - get(Response.Status.OK, getPageQueryParams(page, perPage, customAttributesEnabled), "users"); - return (response.readEntity(new GenericType>() {})); + get(Response.Status.OK, getPageQueryParams(page, perPage, customAttributesEnabled), "users"); + return (response.readEntity(new GenericType>() { + })); } /** @@ -102,7 +103,7 @@ public List getUsers(int page, int perPage) throws GitLabApiException { */ public Pager getUsers(int itemsPerPage) throws GitLabApiException { return (new Pager( - this, User.class, itemsPerPage, createGitLabApiForm().asMap(), "users")); + this, User.class, itemsPerPage, createGitLabApiForm().asMap(), "users")); } /** @@ -134,18 +135,19 @@ public List getActiveUsers() throws GitLabApiException { * *
GitLab Endpoint: GET /users?active=true
* - * @param page the page to get + * @param page the page to get * @param perPage the number of users per page * @return the list of active Users in the specified range * @throws GitLabApiException if any exception occurs */ public List getActiveUsers(int page, int perPage) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("active", true) - .withParam(PAGE_PARAM, page) - .withParam(PER_PAGE_PARAM, perPage); + .withParam("active", true) + .withParam(PAGE_PARAM, page) + .withParam(PER_PAGE_PARAM, perPage); Response response = get(Response.Status.OK, formData.asMap(), "users"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -269,11 +271,12 @@ public List getBlockedUsers() throws GitLabApiException { */ public List getblockedUsers(int page, int perPage) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("blocked", true) - .withParam(PAGE_PARAM, page) - .withParam(PER_PAGE_PARAM, perPage); + .withParam("blocked", true) + .withParam(PAGE_PARAM, page) + .withParam(PER_PAGE_PARAM, perPage); Response response = get(Response.Status.OK, formData.asMap(), "users"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -346,11 +349,12 @@ public Optional getOptionalUser(Long userId) { */ public User getUser(String username) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("username", username, true) - .withParam(PAGE_PARAM, 1) - .withParam(PER_PAGE_PARAM, 1); + .withParam("username", username, true) + .withParam(PAGE_PARAM, 1) + .withParam(PER_PAGE_PARAM, 1); Response response = get(Response.Status.OK, formData.asMap(), "users"); - List users = response.readEntity(new GenericType>() {}); + List users = response.readEntity(new GenericType>() { + }); return (users.isEmpty() ? null : users.get(0)); } @@ -379,7 +383,7 @@ public Optional getOptionalUser(String username) { * * @param email the email of the user to get * @return the User instance for the specified email, or null if not found - * @throws GitLabApiException if any exception occurs + * @throws GitLabApiException if any exception occurs * @throws IllegalArgumentException if email is not valid */ public User getUserByEmail(String email) throws GitLabApiException { @@ -415,19 +419,20 @@ public Optional getOptionalUserByEmail(String email) { * *
GitLab Endpoint: GET /users?extern_uid=:externalUid&provider=:provider
* - * @param provider the provider of the external uid + * @param provider the provider of the external uid * @param externalUid the external UID of the user * @return the User instance for the specified external UID, or null if not found * @throws GitLabApiException if any exception occurs */ public User getUserByExternalUid(String provider, String externalUid) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("provider", provider, true) - .withParam("extern_uid", externalUid, true) - .withParam(PAGE_PARAM, 1) - .withParam(PER_PAGE_PARAM, 1); + .withParam("provider", provider, true) + .withParam("extern_uid", externalUid, true) + .withParam(PAGE_PARAM, 1) + .withParam(PER_PAGE_PARAM, 1); Response response = get(Response.Status.OK, formData.asMap(), "users"); - List users = response.readEntity(new GenericType>() {}); + List users = response.readEntity(new GenericType>() { + }); return (users.isEmpty() ? null : users.get(0)); } @@ -438,7 +443,7 @@ public User getUserByExternalUid(String provider, String externalUid) throws Git * *
GitLab Endpoint: GET /users?extern_uid=:externUid&provider=:provider
* - * @param provider the provider of the external uid + * @param provider the provider of the external uid * @param externalUid the external UID of the user * @return the User for the specified external UID as an Optional instance */ @@ -476,11 +481,12 @@ public List findUsers(String emailOrUsername) throws GitLabApiException { */ public List findUsers(String emailOrUsername, int page, int perPage) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("search", emailOrUsername, true) - .withParam(PAGE_PARAM, page) - .withParam(PER_PAGE_PARAM, perPage); + .withParam("search", emailOrUsername, true) + .withParam(PAGE_PARAM, page) + .withParam(PER_PAGE_PARAM, perPage); Response response = get(Response.Status.OK, formData.asMap(), "users"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -685,14 +691,14 @@ public void deleteUser(Object userIdOrUsername) throws GitLabApiException { *
GitLab Endpoint: DELETE /users/:id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param hardDelete If true, contributions that would usually be moved to the - * ghost user will be deleted instead, as well as groups owned solely by this user + * @param hardDelete If true, contributions that would usually be moved to the + * ghost user will be deleted instead, as well as groups owned solely by this user * @throws GitLabApiException if any exception occurs */ public void deleteUser(Object userIdOrUsername, Boolean hardDelete) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm().withParam("hard_delete ", hardDelete); Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, formData.asMap(), "users", getUserIdOrUsername(userIdOrUsername)); } @@ -719,7 +725,8 @@ public User getCurrentUser() throws GitLabApiException { */ public List getSshKeys() throws GitLabApiException { Response response = get(Response.Status.OK, getDefaultPerPageParam(), "user", "keys"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -738,7 +745,8 @@ public List getSshKeys(Long userId) throws GitLabApiException { } Response response = get(Response.Status.OK, getDefaultPerPageParam(), "users", userId, "keys"); - List keys = response.readEntity(new GenericType>() {}); + List keys = response.readEntity(new GenericType>() { + }); if (keys != null) { keys.forEach(key -> key.setUserId(userId)); @@ -820,6 +828,24 @@ public SshKey addSshKey(Long userId, String title, String key) throws GitLabApiE return (sshKey); } + /** + * Create new key owned by specified user. Available only for admin users. + * + *
GitLab Endpoint: POST /users/:id/keys
+ * + * @param userId the ID of the user to add the SSH key for + * @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(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)); + } + /** * Deletes key owned by currently authenticated user. This is an idempotent function and calling it * on a key that is already deleted or not available results in success. @@ -836,7 +862,7 @@ public void deleteSshKey(Long keyId) throws GitLabApiException { } Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "user", "keys", keyId); } @@ -846,7 +872,7 @@ public void deleteSshKey(Long keyId) throws GitLabApiException { *
GitLab Endpoint: DELETE /users/:id/keys/:key_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param keyId the key ID to delete + * @param keyId the key ID to delete * @throws GitLabApiException if any exception occurs */ public void deleteSshKey(Object userIdOrUsername, Long keyId) throws GitLabApiException { @@ -856,7 +882,7 @@ public void deleteSshKey(Object userIdOrUsername, Long keyId) throws GitLabApiEx } Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "users", getUserIdOrUsername(userIdOrUsername), "keys", keyId); } @@ -879,21 +905,22 @@ public List getImpersonationTokens(Object userIdOrUsername) *
GitLab Endpoint: GET /users/:id/impersonation_tokens
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param state the state of impersonation tokens to list (ALL, ACTIVE, INACTIVE) + * @param state the state of impersonation tokens to list (ALL, ACTIVE, INACTIVE) * @return a list of a specified user's impersonation tokens * @throws GitLabApiException if any exception occurs */ public List getImpersonationTokens(Object userIdOrUsername, ImpersonationState state) - throws GitLabApiException { + throws GitLabApiException { GitLabApiForm formData = - new GitLabApiForm().withParam("state", state).withParam(PER_PAGE_PARAM, getDefaultPerPage()); + new GitLabApiForm().withParam("state", state).withParam(PER_PAGE_PARAM, getDefaultPerPage()); Response response = get( - Response.Status.OK, - formData.asMap(), - "users", - getUserIdOrUsername(userIdOrUsername), - "impersonation_tokens"); - return (response.readEntity(new GenericType>() {})); + Response.Status.OK, + formData.asMap(), + "users", + getUserIdOrUsername(userIdOrUsername), + "impersonation_tokens"); + return (response.readEntity(new GenericType>() { + })); } /** @@ -902,7 +929,7 @@ public List getImpersonationTokens(Object userIdOrUsername, *
GitLab Endpoint: GET /users/:user_id/impersonation_tokens/:impersonation_token_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param tokenId the impersonation token ID to get + * @param tokenId the impersonation token ID to get * @return the specified impersonation token * @throws GitLabApiException if any exception occurs */ @@ -913,12 +940,12 @@ public ImpersonationToken getImpersonationToken(Object userIdOrUsername, Long to } Response response = get( - Response.Status.OK, - null, - "users", - getUserIdOrUsername(userIdOrUsername), - "impersonation_tokens", - tokenId); + Response.Status.OK, + null, + "users", + getUserIdOrUsername(userIdOrUsername), + "impersonation_tokens", + tokenId); return (response.readEntity(ImpersonationToken.class)); } @@ -928,7 +955,7 @@ public ImpersonationToken getImpersonationToken(Object userIdOrUsername, Long to *
GitLab Endpoint: GET /users/:user_id/impersonation_tokens/:impersonation_token_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param tokenId the impersonation token ID to get + * @param tokenId the impersonation token ID to get * @return the specified impersonation token as an Optional instance */ public Optional getOptionalImpersonationToken(Object userIdOrUsername, Long tokenId) { @@ -945,14 +972,14 @@ public Optional getOptionalImpersonationToken(Object userIdO *
GitLab Endpoint: POST /users/:user_id/impersonation_tokens
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param name the name of the impersonation token, required - * @param expiresAt the expiration date of the impersonation token, optional - * @param scopes an array of scopes of the impersonation token + * @param name the name of the impersonation token, required + * @param expiresAt the expiration date of the impersonation token, optional + * @param scopes an array of scopes of the impersonation token * @return the created ImpersonationToken instance * @throws GitLabApiException if any exception occurs */ public ImpersonationToken createImpersonationToken( - Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { + Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, true); } @@ -962,7 +989,7 @@ public ImpersonationToken createImpersonationToken( *
GitLab Endpoint: DELETE /users/:user_id/impersonation_tokens/:impersonation_token_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param tokenId the impersonation token ID to revoke + * @param tokenId the impersonation token ID to revoke * @throws GitLabApiException if any exception occurs */ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) throws GitLabApiException { @@ -972,7 +999,7 @@ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) thro } Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "users", getUserIdOrUsername(userIdOrUsername), "impersonation_tokens", tokenId); } @@ -982,29 +1009,46 @@ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) thro *
GitLab Endpoint: POST /users/:user_id/personal_access_tokens
* * @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance - * @param name the name of the personal access token, required - * @param expiresAt the expiration date of the personal access token, optional - * @param scopes an array of scopes of the personal access token + * @param name the name of the personal access token, required + * @param expiresAt the expiration date of the personal access token, optional + * @param scopes an array of scopes of the personal access token * @return the created PersonalAccessToken instance * @throws GitLabApiException if any exception occurs */ public ImpersonationToken createPersonalAccessToken( - Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { + Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, false); } + /** + * 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(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( - Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes, boolean impersonation) - throws GitLabApiException { + Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes, boolean impersonation) + throws GitLabApiException { if (scopes == null || scopes.length == 0) { throw new RuntimeException("scopes cannot be null or empty"); } GitLabApiForm formData = - new GitLabApiForm().withParam("name", name, true).withParam("expires_at", expiresAt); + new GitLabApiForm().withParam("name", name, true).withParam("expires_at", expiresAt); for (Scope scope : scopes) { formData.withParam("scopes[]", scope.toString()); @@ -1012,7 +1056,7 @@ private ImpersonationToken createPersonalAccessTokenOrImpersonationToken( String tokenTypePathArg = impersonation ? "impersonation_tokens" : "personal_access_tokens"; Response response = post( - Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), tokenTypePathArg); + Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), tokenTypePathArg); return (response.readEntity(ImpersonationToken.class)); } @@ -1037,41 +1081,41 @@ Form userToForm(User user, Integer projectsLimit, CharSequence password, Boolean String skipConfirmationFeildName = create ? "skip_confirmation" : "skip_reconfirmation"; return (new GitLabApiForm() - .withParam("public_email", user.getPublicEmail(), false) - .withParam("email", user.getEmail(), create) - .withParam("password", password, false) - .withParam("reset_password", resetPassword, false) - .withParam("username", user.getUsername(), create) - .withParam("name", user.getName(), create) - .withParam("skype", user.getSkype(), false) - .withParam("linkedin", user.getLinkedin(), false) - .withParam("twitter", user.getTwitter(), false) - .withParam("website_url", user.getWebsiteUrl(), false) - .withParam("organization", user.getOrganization(), false) - .withParam("projects_limit", projectsLimit, false) - .withParam("extern_uid", user.getExternUid(), false) - .withParam("provider", user.getProvider(), false) - .withParam("bio", user.getBio(), false) - .withParam("location", user.getLocation(), false) - .withParam("admin", user.getIsAdmin(), false) - .withParam("theme_id", user.getThemeId(), false) - .withParam("color_scheme_id", user.getColorSchemeId(), false) - .withParam("can_create_group", user.getCanCreateGroup(), false) - .withParam(skipConfirmationFeildName, user.getSkipConfirmation(), false) - .withParam("external", user.getExternal(), false) - .withParam("shared_runners_minutes_limit", user.getSharedRunnersMinutesLimit(), false)); + .withParam("public_email", user.getPublicEmail(), false) + .withParam("email", user.getEmail(), create) + .withParam("password", password, false) + .withParam("reset_password", resetPassword, false) + .withParam("username", user.getUsername(), create) + .withParam("name", user.getName(), create) + .withParam("skype", user.getSkype(), false) + .withParam("linkedin", user.getLinkedin(), false) + .withParam("twitter", user.getTwitter(), false) + .withParam("website_url", user.getWebsiteUrl(), false) + .withParam("organization", user.getOrganization(), false) + .withParam("projects_limit", projectsLimit, false) + .withParam("extern_uid", user.getExternUid(), false) + .withParam("provider", user.getProvider(), false) + .withParam("bio", user.getBio(), false) + .withParam("location", user.getLocation(), false) + .withParam("admin", user.getIsAdmin(), false) + .withParam("theme_id", user.getThemeId(), false) + .withParam("color_scheme_id", user.getColorSchemeId(), false) + .withParam("can_create_group", user.getCanCreateGroup(), false) + .withParam(skipConfirmationFeildName, user.getSkipConfirmation(), false) + .withParam("external", user.getExternal(), false) + .withParam("shared_runners_minutes_limit", user.getSharedRunnersMinutesLimit(), false)); } /** * Creates custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param customAttribute the custom attribute to set + * @param customAttribute the custom attribute to set * @return the created CustomAttribute * @throws GitLabApiException on failure while setting customAttributes */ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, final CustomAttribute customAttribute) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(customAttribute)) { throw new IllegalArgumentException("CustomAttributes can't be null"); } @@ -1082,13 +1126,13 @@ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, fina * Creates custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param key for the customAttribute - * @param value or the customAttribute + * @param key for the customAttribute + * @param value or the customAttribute * @return the created CustomAttribute * @throws GitLabApiException on failure while setting customAttributes */ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, final String key, final String value) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(key) || key.trim().isEmpty()) { throw new IllegalArgumentException("Key can't be null or empty"); @@ -1099,12 +1143,12 @@ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, fina GitLabApiForm formData = new GitLabApiForm().withParam("value", value); Response response = put( - Response.Status.OK, - formData.asMap(), - "users", - getUserIdOrUsername(userIdOrUsername), - "custom_attributes", - key); + Response.Status.OK, + formData.asMap(), + "users", + getUserIdOrUsername(userIdOrUsername), + "custom_attributes", + key); return (response.readEntity(CustomAttribute.class)); } @@ -1112,12 +1156,12 @@ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, fina * Change custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param customAttribute the custome attribute to change + * @param customAttribute the custome attribute to change * @return the changed CustomAttribute * @throws GitLabApiException on failure while changing customAttributes */ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, final CustomAttribute customAttribute) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(customAttribute)) { throw new IllegalArgumentException("CustomAttributes can't be null"); @@ -1132,13 +1176,13 @@ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, fina * Changes custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param key for the customAttribute - * @param value for the customAttribute + * @param key for the customAttribute + * @param value for the customAttribute * @return changedCustomAttribute * @throws GitLabApiException on failure while changing customAttributes */ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, final String key, final String value) - throws GitLabApiException { + throws GitLabApiException { return createCustomAttribute(userIdOrUsername, key, value); } @@ -1146,11 +1190,11 @@ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, fina * Delete a custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param customAttribute to remove + * @param customAttribute to remove * @throws GitLabApiException on failure while deleting customAttributes */ public void deleteCustomAttribute(final Object userIdOrUsername, final CustomAttribute customAttribute) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(customAttribute)) { throw new IllegalArgumentException("customAttributes can't be null"); } @@ -1162,7 +1206,7 @@ public void deleteCustomAttribute(final Object userIdOrUsername, final CustomAtt * Delete a custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param key of the customAttribute to remove + * @param key of the customAttribute to remove * @throws GitLabApiException on failure while deleting customAttributes */ public void deleteCustomAttribute(final Object userIdOrUsername, final String key) throws GitLabApiException { @@ -1192,13 +1236,13 @@ private GitLabApiForm createGitLabApiForm() { *
PUT /users/:id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param avatarFile the File instance of the avatar file to upload + * @param avatarFile the File instance of the avatar file to upload * @return the updated User instance * @throws GitLabApiException if any exception occurs */ public User setUserAvatar(final Object userIdOrUsername, File avatarFile) throws GitLabApiException { Response response = - putUpload(Response.Status.OK, "avatar", avatarFile, "users", getUserIdOrUsername(userIdOrUsername)); + putUpload(Response.Status.OK, "avatar", avatarFile, "users", getUserIdOrUsername(userIdOrUsername)); return (response.readEntity(User.class)); } @@ -1212,7 +1256,8 @@ public User setUserAvatar(final Object userIdOrUsername, File avatarFile) throws */ public List getEmails() throws GitLabApiException { Response response = get(Response.Status.OK, null, "user", "emails"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -1226,7 +1271,8 @@ public List getEmails() throws GitLabApiException { */ public List getEmails(final Object userIdOrUsername) throws GitLabApiException { Response response = get(Response.Status.OK, null, "users", getUserIdOrUsername(userIdOrUsername), "emails"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -1264,18 +1310,18 @@ public Email getEmail(final Long emailId) throws GitLabApiException { *
GitLab Endpoint: POST /user/:id/emails
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param email the email address to add + * @param email the email address to add * @param skipConfirmation skip confirmation and assume e-mail is verified - true or false (default) * @return the Email instance for the added email * @throws GitLabApiException if any exception occurs */ public Email addEmail(final Object userIdOrUsername, String email, Boolean skipConfirmation) - throws GitLabApiException { + throws GitLabApiException { GitLabApiForm formData = - new GitLabApiForm().withParam("email", email, true).withParam("skip_confirmation ", skipConfirmation); + new GitLabApiForm().withParam("email", email, true).withParam("skip_confirmation ", skipConfirmation); Response response = - post(Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), "emails"); + post(Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), "emails"); return (response.readEntity(Email.class)); } @@ -1297,7 +1343,7 @@ public void deleteEmail(final Long emailId) throws GitLabApiException { *
GitLab Endpoint: DELETE /user/:id/emails/:emailId
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param emailId the email ID to delete + * @param emailId the email ID to delete * @throws GitLabApiException if any exception occurs */ public void deleteEmail(final Object userIdOrUsername, final Long emailId) throws GitLabApiException { @@ -1313,7 +1359,8 @@ public void deleteEmail(final Object userIdOrUsername, final Long emailId) throw */ public List listGpgKeys() throws GitLabApiException { Response response = get(Response.Status.OK, null, "user", "gpg_keys"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -1352,7 +1399,8 @@ public void deleteGpgKey(final Long keyId) throws GitLabApiException { */ public List listGpgKeys(final Long userId) throws GitLabApiException { Response response = get(Response.Status.OK, null, "users", userId, "gpg_keys"); - return (response.readEntity(new GenericType>() {})); + return (response.readEntity(new GenericType>() { + })); } /** @@ -1361,7 +1409,7 @@ public List listGpgKeys(final Long userId) throws GitLabApiException { *
GitLab Endpoint: POST /users/:id/gpg_keys
* * @param userId the user in the form of an Long(ID) - * @param key the ASCII-armored exported public GPG key to add + * @param key the ASCII-armored exported public GPG key to add * @throws GitLabApiException if any exception occurs */ public GpgKey addGpgKey(final Long userId, final String key) throws GitLabApiException { @@ -1376,7 +1424,7 @@ public GpgKey addGpgKey(final Long userId, final String key) throws GitLabApiExc *
GitLab Endpoint: DELETE /users/:id/gpg_keys/:keyId
* * @param userId the user in the form of an Long(ID) - * @param keyId the key ID in the form if an Long(ID) + * @param keyId the key ID in the form if an Long(ID) * @throws GitLabApiException if any exception occurs */ public void deleteGpgKey(final Long userId, final Long keyId) throws GitLabApiException { @@ -1399,12 +1447,12 @@ public List getMemberships(Long userId) throws GitLabApiException { /** * 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. * *

GitLab Endpoint: GET /users/:id/memberships
* - * @param userId the ID of the user to get the memberships for + * @param userId the ID of the user to get the memberships for * @param itemsPerPage the number of Membership instances that will be fetched per page * @return a Pager of user's memberships * @throws GitLabApiException if any exception occurs @@ -1461,7 +1509,7 @@ public boolean exists(String username) throws GitLabApiException { } try { Response response = - get(Response.Status.OK, null, getApiClient().getUrlWithBase("users", username, "exists")); + get(Response.Status.OK, null, getApiClient().getUrlWithBase("users", username, "exists")); return response.readEntity(Exists.class).getExists(); } catch (IOException e) { throw new GitLabApiException(e); From c6e96893b98933daf8461b0228a9014a66e11acf Mon Sep 17 00:00:00 2001 From: chenzw <656469722@qq.com> Date: Thu, 21 Nov 2024 18:05:23 +0800 Subject: [PATCH 2/5] refactor --- src/main/java/org/gitlab4j/api/UserApi.java | 268 ++++++++++---------- 1 file changed, 127 insertions(+), 141 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/UserApi.java b/src/main/java/org/gitlab4j/api/UserApi.java index 790fba921..bac6e8bc2 100644 --- a/src/main/java/org/gitlab4j/api/UserApi.java +++ b/src/main/java/org/gitlab4j/api/UserApi.java @@ -68,8 +68,8 @@ public List getUsers() throws GitLabApiException { String url = this.gitLabApi.getGitLabServerUrl(); if (url.startsWith("https://gitlab.com")) { GitLabApi.getLogger() - .warning("Fetching all users from " + url - + " may take many minutes to complete, use Pager getUsers(int) instead."); + .warning("Fetching all users from " + url + + " may take many minutes to complete, use Pager getUsers(int) instead."); } return (getUsers(getDefaultPerPage()).all()); @@ -87,9 +87,8 @@ public List getUsers() throws GitLabApiException { */ public List getUsers(int page, int perPage) throws GitLabApiException { Response response = - get(Response.Status.OK, getPageQueryParams(page, perPage, customAttributesEnabled), "users"); - return (response.readEntity(new GenericType>() { - })); + get(Response.Status.OK, getPageQueryParams(page, perPage, customAttributesEnabled), "users"); + return (response.readEntity(new GenericType>() {})); } /** @@ -103,7 +102,7 @@ public List getUsers(int page, int perPage) throws GitLabApiException { */ public Pager getUsers(int itemsPerPage) throws GitLabApiException { return (new Pager( - this, User.class, itemsPerPage, createGitLabApiForm().asMap(), "users")); + this, User.class, itemsPerPage, createGitLabApiForm().asMap(), "users")); } /** @@ -135,19 +134,18 @@ public List getActiveUsers() throws GitLabApiException { * *
GitLab Endpoint: GET /users?active=true
* - * @param page the page to get + * @param page the page to get * @param perPage the number of users per page * @return the list of active Users in the specified range * @throws GitLabApiException if any exception occurs */ public List getActiveUsers(int page, int perPage) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("active", true) - .withParam(PAGE_PARAM, page) - .withParam(PER_PAGE_PARAM, perPage); + .withParam("active", true) + .withParam(PAGE_PARAM, page) + .withParam(PER_PAGE_PARAM, perPage); Response response = get(Response.Status.OK, formData.asMap(), "users"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -271,12 +269,11 @@ public List getBlockedUsers() throws GitLabApiException { */ public List getblockedUsers(int page, int perPage) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("blocked", true) - .withParam(PAGE_PARAM, page) - .withParam(PER_PAGE_PARAM, perPage); + .withParam("blocked", true) + .withParam(PAGE_PARAM, page) + .withParam(PER_PAGE_PARAM, perPage); Response response = get(Response.Status.OK, formData.asMap(), "users"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -349,12 +346,11 @@ public Optional getOptionalUser(Long userId) { */ public User getUser(String username) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("username", username, true) - .withParam(PAGE_PARAM, 1) - .withParam(PER_PAGE_PARAM, 1); + .withParam("username", username, true) + .withParam(PAGE_PARAM, 1) + .withParam(PER_PAGE_PARAM, 1); Response response = get(Response.Status.OK, formData.asMap(), "users"); - List users = response.readEntity(new GenericType>() { - }); + List users = response.readEntity(new GenericType>() {}); return (users.isEmpty() ? null : users.get(0)); } @@ -383,7 +379,7 @@ public Optional getOptionalUser(String username) { * * @param email the email of the user to get * @return the User instance for the specified email, or null if not found - * @throws GitLabApiException if any exception occurs + * @throws GitLabApiException if any exception occurs * @throws IllegalArgumentException if email is not valid */ public User getUserByEmail(String email) throws GitLabApiException { @@ -419,20 +415,19 @@ public Optional getOptionalUserByEmail(String email) { * *
GitLab Endpoint: GET /users?extern_uid=:externalUid&provider=:provider
* - * @param provider the provider of the external uid + * @param provider the provider of the external uid * @param externalUid the external UID of the user * @return the User instance for the specified external UID, or null if not found * @throws GitLabApiException if any exception occurs */ public User getUserByExternalUid(String provider, String externalUid) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("provider", provider, true) - .withParam("extern_uid", externalUid, true) - .withParam(PAGE_PARAM, 1) - .withParam(PER_PAGE_PARAM, 1); + .withParam("provider", provider, true) + .withParam("extern_uid", externalUid, true) + .withParam(PAGE_PARAM, 1) + .withParam(PER_PAGE_PARAM, 1); Response response = get(Response.Status.OK, formData.asMap(), "users"); - List users = response.readEntity(new GenericType>() { - }); + List users = response.readEntity(new GenericType>() {}); return (users.isEmpty() ? null : users.get(0)); } @@ -443,7 +438,7 @@ public User getUserByExternalUid(String provider, String externalUid) throws Git * *
GitLab Endpoint: GET /users?extern_uid=:externUid&provider=:provider
* - * @param provider the provider of the external uid + * @param provider the provider of the external uid * @param externalUid the external UID of the user * @return the User for the specified external UID as an Optional instance */ @@ -481,12 +476,11 @@ public List findUsers(String emailOrUsername) throws GitLabApiException { */ public List findUsers(String emailOrUsername, int page, int perPage) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() - .withParam("search", emailOrUsername, true) - .withParam(PAGE_PARAM, page) - .withParam(PER_PAGE_PARAM, perPage); + .withParam("search", emailOrUsername, true) + .withParam(PAGE_PARAM, page) + .withParam(PER_PAGE_PARAM, perPage); Response response = get(Response.Status.OK, formData.asMap(), "users"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -691,14 +685,14 @@ public void deleteUser(Object userIdOrUsername) throws GitLabApiException { *
GitLab Endpoint: DELETE /users/:id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param hardDelete If true, contributions that would usually be moved to the - * ghost user will be deleted instead, as well as groups owned solely by this user + * @param hardDelete If true, contributions that would usually be moved to the + * ghost user will be deleted instead, as well as groups owned solely by this user * @throws GitLabApiException if any exception occurs */ public void deleteUser(Object userIdOrUsername, Boolean hardDelete) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm().withParam("hard_delete ", hardDelete); Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, formData.asMap(), "users", getUserIdOrUsername(userIdOrUsername)); } @@ -725,8 +719,7 @@ public User getCurrentUser() throws GitLabApiException { */ public List getSshKeys() throws GitLabApiException { Response response = get(Response.Status.OK, getDefaultPerPageParam(), "user", "keys"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -745,8 +738,7 @@ public List getSshKeys(Long userId) throws GitLabApiException { } Response response = get(Response.Status.OK, getDefaultPerPageParam(), "users", userId, "keys"); - List keys = response.readEntity(new GenericType>() { - }); + List keys = response.readEntity(new GenericType>() {}); if (keys != null) { keys.forEach(key -> key.setUserId(userId)); @@ -862,7 +854,7 @@ public void deleteSshKey(Long keyId) throws GitLabApiException { } Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "user", "keys", keyId); } @@ -872,7 +864,7 @@ public void deleteSshKey(Long keyId) throws GitLabApiException { *
GitLab Endpoint: DELETE /users/:id/keys/:key_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param keyId the key ID to delete + * @param keyId the key ID to delete * @throws GitLabApiException if any exception occurs */ public void deleteSshKey(Object userIdOrUsername, Long keyId) throws GitLabApiException { @@ -882,7 +874,7 @@ public void deleteSshKey(Object userIdOrUsername, Long keyId) throws GitLabApiEx } Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "users", getUserIdOrUsername(userIdOrUsername), "keys", keyId); } @@ -905,22 +897,21 @@ public List getImpersonationTokens(Object userIdOrUsername) *
GitLab Endpoint: GET /users/:id/impersonation_tokens
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param state the state of impersonation tokens to list (ALL, ACTIVE, INACTIVE) + * @param state the state of impersonation tokens to list (ALL, ACTIVE, INACTIVE) * @return a list of a specified user's impersonation tokens * @throws GitLabApiException if any exception occurs */ public List getImpersonationTokens(Object userIdOrUsername, ImpersonationState state) - throws GitLabApiException { + throws GitLabApiException { GitLabApiForm formData = - new GitLabApiForm().withParam("state", state).withParam(PER_PAGE_PARAM, getDefaultPerPage()); + new GitLabApiForm().withParam("state", state).withParam(PER_PAGE_PARAM, getDefaultPerPage()); Response response = get( - Response.Status.OK, - formData.asMap(), - "users", - getUserIdOrUsername(userIdOrUsername), - "impersonation_tokens"); - return (response.readEntity(new GenericType>() { - })); + Response.Status.OK, + formData.asMap(), + "users", + getUserIdOrUsername(userIdOrUsername), + "impersonation_tokens"); + return (response.readEntity(new GenericType>() {})); } /** @@ -929,7 +920,7 @@ public List getImpersonationTokens(Object userIdOrUsername, *
GitLab Endpoint: GET /users/:user_id/impersonation_tokens/:impersonation_token_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param tokenId the impersonation token ID to get + * @param tokenId the impersonation token ID to get * @return the specified impersonation token * @throws GitLabApiException if any exception occurs */ @@ -940,12 +931,12 @@ public ImpersonationToken getImpersonationToken(Object userIdOrUsername, Long to } Response response = get( - Response.Status.OK, - null, - "users", - getUserIdOrUsername(userIdOrUsername), - "impersonation_tokens", - tokenId); + Response.Status.OK, + null, + "users", + getUserIdOrUsername(userIdOrUsername), + "impersonation_tokens", + tokenId); return (response.readEntity(ImpersonationToken.class)); } @@ -955,7 +946,7 @@ public ImpersonationToken getImpersonationToken(Object userIdOrUsername, Long to *
GitLab Endpoint: GET /users/:user_id/impersonation_tokens/:impersonation_token_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param tokenId the impersonation token ID to get + * @param tokenId the impersonation token ID to get * @return the specified impersonation token as an Optional instance */ public Optional getOptionalImpersonationToken(Object userIdOrUsername, Long tokenId) { @@ -972,14 +963,14 @@ public Optional getOptionalImpersonationToken(Object userIdO *
GitLab Endpoint: POST /users/:user_id/impersonation_tokens
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param name the name of the impersonation token, required - * @param expiresAt the expiration date of the impersonation token, optional - * @param scopes an array of scopes of the impersonation token + * @param name the name of the impersonation token, required + * @param expiresAt the expiration date of the impersonation token, optional + * @param scopes an array of scopes of the impersonation token * @return the created ImpersonationToken instance * @throws GitLabApiException if any exception occurs */ public ImpersonationToken createImpersonationToken( - Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { + Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, true); } @@ -989,7 +980,7 @@ public ImpersonationToken createImpersonationToken( *
GitLab Endpoint: DELETE /users/:user_id/impersonation_tokens/:impersonation_token_id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param tokenId the impersonation token ID to revoke + * @param tokenId the impersonation token ID to revoke * @throws GitLabApiException if any exception occurs */ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) throws GitLabApiException { @@ -999,7 +990,7 @@ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) thro } Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, null, "users", getUserIdOrUsername(userIdOrUsername), "impersonation_tokens", tokenId); } @@ -1009,14 +1000,14 @@ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) thro *
GitLab Endpoint: POST /users/:user_id/personal_access_tokens
* * @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance - * @param name the name of the personal access token, required - * @param expiresAt the expiration date of the personal access token, optional - * @param scopes an array of scopes of the personal access token + * @param name the name of the personal access token, required + * @param expiresAt the expiration date of the personal access token, optional + * @param scopes an array of scopes of the personal access token * @return the created PersonalAccessToken instance * @throws GitLabApiException if any exception occurs */ public ImpersonationToken createPersonalAccessToken( - Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { + Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException { return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, false); } @@ -1036,19 +1027,18 @@ 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( - Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes, boolean impersonation) - throws GitLabApiException { + Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes, boolean impersonation) + throws GitLabApiException { if (scopes == null || scopes.length == 0) { throw new RuntimeException("scopes cannot be null or empty"); } GitLabApiForm formData = - new GitLabApiForm().withParam("name", name, true).withParam("expires_at", expiresAt); + new GitLabApiForm().withParam("name", name, true).withParam("expires_at", expiresAt); for (Scope scope : scopes) { formData.withParam("scopes[]", scope.toString()); @@ -1056,7 +1046,7 @@ private ImpersonationToken createPersonalAccessTokenOrImpersonationToken( String tokenTypePathArg = impersonation ? "impersonation_tokens" : "personal_access_tokens"; Response response = post( - Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), tokenTypePathArg); + Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), tokenTypePathArg); return (response.readEntity(ImpersonationToken.class)); } @@ -1081,41 +1071,41 @@ Form userToForm(User user, Integer projectsLimit, CharSequence password, Boolean String skipConfirmationFeildName = create ? "skip_confirmation" : "skip_reconfirmation"; return (new GitLabApiForm() - .withParam("public_email", user.getPublicEmail(), false) - .withParam("email", user.getEmail(), create) - .withParam("password", password, false) - .withParam("reset_password", resetPassword, false) - .withParam("username", user.getUsername(), create) - .withParam("name", user.getName(), create) - .withParam("skype", user.getSkype(), false) - .withParam("linkedin", user.getLinkedin(), false) - .withParam("twitter", user.getTwitter(), false) - .withParam("website_url", user.getWebsiteUrl(), false) - .withParam("organization", user.getOrganization(), false) - .withParam("projects_limit", projectsLimit, false) - .withParam("extern_uid", user.getExternUid(), false) - .withParam("provider", user.getProvider(), false) - .withParam("bio", user.getBio(), false) - .withParam("location", user.getLocation(), false) - .withParam("admin", user.getIsAdmin(), false) - .withParam("theme_id", user.getThemeId(), false) - .withParam("color_scheme_id", user.getColorSchemeId(), false) - .withParam("can_create_group", user.getCanCreateGroup(), false) - .withParam(skipConfirmationFeildName, user.getSkipConfirmation(), false) - .withParam("external", user.getExternal(), false) - .withParam("shared_runners_minutes_limit", user.getSharedRunnersMinutesLimit(), false)); + .withParam("public_email", user.getPublicEmail(), false) + .withParam("email", user.getEmail(), create) + .withParam("password", password, false) + .withParam("reset_password", resetPassword, false) + .withParam("username", user.getUsername(), create) + .withParam("name", user.getName(), create) + .withParam("skype", user.getSkype(), false) + .withParam("linkedin", user.getLinkedin(), false) + .withParam("twitter", user.getTwitter(), false) + .withParam("website_url", user.getWebsiteUrl(), false) + .withParam("organization", user.getOrganization(), false) + .withParam("projects_limit", projectsLimit, false) + .withParam("extern_uid", user.getExternUid(), false) + .withParam("provider", user.getProvider(), false) + .withParam("bio", user.getBio(), false) + .withParam("location", user.getLocation(), false) + .withParam("admin", user.getIsAdmin(), false) + .withParam("theme_id", user.getThemeId(), false) + .withParam("color_scheme_id", user.getColorSchemeId(), false) + .withParam("can_create_group", user.getCanCreateGroup(), false) + .withParam(skipConfirmationFeildName, user.getSkipConfirmation(), false) + .withParam("external", user.getExternal(), false) + .withParam("shared_runners_minutes_limit", user.getSharedRunnersMinutesLimit(), false)); } /** * Creates custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param customAttribute the custom attribute to set + * @param customAttribute the custom attribute to set * @return the created CustomAttribute * @throws GitLabApiException on failure while setting customAttributes */ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, final CustomAttribute customAttribute) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(customAttribute)) { throw new IllegalArgumentException("CustomAttributes can't be null"); } @@ -1126,13 +1116,13 @@ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, fina * Creates custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param key for the customAttribute - * @param value or the customAttribute + * @param key for the customAttribute + * @param value or the customAttribute * @return the created CustomAttribute * @throws GitLabApiException on failure while setting customAttributes */ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, final String key, final String value) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(key) || key.trim().isEmpty()) { throw new IllegalArgumentException("Key can't be null or empty"); @@ -1143,12 +1133,12 @@ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, fina GitLabApiForm formData = new GitLabApiForm().withParam("value", value); Response response = put( - Response.Status.OK, - formData.asMap(), - "users", - getUserIdOrUsername(userIdOrUsername), - "custom_attributes", - key); + Response.Status.OK, + formData.asMap(), + "users", + getUserIdOrUsername(userIdOrUsername), + "custom_attributes", + key); return (response.readEntity(CustomAttribute.class)); } @@ -1156,12 +1146,12 @@ public CustomAttribute createCustomAttribute(final Object userIdOrUsername, fina * Change custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param customAttribute the custome attribute to change + * @param customAttribute the custome attribute to change * @return the changed CustomAttribute * @throws GitLabApiException on failure while changing customAttributes */ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, final CustomAttribute customAttribute) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(customAttribute)) { throw new IllegalArgumentException("CustomAttributes can't be null"); @@ -1176,13 +1166,13 @@ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, fina * Changes custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param key for the customAttribute - * @param value for the customAttribute + * @param key for the customAttribute + * @param value for the customAttribute * @return changedCustomAttribute * @throws GitLabApiException on failure while changing customAttributes */ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, final String key, final String value) - throws GitLabApiException { + throws GitLabApiException { return createCustomAttribute(userIdOrUsername, key, value); } @@ -1190,11 +1180,11 @@ public CustomAttribute changeCustomAttribute(final Object userIdOrUsername, fina * Delete a custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param customAttribute to remove + * @param customAttribute to remove * @throws GitLabApiException on failure while deleting customAttributes */ public void deleteCustomAttribute(final Object userIdOrUsername, final CustomAttribute customAttribute) - throws GitLabApiException { + throws GitLabApiException { if (Objects.isNull(customAttribute)) { throw new IllegalArgumentException("customAttributes can't be null"); } @@ -1206,7 +1196,7 @@ public void deleteCustomAttribute(final Object userIdOrUsername, final CustomAtt * Delete a custom attribute for the given user * * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param key of the customAttribute to remove + * @param key of the customAttribute to remove * @throws GitLabApiException on failure while deleting customAttributes */ public void deleteCustomAttribute(final Object userIdOrUsername, final String key) throws GitLabApiException { @@ -1236,13 +1226,13 @@ private GitLabApiForm createGitLabApiForm() { *
PUT /users/:id
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param avatarFile the File instance of the avatar file to upload + * @param avatarFile the File instance of the avatar file to upload * @return the updated User instance * @throws GitLabApiException if any exception occurs */ public User setUserAvatar(final Object userIdOrUsername, File avatarFile) throws GitLabApiException { Response response = - putUpload(Response.Status.OK, "avatar", avatarFile, "users", getUserIdOrUsername(userIdOrUsername)); + putUpload(Response.Status.OK, "avatar", avatarFile, "users", getUserIdOrUsername(userIdOrUsername)); return (response.readEntity(User.class)); } @@ -1256,8 +1246,7 @@ public User setUserAvatar(final Object userIdOrUsername, File avatarFile) throws */ public List getEmails() throws GitLabApiException { Response response = get(Response.Status.OK, null, "user", "emails"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -1271,8 +1260,7 @@ public List getEmails() throws GitLabApiException { */ public List getEmails(final Object userIdOrUsername) throws GitLabApiException { Response response = get(Response.Status.OK, null, "users", getUserIdOrUsername(userIdOrUsername), "emails"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -1310,18 +1298,18 @@ public Email getEmail(final Long emailId) throws GitLabApiException { *
GitLab Endpoint: POST /user/:id/emails
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param email the email address to add + * @param email the email address to add * @param skipConfirmation skip confirmation and assume e-mail is verified - true or false (default) * @return the Email instance for the added email * @throws GitLabApiException if any exception occurs */ public Email addEmail(final Object userIdOrUsername, String email, Boolean skipConfirmation) - throws GitLabApiException { + throws GitLabApiException { GitLabApiForm formData = - new GitLabApiForm().withParam("email", email, true).withParam("skip_confirmation ", skipConfirmation); + new GitLabApiForm().withParam("email", email, true).withParam("skip_confirmation ", skipConfirmation); Response response = - post(Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), "emails"); + post(Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), "emails"); return (response.readEntity(Email.class)); } @@ -1343,7 +1331,7 @@ public void deleteEmail(final Long emailId) throws GitLabApiException { *
GitLab Endpoint: DELETE /user/:id/emails/:emailId
* * @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance - * @param emailId the email ID to delete + * @param emailId the email ID to delete * @throws GitLabApiException if any exception occurs */ public void deleteEmail(final Object userIdOrUsername, final Long emailId) throws GitLabApiException { @@ -1359,8 +1347,7 @@ public void deleteEmail(final Object userIdOrUsername, final Long emailId) throw */ public List listGpgKeys() throws GitLabApiException { Response response = get(Response.Status.OK, null, "user", "gpg_keys"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -1399,8 +1386,7 @@ public void deleteGpgKey(final Long keyId) throws GitLabApiException { */ public List listGpgKeys(final Long userId) throws GitLabApiException { Response response = get(Response.Status.OK, null, "users", userId, "gpg_keys"); - return (response.readEntity(new GenericType>() { - })); + return (response.readEntity(new GenericType>() {})); } /** @@ -1409,7 +1395,7 @@ public List listGpgKeys(final Long userId) throws GitLabApiException { *
GitLab Endpoint: POST /users/:id/gpg_keys
* * @param userId the user in the form of an Long(ID) - * @param key the ASCII-armored exported public GPG key to add + * @param key the ASCII-armored exported public GPG key to add * @throws GitLabApiException if any exception occurs */ public GpgKey addGpgKey(final Long userId, final String key) throws GitLabApiException { @@ -1424,7 +1410,7 @@ public GpgKey addGpgKey(final Long userId, final String key) throws GitLabApiExc *
GitLab Endpoint: DELETE /users/:id/gpg_keys/:keyId
* * @param userId the user in the form of an Long(ID) - * @param keyId the key ID in the form if an Long(ID) + * @param keyId the key ID in the form if an Long(ID) * @throws GitLabApiException if any exception occurs */ public void deleteGpgKey(final Long userId, final Long keyId) throws GitLabApiException { @@ -1447,12 +1433,12 @@ public List getMemberships(Long userId) throws GitLabApiException { /** * 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. * *

GitLab Endpoint: GET /users/:id/memberships
* - * @param userId the ID of the user to get the memberships for + * @param userId the ID of the user to get the memberships for * @param itemsPerPage the number of Membership instances that will be fetched per page * @return a Pager of user's memberships * @throws GitLabApiException if any exception occurs @@ -1509,7 +1495,7 @@ public boolean exists(String username) throws GitLabApiException { } try { Response response = - get(Response.Status.OK, null, getApiClient().getUrlWithBase("users", username, "exists")); + get(Response.Status.OK, null, getApiClient().getUrlWithBase("users", username, "exists")); return response.readEntity(Exists.class).getExists(); } catch (IOException e) { throw new GitLabApiException(e); From 529460058533825db83a9e9f6a0ec5e93cea78a9 Mon Sep 17 00:00:00 2001 From: chenzw <656469722@qq.com> Date: Fri, 22 Nov 2024 09:46:35 +0800 Subject: [PATCH 3/5] style --- src/main/java/org/gitlab4j/api/UserApi.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/UserApi.java b/src/main/java/org/gitlab4j/api/UserApi.java index bac6e8bc2..0fbdf69ea 100644 --- a/src/main/java/org/gitlab4j/api/UserApi.java +++ b/src/main/java/org/gitlab4j/api/UserApi.java @@ -833,7 +833,10 @@ 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); + 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)); } @@ -1024,7 +1027,7 @@ public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException { } Response.Status expectedStatus = - (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); + (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 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 4/5] 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( From 07e3a69e499dad69541dbf0516113580143a9b10 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Thu, 28 Nov 2024 20:09:45 +0100 Subject: [PATCH 5/5] 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) {