-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathrepos_service.dart
1295 lines (1205 loc) · 45.5 KB
/
repos_service.dart
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
import 'dart:async';
import 'dart:convert';
import 'package:github/src/common.dart';
import 'package:github/src/common/model/repos_releases.dart';
import 'package:github/src/common/model/users.dart';
import 'package:github/src/common/util/pagination.dart';
import 'package:github/src/common/util/utils.dart';
import 'package:http/http.dart' as http;
/// The [RepositoriesService] handles communication with repository related
/// methods of the GitHub API.
///
/// API docs: https://developer.github.com/v3/repos/
class RepositoriesService extends Service {
RepositoriesService(GitHub github) : super(github);
/// Lists the repositories of the currently authenticated user.
///
/// API docs: https://developer.github.com/v3/repos/#list-your-repositories
Stream<Repository> listRepositories(
{String type = 'owner',
String sort = 'full_name',
String direction = 'asc'}) {
final params = <String, dynamic>{
'type': type,
'sort': sort,
'direction': direction,
};
return PaginationHelper(github).objects<Map<String, dynamic>, Repository>(
'GET',
'/user/repos',
(i) => Repository.fromJson(i),
params: params,
);
}
/// Lists the repositories of the user specified by [user] in a streamed fashion.
///
/// API docs: https://developer.github.com/v3/repos/#list-repositories-for-a-user
Stream<Repository> listUserRepositories(String user,
{String type = 'owner',
String sort = 'full_name',
String direction = 'asc'}) {
ArgumentError.checkNotNull(user);
final params = <String, dynamic>{
'type': type,
'sort': sort,
'direction': direction
};
return PaginationHelper(github).objects<Map<String, dynamic>, Repository>(
'GET',
'/users/$user/repos',
(i) => Repository.fromJson(i),
params: params,
);
}
/// List repositories for the specified [org].
///
/// API docs: https://developer.github.com/v3/repos/#list-organization-repositories
Stream<Repository> listOrganizationRepositories(String org,
{String type = 'all'}) {
ArgumentError.checkNotNull(org);
final params = <String, dynamic>{'type': type};
return PaginationHelper(github).objects<Map<String, dynamic>, Repository>(
'GET',
'/orgs/$org/repos',
(i) => Repository.fromJson(i),
params: params,
);
}
/// Lists all the public repositories on GitHub, in the order that they were
/// created.
///
/// If [limit] is not null, it is used to specify the amount of repositories to fetch.
/// If [limit] is null, it will fetch ALL the repositories on GitHub.
///
/// API docs: https://developer.github.com/v3/repos/#list-all-public-repositories
Stream<Repository> listPublicRepositories({int limit = 50, DateTime? since}) {
final params = <String, dynamic>{};
if (since != null) {
params['since'] = since.toIso8601String();
}
final pages = (limit / 30).ceil();
return PaginationHelper(github)
.fetchStreamed('GET', '/repositories', pages: pages, params: params)
.expand<Repository>((http.Response response) {
final list = jsonDecode(response.body) as List<Map<String, dynamic>>;
return list.map((Map<String, dynamic> it) => Repository.fromJson(it));
});
}
/// Creates a repository with [repository]. If an [org] is specified, the new
/// repository will be created under that organization. If no [org] is
/// specified, it will be created for the authenticated user.
///
/// API docs: https://developer.github.com/v3/repos/#create
Future<Repository> createRepository(CreateRepository repository,
{String? org}) async {
ArgumentError.checkNotNull(repository);
if (org != null) {
return github.postJSON<Map<String, dynamic>, Repository>(
'/orgs/$org/repos',
body: GitHubJson.encode(repository),
convert: (i) => Repository.fromJson(i),
);
} else {
return github.postJSON<Map<String, dynamic>, Repository>(
'/user/repos',
body: GitHubJson.encode(repository),
convert: (i) => Repository.fromJson(i),
);
}
}
Future<LicenseDetails> getLicense(RepositorySlug slug) async {
ArgumentError.checkNotNull(slug);
return github.getJSON<Map<String, dynamic>, LicenseDetails>(
'/repos/${slug.owner}/${slug.name}/license',
convert: (json) => LicenseDetails.fromJson(json),
);
}
/// Fetches the repository specified by the [slug].
///
/// API docs: https://developer.github.com/v3/repos/#get
Future<Repository> getRepository(RepositorySlug slug) async {
ArgumentError.checkNotNull(slug);
return github.getJSON<Map<String, dynamic>, Repository>(
'/repos/${slug.owner}/${slug.name}',
convert: (i) => Repository.fromJson(i),
statusCode: StatusCodes.OK,
fail: (http.Response response) {
if (response.statusCode == 404) {
throw RepositoryNotFound(github, slug.fullName);
}
},
);
}
/// Fetches a list of repositories specified by [slugs].
Stream<Repository> getRepositories(List<RepositorySlug> slugs) async* {
for (final slug in slugs) {
final repo = await getRepository(slug);
yield repo;
}
}
/// Edit a Repository.
///
/// API docs: https://developer.github.com/v3/repos/#edit
Future<Repository> editRepository(RepositorySlug slug,
{String? name,
String? description,
String? homepage,
bool? private,
bool? hasIssues,
bool? hasWiki,
bool? hasDownloads}) async {
ArgumentError.checkNotNull(slug);
final data = createNonNullMap({
'name': name!,
'description': description!,
'homepage': homepage!,
'private': private!,
'has_issues': hasIssues!,
'has_wiki': hasWiki!,
'has_downloads': hasDownloads!,
'default_branch': 'defaultBranch'
});
return github.postJSON(
'/repos/${slug.fullName}',
body: GitHubJson.encode(data),
statusCode: 200,
);
}
/// Deletes a repository.
///
/// Returns true if it was successfully deleted.
///
/// API docs: https://developer.github.com/v3/repos/#delete-a-repository
Future<bool> deleteRepository(RepositorySlug slug) async {
ArgumentError.checkNotNull(slug);
return github
.request(
'DELETE',
'/repos/${slug.fullName}',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
/// Lists the contributors of the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/#list-contributors
Stream<Contributor> listContributors(RepositorySlug slug,
{bool anon = false}) {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(anon);
return PaginationHelper(github).objects<Map<String, dynamic>, Contributor>(
'GET',
'/repos/${slug.fullName}/contributors',
(i) => Contributor.fromJson(i),
params: <String, dynamic>{'anon': anon.toString()},
);
}
/// Lists the teams of the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/#list-teams
Stream<Team> listTeams(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, Team>(
'GET',
'/repos/${slug.fullName}/teams',
(i) => Team.fromJson(i),
);
}
/// Gets a language breakdown for the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/#list-languages
Future<LanguageBreakdown> listLanguages(RepositorySlug slug) async {
ArgumentError.checkNotNull(slug);
return github.getJSON<Map<String, dynamic>, LanguageBreakdown>(
'/repos/${slug.fullName}/languages',
statusCode: StatusCodes.OK,
convert: (input) => LanguageBreakdown(input.cast<String, int>()),
);
}
/// Lists the tags of the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/#list-tags
Stream<Tag> listTags(RepositorySlug slug,
{int page = 1, int? pages, int perPage = 30}) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, Tag>(
'GET', '/repos/${slug.fullName}/tags', (i) => Tag.fromJson(i),
pages: pages, params: {'page': page, 'per_page': perPage});
}
/// Lists the branches of the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/#list-branches
Stream<Branch> listBranches(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, Branch>(
'GET',
'/repos/${slug.fullName}/branches',
(i) => Branch.fromJson(i),
);
}
/// Fetches the specified branch.
///
/// API docs: https://developer.github.com/v3/repos/#get-branch
Future<Branch> getBranch(RepositorySlug slug, String branch) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(branch);
return github.getJSON<Map<String, dynamic>, Branch>(
'/repos/${slug.fullName}/branches/$branch',
convert: (i) => Branch.fromJson(i),
);
}
/// Lists the users that have access to the repository identified by [slug].
///
/// API docs: https://developer.github.com/v3/repos/collaborators/#list
Stream<Collaborator> listCollaborators(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, Collaborator>(
'GET',
'/repos/${slug.fullName}/collaborators',
(json) => Collaborator.fromJson(json),
);
}
Future<bool> isCollaborator(RepositorySlug slug, String user) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(user);
var catchError = false;
http.Response response;
try {
response = await github.request(
'GET',
'/repos/${slug.fullName}/collaborators/$user',
statusCode: StatusCodes.NO_CONTENT,
fail: (response) {
if (response.statusCode == StatusCodes.NOT_FOUND) {
catchError = true;
}
},
);
if (response.statusCode == StatusCodes.NO_CONTENT) {
return true;
}
} catch (e) {
if (!catchError) {
rethrow;
}
}
return false;
}
Future<bool> addCollaborator(RepositorySlug slug, String user) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(user);
return github
.request(
'PUT',
'/repos/${slug.fullName}/collaborators/$user',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
Future<bool> removeCollaborator(RepositorySlug slug, String user) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(user);
return github
.request(
'DELETE',
'/repos/${slug.fullName}/collaborators/$user',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
/// Returns a list of all comments for a specific commit.
///
/// https://developer.github.com/v3/repos/comments/#list-comments-for-a-single-commit
Stream<CommitComment> listSingleCommitComments(
RepositorySlug slug,
RepositoryCommit commit,
) {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(commit);
return PaginationHelper(github)
.objects<Map<String, dynamic>, CommitComment>(
'GET',
'/repos/${slug.fullName}/commits/${commit.sha}/comments',
(i) => CommitComment.fromJson(i),
statusCode: StatusCodes.OK,
);
}
/// Returns a list of all commit comments in a repository.
///
/// https://developer.github.com/v3/repos/comments/#list-commit-comments-for-a-repository
Stream<CommitComment> listCommitComments(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github)
.objects<Map<String, dynamic>, CommitComment>(
'GET',
'repos/${slug.fullName}/comments',
(i) => CommitComment.fromJson(i),
statusCode: StatusCodes.OK,
);
}
/// Create a comment for a commit using its sha.
/// * [body]: The contents of the comment.
/// * [path]: Relative path of the file to comment on.
/// * [position]: Line index in the diff to comment on.
/// * [line]: **Deprecated**. Use position parameter instead. Line number in the file to comment on.
///
/// https://developer.github.com/v3/repos/comments/#create-a-commit-comment
Future<CommitComment> createCommitComment(
RepositorySlug slug,
RepositoryCommit commit, {
required String body,
String? path,
int? position,
@Deprecated('Use position parameter instead') int? line,
}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(commit);
final data = createNonNullMap({
'body': body,
'path': path!,
'position': position!,
'line': line!,
});
return github.postJSON<Map<String, dynamic>, CommitComment>(
'/repos/${slug.fullName}/commits/${commit.sha}/comments',
body: GitHubJson.encode(data),
statusCode: StatusCodes.CREATED,
convert: (i) => CommitComment.fromJson(i),
);
}
/// Retrieve a commit comment by its id.
///
/// https://developer.github.com/v3/repos/comments/#get-a-single-commit-comment
Future<CommitComment> getCommitComment(RepositorySlug slug,
{required int id}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
return github.getJSON<Map<String, dynamic>, CommitComment>(
'/repos/${slug.fullName}/comments/$id',
statusCode: StatusCodes.OK,
convert: (i) => CommitComment.fromJson(i),
);
}
/// Update a commit comment
/// * [id]: id of the comment to update.
/// * [body]: new body of the comment.
///
/// Returns the updated commit comment.
///
/// https://developer.github.com/v3/repos/comments/#update-a-commit-comment
Future<CommitComment> updateCommitComment(RepositorySlug slug,
{required int id, required String body}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
ArgumentError.checkNotNull(body);
return github.postJSON<Map<String, dynamic>, CommitComment>(
'/repos/${slug.fullName}/comments/$id',
body: GitHubJson.encode(createNonNullMap({'body': body})),
statusCode: StatusCodes.OK,
convert: (i) => CommitComment.fromJson(i),
);
}
/// Delete a commit comment.
/// *[id]: id of the comment to delete.
///
/// https://developer.github.com/v3/repos/comments/#delete-a-commit-comment
Future<bool> deleteCommitComment(RepositorySlug slug,
{required int id}) async {
ArgumentError.checkNotNull(slug);
return github
.request(
'DELETE',
'/repos/${slug.fullName}/comments/$id',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
/// Lists the commits of the provided repository [slug].
///
/// API docs: https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository
Stream<RepositoryCommit> listCommits(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github)
.objects<Map<String, dynamic>, RepositoryCommit>(
'GET',
'/repos/${slug.fullName}/commits',
(i) => RepositoryCommit.fromJson(i),
);
}
/// Fetches the specified commit.
///
/// API docs: https://developer.github.com/v3/repos/commits/#get-a-single-commit
Future<RepositoryCommit> getCommit(RepositorySlug slug, String sha) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(sha);
return github.getJSON<Map<String, dynamic>, RepositoryCommit>(
'/repos/${slug.fullName}/commits/$sha',
convert: (i) => RepositoryCommit.fromJson(i),
statusCode: StatusCodes.OK,
);
}
Future<String> getCommitDiff(RepositorySlug slug, String sha) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(sha);
return github
.request(
'GET',
'/repos/${slug.fullName}/commits/$sha',
headers: <String, String>{
'Accept': 'application/vnd.github.VERSION.diff'
},
statusCode: StatusCodes.OK,
)
.then((r) => r.body);
}
/// [refBase] and [refHead] can be the same value for a branch, commit, or ref
/// in [slug] or specify other repositories by using `repo:ref` syntax.
///
/// API docs: https://developer.github.com/v3/repos/commits/#compare-two-commits
Future<GitHubComparison> compareCommits(
RepositorySlug slug,
String refBase,
String refHead,
) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(refBase);
ArgumentError.checkNotNull(refHead);
return github.getJSON<Map<String, dynamic>, GitHubComparison>(
'/repos/${slug.fullName}/compare/$refBase...$refHead',
convert: (j) => GitHubComparison.fromJson(j),
);
}
/// Fetches the readme file for a repository.
///
/// The name of the commit/branch/tag may be specified with [ref]. If no [ref]
/// is defined, the repository's default branch is used (usually master).
///
/// API docs: https://developer.github.com/v3/repos/contents/#get-the-readme
Future<GitHubFile> getReadme(RepositorySlug slug, {String? ref}) async {
ArgumentError.checkNotNull(slug);
final headers = <String, String>{};
var url = '/repos/${slug.fullName}/readme';
if (ref != null) {
url += '?ref=$ref';
}
return github.getJSON(url, headers: headers, statusCode: StatusCodes.OK,
fail: (http.Response response) {
if (response.statusCode == StatusCodes.NOT_FOUND) {
throw NotFound(github, response.body);
}
}, convert: (Map<String, dynamic> input) {
var file = GitHubFile.fromJson(input);
file.sourceRepository = slug;
return file;
});
}
/// Fetches content in a repository at the specified [path].
///
/// When the [path] references a file, the returned [RepositoryContents]
/// contains the metadata AND content of a single file.
///
/// When the [path] references a directory, the returned [RepositoryContents]
/// contains the metadata of all the files and/or subdirectories.
///
/// Use [RepositoryContents.isFile] or [RepositoryContents.isDirectory] to
/// distinguish between both result types.
///
/// The name of the commit/branch/tag may be specified with [ref]. If no [ref]
/// is defined, the repository's default branch is used (usually master).
///
/// API docs: https://developer.github.com/v3/repos/contents/#get-contents
Future<RepositoryContents> getContents(RepositorySlug slug, String path,
{String? ref}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(path);
var url = '/repos/${slug.fullName}/contents/$path';
if (ref != null) {
url += '?ref=$ref';
}
return github.getJSON(
url,
convert: (dynamic input) {
final contents = RepositoryContents();
if (input is Map) {
// Weird one-off. If the content of `input` is JSON w/ a message
// it was likely a 404 – but we don't have the status code here
// But we can guess an the JSON content
if (input.containsKey('message')) {
throw GitHubError(github, input['message'],
apiUrl: input['documentation_url']);
}
contents.file = GitHubFile.fromJson(input as Map<String, dynamic>);
} else {
contents.tree =
(input as List).map((it) => GitHubFile.fromJson(it)).toList();
}
return contents;
},
);
}
/// Creates a new file in a repository.
///
/// API docs: https://developer.github.com/v3/repos/contents/#create-a-file
Future<ContentCreation> createFile(
RepositorySlug slug, CreateFile file) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(file);
final response = await github.request(
'PUT',
'/repos/${slug.fullName}/contents/${file.path}',
body: GitHubJson.encode(file),
);
return ContentCreation.fromJson(
jsonDecode(response.body) as Map<String, dynamic>);
}
/// Updates the specified file.
///
/// API docs: https://developer.github.com/v3/repos/contents/#update-a-file
Future<ContentCreation> updateFile(RepositorySlug slug, String path,
String message, String content, String sha,
{String? branch}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(path);
final map = createNonNullMap({
'message': message,
'content': content,
'sha': sha,
'branch': branch!,
});
final response = await github.request(
'PUT',
'/repos/${slug.fullName}/contents/$path',
body: GitHubJson.encode(map),
);
return ContentCreation.fromJson(
jsonDecode(response.body) as Map<String, dynamic>);
}
/// Deletes the specified file.
///
/// API docs: https://developer.github.com/v3/repos/contents/#delete-a-file
Future<ContentCreation> deleteFile(RepositorySlug slug, String path,
String message, String sha, String branch) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(path);
final map =
createNonNullMap({'message': message, 'sha': sha, 'branch': branch});
final response = await github.request(
'DELETE',
'/repos/${slug.fullName}/contents/$path',
body: GitHubJson.encode(map),
statusCode: StatusCodes.OK,
);
return ContentCreation.fromJson(
jsonDecode(response.body) as Map<String, dynamic>);
}
/// Gets an archive link for the specified repository and reference.
///
/// API docs: https://developer.github.com/v3/repos/contents/#get-archive-link
Future<String?> getArchiveLink(RepositorySlug slug, String ref,
{String format = 'tarball'}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(ref);
ArgumentError.checkNotNull(format);
final response = await github.request(
'GET',
'/repos/${slug.fullName}/$format/$ref',
statusCode: StatusCodes.FOUND,
);
return response.headers['Location'];
}
/// Lists the forks of the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/forks/#list-forks
Stream<Repository> listForks(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, Repository>(
'GET',
'/repos/${slug.fullName}/forks',
(i) => Repository.fromJson(i),
);
}
/// Creates a fork for the authenticated user.
///
/// API docs: https://developer.github.com/v3/repos/forks/#create-a-fork
Future<Repository> createFork(RepositorySlug slug, [CreateFork? fork]) async {
ArgumentError.checkNotNull(slug);
fork ??= CreateFork();
return github.postJSON<Map<String, dynamic>, Repository>(
'/repos/${slug.fullName}/forks',
body: GitHubJson.encode(fork),
convert: (i) => Repository.fromJson(i),
);
}
/// Lists the hooks of the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/hooks/#list-hooks
Stream<Hook> listHooks(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, Hook>(
'GET',
'/repos/${slug.fullName}/hooks',
(i) => Hook.fromJson(i)..repoName = slug.fullName,
);
}
/// Fetches a single hook by [id].
///
/// API docs: https://developer.github.com/v3/repos/hooks/#get-single-hook
Future<Hook> getHook(RepositorySlug slug, int id) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
return github.getJSON<Map<String, dynamic>, Hook>(
'/repos/${slug.fullName}/hooks/$id',
convert: (i) => Hook.fromJson(i)..repoName = slug.fullName,
);
}
/// Creates a repository hook based on the specified [hook].
///
/// API docs: https://developer.github.com/v3/repos/hooks/#create-a-hook
Future<Hook> createHook(RepositorySlug slug, CreateHook hook) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(hook);
return github.postJSON<Map<String, dynamic>, Hook>(
'/repos/${slug.fullName}/hooks',
convert: (i) => Hook.fromJson(i)..repoName = slug.fullName,
body: GitHubJson.encode(hook),
);
}
/// Edits a hook.
/// * [configUrl]: The URL to which the payloads will be delivered.
/// * [configContentType]: The media type used to serialize the payloads. Supported values include json and form. The default is form.
/// * [configSecret]: If provided, the secret will be used as the key to generate the HMAC hex digest value in the X-Hub-Signature header.
/// * [configInsecureSsl]: Determines whether the SSL certificate of the host for url will be verified when delivering payloads. We strongly recommend not setting this to true as you are subject to man-in-the-middle and other attacks.
/// * [events]: Determines what events the hook is triggered for. This replaces the entire array of events. Default: ['push'].
/// * [addEvents]: Determines a list of events to be added to the list of events that the Hook triggers for.
/// * [removeEvents]: Determines a list of events to be removed from the list of events that the Hook triggers for.
/// * [active]: Determines if notifications are sent when the webhook is triggered. Set to true to send notifications.
///
/// Leave blank the unedited fields.
/// Returns the edited hook.
///
/// https://developer.github.com/v3/repos/hooks/#edit-a-hook
Future<Hook> editHook(
RepositorySlug slug,
Hook hookToEdit, {
String? configUrl,
String? configContentType,
String? configSecret,
bool? configInsecureSsl,
List<String>? events,
List<String>? addEvents,
List<String>? removeEvents,
bool? active,
}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(hookToEdit);
ArgumentError.checkNotNull(configUrl ?? hookToEdit.config!.url);
if (configContentType != 'json' && configContentType != 'form') {
throw ArgumentError.value(configContentType, 'configContentType');
}
return github.postJSON<Map<String, dynamic>, Hook>(
'/repos/${slug.fullName}/hooks/${hookToEdit.id.toString()}',
statusCode: StatusCodes.OK,
convert: (i) => Hook.fromJson(i)..repoName = slug.fullName,
body: GitHubJson.encode(createNonNullMap(<String, dynamic>{
'active': active ?? hookToEdit.active,
'events': events ?? hookToEdit.events,
'add_events': addEvents,
'remove_events': removeEvents,
'config': <String, dynamic>{
'url': configUrl ?? hookToEdit.config!.url,
'content_type': configContentType ?? hookToEdit.config!.contentType,
'secret': configSecret ?? hookToEdit.config!.secret,
'insecure_ssl':
configInsecureSsl == null || !configInsecureSsl ? '0' : '1',
},
})),
);
}
/// Triggers a hook with the latest push.
///
/// API docs: https://developer.github.com/v3/repos/hooks/#test-a-push-hook
Future<bool> testPushHook(RepositorySlug slug, int id) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
return github
.request(
'POST',
'/repos/${slug.fullName}/hooks/$id/tests',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
/// Pings the hook.
///
/// API docs: https://developer.github.com/v3/repos/hooks/#ping-a-hook
Future<bool> pingHook(RepositorySlug slug, int id) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
return github
.request(
'POST',
'/repos/${slug.fullName}/hooks/$id/pings',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
Future<bool> deleteHook(RepositorySlug slug, int id) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
return github
.request(
'DELETE',
'/repos/${slug.fullName}/hooks/$id',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
// TODO: Implement other hook methods: https://developer.github.com/v3/repos/hooks/
/// Lists the deploy keys for a repository.
///
/// API docs: https://developer.github.com/v3/repos/keys/#list
Stream<PublicKey> listDeployKeys(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, PublicKey>(
'GET',
'/repos/${slug.fullName}/keys',
(i) => PublicKey.fromJson(i),
);
}
/// Get a deploy key.
/// * [id]: id of the key to retrieve.
///
/// https://developer.github.com/v3/repos/keys/#get
Future<PublicKey> getDeployKey(RepositorySlug slug, {required int id}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
return github.getJSON<Map<String, dynamic>, PublicKey>(
'/repos/${slug.fullName}/keys/$id',
statusCode: StatusCodes.OK,
convert: (i) => PublicKey.fromJson(i),
);
}
/// Adds a deploy key for a repository.
///
/// API docs: https://developer.github.com/v3/repos/keys/#create
Future<PublicKey> createDeployKey(
RepositorySlug slug, CreatePublicKey key) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(key);
return github.postJSON<Map<String, dynamic>, PublicKey>(
'/repos/${slug.fullName}/keys',
body: GitHubJson.encode(key),
statusCode: StatusCodes.CREATED,
convert: (i) => PublicKey.fromJson(i),
);
}
/// Delete a deploy key.
///
/// https://developer.github.com/v3/repos/keys/#delete
Future<bool> deleteDeployKey(
{required RepositorySlug slug, required PublicKey key}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(key);
return github
.request(
'DELETE',
'/repos/${slug.fullName}/keys/${key.id}',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
}
/// Merges a branch in the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/merging/#perform-a-merge
Future<RepositoryCommit> merge(RepositorySlug slug, CreateMerge merge) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(merge);
return github.postJSON<Map<String, dynamic>, RepositoryCommit>(
'/repos/${slug.fullName}/merges',
body: GitHubJson.encode(merge),
convert: (i) => RepositoryCommit.fromJson(i),
statusCode: StatusCodes.CREATED,
);
}
/// Fetches the GitHub pages information for the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/pages/#get-information-about-a-pages-site
Future<RepositoryPages> getPagesInfo(RepositorySlug slug) async {
ArgumentError.checkNotNull(slug);
return github.getJSON<Map<String, dynamic>, RepositoryPages>(
'/repos/${slug.fullName}/pages',
statusCode: StatusCodes.OK,
convert: (i) => RepositoryPages.fromJson(i),
);
}
/// List Pages builds.
///
/// API docs: https://developer.github.com/v3/repos/pages/#list-pages-builds
Stream<PageBuild> listPagesBuilds(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, PageBuild>(
'GET',
'/repos/${slug.fullName}/pages/builds',
(i) => PageBuild.fromJson(i),
statusCode: StatusCodes.OK,
);
}
/// Get latest Pages build.
///
/// API docs: https://developer.github.com/v3/repos/pages/#list-latest-pages-build
Future<PageBuild> getLatestPagesBuild(RepositorySlug slug) async {
ArgumentError.checkNotNull(slug);
return github.getJSON(
'/repos/${slug.fullName}/pages/builds/latest',
convert: (dynamic i) => PageBuild.fromJson(i),
statusCode: StatusCodes.OK,
);
}
// Releases
/// Lists releases for the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
Stream<Release> listReleases(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects<Map<String, dynamic>, Release>(
'GET',
'/repos/${slug.fullName}/releases',
(i) => Release.fromJson(i),
);
}
/// Lists the latest release for the specified repository.
///
/// API docs: https://developer.github.com/v3/repos/releases/#get-the-latest-release
Future<Release> getLatestRelease(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return github.getJSON<Map<String, dynamic>, Release>(
'/repos/${slug.fullName}/releases/latest',
convert: (i) => Release.fromJson(i),
statusCode: StatusCodes.OK,
);
}
/// Fetches a single release by the release ID.
///
/// API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release
Future<Release> getReleaseById(RepositorySlug slug, int id) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(id);
return github.getJSON<Map<String, dynamic>, Release>(
'/repos/${slug.fullName}/releases/$id',
convert: (i) => Release.fromJson(i),
);
}
/// Fetches a single release by the release tag name.
///
/// Throws a [ReleaseNotFound] exception if the release
/// doesn't exist.
///
/// API docs: https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
Future<Release> getReleaseByTagName(
RepositorySlug slug, String? tagName) async {
return github.getJSON(
'/repos/${slug.fullName}/releases/tags/$tagName',
convert: (dynamic i) => Release.fromJson(i),
statusCode: StatusCodes.OK,
fail: (http.Response response) {
if (response.statusCode == 404) {
throw ReleaseNotFound.fromTagName(github, tagName);
}
},
);
}
/// Creates a Release based on the specified [createRelease].
///
/// If [getIfExists] is true, this returns an already existing release instead of an error.
/// Defaults to true.
/// API docs: https://developer.github.com/v3/repos/releases/#create-a-release
Future<Release> createRelease(
RepositorySlug slug,
CreateRelease createRelease, {
bool getIfExists = true,
}) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(createRelease);
final release = await github.postJSON<Map<String, dynamic>, Release>(
'/repos/${slug.fullName}/releases',
convert: (i) => Release.fromJson(i),
body: GitHubJson.encode(createRelease.toJson()),
statusCode: StatusCodes.CREATED);