-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathGitlabAPI.java
4267 lines (3788 loc) · 177 KB
/
GitlabAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.gitlab.api;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.gitlab.api.http.GitlabHTTPRequestor;
import org.gitlab.api.http.Query;
import org.gitlab.api.models.*;
import org.gitlab.api.query.CommitsQuery;
import org.gitlab.api.query.PaginationQuery;
import org.gitlab.api.query.PipelinesQuery;
import org.gitlab.api.query.ProjectsQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import static org.gitlab.api.http.Method.*;
/**
* Gitlab API Wrapper class
*
* @author @timols (Tim O)
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class GitlabAPI {
private static final Logger LOG = LoggerFactory.getLogger(GitlabAPI.class);
public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private static final String DEFAULT_API_NAMESPACE = "/api/v4";
private static final String PARAM_SUDO = "sudo";
private static final String PARAM_WITH_PROJECTS = "with_projects";
private static final String PARAM_MAX_ITEMS_PER_PAGE = new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).toString();
private final String hostUrl;
private final String apiToken;
private final TokenType tokenType;
private AuthMethod authMethod;
private final String apiNamespace;
private boolean ignoreCertificateErrors = false;
private Proxy proxy;
private int defaultTimeout = 0;
private int readTimeout = defaultTimeout;
private int connectionTimeout = defaultTimeout;
private String userAgent = GitlabAPI.class.getCanonicalName() + "/" + System.getProperty("java.version");
private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method, String apiNamespace) {
this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
this.apiToken = apiToken;
this.tokenType = tokenType;
this.authMethod = method;
this.apiNamespace = apiNamespace;
}
private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) {
this(hostUrl, apiToken, tokenType, method, DEFAULT_API_NAMESPACE);
}
public static GitlabSession connect(String hostUrl, String username, String password) throws IOException {
String tailUrl = GitlabSession.URL;
GitlabAPI api = connect(hostUrl, null, null, (AuthMethod) null);
return api.dispatch().with("login", username).with("password", password)
.to(tailUrl, GitlabSession.class);
}
public static GitlabAPI connect(String hostUrl, String apiToken) {
return new GitlabAPI(hostUrl, apiToken, TokenType.PRIVATE_TOKEN, AuthMethod.HEADER);
}
public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType) {
return new GitlabAPI(hostUrl, apiToken, tokenType, AuthMethod.HEADER);
}
public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) {
return new GitlabAPI(hostUrl, apiToken, tokenType, method);
}
public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType, String apiNamespace) {
return new GitlabAPI(hostUrl, apiToken, tokenType, AuthMethod.HEADER, apiNamespace);
}
public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method, String apiNamespace) {
return new GitlabAPI(hostUrl, apiToken, tokenType, method, apiNamespace);
}
public GitlabAPI ignoreCertificateErrors(boolean ignoreCertificateErrors) {
this.ignoreCertificateErrors = ignoreCertificateErrors;
return this;
}
public GitlabAPI proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public int getResponseReadTimeout() {
return readTimeout;
}
/**
* @deprecated use this.getResponseReadTimeout() method
*/
@Deprecated
public int getRequestTimeout() {
return getResponseReadTimeout();
}
/**
* @deprecated use this.setResponseReadTimeout(int readTimeout) method
*/
@Deprecated
public GitlabAPI setRequestTimeout(int readTimeout) {
setResponseReadTimeout(readTimeout);
return this;
}
public GitlabAPI setResponseReadTimeout(int readTimeout) {
if (readTimeout < 0) {
LOG.warn("The value of the \"Response Read Timeout\" parameter can not be negative. " +
"The default value [{}] will be used.", defaultTimeout);
this.readTimeout = defaultTimeout;
} else {
this.readTimeout = readTimeout;
}
return this;
}
public int getConnectionTimeout() {
return connectionTimeout;
}
public GitlabAPI setConnectionTimeout(int connectionTimeout) {
if (connectionTimeout < 0) {
LOG.warn("The value of the \"Connection Timeout\" parameter can not be negative. " +
"The default value [{}] will be used.", defaultTimeout);
this.connectionTimeout = defaultTimeout;
} else {
this.connectionTimeout = connectionTimeout;
}
return this;
}
public GitlabHTTPRequestor retrieve() {
return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod);
}
public GitlabHTTPRequestor dispatch() {
return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod).method(POST);
}
public GitlabHTTPRequestor put() {
return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod).method(PUT);
}
public boolean isIgnoreCertificateErrors() {
return ignoreCertificateErrors;
}
public Proxy getProxy() {
return proxy;
}
public URL getAPIUrl(String tailAPIUrl) throws IOException {
if (!tailAPIUrl.startsWith("/")) {
tailAPIUrl = "/" + tailAPIUrl;
}
return new URL(hostUrl + apiNamespace + tailAPIUrl);
}
public URL getUrl(String tailAPIUrl) throws IOException {
if (!tailAPIUrl.startsWith("/")) {
tailAPIUrl = "/" + tailAPIUrl;
}
return new URL(hostUrl + tailAPIUrl);
}
public String getHost() {
return hostUrl;
}
public List<GitlabUser> getUsers() {
String tailUrl = GitlabUser.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabUser[].class);
}
/**
* Finds users by email address or username.
*
* @param emailOrUsername Some portion of the email address or username
* @return A non-null List of GitlabUser instances. If the search term is
* null or empty a List with zero GitlabUsers is returned.
* @throws IOException on gitlab api call error
*/
public List<GitlabUser> findUsers(String emailOrUsername) throws IOException {
List<GitlabUser> users = new ArrayList<>();
if (emailOrUsername != null && !emailOrUsername.equals("")) {
String tailUrl = GitlabUser.URL + "?search=" + emailOrUsername;
GitlabUser[] response = retrieve().to(tailUrl, GitlabUser[].class);
users = Arrays.asList(response);
}
return users;
}
/**
* Return API User
*/
public GitlabUser getUser() throws IOException {
String tailUrl = GitlabUser.USER_URL;
return retrieve().to(tailUrl, GitlabUser.class);
}
public GitlabUser getUser(Integer userId) throws IOException {
String tailUrl = GitlabUser.URL + "/" + userId;
return retrieve().to(tailUrl, GitlabUser.class);
}
public GitlabUser getUserViaSudo(String username) throws IOException {
String tailUrl = GitlabUser.USER_URL + "?" + PARAM_SUDO + "=" + username;
return retrieve().to(tailUrl, GitlabUser.class);
}
/**
* Create a new User
*
* @param email User email
* @param password Password
* @param username User name
* @param fullName Full name
* @param skypeId Skype Id
* @param linkedIn LinkedIn
* @param twitter Twitter
* @param website_url Website URL
* @param projects_limit Projects limit
* @param extern_uid External User ID
* @param extern_provider_name External Provider Name
* @param bio Bio
* @param isAdmin Is Admin
* @param can_create_group Can Create Group
* @param skip_confirmation Skip Confirmation
* @param external External
* @return A GitlabUser
* @throws IOException on gitlab api call error
* @see <a href="http://doc.gitlab.com/ce/api/users.html">http://doc.gitlab.com/ce/api/users.html</a>
*/
public GitlabUser createUser(String email, String password, String username,
String fullName, String skypeId, String linkedIn,
String twitter, String website_url, Integer projects_limit,
String extern_uid, String extern_provider_name,
String bio, Boolean isAdmin, Boolean can_create_group,
Boolean skip_confirmation, Boolean external) throws IOException {
Query query = new Query()
.append("email", email)
.appendIf("skip_confirmation", skip_confirmation)
.appendIf("password", password)
.appendIf("username", username)
.appendIf("name", fullName)
.appendIf("skype", skypeId)
.appendIf("linkedin", linkedIn)
.appendIf("twitter", twitter)
.appendIf("website_url", website_url)
.appendIf("projects_limit", projects_limit)
.appendIf("extern_uid", extern_uid)
.appendIf("provider", extern_provider_name)
.appendIf("bio", bio)
.appendIf("admin", isAdmin)
.appendIf("can_create_group", can_create_group)
.appendIf("external", external);
String tailUrl = GitlabUser.USERS_URL + query.toString();
return dispatch().to(tailUrl, GitlabUser.class);
}
/**
* Create a new user. This may succeed only if the requester is an administrator.
*
* @param request An object that represents the parameters for the request.
* @return {@link GitlabUser}
* @throws IOException on gitlab api call error
*/
public GitlabUser createUser(CreateUserRequest request) throws IOException {
String tailUrl = GitlabUser.USERS_URL + request.toQuery().toString();
return dispatch().to(tailUrl, GitlabUser.class);
}
/**
* Update a user
*
* @param targetUserId User ID
* @param email User email
* @param password Password
* @param username User name
* @param fullName Full name
* @param skypeId Skype Id
* @param linkedIn LinkedIn
* @param twitter Twitter
* @param website_url Website URL
* @param projects_limit Projects limit
* @param extern_uid External User ID
* @param extern_provider_name External Provider Name
* @param bio Bio
* @param isAdmin Is Admin
* @param can_create_group Can Create Group
* @param external External
* @return The Updated User
* @throws IOException on gitlab api call error
*/
public GitlabUser updateUser(Integer targetUserId,
String email, String password, String username,
String fullName, String skypeId, String linkedIn,
String twitter, String website_url, Integer projects_limit,
String extern_uid, String extern_provider_name,
String bio, Boolean isAdmin, Boolean can_create_group, Boolean external) throws IOException {
Query query = new Query()
.append("email", email)
.appendIf("password", password)
.appendIf("username", username)
.appendIf("name", fullName)
.appendIf("skype", skypeId)
.appendIf("linkedin", linkedIn)
.appendIf("twitter", twitter)
.appendIf("website_url", website_url)
.appendIf("projects_limit", projects_limit)
.appendIf("extern_uid", extern_uid)
.appendIf("provider", extern_provider_name)
.appendIf("bio", bio)
.appendIf("admin", isAdmin)
.appendIf("can_create_group", can_create_group)
.appendIf("external", external);
String tailUrl = GitlabUser.USERS_URL + "/" + targetUserId + query.toString();
return retrieve().method(PUT).to(tailUrl, GitlabUser.class);
}
/**
* Block a user
*
* @param targetUserId The id of the Gitlab user
* @throws IOException on gitlab api call error
*/
public void blockUser(Integer targetUserId) throws IOException {
String tailUrl = GitlabUser.USERS_URL + "/" + targetUserId + GitlabUser.BLOCK_URL;
retrieve().method(POST).to(tailUrl, Void.class);
}
/**
* Unblock a user
*
* @param targetUserId The id of the Gitlab user
* @throws IOException on gitlab api call error
*/
public void unblockUser(Integer targetUserId) throws IOException {
String tailUrl = GitlabUser.USERS_URL + "/" + targetUserId + GitlabUser.UNBLOCK_URL;
retrieve().method(POST).to(tailUrl, Void.class);
}
/**
* Create a new ssh key for the user
*
* @param targetUserId The id of the Gitlab user
* @param title The title of the ssh key
* @param key The public key
* @return The new GitlabSSHKey
* @throws IOException on gitlab api call error
*/
public GitlabSSHKey createSSHKey(Integer targetUserId, String title, String key) throws IOException {
Query query = new Query()
.append("title", title)
.append("key", key);
String tailUrl = GitlabUser.USERS_URL + "/" + targetUserId + GitlabSSHKey.KEYS_URL + query.toString();
return dispatch().to(tailUrl, GitlabSSHKey.class);
}
/**
* Create a new ssh key for the authenticated user.
*
* @param title The title of the ssh key
* @param key The public key
* @return The new GitlabSSHKey
* @throws IOException on gitlab api call error
*/
public GitlabSSHKey createSSHKey(String title, String key) throws IOException {
Query query = new Query()
.append("title", title)
.append("key", key);
String tailUrl = GitlabUser.USER_URL + GitlabSSHKey.KEYS_URL + query.toString();
return dispatch().to(tailUrl, GitlabSSHKey.class);
}
/**
* Delete user's ssh key
*
* @param targetUserId The id of the Gitlab user
* @param targetKeyId The id of the Gitlab ssh key
* @throws IOException on gitlab api call error
*/
public void deleteSSHKey(Integer targetUserId, Integer targetKeyId) throws IOException {
String tailUrl = GitlabUser.USERS_URL + "/" + targetUserId + GitlabSSHKey.KEYS_URL + "/" + targetKeyId;
retrieve().method(DELETE).to(tailUrl, Void.class);
}
/**
* Gets all ssh keys for a user
*
* @param targetUserId The id of the GitLab User
* @return The list of user ssh keys
* @throws IOException on gitlab api call error
*/
public List<GitlabSSHKey> getSSHKeys(Integer targetUserId) throws IOException {
String tailUrl = GitlabUser.USERS_URL + "/" + targetUserId + GitlabSSHKey.KEYS_URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabSSHKey[].class));
}
/**
* Get key with user information by ID of an SSH key.
*
* @param keyId The ID of an SSH key
* @return The SSH key with user information
* @throws IOException on gitlab api call error
*/
public GitlabSSHKey getSSHKey(Integer keyId) throws IOException {
String tailUrl = GitlabSSHKey.KEYS_URL + "/" + keyId;
return retrieve().to(tailUrl, GitlabSSHKey.class);
}
/**
* Delete a user
*
* @param targetUserId The target User ID
* @throws IOException on gitlab api call error
*/
public void deleteUser(Integer targetUserId) throws IOException {
String tailUrl = GitlabUser.USERS_URL + "/" + targetUserId;
retrieve().method(DELETE).to(tailUrl, Void.class);
}
public GitlabGroup getGroup(Integer groupId) throws IOException {
return getGroup(groupId.toString());
}
public GitlabGroup getGroupWithoutProjects(Integer groupId) throws IOException {
return getGroupWithoutProjects(groupId.toString());
}
/**
* Get a group by path. Don't include the projects.
*
* @param path Path of the group
* @return {@link GitlabGroup} object
*
* @throws IOException on gitlab api call error
*/
public GitlabGroup getGroupWithoutProjects(String path) throws IOException {
return getGroup(path, false);
}
/**
* Get a group by path, including its projects.
*
* @param path Path of the group
* @return {@link GitlabGroup} object
*
* @throws IOException on gitlab api call error
*/
public GitlabGroup getGroup(String path) throws IOException {
return getGroup(path, true);
}
/**
* Get a group by path
*
* @param path Path of the group
* @param withProjects If true, include the projects
* @return {@link GitlabGroup} object
*
* @throws IOException on gitlab api call error
*/
public GitlabGroup getGroup(String path, boolean withProjects) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + URLEncoder.encode(path, "UTF-8");
Query query = new Query()
.append(PARAM_WITH_PROJECTS, "" + withProjects);
return retrieve().to(tailUrl + query.toString(), GitlabGroup.class);
}
public List<GitlabGroup> getGroups() throws IOException {
return getGroupsViaSudo(null, new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE));
}
public List<GitlabGroup> getGroupsViaSudo(String username, Pagination pagination) throws IOException {
String tailUrl = GitlabGroup.URL;
Query query = new Query()
.appendIf(PARAM_SUDO, username);
if (pagination != null) {
query.mergeWith(pagination.asQuery());
}
return retrieve().getAll(tailUrl + query.toString(), GitlabGroup[].class);
}
/**
* Get all the projects for a group.
*
* @param group the target group
* @return a list of projects for the group
*/
public List<GitlabProject> getGroupProjects(GitlabGroup group) {
return getGroupProjects(group.getId());
}
/**
* Get all the projects for a group.
*
* @param groupId the target group's id.
* @return a list of projects for the group
*/
public List<GitlabProject> getGroupProjects(Integer groupId) {
String tailUrl = GitlabGroup.URL + "/" + groupId + GitlabProject.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Gets all members of a Group
*
* @param group The GitLab Group
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(GitlabGroup group) {
return getGroupMembers(group.getId());
}
/**
* Gets all members of a Group
*
* @param groupId The id of the GitLab Group
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(Integer groupId) {
String tailUrl = GitlabGroup.URL + "/" + groupId + GitlabGroupMember.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabGroupMember[].class);
}
/**
* Creates a Group
*
* @param name The name of the group. The
* name will also be used as the path
* of the group.
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroup(String name) throws IOException {
return createGroup(name, name);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroup(String name, String path) throws IOException {
return createGroup(name, path, null, null, null);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
* @param sudoUser The user to create the group on behalf of
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroupViaSudo(String name, String path, GitlabUser sudoUser) throws IOException {
return createGroup(name, path, null, null, sudoUser);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
* @param ldapCn LDAP Group Name to sync with, null otherwise
* @param ldapAccess Access level for LDAP group members, null otherwise
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroup(String name, String path, String ldapCn, GitlabAccessLevel ldapAccess) throws IOException {
return createGroup(name, path, ldapCn, ldapAccess, null);
}
/**
* Creates a Group
*
* @param request An object that represents the parameters for the request.
* @param sudoUser The user for whom we're creating the group
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroup(CreateGroupRequest request, GitlabUser sudoUser) throws IOException {
Query query = request.toQuery();
query.appendIf(PARAM_SUDO, sudoUser != null ? sudoUser.getId() : null);
String tailUrl = GitlabGroup.URL + query.toString();
return dispatch().to(tailUrl, GitlabGroup.class);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
* @param ldapCn LDAP Group Name to sync with, null otherwise
* @param ldapAccess Access level for LDAP group members, null otherwise
* @param sudoUser The user to create the group on behalf of
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroup(String name, String path, String ldapCn, GitlabAccessLevel ldapAccess, GitlabUser sudoUser) throws IOException {
return createGroup(name, path, ldapCn, ldapAccess, sudoUser, null);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
* @param ldapCn LDAP Group Name to sync with, null otherwise
* @param ldapAccess Access level for LDAP group members, null otherwise
* @param sudoUser The user to create the group on behalf of
* @param parentId The id of a parent group; the new group will be its subgroup
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroup(String name, String path, String ldapCn, GitlabAccessLevel ldapAccess, GitlabUser sudoUser, Integer parentId) throws IOException {
Query query = new Query()
.append("name", name)
.append("path", path)
.appendIf("ldap_cn", ldapCn)
.appendIf("ldap_access", ldapAccess)
.appendIf(PARAM_SUDO, sudoUser != null ? sudoUser.getId() : null)
.appendIf("parent_id", parentId);
String tailUrl = GitlabGroup.URL + query.toString();
return dispatch().to(tailUrl, GitlabGroup.class);
}
/**
* Creates a Group
*
* @param group The gitlab Group object
* @param sudoUser The user to create the group on behalf of
*
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup createGroup(GitlabGroup group, GitlabUser sudoUser) throws IOException {
Query query = new Query()
.append("name", group.getName())
.append("path", group.getPath())
.appendIf("description", group.getDescription())
.appendIf("membership_lock", group.getMembershipLock())
.appendIf("share_with_group_lock", group.getShareWithGroupLock())
.appendIf("visibility", group.getVisibility().toString())
.appendIf("lfs_enabled", group.isLfsEnabled())
.appendIf("request_access_enabled", group.isRequestAccessEnabled())
.appendIf("shared_runners_minutes_limit", group.getSharedRunnersMinutesLimit())
.appendIf("ldap_cn", group.getLdapCn())
.appendIf("ldap_access", group.getLdapAccess())
.appendIf(PARAM_SUDO, sudoUser != null ? sudoUser.getId() : null);
String tailUrl = GitlabGroup.URL + query.toString();
return dispatch().to(tailUrl, GitlabGroup.class);
}
/**
* Updates a Group
*
* @param group the group object
* @param sudoUser The user to create the group on behalf of
* @return The GitLab Group
* @throws IOException on gitlab api call error
*/
public GitlabGroup updateGroup(GitlabGroup group, GitlabUser sudoUser) throws IOException {
Query query = new Query()
.appendIf("name", group.getName())
.appendIf("path", group.getPath())
.appendIf("description", group.getDescription())
.appendIf("membership_lock", group.getMembershipLock())
.appendIf("share_with_group_lock", group.getShareWithGroupLock())
.appendIf("visibility", group.getVisibility().toString())
.appendIf("lfs_enabled", group.isLfsEnabled())
.appendIf("request_access_enabled", group.isRequestAccessEnabled())
.appendIf("shared_runners_minutes_limit", group.getSharedRunnersMinutesLimit())
.appendIf("ldap_cn", group.getLdapCn())
.appendIf("ldap_access", group.getLdapAccess())
.appendIf(PARAM_SUDO, sudoUser != null ? sudoUser.getId() : null);
String tailUrl = GitlabGroup.URL + "/" + group.getId() + query.toString();
return retrieve().method(PUT).to(tailUrl, GitlabGroup.class);
}
/**
* Add a group member.
*
* @param group the GitlabGroup
* @param user the GitlabUser
* @param accessLevel the GitlabAccessLevel
* @return the GitlabGroupMember
* @throws IOException on gitlab api call error
*/
public GitlabGroupMember addGroupMember(GitlabGroup group, GitlabUser user, GitlabAccessLevel accessLevel) throws IOException {
return addGroupMember(group.getId(), user.getId(), accessLevel);
}
/**
* Add a group member.
*
* @param groupId the group id
* @param userId the user id
* @param accessLevel the GitlabAccessLevel
* @return the GitlabGroupMember
* @throws IOException on gitlab api call error
*/
public GitlabGroupMember addGroupMember(Integer groupId, Integer userId, GitlabAccessLevel accessLevel) throws IOException {
Query query = new Query()
.appendIf("id", groupId)
.appendIf("user_id", userId)
.appendIf("access_level", accessLevel);
String tailUrl = GitlabGroup.URL + "/" + groupId + GitlabProjectMember.URL + query.toString();
return dispatch().to(tailUrl, GitlabGroupMember.class);
}
/**
* Delete a group member.
*
* @param group the GitlabGroup
* @param user the GitlabUser
* @throws IOException on gitlab api call error
*/
public void deleteGroupMember(GitlabGroup group, GitlabUser user) throws IOException {
deleteGroupMember(group.getId(), user.getId());
}
/**
* Delete a group member.
*
* @param groupId the group id
* @param userId the user id
* @throws IOException on gitlab api call error
*/
public void deleteGroupMember(Integer groupId, Integer userId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId + "/" + GitlabGroupMember.URL + "/" + userId;
retrieve().method(DELETE).to(tailUrl, Void.class);
}
/**
* Delete a group.
*
* @param groupId the group id
* @throws IOException on gitlab api call error
*/
public void deleteGroup(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId;
retrieve().method(DELETE).to(tailUrl, Void.class);
}
/**
* Get's all projects in Gitlab, requires sudo user
*
* @return A list of gitlab projects
*/
public List<GitlabProject> getAllProjects() {
String tailUrl = GitlabProject.URL;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Get Project by project Id
*
* @param projectId - gitlab project Id
* @return {@link GitlabProject}
* @throws IOException on gitlab api call error
*/
public GitlabProject getProject(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId);
return retrieve().to(tailUrl, GitlabProject.class);
}
/**
* use namespace & project name to get project
*/
public GitlabProject getProject(String namespace, String projectName) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeGroupId(namespace) + "%2F" + sanitizeProjectId(projectName);
return retrieve().to(tailUrl, GitlabProject.class);
}
/*
* use project id to get Project JSON
*/
public String getProjectJson(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId);
return retrieve().to(tailUrl, String.class);
}
/*
* use namespace & project name to get project
*/
public String getProjectJson(String namespace, String projectName) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeGroupId(namespace) + "%2F" + sanitizeProjectId(projectName);
return retrieve().to(tailUrl, String.class);
}
/**
* Get a list of projects accessible by the authenticated user.
*
* @return A list of gitlab projects
*/
public List<GitlabProject> getProjects() {
String tailUrl = GitlabProject.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Get a list of projects of size perPage accessible by the authenticated user.
*
* @param page page offset.
* @param perPage number elements to get after page offset.
* @return A list of gitlab projects
* @throws IOException on Gitlab API call error
*/
public List<GitlabProject> getProjectsWithPagination(int page, int perPage) throws IOException {
Pagination pagination = new Pagination()
.withPage(page)
.withPerPage(perPage);
return getProjectsWithPagination(pagination);
}
/**
* Get a list of projects accessible by the authenticated user.
*
* @return A list of gitlab projects
*/
public List<GitlabProject> getProjects(ProjectsQuery projectsQuery) {
String tailUrl = GitlabProject.URL + projectsQuery;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Get a list of projects by pagination accessible by the authenticated user.
*
* @param pagination
* @return
* @throws IOException on gitlab api call error
*/
public List<GitlabProject> getProjectsWithPagination(Pagination pagination) throws IOException {
StringBuilder tailUrl = new StringBuilder(GitlabProject.URL);
if (pagination != null) {
Query query = pagination.asQuery();
tailUrl.append(query.toString());
}
return Arrays.asList(retrieve().method(GET).to(tailUrl.toString(), GitlabProject[].class));
}
/**
* Get a list of projects owned by the authenticated user.
*
* @return A list of gitlab projects
* @throws IOException on gitlab api call error
*/
public List<GitlabProject> getOwnedProjects() throws IOException {
Query query = new Query().append("owned", "true");
query.mergeWith(new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).asQuery());
String tailUrl = GitlabProject.URL + query.toString();
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Get a list of projects that the authenticated user is a member of.
*
* @return A list of gitlab projects
* @throws IOException on gitlab api call error
*/
public List<GitlabProject> getMembershipProjects() throws IOException {
Query query = new Query().append("membership", "true");
query.mergeWith(new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).asQuery());
String tailUrl = GitlabProject.URL + query.toString();
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Get a list of projects starred by the authenticated user.
*
* @return A list of gitlab projects
* @throws IOException on gitlab api call error
*/
public List<GitlabProject> getStarredProjects() throws IOException {
Query query = new Query().append("starred", "true");
query.mergeWith(new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).asQuery());
String tailUrl = GitlabProject.URL + query.toString();
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Get a list of projects accessible by the authenticated user.
*
* @return A list of gitlab projects
* @throws IOException on gitlab api call error
*/
public List<GitlabProject> getProjectsViaSudo(GitlabUser user) throws IOException {
Query query = new Query()
.appendIf(PARAM_SUDO, user.getId());
query.mergeWith(new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).asQuery());
String tailUrl = GitlabProject.URL + query.toString();
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Get a list of projects of perPage elements accessible by the authenticated user given page offset
*
* @param user Gitlab User to invoke sudo with
* @param page Page offset
* @param perPage Number of elements to get after page offset
* @return A list of gitlab projects
* @throws IOException Gitlab API call error
*/
public List<GitlabProject> getProjectsViaSudoWithPagination(GitlabUser user, int page, int perPage) throws IOException {
Pagination pagination = new Pagination()
.withPage(page)
.withPerPage(perPage);
return getProjectsViaSudoWithPagination(user, pagination);
}
/**
* Get a list of projects of with Pagination.
*
* @param user Gitlab User to invoke sudo with
* @param pagination
* @return A list of gitlab projects
* @throws IOException Gitlab API call error
*/
public List<GitlabProject> getProjectsViaSudoWithPagination(GitlabUser user, Pagination pagination) throws IOException {
StringBuilder tailUrl = new StringBuilder(GitlabProject.URL);
Query query = new Query()
.appendIf(PARAM_SUDO, user.getId());
if (pagination != null) {
query.mergeWith(pagination.asQuery());
}
tailUrl.append(query.toString());
return Arrays.asList(retrieve().method(GET).to(tailUrl.toString(), GitlabProject[].class));
}