-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathGitlabAPI.java
More file actions
2593 lines (2290 loc) · 103 KB
/
GitlabAPI.java
File metadata and controls
2593 lines (2290 loc) · 103 KB
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 java.io.*;
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;
/**
* Gitlab API Wrapper class
*
* @author @timols (Tim O)
*/
@SuppressWarnings("unused")
public class GitlabAPI {
public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private static final String API_NAMESPACE = "/api/v4";
private static final String PARAM_SUDO = "sudo";
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 boolean ignoreCertificateErrors = false;
private Proxy proxy;
private int requestTimeout = 0;
private String userAgent = GitlabAPI.class.getCanonicalName() + "/" + System.getProperty("java.version");
private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) {
this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
this.apiToken = apiToken;
this.tokenType = tokenType;
this.authMethod = method;
}
public static GitlabSession connect(String hostUrl, String username, String password) throws IOException {
String tailUrl = GitlabSession.URL;
GitlabAPI api = connect(hostUrl, null, null, 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 GitlabAPI ignoreCertificateErrors(boolean ignoreCertificateErrors) {
this.ignoreCertificateErrors = ignoreCertificateErrors;
return this;
}
public GitlabAPI proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public int getRequestTimeout() {
return requestTimeout;
}
public GitlabAPI setRequestTimeout(int requestTimeout) {
this.requestTimeout = requestTimeout;
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 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 + API_NAMESPACE + tailAPIUrl);
}
public URL getUrl(String tailAPIUrl) throws IOException {
if (!tailAPIUrl.startsWith("/")) {
tailAPIUrl = "/" + tailAPIUrl;
}
return new URL(hostUrl + tailAPIUrl);
}
public List<GitlabUser> getUsers() throws IOException {
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
*/
public List<GitlabUser> findUsers(String emailOrUsername) throws IOException {
List<GitlabUser> users = new ArrayList<GitlabUser>();
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
* @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) throws IOException {
Query query = new Query()
.append("email", email)
.appendIf("confirm", skip_confirmation == null ? null : !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);
String tailUrl = GitlabUser.USERS_URL + query.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
* @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) 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);
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);
}
/**
* 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());
}
/**
* Get a group by path
*
* @param path Path of the group
* @return
* @throws IOException
*/
public GitlabGroup getGroup(String path) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + path;
return retrieve().to(tailUrl, 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
* @throws IOException
*/
public List<GitlabProject> getGroupProjects(GitlabGroup group) throws IOException {
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
* @throws IOException
*/
public List<GitlabProject> getGroupProjects(Integer groupId) throws IOException {
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
* @throws IOException on gitlab api call error
*/
public List<GitlabGroupMember> getGroupMembers(GitlabGroup group) throws IOException {
return getGroupMembers(group.getId());
}
/**
* Gets all members of a Group
*
* @param groupId The id of the GitLab Group
* @return The Group Members
* @throws IOException on gitlab api call error
*/
public List<GitlabGroupMember> getGroupMembers(Integer groupId) throws IOException {
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 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 {
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);
String tailUrl = GitlabGroup.URL + query.toString();
return dispatch().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);
}
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 + "?search=" + projectName;
GitlabProject[] projects = retrieve().to(tailUrl, GitlabProject[].class);
for(GitlabProject gp : projects) {
String pNamespace = gp.getNamespace().getName();
if(pNamespace.equals(namespace)) {
return gp;
}
}
throw new FileNotFoundException("Project not found: "+namespace+" / "+projectName);
}
/**
*
* Get a list of projects accessible by the authenticated user.
*
* @return A list of gitlab projects
* @throws IOException
*/
public List<GitlabProject> getProjects() throws IOException {
String tailUrl = GitlabProject.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
*
* Get a list of projects owned by the authenticated user.
*
* @return A list of gitlab projects
* @throws IOException
*/
public List<GitlabProject> getOwnedProjects() throws IOException {
Query query = new Query().append("owner", "true");
String tailUrl = GitlabProject.URL + query.toString() + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
*
* Get a list of projects starred by the authenticated user.
*
* @return A list of gitlab projects
* @throws IOException
*/
public List<GitlabProject> getStarredProjects() throws IOException {
Query query = new Query().append("starred", "true");
String tailUrl = GitlabProject.URL + query.toString() + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
*
* Get a list of projects accessible by the authenticated user.
*
* @return A list of gitlab projects
* @throws IOException
*/
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);
}
/**
* Uploads a file to a project
*
* @param project
* @param file
* @return
* @throws IOException
*/
public GitlabUpload uploadFile(GitlabProject project, File file) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(project.getId()) + GitlabUpload.URL;
return dispatch().withAttachment("file", file).to(tailUrl, GitlabUpload.class);
}
/**
*
* Gets a list of a project's jobs in Gitlab
*
* @param project the project
* @return A list of project jobs
* @throws IOException
*/
public List<GitlabJob> getProjectJobs(GitlabProject project) throws IOException {
return getProjectJobs(project.getId());
}
/**
*
* Gets a list of a project's jobs in Gitlab
*
* @param projectId the project id
* @return A list of project jobs
* @throws IOException
*/
public List<GitlabJob> getProjectJobs(Integer projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabJob.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabJob[].class);
}
/**
*
* Gets a build for a project
*
* @param projectId the project id
* @param jobId the build id
* @return A list of project jobs
* @throws IOException
*/
public GitlabJob getProjectJob(Integer projectId, Integer jobId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabJob.URL + "/" + jobId;
return retrieve().to(tailUrl, GitlabJob.class);
}
/**
* Get build artifacts of a project build
*
* @param project The Project
* @param job The build
* @throws IOException on gitlab api call error
*/
public byte[] getJobArtifact(GitlabProject project, GitlabJob job) throws IOException {
return getJobArtifact(project.getId(), job.getId());
}
/**
* Get build artifacts of a project build
*
* @param projectId The Project's Id
* @param jobId The build's Id
* @throws IOException on gitlab api call error
*/
public byte[] getJobArtifact(Integer projectId, Integer jobId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabJob.URL + "/" + jobId + "/artifacts";
return retrieve().to(tailUrl, byte[].class);
}
/**
* Creates a private Project
*
* @param name The name of the project
* @return The GitLab Project
* @throws IOException on gitlab api call error
*/
public GitlabProject createProject(String name) throws IOException {
return createProject(name, null, null, null, null, null, null, null, null, null, null);
}
/**
* Creates a group Project
*
* @param name The name of the project
* @param group The group for which the project should be crated
* @return The GitLab Project
* @throws IOException on gitlab api call error
*/
public GitlabProject createProjectForGroup(String name, GitlabGroup group) throws IOException {
return createProjectForGroup(name, group, null);
}
/**
* Creates a group Project
*
* @param name The name of the project
* @param group The group for which the project should be crated
* @param description The project description
* @return The GitLab Project
* @throws IOException on gitlab api call error
*/
public GitlabProject createProjectForGroup(String name, GitlabGroup group, String description) throws IOException {
return createProjectForGroup(name, group, description, null);
}
/**
* Creates a group Project
*
* @param name The name of the project
* @param group The group for which the project should be crated
* @param description The project description
* @param visibility The project visibility level (private: 0, internal: 10, public: 20)
* @return The GitLab Project
* @throws IOException on gitlab api call error
*/
public GitlabProject createProjectForGroup(String name, GitlabGroup group, String description, String visibility) throws IOException {
return createProject(name, group.getId(), description, null, null, null, null, null, null, visibility, null);
}
/**
* Creates a Project
*
* @param name The name of the project
* @param namespaceId The Namespace for the new project, otherwise null indicates to use the GitLab default (user)
* @param description A description for the project, null otherwise
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param visibility The visibility level of the project, otherwise null indicates to use GitLab default
* @param importUrl The Import URL for the project, otherwise null
* @return the Gitlab Project
* @throws IOException on gitlab api call error
*/
@Deprecated
public GitlabProject createProject(String name, Integer namespaceId, String description, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled, Boolean wikiEnabled, Boolean snippetsEnabled, Boolean publik, String visibility, String importUrl) throws IOException {
Query query = new Query()
.append("name", name)
.appendIf("namespace_id", namespaceId)
.appendIf("description", description)
.appendIf("issues_enabled", issuesEnabled)
.appendIf("wall_enabled", wallEnabled)
.appendIf("merge_requests_enabled", mergeRequestsEnabled)
.appendIf("wiki_enabled", wikiEnabled)
.appendIf("snippets_enabled", snippetsEnabled)
.appendIf("visibility", visibility)
.appendIf("import_url", importUrl);
String tailUrl = GitlabProject.URL + query.toString();
return dispatch().to(tailUrl, GitlabProject.class);
}
/**
* Creates a Project for a specific User
*
* @param userId The id of the user to create the project for
* @param name The name of the project
* @return The GitLab Project
* @throws IOException on gitlab api call error
*/
public GitlabProject createUserProject(Integer userId, String name) throws IOException {
return createUserProject(userId, name, null, null, null, null, null, null, null, null, null);
}
/**
* Creates a Project for a specific User
*
* @param userId The id of the user to create the project for
* @param name The name of the project
* @param description A description for the project, null otherwise
* @param defaultBranch The default branch for the project, otherwise null indicates to use GitLab default (master)
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param visibility The visibility level of the project, otherwise null indicates to use GitLab default
* @param importUrl The Import URL for the project, otherwise null
* @return The GitLab Project
* @throws IOException on gitlab api call error
*/
@Deprecated
public GitlabProject createUserProject(Integer userId, String name, String description, String defaultBranch, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled, Boolean wikiEnabled, Boolean snippetsEnabled, String visibility, String importUrl) throws IOException {
Query query = new Query()
.append("name", name)
.appendIf("description", description)
.appendIf("default_branch", defaultBranch)
.appendIf("issues_enabled", issuesEnabled)
.appendIf("wall_enabled", wallEnabled)
.appendIf("merge_requests_enabled", mergeRequestsEnabled)
.appendIf("wiki_enabled", wikiEnabled)
.appendIf("snippets_enabled", snippetsEnabled)
.appendIf("visibility", visibility)
.appendIf("import_url", importUrl);
String tailUrl = GitlabProject.URL + "/user/" + userId + query.toString();
return dispatch().to(tailUrl, GitlabProject.class);
}
/**
* Updates a Project
*
* @param projectId The id of the project to update
* @param name The name of the project
* @param description A description for the project, null otherwise
* @param defaultBranch The branch displayed in the Gitlab UI when a user navigates to the project
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param visibilityLevel The visibility level of the project, otherwise null indicates to use GitLab default
* @return the Gitlab Project
* @throws IOException on gitlab api call error
*/
@Deprecated
public GitlabProject updateProject(
Integer projectId,
String name,
String description,
String defaultBranch,
Boolean issuesEnabled,
Boolean wallEnabled,
Boolean mergeRequestsEnabled,
Boolean wikiEnabled,
Boolean snippetsEnabled,
String visibility)
throws IOException
{
Query query = new Query()
.appendIf("name", name)
.appendIf("description", description)
.appendIf("default_branch", defaultBranch)
.appendIf("issues_enabled", issuesEnabled)
.appendIf("wall_enabled", wallEnabled)
.appendIf("merge_requests_enabled", mergeRequestsEnabled)
.appendIf("wiki_enabled", wikiEnabled)
.appendIf("snippets_enabled", snippetsEnabled)
.appendIf("visibility", visibility);
String tailUrl = GitlabProject.URL + "/" + projectId + query.toString();
return retrieve().method("PUT").to(tailUrl, GitlabProject.class);
}
/**
* Delete a Project.
*
* @param projectId The id of the project to delete
* @throws IOException on gitlab api call error
*/
public void deleteProject(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId);
retrieve().method("DELETE").to(tailUrl, null);
}
public List<GitlabMergeRequest> getOpenMergeRequests(Serializable projectId) throws IOException {
Query query = new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).asQuery();
query.append("state", "opened");
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabMergeRequest.URL + query;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}
public List<GitlabMergeRequest> getOpenMergeRequests(GitlabProject project) throws IOException {
Query query = new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).asQuery();
query.append("state", "opened");
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL + query;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}
public List<GitlabMergeRequest> getMergeRequests(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabMergeRequest.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}
public List<GitlabMergeRequest> getMergeRequests(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}
public List<GitlabMergeRequest> getAllMergeRequests(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}
/**
* Return Merge Request.
*
* @param projectId The id of the project
* @param mergeRequestIid The iid of the merge request
* @return the Gitlab Merge Request
* @throws IOException on gitlab api call error
*/
public GitlabMergeRequest getMergeRequestByIid(Serializable projectId, Integer mergeRequestIid) throws IOException {
Query query = new Query()
.append("iid", mergeRequestIid.toString());
query.mergeWith(new Pagination().withPerPage(Pagination.MAX_ITEMS_PER_PAGE).asQuery());
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabMergeRequest.URL + query.toString();
List<GitlabMergeRequest> ls = retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
if (ls.size() == 0) {
throw new FileNotFoundException();
}
return ls.get(0);
}
/**
* Return a Merge Request including its changes.
*
* @param projectId The id of the project
* @param mergeRequestId The id of the merge request
* @return the Gitlab Merge Request
* @throws IOException on gitlab api call error
*/
public GitlabMergeRequest getMergeRequestChanges(Serializable projectId, Integer mergeRequestId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + "/merge_requests/" + mergeRequestId + "/changes";
return retrieve().to(tailUrl, GitlabMergeRequest.class);
}
public GitlabMergeRequest getMergeRequest(GitlabProject project, Integer mergeRequestId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + "/merge_requests/" + mergeRequestId;
return retrieve().to(tailUrl, GitlabMergeRequest.class);
}
/**
* Create a new MergeRequest
*
* @param projectId
* @param sourceBranch
* @param targetBranch
* @param assigneeId
* @param title
* @return GitlabMergeRequest
* @throws IOException on gitlab api call error
*/
public GitlabMergeRequest createMergeRequest(Serializable projectId, String sourceBranch, String targetBranch,
Integer assigneeId, String title) throws IOException {
Query query = new Query()
.appendIf("target_branch", targetBranch)