-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGHRepositoryTest.java
More file actions
2022 lines (1802 loc) · 73 KB
/
GHRepositoryTest.java
File metadata and controls
2022 lines (1802 loc) · 73 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.kohsuke.github;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Sets;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
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.time.Instant;
import java.time.LocalDate;
import java.util.*;
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.GPGVERIFY_ERROR;
import static org.kohsuke.github.GHVerification.Reason.UNKNOWN_SIGNATURE_TYPE;
// TODO: Auto-generated Javadoc
/**
* The Class GHRepositoryTest.
*
* @author Liam Newman
*/
public class GHRepositoryTest extends AbstractGitHubWireMockTest {
/**
* Create default GHRepositoryTest instance
*/
public GHRepositoryTest() {
}
/**
* Latest repository exist.
*/
@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();
}
}
/**
* Latest repository not exist.
*/
@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();
}
}
/**
* Adds the collaborators.
*
* @throws Exception
* the exception
*/
@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, RepositoryRole.from(GHOrganization.Permission.PUSH));
GHPersonSet<GHUser> collabs = repo.getCollaborators();
GHUser colabUser = collabs.byLogin("jimmysombrero");
assertThat(colabUser.getAvatarUrl(), equalTo("https://avatars3.githubusercontent.com/u/12157727?v=4"));
assertThat(colabUser.getHtmlUrl().toString(), equalTo("https://github.com/jimmysombrero"));
assertThat(colabUser.getLocation(), nullValue());
assertThat(user.getName(), equalTo(colabUser.getName()));
}
/**
* Adds the collaborators repo perm.
*
* @throws Exception
* the exception
*/
@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()));
}
/**
* Archive.
*
* @throws Exception
* the exception
*/
@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 demoing the issue with a user having the maintain permission on a repository.
*
* Test checking the permission fallback mechanism in case the Github API changes. The test was recorded at a time a
* new permission was added by mistake. If a re-recording it is needed, you'll like have to manually edit the
* generated mocks to get a non existing permission See
* https://github.com/hub4j/github-api/issues/1671#issuecomment-1577515662 for the details.
*
* @throws IOException
* the exception
*/
@Test
public void cannotRetrievePermissionMaintainUser() throws IOException {
GHRepository r = gitHub.getRepository("hub4j-test-org/maintain-permission-issue");
GHPermissionType permission = r.getPermission("alecharp");
assertThat(permission.toString(), is("UNKNOWN"));
}
/**
* Check stargazers count.
*
* @throws Exception
* the exception
*/
@Test
public void checkStargazersCount() throws Exception {
snapshotNotAllowed();
GHRepository repo = getTempRepository();
int stargazersCount = repo.getStargazersCount();
assertThat(stargazersCount, equalTo(10));
}
/**
* Check watchers count.
*
* @throws Exception
* the exception
*/
@Test
public void checkWatchersCount() throws Exception {
snapshotNotAllowed();
GHRepository repo = getTempRepository();
int watchersCount = repo.getWatchersCount();
assertThat(watchersCount, equalTo(10));
}
/**
* Creates the dispatch event with client payload.
*
* @throws Exception
* the exception
*/
@Test
public void createDispatchEventWithClientPayload() throws Exception {
GHRepository repository = getTempRepository();
Map<String, Object> clientPayload = new HashMap<>();
clientPayload.put("name", "joe.doe");
clientPayload.put("list", new ArrayList<>());
repository.dispatch("test", clientPayload);
}
/**
* Creates the dispatch event without client payload.
*
* @throws Exception
* the exception
*/
@Test
public void createDispatchEventWithoutClientPayload() throws Exception {
GHRepository repository = getTempRepository();
repository.dispatch("test", null);
}
/**
* Creates the secret.
*
* @throws Exception
* the exception
*/
@Test
public void createSecret() throws Exception {
GHRepository repo = getTempRepository();
repo.createSecret("secret", "encrypted", "public");
}
/**
* Creates the signed commit unknown signature type.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@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));
}
/**
* Creates the signed commit verify error.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@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));
}
/**
* Gets the branch non existent but 200 status.
*
* @throws Exception
* the exception
*/
// 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"));
}
}
/**
* Gets the branch URL encoded.
*
* @throws Exception
* the exception
*/
@Test
public void getBranch_URLEncoded() throws Exception {
GHRepository repo = getRepository();
GHBranch branch = repo.getBranch("test/#UrlEncode");
assertThat(branch.getName(), is("test/#UrlEncode"));
}
/**
* Gets the check runs.
*
* @throws Exception
* the exception
*/
@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();
}
}
/**
* Filter out the checks from a reference
*
* @throws Exception
* the exception
*/
@Test
public void getCheckRunsWithParams() throws Exception {
final int expectedCount = 1;
// Use github-api repository as it has checks set up
final Map<String, Object> params = new HashMap<>(1);
params.put("check_name", "build-only (Java 17)");
PagedIterable<GHCheckRun> checkRuns = gitHub.getOrganization("hub4j")
.getRepository("github-api")
.getCheckRuns("54d60fbb53b4efa19f3081417bfb6a1de30c55e4", params);
// 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));
}
/**
* Gets the collaborators.
*
* @throws Exception
* the exception
*/
@Test
public void getCollaborators() throws Exception {
GHRepository repo = getRepository(gitHub);
GHPersonSet<GHUser> collaborators = repo.getCollaborators();
assertThat(collaborators.size(), greaterThan(0));
}
/**
* Gets the commits between over 250.
*
* @throws Exception
* the exception
*/
@Test
public void getCommitsBetweenOver250() throws Exception {
GHRepository repository = getRepository();
int startingCount = mockGitHub.getRequestCount();
GHCompare compare = repository.getCompare("4261c42949915816a9f246eb14c3dfd21a637bc2",
"94ff089e60064bfa43e374baeb10846f7ce82f40");
int actualCount = 0;
for (GHCompare.Commit item : compare.getCommits()) {
assertThat(item, notNullValue());
actualCount++;
}
assertThat(compare.getTotalCommits(), is(283));
assertThat(actualCount, is(250));
assertThat(mockGitHub.getRequestCount(), equalTo(startingCount + 1));
// Additional GHCompare checks
assertThat(compare.getAheadBy(), equalTo(283));
assertThat(compare.getBehindBy(), equalTo(0));
assertThat(compare.getStatus(), equalTo(GHCompare.Status.ahead));
assertThat(compare.getDiffUrl().toString(),
endsWith(
"compare/4261c42949915816a9f246eb14c3dfd21a637bc2...94ff089e60064bfa43e374baeb10846f7ce82f40.diff"));
assertThat(compare.getHtmlUrl().toString(),
endsWith(
"compare/4261c42949915816a9f246eb14c3dfd21a637bc2...94ff089e60064bfa43e374baeb10846f7ce82f40"));
assertThat(compare.getPatchUrl().toString(),
endsWith(
"compare/4261c42949915816a9f246eb14c3dfd21a637bc2...94ff089e60064bfa43e374baeb10846f7ce82f40.patch"));
assertThat(compare.getPermalinkUrl().toString(),
endsWith("compare/hub4j-test-org:4261c42...hub4j-test-org:94ff089"));
assertThat(compare.getUrl().toString(),
endsWith(
"compare/4261c42949915816a9f246eb14c3dfd21a637bc2...94ff089e60064bfa43e374baeb10846f7ce82f40"));
assertThat(compare.getBaseCommit().getSHA1(), equalTo("4261c42949915816a9f246eb14c3dfd21a637bc2"));
assertThat(compare.getMergeBaseCommit().getSHA1(), equalTo("4261c42949915816a9f246eb14c3dfd21a637bc2"));
// it appears this field is not present in the returned JSON. Strange.
assertThat(compare.getMergeBaseCommit().getCommit().getSha(), nullValue());
assertThat(compare.getMergeBaseCommit().getCommit().getUrl(),
endsWith("/commits/4261c42949915816a9f246eb14c3dfd21a637bc2"));
assertThat(compare.getMergeBaseCommit().getCommit().getMessage(),
endsWith("[maven-release-plugin] prepare release github-api-1.123"));
assertThat(compare.getMergeBaseCommit().getCommit().getAuthor().getName(), equalTo("Liam Newman"));
assertThat(compare.getMergeBaseCommit().getCommit().getCommitter().getName(), equalTo("Liam Newman"));
assertThat(compare.getMergeBaseCommit().getCommit().getTree().getSha(),
equalTo("5da98090976978c93aba0bdfa550e05675543f99"));
assertThat(compare.getMergeBaseCommit().getCommit().getTree().getUrl(),
endsWith("/git/trees/5da98090976978c93aba0bdfa550e05675543f99"));
assertThat(compare.getFiles().length, equalTo(300));
assertThat(compare.getFiles()[0].getFileName(), equalTo(".github/PULL_REQUEST_TEMPLATE.md"));
assertThat(compare.getFiles()[0].getLinesAdded(), equalTo(8));
assertThat(compare.getFiles()[0].getLinesChanged(), equalTo(15));
assertThat(compare.getFiles()[0].getLinesDeleted(), equalTo(7));
assertThat(compare.getFiles()[0].getFileName(), equalTo(".github/PULL_REQUEST_TEMPLATE.md"));
assertThat(compare.getFiles()[0].getPatch(), startsWith("@@ -1,15 +1,16 @@"));
assertThat(compare.getFiles()[0].getPreviousFilename(), nullValue());
assertThat(compare.getFiles()[0].getStatus(), equalTo("modified"));
assertThat(compare.getFiles()[0].getSha(), equalTo("e4234f5f6f39899282a6ef1edff343ae1269222e"));
assertThat(compare.getFiles()[0].getBlobUrl().toString(),
endsWith("/blob/94ff089e60064bfa43e374baeb10846f7ce82f40/.github/PULL_REQUEST_TEMPLATE.md"));
assertThat(compare.getFiles()[0].getRawUrl().toString(),
endsWith("/raw/94ff089e60064bfa43e374baeb10846f7ce82f40/.github/PULL_REQUEST_TEMPLATE.md"));
}
/**
* Gets the commits between paged.
*
* @throws Exception
* the exception
*/
@Test
public void getCommitsBetweenPaged() throws Exception {
GHRepository repository = getRepository();
int startingCount = mockGitHub.getRequestCount();
repository.setCompareUsePaginatedCommits(true);
GHCompare compare = repository.getCompare("4261c42949915816a9f246eb14c3dfd21a637bc2",
"94ff089e60064bfa43e374baeb10846f7ce82f40");
int actualCount = 0;
for (GHCompare.Commit item : compare.getCommits()) {
assertThat(item, notNullValue());
actualCount++;
}
assertThat(compare.getTotalCommits(), is(283));
assertThat(actualCount, is(283));
assertThat(mockGitHub.getRequestCount(), equalTo(startingCount + 4));
}
/**
* Gets the delete branch on merge.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getDeleteBranchOnMerge() throws IOException {
GHRepository r = getRepository();
assertThat(r.isDeleteBranchOnMerge(), notNullValue());
}
/**
* Gets the last commit status.
*
* @throws Exception
* the exception
*/
@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"));
}
/**
* Gets the permission.
*
* @throws Exception
* the exception
*/
@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
}
}
}
/**
* Gets the post commit hooks.
*
* @throws Exception
* the exception
*/
@Test
public void getPostCommitHooks() throws Exception {
GHRepository repo = getRepository(gitHub);
Set<URL> postcommitHooks = setupPostCommitHooks(repo);
assertThat(postcommitHooks, is(empty()));
}
/**
* Gets the public key.
*
* @throws Exception
* the exception
*/
@Test
public void getPublicKey() throws Exception {
GHRepository repo = getTempRepository();
GHRepositoryPublicKey publicKey = repo.getPublicKey();
assertThat(publicKey, notNullValue());
assertThat(publicKey.getKey(), equalTo("test-key"));
assertThat(publicKey.getKeyId(), equalTo("key-id"));
}
/**
* Gets the ref.
*
* @throws Exception
* the exception
*/
@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\"}"));
}
}
/**
* Gets the refs.
*
* @throws Exception
* the exception
*/
@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"));
}
/**
* Gets the refs empty tags.
*
* @throws Exception
* the exception
*/
@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\"}"));
}
}
/**
* Gets the refs heads.
*
* @throws Exception
* the exception
*/
@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"));
}
/**
* Gets the release by tag name does not exist.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getReleaseByTagNameDoesNotExist() throws IOException {
GHRelease release = getRepository().getReleaseByTagName("foo-bar-baz");
assertThat(release, nullValue());
}
/**
* Gets the release by tag name exists.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@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"));
}
/**
* Gets the release does not exist.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getReleaseDoesNotExist() throws IOException {
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(Long.MAX_VALUE);
assertThat(release, nullValue());
}
/**
* Gets the release exists.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getReleaseExists() throws IOException {
GHRelease release = gitHub.getOrganization("github").getRepository("hub").getRelease(6839710);
assertThat(release.getTagName(), equalTo("v2.3.0-pre10"));
}
/**
* Gh repository search builder fork default reset forks search terms.
*/
@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));
}
/**
* Gh repository search builder ignores unknown visibility.
*/
@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));
}
/**
* Checks for permission.
*
* @throws Exception
* the exception
*/
@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));
}
/**
* Checks if is disabled.
*
* @throws Exception
* the exception
*/
@Test
public void isDisabled() throws Exception {
GHRepository r = getRepository();
assertThat(r.isDisabled(), is(false));
}
/**
* Checks if is disabled true.
*
* @throws Exception
* the exception
*/
@Test
public void isDisabledTrue() throws Exception {
GHRepository r = getRepository();
assertThat(r.isDisabled(), is(true));
}
/**
* List collaborators.
*
* @throws Exception
* the exception
*/
@Test
public void listCollaborators() throws Exception {
GHRepository repo = getRepository();
List<GHUser> collaborators = repo.listCollaborators().toList();
assertThat(collaborators.size(), greaterThan(10));
}
/**
* List collaborators filtered.
*
* @throws Exception
* the exception
*/
@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()));
}
/**
* List commit comments no comments.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@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());
}
/**
* List commit comments some comments.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@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"));
}
/**
* List commits between.
*
* @throws Exception
* the exception
*/
@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));
}
/**
* List commits between paginated.
*
* @throws Exception
* the exception
*/
@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));
}
/**
* List contributors.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@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));
}
/**
* List contributors.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void listContributorsAnon() throws IOException {
GHRepository r = gitHub.getOrganization("hub4j").getRepository("github-api");
int i = 0;
boolean kohsuke = false;
for (GHRepository.Contributor c : r.listContributors(true)) {
if (c.getType().equals("Anonymous")) {
assertThat(c.getContributions(), is(3));
kohsuke = true;
}
if (++i > 1) {
break;
}
}
assertThat(kohsuke, is(true));
}
/**
* List empty contributors.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@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()));
}
/**
* List languages.
*
* @throws IOException
* Signals that an I/O exception has occurred.