Skip to content

Commit 6c9cf0f

Browse files
Ruben Vittgmessner
authored andcommitted
feat: changing customAttributes for users (#255)
1 parent d7d98fc commit 6c9cf0f

File tree

1 file changed

+118
-12
lines changed

1 file changed

+118
-12
lines changed

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

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import org.gitlab4j.api.GitLabApi.ApiVersion;
4+
import org.gitlab4j.api.models.CustomAttribute;
45
import org.gitlab4j.api.models.ImpersonationToken;
56
import org.gitlab4j.api.models.ImpersonationToken.Scope;
67
import org.gitlab4j.api.models.SshKey;
@@ -11,6 +12,7 @@
1112
import javax.ws.rs.core.Response;
1213
import java.util.Date;
1314
import java.util.List;
15+
import java.util.Objects;
1416
import java.util.Optional;
1517

1618
/**
@@ -78,7 +80,7 @@ public List<User> getUsers(int page, int perPage) throws GitLabApiException {
7880
* @throws GitLabApiException if any exception occurs
7981
*/
8082
public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException {
81-
return (new Pager<User>(this, User.class, itemsPerPage, creatGitLabApiForm().asMap(), "users"));
83+
return (new Pager<User>(this, User.class, itemsPerPage, createGitLabApiForm().asMap(), "users"));
8284
}
8385

8486
/**
@@ -90,7 +92,7 @@ public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException {
9092
* @throws GitLabApiException if any exception occurs
9193
*/
9294
public List<User> getActiveUsers() throws GitLabApiException {
93-
GitLabApiForm formData = creatGitLabApiForm()
95+
GitLabApiForm formData = createGitLabApiForm()
9496
.withParam("active", true)
9597
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
9698
Response response = get(Response.Status.OK, formData.asMap(), "users");
@@ -109,7 +111,7 @@ public List<User> getActiveUsers() throws GitLabApiException {
109111
* @throws GitLabApiException if any exception occurs
110112
*/
111113
public List<User> getActiveUsers(int page, int perPage) throws GitLabApiException {
112-
GitLabApiForm formData = creatGitLabApiForm()
114+
GitLabApiForm formData = createGitLabApiForm()
113115
.withParam("active", true)
114116
.withParam(PAGE_PARAM, page)
115117
.withParam(PER_PAGE_PARAM, perPage);
@@ -128,7 +130,7 @@ public List<User> getActiveUsers(int page, int perPage) throws GitLabApiExceptio
128130
* @throws GitLabApiException if any exception occurs
129131
*/
130132
public Pager<User> getActiveUsers(int itemsPerPage) throws GitLabApiException {
131-
GitLabApiForm formData = creatGitLabApiForm().withParam("active", true);
133+
GitLabApiForm formData = createGitLabApiForm().withParam("active", true);
132134
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
133135
}
134136

@@ -182,7 +184,7 @@ public void unblockUser(Integer userId) throws GitLabApiException {
182184
* @throws GitLabApiException if any exception occurs
183185
*/
184186
public List<User> getBlockedUsers() throws GitLabApiException {
185-
GitLabApiForm formData = creatGitLabApiForm()
187+
GitLabApiForm formData = createGitLabApiForm()
186188
.withParam("blocked", true)
187189
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
188190
Response response = get(Response.Status.OK, formData.asMap(), "users");
@@ -201,7 +203,7 @@ public List<User> getBlockedUsers() throws GitLabApiException {
201203
* @throws GitLabApiException if any exception occurs
202204
*/
203205
public List<User> getblockedUsers(int page, int perPage) throws GitLabApiException {
204-
GitLabApiForm formData = creatGitLabApiForm()
206+
GitLabApiForm formData = createGitLabApiForm()
205207
.withParam("blocked", true)
206208
.withParam(PAGE_PARAM, page)
207209
.withParam(PER_PAGE_PARAM, perPage);
@@ -220,7 +222,7 @@ public List<User> getblockedUsers(int page, int perPage) throws GitLabApiExcepti
220222
* @throws GitLabApiException if any exception occurs
221223
*/
222224
public Pager<User> getBlockedUsers(int itemsPerPage) throws GitLabApiException {
223-
GitLabApiForm formData = creatGitLabApiForm().withParam("blocked", true);
225+
GitLabApiForm formData = createGitLabApiForm().withParam("blocked", true);
224226
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
225227
}
226228

@@ -267,7 +269,7 @@ public Optional<User> getOptionalUser(int userId) {
267269
* @throws GitLabApiException if any exception occurs
268270
*/
269271
public User getUser(String username) throws GitLabApiException {
270-
GitLabApiForm formData = creatGitLabApiForm().withParam("username", username, true);
272+
GitLabApiForm formData = createGitLabApiForm().withParam("username", username, true);
271273
Response response = get(Response.Status.OK, formData.asMap(), "users");
272274
List<User> users = response.readEntity(new GenericType<List<User>>() {
273275
});
@@ -302,7 +304,7 @@ public Optional<User> getOptionalUser(String username) {
302304
* @throws GitLabApiException if any exception occurs
303305
*/
304306
public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
305-
GitLabApiForm formData = creatGitLabApiForm()
307+
GitLabApiForm formData = createGitLabApiForm()
306308
.withParam("search", emailOrUsername, true)
307309
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
308310
Response response = get(Response.Status.OK, formData.asMap(), "users");
@@ -322,7 +324,7 @@ public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
322324
* @throws GitLabApiException if any exception occurs
323325
*/
324326
public List<User> findUsers(String emailOrUsername, int page, int perPage) throws GitLabApiException {
325-
GitLabApiForm formData = creatGitLabApiForm()
327+
GitLabApiForm formData = createGitLabApiForm()
326328
.withParam("search", emailOrUsername, true)
327329
.withParam(PAGE_PARAM, page)
328330
.withParam(PER_PAGE_PARAM, perPage);
@@ -342,7 +344,7 @@ public List<User> findUsers(String emailOrUsername, int page, int perPage) throw
342344
* @throws GitLabApiException if any exception occurs
343345
*/
344346
public Pager<User> findUsers(String emailOrUsername, int itemsPerPage) throws GitLabApiException {
345-
GitLabApiForm formData = creatGitLabApiForm().withParam("search", emailOrUsername, true);
347+
GitLabApiForm formData = createGitLabApiForm().withParam("search", emailOrUsername, true);
346348
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
347349
}
348350

@@ -907,14 +909,118 @@ Form userToForm(User user, Integer projectsLimit, CharSequence password, Boolean
907909
.withParam("shared_runners_minutes_limit", user.getSharedRunnersMinutesLimit(), false));
908910
}
909911

912+
/**
913+
* Creates custom attribute for the given user
914+
*
915+
* @param userId to set the customAttributes for
916+
* @param customAttribute to set
917+
* @return createdCustomAttribute
918+
* @throws GitLabApiException on failure while setting customAttributes
919+
*/
920+
public CustomAttribute createCustomAttribute(final Integer userId, final CustomAttribute customAttribute) throws GitLabApiException {
921+
if (Objects.isNull(customAttribute)) {
922+
throw new IllegalArgumentException("CustomAttributes can't be null");
923+
}
924+
return createCustomAttribute(userId, customAttribute.getKey(), customAttribute.getValue());
925+
}
926+
927+
/**
928+
* Creates custom attribute for the given user
929+
*
930+
* @param userId to set the customAttributes for
931+
* @param key for the customAttribute
932+
* @param value for the customAttribute
933+
* @return createdCustomAttribute
934+
* @throws GitLabApiException on failure while setting customAttributes
935+
*/
936+
public CustomAttribute createCustomAttribute(final Integer userId, final String key, final String value) throws GitLabApiException {
937+
if (Objects.isNull(userId)) {
938+
throw new IllegalArgumentException("UserId can't be null.");
939+
}
940+
if (Objects.isNull(key) || key.trim().isEmpty()) {
941+
throw new IllegalArgumentException("Key can't be null or empty");
942+
}
943+
if (Objects.isNull(value) || value.trim().isEmpty()) {
944+
throw new IllegalArgumentException("Value can't be null or empty");
945+
}
946+
947+
GitLabApiForm formData = new GitLabApiForm()
948+
.withParam("value", value);
949+
Response response = put(Response.Status.OK, formData.asMap(), "users", userId, "custom_attributes", key);
950+
return (response.readEntity(CustomAttribute.class));
951+
}
952+
953+
/**
954+
* Change custom attribute for the given user
955+
*
956+
* @param userId to change the customAttributes for
957+
* @param customAttribute to change
958+
* @return changedCustomAttribute
959+
* @throws GitLabApiException on failure while changing customAttributes
960+
*/
961+
public CustomAttribute changeCustomAttribute(final Integer userId, final CustomAttribute customAttribute) throws GitLabApiException {
962+
if (Objects.isNull(customAttribute)) {
963+
throw new IllegalArgumentException("CustomAttributes can't be null");
964+
}
965+
966+
//changing & creating custom attributes is the same call in gitlab api
967+
// -> https://docs.gitlab.com/ce/api/custom_attributes.html#set-custom-attribute
968+
return createCustomAttribute(userId, customAttribute.getKey(), customAttribute.getValue());
969+
}
970+
971+
/**
972+
* Changes custom attribute for the given user
973+
*
974+
* @param userId to change the customAttribute for
975+
* @param key for the customAttribute
976+
* @param value for the customAttribute
977+
* @return changedCustomAttribute
978+
* @throws GitLabApiException on failure while changing customAttributes
979+
*/
980+
public CustomAttribute changeCustomAttribute(final Integer userId, final String key, final String value) throws GitLabApiException {
981+
return createCustomAttribute(userId, key, value);
982+
}
983+
984+
/**
985+
* Delete a custom attribute for the given user
986+
*
987+
* @param userId to delete the customAttribute for
988+
* @param customAttribute to remove
989+
* @throws GitLabApiException on failure while deleting customAttributes
990+
*/
991+
public void deleteCustomAttribute(final Integer userId, final CustomAttribute customAttribute) throws GitLabApiException {
992+
if (Objects.isNull(customAttribute)) {
993+
throw new IllegalArgumentException("customAttributes can't be null");
994+
}
995+
996+
deleteCustomAttribute(userId, customAttribute.getKey());
997+
}
998+
999+
/**
1000+
* Delete a custom attribute for the given user
1001+
*
1002+
* @param userId to delete the customAttribute for
1003+
* @param key of the customAttribute to remove
1004+
* @throws GitLabApiException on failure while deleting customAttributes
1005+
*/
1006+
public void deleteCustomAttribute(final Integer userId, final String key) throws GitLabApiException {
1007+
if (Objects.isNull(userId)) {
1008+
throw new IllegalArgumentException("UserId can't be null");
1009+
}
1010+
if (Objects.isNull(key) || key.trim().isEmpty()) {
1011+
throw new IllegalArgumentException("Key can't be null or empty");
1012+
}
1013+
delete(Response.Status.OK, null, "users", userId, "custom_attributes", key);
1014+
}
1015+
9101016
/**
9111017
* Creates a GitLabApiForm instance that will optionally include the
9121018
* with_custom_attributes query param if enabled.
9131019
*
9141020
* @return a GitLabApiForm instance that will optionally include the
9151021
* with_custom_attributes query param if enabled
9161022
*/
917-
private GitLabApiForm creatGitLabApiForm() {
1023+
private GitLabApiForm createGitLabApiForm() {
9181024
GitLabApiForm formData = new GitLabApiForm();
9191025
return (customAttributesEnabled ? formData.withParam("with_custom_attributes", true) : formData);
9201026
}

0 commit comments

Comments
 (0)