forked from hub4j/github-api
/
GHRepositoryTest.java
1102 lines (930 loc) · 45.6 KB
/
GHRepositoryTest.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.kohsuke.github;
import com.fasterxml.jackson.databind.JsonMappingException;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.kohsuke.github.GHCheckRun.Conclusion;
import org.kohsuke.github.GHOrganization.RepositoryRole;
import org.kohsuke.github.GHRepository.Visibility;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThrows;
import static org.kohsuke.github.GHVerification.Reason.*;
/**
* @author Liam Newman
*/
public class GHRepositoryTest extends AbstractGitHubWireMockTest {
protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}
private GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization("hub4j-test-org").getRepository("github-api");
}
@Test
public void testZipball() throws IOException {
getTempRepository().readZip((InputStream inputstream) -> {
return new ByteArrayInputStream(IOUtils.toByteArray(inputstream));
}, null);
}
@Test
public void testTarball() throws IOException {
getTempRepository().readTar((InputStream inputstream) -> {
return new ByteArrayInputStream(IOUtils.toByteArray(inputstream));
}, null);
}
@Test
public void testGetters() throws IOException {
GHRepository r = getTempRepository();
assertThat(r.hasAdminAccess(), is(true));
assertThat(r.hasDownloads(), is(true));
assertThat(r.hasIssues(), is(true));
assertThat(r.hasPages(), is(false));
assertThat(r.hasProjects(), is(true));
assertThat(r.hasPullAccess(), is(true));
assertThat(r.hasPushAccess(), is(true));
assertThat(r.hasWiki(), is(true));
assertThat(r.isAllowMergeCommit(), is(true));
assertThat(r.isAllowRebaseMerge(), is(true));
assertThat(r.isAllowSquashMerge(), is(true));
String httpTransport = "https://github.com/hub4j-test-org/temp-testGetters.git";
assertThat(r.getHttpTransportUrl(), equalTo(httpTransport));
assertThat(r.gitHttpTransportUrl(), equalTo(httpTransport));
assertThat(r.getName(), equalTo("temp-testGetters"));
assertThat(r.getFullName(), equalTo("hub4j-test-org/temp-testGetters"));
}
@Test
public void archive() throws Exception {
// Archive is a one-way action in the API.
// After taking snapshot, manual state reset is required.
snapshotNotAllowed();
GHRepository repo = getRepository();
assertThat(repo.isArchived(), is(false));
repo.archive();
assertThat(repo.isArchived(), is(true));
assertThat(getRepository().isArchived(), is(true));
}
@Test
public void isDisabled() throws Exception {
GHRepository r = getRepository();
assertThat(r.isDisabled(), is(false));
}
@Test
public void isDisabledTrue() throws Exception {
GHRepository r = getRepository();
assertThat(r.isDisabled(), is(true));
}
@Test
public void getBranch_URLEncoded() throws Exception {
GHRepository repo = getRepository();
GHBranch branch = repo.getBranch("test/#UrlEncode");
assertThat(branch.getName(), is("test/#UrlEncode"));
}
@Test
public void createSignedCommitVerifyError() throws IOException {
GHRepository repository = getRepository();
GHTree ghTree = new GHTreeBuilder(repository).textEntry("a", "", false).create();
GHVerification verification = repository.createCommit()
.message("test signing")
.withSignature("-----BEGIN PGP SIGNATURE-----\ninvalid\n-----END PGP SIGNATURE-----")
.tree(ghTree.getSha())
.create()
.getCommitShortInfo()
.getVerification();
assertThat(verification.getReason(), equalTo(GPGVERIFY_ERROR));
}
@Test
public void createSignedCommitUnknownSignatureType() throws IOException {
GHRepository repository = getRepository();
GHTree ghTree = new GHTreeBuilder(repository).textEntry("a", "", false).create();
GHVerification verification = repository.createCommit()
.message("test signing")
.withSignature("unknown")
.tree(ghTree.getSha())
.create()
.getCommitShortInfo()
.getVerification();
assertThat(verification.getReason(), equalTo(UNKNOWN_SIGNATURE_TYPE));
}
@Test
public void listStargazers() throws IOException {
GHRepository repository = getRepository();
assertThat(repository.listStargazers2().toList(), is(empty()));
repository = gitHub.getOrganization("hub4j").getRepository("github-api");
Iterable<GHStargazer> stargazers = repository.listStargazers2();
GHStargazer stargazer = stargazers.iterator().next();
assertThat(stargazer.getStarredAt(), equalTo(new Date(1271650383000L)));
assertThat(stargazer.getUser().getLogin(), equalTo("nielswind"));
assertThat(stargazer.getRepository(), sameInstance(repository));
}
// Issue #607
@Test
public void getBranchNonExistentBut200Status() throws Exception {
// Manually changed the returned status to 200 so dont take a new snapshot
this.snapshotNotAllowed();
// This should *never* happen but with mocking it was discovered
GHRepository repo = getRepository();
try {
GHBranch branch = repo.getBranch("test/NonExistent");
fail();
} catch (Exception e) {
// I dont really love this but I wanted to get to the root wrapped cause
assertThat(e, instanceOf(IOException.class));
assertThat(e.getMessage(),
equalTo("Server returned HTTP response code: 200, message: '404 Not Found' for URL: "
+ mockGitHub.apiServer().baseUrl()
+ "/repos/hub4j-test-org/github-api/branches/test/NonExistent"));
}
}
@Test
public void subscription() throws Exception {
GHRepository r = getRepository();
assertThat(r.getSubscription(), nullValue());
GHSubscription s = r.subscribe(true, false);
try {
assertThat(r, equalTo(s.getRepository()));
assertThat(s.isIgnored(), equalTo(false));
assertThat(s.isSubscribed(), equalTo(true));
assertThat(s.getRepositoryUrl().toString(), containsString("/repos/hub4j-test-org/github-api"));
assertThat(s.getUrl().toString(), containsString("/repos/hub4j-test-org/github-api/subscription"));
assertThat(s.getReason(), nullValue());
assertThat(s.getCreatedAt(), equalTo(new Date(1611377286000L)));
} finally {
s.delete();
}
assertThat(r.getSubscription(), nullValue());
}
@Test
public void testSetPublic() throws Exception {
kohsuke();
GHUser myself = gitHub.getMyself();
String repoName = "test-repo-public";
GHRepository repo = gitHub.createRepository(repoName).private_(false).create();
try {
assertThat(repo.isPrivate(), is(false));
repo.setPrivate(true);
assertThat(myself.getRepository(repoName).isPrivate(), is(true));
repo.setPrivate(false);
assertThat(myself.getRepository(repoName).isPrivate(), is(false));
} finally {
repo.delete();
}
}
@Test
public void testUpdateRepository() throws Exception {
String homepage = "https://github-api.kohsuke.org/apidocs/index.html";
String description = "A test repository for update testing via the github-api project";
GHRepository repo = getTempRepository();
GHRepository.Updater builder = repo.update();
// one merge option is always required
GHRepository updated = builder.allowRebaseMerge(false)
.allowSquashMerge(false)
.deleteBranchOnMerge(true)
.description(description)
.downloads(false)
.downloads(false)
.homepage(homepage)
.issues(false)
.private_(true)
.projects(false)
.wiki(false)
.done();
assertThat(updated.isAllowMergeCommit(), is(true));
assertThat(updated.isAllowRebaseMerge(), is(false));
assertThat(updated.isAllowSquashMerge(), is(false));
assertThat(updated.isDeleteBranchOnMerge(), is(true));
assertThat(updated.isPrivate(), is(true));
assertThat(updated.hasDownloads(), is(false));
assertThat(updated.hasIssues(), is(false));
assertThat(updated.hasProjects(), is(false));
assertThat(updated.hasWiki(), is(false));
assertThat(updated.getHomepage(), equalTo(homepage));
assertThat(updated.getDescription(), equalTo(description));
// test the other merge option and making the repo public again
GHRepository redux = updated.update().allowMergeCommit(false).allowRebaseMerge(true).private_(false).done();
assertThat(redux.isAllowMergeCommit(), is(false));
assertThat(redux.isAllowRebaseMerge(), is(true));
assertThat(redux.isPrivate(), is(false));
String updatedDescription = "updated using set()";
redux = redux.set().description(updatedDescription);
assertThat(redux.getDescription(), equalTo(updatedDescription));
}
@Test
public void testGetRepositoryWithVisibility() throws IOException {
snapshotNotAllowed();
final String repoName = "test-repo-visibility";
final GHRepository repo = getTempRepository(repoName);
assertThat(repo.getVisibility(), equalTo(Visibility.PUBLIC));
repo.setVisibility(Visibility.INTERNAL);
assertThat(gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility(),
equalTo(Visibility.INTERNAL));
repo.setVisibility(Visibility.PRIVATE);
assertThat(gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility(),
equalTo(Visibility.PRIVATE));
repo.setVisibility(Visibility.PUBLIC);
assertThat(gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility(),
equalTo(Visibility.PUBLIC));
// deliberately bogus response in snapshot
assertThat(gitHub.getRepository(repo.getOwnerName() + "/" + repo.getName()).getVisibility(),
equalTo(Visibility.UNKNOWN));
}
@Test
public void listContributors() throws IOException {
GHRepository r = gitHub.getOrganization("hub4j").getRepository("github-api");
int i = 0;
boolean kohsuke = false;
for (GHRepository.Contributor c : r.listContributors()) {
if (c.getLogin().equals("kohsuke")) {
assertThat(c.getContributions(), greaterThan(0));
kohsuke = true;
}
if (i++ > 5) {
break;
}
}
assertThat(kohsuke, is(true));
}
@Test
public void getPermission() throws Exception {
kohsuke();
GHRepository r = gitHub.getRepository("hub4j-test-org/test-permission");
assertThat(r.getPermission("kohsuke"), equalTo(GHPermissionType.ADMIN));
assertThat(r.getPermission("dude"), equalTo(GHPermissionType.READ));
r = gitHub.getOrganization("apache").getRepository("groovy");
try {
r.getPermission("jglick");
fail();
} catch (HttpException x) {
// x.printStackTrace(); // good
assertThat(x.getResponseCode(), equalTo(403));
}
if (false) {
// can't easily test this; there's no private repository visible to the test user
r = gitHub.getOrganization("cloudbees").getRepository("private-repo-not-writable-by-me");
try {
r.getPermission("jglick");
fail();
} catch (FileNotFoundException x) {
x.printStackTrace(); // good
}
}
}
@Test
public void hasPermission() throws Exception {
kohsuke();
GHRepository publicRepository = gitHub.getRepository("hub4j-test-org/test-permission");
assertThat(publicRepository.hasPermission("kohsuke", GHPermissionType.ADMIN), equalTo(true));
assertThat(publicRepository.hasPermission("kohsuke", GHPermissionType.WRITE), equalTo(true));
assertThat(publicRepository.hasPermission("kohsuke", GHPermissionType.READ), equalTo(true));
assertThat(publicRepository.hasPermission("kohsuke", GHPermissionType.NONE), equalTo(false));
assertThat(publicRepository.hasPermission("dude", GHPermissionType.ADMIN), equalTo(false));
assertThat(publicRepository.hasPermission("dude", GHPermissionType.WRITE), equalTo(false));
assertThat(publicRepository.hasPermission("dude", GHPermissionType.READ), equalTo(true));
assertThat(publicRepository.hasPermission("dude", GHPermissionType.NONE), equalTo(false));
// also check the GHUser method
GHUser kohsuke = gitHub.getUser("kohsuke");
assertThat(publicRepository.hasPermission(kohsuke, GHPermissionType.ADMIN), equalTo(true));
assertThat(publicRepository.hasPermission(kohsuke, GHPermissionType.WRITE), equalTo(true));
assertThat(publicRepository.hasPermission(kohsuke, GHPermissionType.READ), equalTo(true));
assertThat(publicRepository.hasPermission(kohsuke, GHPermissionType.NONE), equalTo(false));
// check NONE on a private project
GHRepository privateRepository = gitHub.getRepository("hub4j-test-org/test-permission-private");
assertThat(privateRepository.hasPermission("dude", GHPermissionType.ADMIN), equalTo(false));
assertThat(privateRepository.hasPermission("dude", GHPermissionType.WRITE), equalTo(false));
assertThat(privateRepository.hasPermission("dude", GHPermissionType.READ), equalTo(false));
assertThat(privateRepository.hasPermission("dude", GHPermissionType.NONE), equalTo(true));
}
@Test
public void LatestRepositoryExist() {
try {
// add the repository that have latest release
GHRelease release = gitHub.getRepository("kamontat/CheckIDNumber").getLatestRelease();
assertThat(release.getTagName(), equalTo("v3.0"));
} catch (IOException e) {
e.printStackTrace();
fail();
}
}
@Test
public void addCollaborators() throws Exception {
GHRepository repo = getRepository();
GHUser user = getUser();
List<GHUser> users = new ArrayList<>();
users.add(user);
users.add(gitHub.getUser("jimmysombrero2"));
repo.addCollaborators(users, GHOrganization.Permission.PUSH);
GHPersonSet<GHUser> collabs = repo.getCollaborators();
GHUser colabUser = collabs.byLogin("jimmysombrero");
assertThat(user.getName(), equalTo(colabUser.getName()));
}
@Test
public void addCollaboratorsRepoPerm() throws Exception {
GHRepository repo = getRepository();
GHUser user = getUser();
RepositoryRole role = RepositoryRole.from(GHOrganization.Permission.PULL);
repo.addCollaborators(role, user);
GHPersonSet<GHUser> collabs = repo.getCollaborators();
GHUser colabUser = collabs.byLogin("jgangemi");
assertThat(user.getName(), equalTo(colabUser.getName()));
}
@Test
public void LatestRepositoryNotExist() {
try {
// add the repository that `NOT` have latest release
GHRelease release = gitHub.getRepository("kamontat/Java8Example").getLatestRelease();
assertThat(release, nullValue());
} catch (IOException e) {
e.printStackTrace();
fail();
}
}
@Test
public void listReleases() throws IOException {
PagedIterable<GHRelease> releases = gitHub.getOrganization("github").getRepository("hub").listReleases();
assertThat(releases, is(not(emptyIterable())));
}
@Test
public void getReleaseExists() throws IOException {
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(6839710);
assertThat(release.getTagName(), equalTo("v2.3.0-pre10"));
}
@Test
public void getReleaseDoesNotExist() throws IOException {
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(Long.MAX_VALUE);
assertThat(release, nullValue());
}
@Test
public void getReleaseByTagNameExists() throws IOException {
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getReleaseByTagName("v2.3.0-pre10");
assertThat(release, notNullValue());
assertThat(release.getTagName(), equalTo("v2.3.0-pre10"));
}
@Test
public void getReleaseByTagNameDoesNotExist() throws IOException {
GHRelease release = getRepository().getReleaseByTagName("foo-bar-baz");
assertThat(release, nullValue());
}
@Test
public void listLanguages() throws IOException {
GHRepository r = gitHub.getRepository("hub4j/github-api");
String mainLanguage = r.getLanguage();
assertThat(mainLanguage, equalTo("Java"));
Map<String, Long> languages = r.listLanguages();
assertThat(languages.containsKey(mainLanguage), is(true));
assertThat(languages.get("Java"), greaterThan(100000L));
}
@Test
public void listCommitCommentsNoComments() throws IOException {
List<GHCommitComment> commitComments = getRepository()
.listCommitComments("c413fc1e3057332b93850ea48202627d29a37de5")
.toList();
assertThat("Commit has no comments", commitComments.isEmpty());
commitComments = getRepository().getCommit("c413fc1e3057332b93850ea48202627d29a37de5").listComments().toList();
assertThat("Commit has no comments", commitComments.isEmpty());
}
@Test
public void searchAllPublicAndForkedRepos() throws IOException {
PagedSearchIterable<GHRepository> list = gitHub.searchRepositories()
.user("t0m4uk1991")
.visibility(GHRepository.Visibility.PUBLIC)
.fork(GHFork.PARENT_AND_FORKS)
.list();
List<GHRepository> u = list.toList();
assertThat(u.size(), is(14));
assertThat(u.stream().filter(item -> item.getName().equals("github-api")).count(), is(1L));
assertThat(u.stream().filter(item -> item.getName().equals("Complete-Python-3-Bootcamp")).count(), is(1L));
}
@Test
public void searchForPublicForkedOnlyRepos() throws IOException {
PagedSearchIterable<GHRepository> list = gitHub.searchRepositories()
.user("t0m4uk1991")
.visibility(GHRepository.Visibility.PUBLIC)
.fork(GHFork.FORKS_ONLY)
.list();
List<GHRepository> u = list.toList();
assertThat(u.size(), is(2));
assertThat(u.get(0).getName(), is("github-api"));
assertThat(u.get(1).getName(), is("Complete-Python-3-Bootcamp"));
}
@Test
public void ghRepositorySearchBuilderIgnoresUnknownVisibility() {
GHRepositorySearchBuilder ghRepositorySearchBuilder;
GHException exception = assertThrows(GHException.class,
() -> new GHRepositorySearchBuilder(gitHub).visibility(Visibility.UNKNOWN));
assertThat(exception.getMessage(),
startsWith("UNKNOWN is a placeholder for unexpected values encountered when reading data."));
ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub).visibility(Visibility.PUBLIC);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("is:")).count(), is(1L));
ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub).visibility(Visibility.PRIVATE);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("is:")).count(), is(1L));
ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub).visibility(Visibility.INTERNAL);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("is:")).count(), is(1L));
}
@Test
public void ghRepositorySearchBuilderForkDefaultResetForksSearchTerms() {
GHRepositorySearchBuilder ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub);
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHFork.PARENT_AND_FORKS);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:true")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(1L));
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHFork.FORKS_ONLY);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:only")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(2L));
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHFork.PARENT_ONLY);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
}
@Test
public void ghRepositorySearchBuilderForkDeprecatedEnum() {
GHRepositorySearchBuilder ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub);
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.PARENT_AND_FORKS);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:true")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(1L));
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.FORKS_ONLY);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:only")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(2L));
ghRepositorySearchBuilder = ghRepositorySearchBuilder.fork(GHRepositorySearchBuilder.Fork.PARENT_ONLY);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
}
@Test
public void ghRepositorySearchBuilderForkDeprecatedString() {
GHRepositorySearchBuilder ghRepositorySearchBuilder = new GHRepositorySearchBuilder(gitHub);
ghRepositorySearchBuilder = ghRepositorySearchBuilder.forks(GHFork.PARENT_AND_FORKS.toString());
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:true")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(1L));
ghRepositorySearchBuilder = ghRepositorySearchBuilder.forks(GHFork.FORKS_ONLY.toString());
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:only")).count(), is(1L));
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(2L));
ghRepositorySearchBuilder = ghRepositorySearchBuilder.forks(null);
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
}
@Test
public void listCommitCommentsSomeComments() throws IOException {
List<GHCommitComment> commitComments = getRepository()
.listCommitComments("499d91f9f846b0087b2a20cf3648b49dc9c2eeef")
.toList();
assertThat("Two comments present", commitComments.size(), equalTo(2));
assertThat("Comment text found",
commitComments.stream().map(GHCommitComment::getBody).collect(Collectors.toList()),
containsInAnyOrder("comment 1", "comment 2"));
commitComments = getRepository().getCommit("499d91f9f846b0087b2a20cf3648b49dc9c2eeef").listComments().toList();
assertThat("Two comments present", commitComments.size(), equalTo(2));
assertThat("Comment text found",
commitComments.stream().map(GHCommitComment::getBody).collect(Collectors.toList()),
containsInAnyOrder("comment 1", "comment 2"));
}
@Test // Issue #261
public void listEmptyContributors() throws IOException {
assertThat("This list should be empty, but should return a valid empty iterable.",
gitHub.getRepository(GITHUB_API_TEST_ORG + "/empty").listContributors(),
is(emptyIterable()));
}
@Test
public void searchRepositories() throws Exception {
PagedSearchIterable<GHRepository> r = gitHub.searchRepositories()
.q("tetris")
.language("assembly")
.sort(GHRepositorySearchBuilder.Sort.STARS)
.list();
GHRepository u = r.iterator().next();
// System.out.println(u.getName());
assertThat(u.getId(), notNullValue());
assertThat(u.getLanguage(), equalTo("Assembly"));
assertThat(r.getTotalCount(), greaterThan(0));
}
@Test
public void searchOrgForRepositories() throws Exception {
PagedSearchIterable<GHRepository> r = gitHub.searchRepositories().org("hub4j-test-org").list();
GHRepository u = r.iterator().next();
assertThat(u.getOwnerName(), equalTo("hub4j-test-org"));
assertThat(r.getTotalCount(), greaterThan(0));
}
@Test // issue #162
public void testIssue162() throws Exception {
GHRepository r = gitHub.getRepository("hub4j/github-api");
List<GHContent> contents = r.getDirectoryContent("", "gh-pages");
for (GHContent content : contents) {
if (content.isFile()) {
String content1 = content.getContent();
String content2 = r.getFileContent(content.getPath(), "gh-pages").getContent();
// System.out.println(content.getPath());
assertThat(content2, equalTo(content1));
}
}
}
@Test
public void markDown() throws Exception {
assertThat(IOUtils.toString(gitHub.renderMarkdown("**Test日本語**")).trim(),
equalTo("<p><strong>Test日本語</strong></p>"));
String actual = IOUtils.toString(
gitHub.getRepository("hub4j/github-api").renderMarkdown("@kohsuke to fix issue #1", MarkdownMode.GFM));
// System.out.println(actual);
assertThat(actual, containsString("href=\"https://github.com/kohsuke\""));
assertThat(actual, containsString("href=\"https://github.com/hub4j/github-api/pull/1\""));
assertThat(actual, containsString("class=\"user-mention\""));
assertThat(actual, containsString("class=\"issue-link "));
assertThat(actual, containsString("to fix issue"));
}
@Test
public void setMergeOptions() throws IOException {
// String repoName = "hub4j-test-org/test-mergeoptions";
GHRepository r = getTempRepository();
// at least one merge option must be selected
// flip all the values at least once
r.allowSquashMerge(true);
r.allowMergeCommit(false);
r.allowRebaseMerge(false);
r = gitHub.getRepository(r.getFullName());
assertThat(r.isAllowMergeCommit(), is(false));
assertThat(r.isAllowRebaseMerge(), is(false));
assertThat(r.isAllowSquashMerge(), is(true));
// flip the last value
r.allowMergeCommit(true);
r.allowRebaseMerge(true);
r.allowSquashMerge(false);
r = gitHub.getRepository(r.getFullName());
assertThat(r.isAllowMergeCommit(), is(true));
assertThat(r.isAllowRebaseMerge(), is(true));
assertThat(r.isAllowSquashMerge(), is(false));
}
@Test
public void getDeleteBranchOnMerge() throws IOException {
GHRepository r = getRepository();
assertThat(r.isDeleteBranchOnMerge(), notNullValue());
}
@Test
public void setDeleteBranchOnMerge() throws IOException {
GHRepository r = getRepository();
// enable auto delete
r.deleteBranchOnMerge(true);
r = gitHub.getRepository(r.getFullName());
assertThat(r.isDeleteBranchOnMerge(), is(true));
// flip the last value
r.deleteBranchOnMerge(false);
r = gitHub.getRepository(r.getFullName());
assertThat(r.isDeleteBranchOnMerge(), is(false));
}
@Test
public void testSetTopics() throws Exception {
GHRepository repo = getRepository(gitHub);
List<String> topics = new ArrayList<>();
topics.add("java");
topics.add("api-test-dummy");
repo.setTopics(topics);
assertThat("Topics retain input order (are not sort when stored)",
repo.listTopics(),
contains("java", "api-test-dummy"));
topics = new ArrayList<>();
topics.add("ordered-state");
topics.add("api-test-dummy");
topics.add("java");
repo.setTopics(topics);
assertThat("Topics behave as a set and retain order from previous calls",
repo.listTopics(),
contains("java", "api-test-dummy", "ordered-state"));
topics = new ArrayList<>();
topics.add("ordered-state");
topics.add("api-test-dummy");
repo.setTopics(topics);
assertThat("Topics retain order even when some are removed",
repo.listTopics(),
contains("api-test-dummy", "ordered-state"));
topics = new ArrayList<>();
repo.setTopics(topics);
assertThat("Topics can be set to empty", repo.listTopics(), is(empty()));
}
@Test
public void getCollaborators() throws Exception {
GHRepository repo = getRepository(gitHub);
GHPersonSet<GHUser> collaborators = repo.getCollaborators();
assertThat(collaborators.size(), greaterThan(0));
}
@Test
public void getPostCommitHooks() throws Exception {
GHRepository repo = getRepository(gitHub);
Set<URL> postcommitHooks = repo.getPostCommitHooks();
assertThat(postcommitHooks, is(empty()));
}
@Test
public void getRefs() throws Exception {
GHRepository repo = getTempRepository();
GHRef[] refs = repo.getRefs();
assertThat(refs, notNullValue());
assertThat(refs.length, equalTo(1));
assertThat(refs[0].getRef(), equalTo("refs/heads/main"));
}
@Test
public void getRefsHeads() throws Exception {
GHRepository repo = getTempRepository();
GHRef[] refs = repo.getRefs("heads");
assertThat(refs, notNullValue());
assertThat(refs.length, equalTo(1));
assertThat(refs[0].getRef(), equalTo("refs/heads/main"));
}
@Test
public void getRefsEmptyTags() throws Exception {
GHRepository repo = getTempRepository();
try {
repo.getRefs("tags");
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(),
containsString(
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
}
}
@Test
public void listRefs() throws Exception {
GHRepository repo = getRepository();
List<GHRef> ghRefs;
// handle refs/*
ghRefs = repo.listRefs("heads").toList();
List<GHRef> ghRefsWithPrefix = repo.listRefs("refs/heads").toList();
assertThat(ghRefs, notNullValue());
assertThat(ghRefs.size(), greaterThan(3));
assertThat(ghRefs.get(0).getRef(), equalTo("refs/heads/changes"));
assertThat(ghRefsWithPrefix.size(), equalTo(ghRefs.size()));
assertThat(ghRefsWithPrefix.get(0).getRef(), equalTo(ghRefs.get(0).getRef()));
// git/refs/heads/gh-pages
// passing a specific ref to listRefs will fail to parse due to returning a single item not an array
try {
ghRefs = repo.listRefs("heads/gh-pages").toList();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.getCause(), instanceOf(JsonMappingException.class));
}
// git/refs/heads/gh
ghRefs = repo.listRefs("heads/gh").toList();
assertThat(ghRefs, notNullValue());
assertThat(ghRefs.size(), equalTo(1));
assertThat(ghRefs.get(0).getRef(), equalTo("refs/heads/gh-pages"));
// git/refs/headz
try {
ghRefs = repo.listRefs("headz").toList();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(),
containsString(
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
}
}
@Test
public void getRef() throws Exception {
GHRepository repo = getRepository();
GHRef ghRef;
// handle refs/*
ghRef = repo.getRef("heads/gh-pages");
GHRef ghRefWithPrefix = repo.getRef("refs/heads/gh-pages");
assertThat(ghRef, notNullValue());
assertThat(ghRef.getRef(), equalTo("refs/heads/gh-pages"));
assertThat(ghRefWithPrefix.getRef(), equalTo(ghRef.getRef()));
assertThat(ghRefWithPrefix.getObject().getType(), equalTo("commit"));
assertThat(ghRefWithPrefix.getObject().getUrl().toString(),
containsString("/repos/hub4j-test-org/github-api/git/commits/"));
// git/refs/heads/gh-pages
ghRef = repo.getRef("heads/gh-pages");
assertThat(ghRef, notNullValue());
assertThat(ghRef.getRef(), equalTo("refs/heads/gh-pages"));
// git/refs/heads/gh
try {
ghRef = repo.getRef("heads/gh");
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(),
containsString(
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
}
// git/refs/headz
try {
ghRef = repo.getRef("headz");
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(),
containsString(
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
}
}
@Test
public void listRefsHeads() throws Exception {
GHRepository repo = getTempRepository();
List<GHRef> refs = repo.listRefs("heads").toList();
assertThat(refs, notNullValue());
assertThat(refs.size(), equalTo(1));
assertThat(refs.get(0).getRef(), equalTo("refs/heads/main"));
}
@Test
public void listRefsEmptyTags() throws Exception {
try {
GHRepository repo = getTempRepository();
repo.listRefs("tags").toList();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(), containsString("/repos/hub4j-test-org/temp-listRefsEmptyTags/git/refs/tags"));
}
}
@Test
public void listTagsEmpty() throws Exception {
GHRepository repo = getTempRepository();
List<GHTag> refs = repo.listTags().toList();
assertThat(refs, notNullValue());
assertThat(refs, is(empty()));
}
@Test
public void listTags() throws Exception {
GHRepository repo = getRepository();
List<GHTag> refs = repo.listTags().withPageSize(33).toList();
assertThat(refs, notNullValue());
assertThat(refs.size(), greaterThan(90));
}
@Test
public void checkWatchersCount() throws Exception {
snapshotNotAllowed();
GHRepository repo = getTempRepository();
int watchersCount = repo.getWatchersCount();
assertThat(watchersCount, equalTo(10));
}
@Test
public void checkStargazersCount() throws Exception {
snapshotNotAllowed();
GHRepository repo = getTempRepository();
int stargazersCount = repo.getStargazersCount();
assertThat(stargazersCount, equalTo(10));
}
@Test
public void listCollaborators() throws Exception {
GHRepository repo = getRepository();
List<GHUser> collaborators = repo.listCollaborators().toList();
assertThat(collaborators.size(), greaterThan(10));
}
@Test
public void listCollaboratorsFiltered() throws Exception {
GHRepository repo = getRepository();
List<GHUser> allCollaborators = repo.listCollaborators().toList();
List<GHUser> filteredCollaborators = repo.listCollaborators(GHRepository.CollaboratorAffiliation.OUTSIDE)
.toList();
assertThat(filteredCollaborators.size(), lessThan(allCollaborators.size()));
}
@Test
public void getCheckRuns() throws Exception {
final int expectedCount = 8;
// Use github-api repository as it has checks set up
PagedIterable<GHCheckRun> checkRuns = gitHub.getOrganization("hub4j")
.getRepository("github-api")
.getCheckRuns("78b9ff49d47daaa158eb373c4e2e040f739df8b9");
// Check if the paging works correctly
assertThat(checkRuns.withPageSize(2).iterator().nextPage(), hasSize(2));
// Check if the checkruns are all succeeded and if we got all of them
int checkRunsCount = 0;
for (GHCheckRun checkRun : checkRuns) {
assertThat(checkRun.getConclusion(), equalTo(Conclusion.SUCCESS));
checkRunsCount++;
}
assertThat(checkRunsCount, equalTo(expectedCount));
// Check that we can call update on the results
for (GHCheckRun checkRun : checkRuns) {
checkRun.update();
}
}
@Test
public void getLastCommitStatus() throws Exception {
GHCommitStatus status = getRepository().getLastCommitStatus("8051615eff597f4e49f4f47625e6fc2b49f26bfc");
assertThat(status.getId(), equalTo(9027542286L));
assertThat(status.getState(), equalTo(GHCommitState.SUCCESS));
assertThat(status.getContext(), equalTo("ci/circleci: build"));
}
@Test
public void listCommitsBetween() throws Exception {
GHRepository repository = getRepository();
int startingCount = mockGitHub.getRequestCount();
GHCompare compare = repository.getCompare("e46a9f3f2ac55db96de3c5c4706f2813b3a96465",
"8051615eff597f4e49f4f47625e6fc2b49f26bfc");
int actualCount = 0;
for (GHCompare.Commit item : compare.listCommits().withPageSize(5)) {
assertThat(item, notNullValue());
actualCount++;
}
assertThat(compare.getTotalCommits(), is(9));
assertThat(actualCount, is(9));
assertThat(mockGitHub.getRequestCount(), equalTo(startingCount + 1));
}
@Test
public void listCommitsBetweenPaginated() throws Exception {
GHRepository repository = getRepository();
int startingCount = mockGitHub.getRequestCount();
repository.setCompareUsePaginatedCommits(true);
GHCompare compare = repository.getCompare("e46a9f3f2ac55db96de3c5c4706f2813b3a96465",
"8051615eff597f4e49f4f47625e6fc2b49f26bfc");
int actualCount = 0;
for (GHCompare.Commit item : compare.listCommits().withPageSize(5)) {
assertThat(item, notNullValue());
actualCount++;
}
assertThat(compare.getTotalCommits(), is(9));
assertThat(actualCount, is(9));
assertThat(mockGitHub.getRequestCount(), equalTo(startingCount + 3));
}