1
1
package org .gitlab4j .api ;
2
2
3
3
import org .gitlab4j .api .GitLabApi .ApiVersion ;
4
+ import org .gitlab4j .api .models .CustomAttribute ;
4
5
import org .gitlab4j .api .models .ImpersonationToken ;
5
6
import org .gitlab4j .api .models .ImpersonationToken .Scope ;
6
7
import org .gitlab4j .api .models .SshKey ;
11
12
import javax .ws .rs .core .Response ;
12
13
import java .util .Date ;
13
14
import java .util .List ;
15
+ import java .util .Objects ;
14
16
import java .util .Optional ;
15
17
16
18
/**
@@ -78,7 +80,7 @@ public List<User> getUsers(int page, int perPage) throws GitLabApiException {
78
80
* @throws GitLabApiException if any exception occurs
79
81
*/
80
82
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" ));
82
84
}
83
85
84
86
/**
@@ -90,7 +92,7 @@ public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException {
90
92
* @throws GitLabApiException if any exception occurs
91
93
*/
92
94
public List <User > getActiveUsers () throws GitLabApiException {
93
- GitLabApiForm formData = creatGitLabApiForm ()
95
+ GitLabApiForm formData = createGitLabApiForm ()
94
96
.withParam ("active" , true )
95
97
.withParam (PER_PAGE_PARAM , getDefaultPerPage ());
96
98
Response response = get (Response .Status .OK , formData .asMap (), "users" );
@@ -109,7 +111,7 @@ public List<User> getActiveUsers() throws GitLabApiException {
109
111
* @throws GitLabApiException if any exception occurs
110
112
*/
111
113
public List <User > getActiveUsers (int page , int perPage ) throws GitLabApiException {
112
- GitLabApiForm formData = creatGitLabApiForm ()
114
+ GitLabApiForm formData = createGitLabApiForm ()
113
115
.withParam ("active" , true )
114
116
.withParam (PAGE_PARAM , page )
115
117
.withParam (PER_PAGE_PARAM , perPage );
@@ -128,7 +130,7 @@ public List<User> getActiveUsers(int page, int perPage) throws GitLabApiExceptio
128
130
* @throws GitLabApiException if any exception occurs
129
131
*/
130
132
public Pager <User > getActiveUsers (int itemsPerPage ) throws GitLabApiException {
131
- GitLabApiForm formData = creatGitLabApiForm ().withParam ("active" , true );
133
+ GitLabApiForm formData = createGitLabApiForm ().withParam ("active" , true );
132
134
return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "users" ));
133
135
}
134
136
@@ -182,7 +184,7 @@ public void unblockUser(Integer userId) throws GitLabApiException {
182
184
* @throws GitLabApiException if any exception occurs
183
185
*/
184
186
public List <User > getBlockedUsers () throws GitLabApiException {
185
- GitLabApiForm formData = creatGitLabApiForm ()
187
+ GitLabApiForm formData = createGitLabApiForm ()
186
188
.withParam ("blocked" , true )
187
189
.withParam (PER_PAGE_PARAM , getDefaultPerPage ());
188
190
Response response = get (Response .Status .OK , formData .asMap (), "users" );
@@ -201,7 +203,7 @@ public List<User> getBlockedUsers() throws GitLabApiException {
201
203
* @throws GitLabApiException if any exception occurs
202
204
*/
203
205
public List <User > getblockedUsers (int page , int perPage ) throws GitLabApiException {
204
- GitLabApiForm formData = creatGitLabApiForm ()
206
+ GitLabApiForm formData = createGitLabApiForm ()
205
207
.withParam ("blocked" , true )
206
208
.withParam (PAGE_PARAM , page )
207
209
.withParam (PER_PAGE_PARAM , perPage );
@@ -220,7 +222,7 @@ public List<User> getblockedUsers(int page, int perPage) throws GitLabApiExcepti
220
222
* @throws GitLabApiException if any exception occurs
221
223
*/
222
224
public Pager <User > getBlockedUsers (int itemsPerPage ) throws GitLabApiException {
223
- GitLabApiForm formData = creatGitLabApiForm ().withParam ("blocked" , true );
225
+ GitLabApiForm formData = createGitLabApiForm ().withParam ("blocked" , true );
224
226
return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "users" ));
225
227
}
226
228
@@ -267,7 +269,7 @@ public Optional<User> getOptionalUser(int userId) {
267
269
* @throws GitLabApiException if any exception occurs
268
270
*/
269
271
public User getUser (String username ) throws GitLabApiException {
270
- GitLabApiForm formData = creatGitLabApiForm ().withParam ("username" , username , true );
272
+ GitLabApiForm formData = createGitLabApiForm ().withParam ("username" , username , true );
271
273
Response response = get (Response .Status .OK , formData .asMap (), "users" );
272
274
List <User > users = response .readEntity (new GenericType <List <User >>() {
273
275
});
@@ -302,7 +304,7 @@ public Optional<User> getOptionalUser(String username) {
302
304
* @throws GitLabApiException if any exception occurs
303
305
*/
304
306
public List <User > findUsers (String emailOrUsername ) throws GitLabApiException {
305
- GitLabApiForm formData = creatGitLabApiForm ()
307
+ GitLabApiForm formData = createGitLabApiForm ()
306
308
.withParam ("search" , emailOrUsername , true )
307
309
.withParam (PER_PAGE_PARAM , getDefaultPerPage ());
308
310
Response response = get (Response .Status .OK , formData .asMap (), "users" );
@@ -322,7 +324,7 @@ public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
322
324
* @throws GitLabApiException if any exception occurs
323
325
*/
324
326
public List <User > findUsers (String emailOrUsername , int page , int perPage ) throws GitLabApiException {
325
- GitLabApiForm formData = creatGitLabApiForm ()
327
+ GitLabApiForm formData = createGitLabApiForm ()
326
328
.withParam ("search" , emailOrUsername , true )
327
329
.withParam (PAGE_PARAM , page )
328
330
.withParam (PER_PAGE_PARAM , perPage );
@@ -342,7 +344,7 @@ public List<User> findUsers(String emailOrUsername, int page, int perPage) throw
342
344
* @throws GitLabApiException if any exception occurs
343
345
*/
344
346
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 );
346
348
return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "users" ));
347
349
}
348
350
@@ -907,14 +909,118 @@ Form userToForm(User user, Integer projectsLimit, CharSequence password, Boolean
907
909
.withParam ("shared_runners_minutes_limit" , user .getSharedRunnersMinutesLimit (), false ));
908
910
}
909
911
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
+
910
1016
/**
911
1017
* Creates a GitLabApiForm instance that will optionally include the
912
1018
* with_custom_attributes query param if enabled.
913
1019
*
914
1020
* @return a GitLabApiForm instance that will optionally include the
915
1021
* with_custom_attributes query param if enabled
916
1022
*/
917
- private GitLabApiForm creatGitLabApiForm () {
1023
+ private GitLabApiForm createGitLabApiForm () {
918
1024
GitLabApiForm formData = new GitLabApiForm ();
919
1025
return (customAttributesEnabled ? formData .withParam ("with_custom_attributes" , true ) : formData );
920
1026
}
0 commit comments