Skip to content
Navigation Menu
{{ message }}
forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo.py
More file actions
2112 lines (1769 loc) · 82.9 KB
/
Copy pathrepo.py
File metadata and controls
2112 lines (1769 loc) · 82.9 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
# -*- coding: utf-8 -*-
"""
github3.repos.repo
==================
This module contains the Repository object which is used to access the various
parts of GitHub's Repository API.
"""
from __future__ import unicode_literals
from base64 import b64encode
from json import dumps
from uritemplate import URITemplate
from .. import users
from ..decorators import requires_auth
from ..events import Event
from ..git import Blob, Commit, Reference, Tag, Tree
from ..issues import Issue, issue_params
from ..issues.event import IssueEvent
from ..issues.label import Label
from ..issues.milestone import Milestone
from ..licenses import License
from ..models import GitHubCore
from ..notifications import Subscription, Thread
from ..pulls import PullRequest
from ..utils import stream_response_to_file, timestamp_parameter
from .branch import Branch
from .comment import RepoComment
from .commit import RepoCommit
from .comparison import Comparison
from .contents import Contents, validate_commmitter
from .deployment import Deployment
from .hook import Hook
from .issue_import import ImportedIssue
from .pages import PagesBuild, PagesInfo
from .release import Asset, Release
from .stats import ContributorStats
from .status import Status
from .tag import RepoTag
class Repository(GitHubCore):
"""The :class:`Repository <Repository>` object.
It represents how GitHub sends information about repositories.
Two repository instances can be checked like so::
r1 == r2
r1 != r2
And is equivalent to::
r1.id == r2.id
r1.id != r2.id
See also: http://developer.github.com/v3/repos/
"""
STAR_HEADERS = {
'Accept': 'application/vnd.github.v3.star+json'
}
def _update_attributes(self, repo):
self._api = self._get_attribute(repo, 'url')
#: URL used to clone via HTTPS.
self.clone_url = self._get_attribute(repo, 'clone_url')
#: ``datetime`` object representing when the Repository was created.
self.created_at = self._strptime_attribute(repo, 'created_at')
#: Description of the repository.
self.description = self._get_attribute(repo, 'description')
#: The number of forks of this repository.
self.forks_count = self._get_attribute(repo, 'forks_count')
#: The number of forks of this repository. For backward compatibility
self.fork_count = self.forks_count
#: Is this repository a fork?
self.fork = self._get_attribute(repo, 'fork')
#: Full name as login/name
self.full_name = self._get_attribute(repo, 'full_name')
# Clone url using git, e.g. git://github.com/sigmavirus24/github3.py
#: Plain git url for an anonymous clone.
self.git_url = self._get_attribute(repo, 'git_url')
#: Whether or not this repository has downloads enabled
self.has_downloads = self._get_attribute(repo, 'has_downloads')
#: Whether or not this repository has an issue tracker
self.has_issues = self._get_attribute(repo, 'has_issues')
#: Whether or not this repository has the wiki enabled
self.has_wiki = self._get_attribute(repo, 'has_wiki')
# e.g. https://sigmavirus24.github.com/github3.py
#: URL of the home page for the project.
self.homepage = self._get_attribute(repo, 'homepage')
#: URL of the pure diff of the pull request
self.diff_url = self._get_attribute(repo, 'diff_url')
#: URL of the pure patch of the pull request
self.patch_url = self._get_attribute(repo, 'patch_url')
#: API URL of the issue representation of this Pull Request
self.issue_url = self._get_attribute(repo, 'issue_url')
# e.g. https://github.com/sigmavirus24/github3.py
#: URL of the project at GitHub.
self.html_url = self._get_attribute(repo, 'html_url')
#: Unique id of the repository.
self.id = self._get_attribute(repo, 'id')
#: Language property.
self.language = self._get_attribute(repo, 'language')
# License containing only key, name, url & featured
#: :class:`License <github3.licenses.License>` object representing the
#: repository license.
self.original_license = self._class_attribute(
repo, 'license', License, self
)
#: Mirror property.
self.mirror_url = self._get_attribute(repo, 'mirror_url')
# Repository name, e.g. github3.py
#: Name of the repository.
self.name = self._get_attribute(repo, 'name')
#: Number of open issues on the repository. DEPRECATED
self.open_issues = self._get_attribute(repo, 'open_issues')
#: Number of open issues on the repository
self.open_issues_count = self._get_attribute(repo, 'open_issues_count')
# Repository owner's name
#: :class:`User <github3.users.User>` object representing the
#: repository owner.
self.owner = self._class_attribute(
repo, 'owner', users.ShortUser, self)
#: Is this repository private?
self.private = self._get_attribute(repo, 'private')
#: Permissions for this repository
self.permissions = self._get_attribute(repo, 'permissions')
#: ``datetime`` object representing the last time commits were pushed
#: to the repository.
self.pushed_at = self._strptime_attribute(repo, 'pushed_at')
#: Size of the repository.
self.size = self._get_attribute(repo, 'size')
# The number of stargazers
#: Number of users who starred the repository
self.stargazers_count = self._get_attribute(repo, 'stargazers_count')
#: ``datetime`` object representing when the repository was starred
self.starred_at = self._strptime_attribute(repo, 'starred_at')
# SSH url e.g. git@github.com/sigmavirus24/github3.py
#: URL to clone the repository via SSH.
self.ssh_url = self._get_attribute(repo, 'ssh_url')
#: If it exists, url to clone the repository via SVN.
self.svn_url = self._get_attribute(repo, 'svn_url')
#: ``datetime`` object representing the last time the repository was
#: updated.
self.updated_at = self._strptime_attribute(repo, 'updated_at')
# The number of watchers
#: Number of users watching the repository.
self.watchers = self._get_attribute(repo, 'watchers')
#: Parent of this fork, if it exists :class:`Repository`
self.source = self._class_attribute(repo, 'source', Repository, self)
#: Parent of this fork, if it exists :class:`Repository`
self.parent = self._class_attribute(repo, 'parent', Repository, self)
#: default branch for the repository
self.default_branch = self._get_attribute(repo, 'default_branch')
#: master (default) branch for the repository
self.master_branch = self._get_attribute(repo, 'master_branch')
#: Teams url (not a template)
self.teams_url = self._get_attribute(repo, 'teams_url')
#: Hooks url (not a template)
self.hooks_url = self._get_attribute(repo, 'hooks_url')
#: Events url (not a template)
self.events_url = self._get_attribute(repo, 'events_url')
#: Tags url (not a template)
self.tags_url = self._get_attribute(repo, 'tags_url')
#: Languages url (not a template)
self.languages_url = self._get_attribute(repo, 'languages_url')
#: Stargazers url (not a template)
self.stargazers_url = self._get_attribute(repo, 'stargazers_url')
#: Contributors url (not a template)
self.contributors_url = self._get_attribute(repo, 'contributors_url')
#: Subscribers url (not a template)
self.subscribers_url = self._get_attribute(repo, 'subscribers_url')
#: Subscription url (not a template)
self.subscription_url = self._get_attribute(repo, 'subscription_url')
#: Merges url (not a template)
self.merges_url = self._get_attribute(repo, 'merges_url')
#: Downloads url (not a template)
self.download_url = self._get_attribute(repo, 'downloads_url')
# Template URLS
#: Issue events URL Template. Expand with ``number``
self.issue_events_urlt = self._class_attribute(
repo,
'issue_events_url',
URITemplate
)
#: Assignees URL Template. Expand with ``user``
self.assignees_urlt = self._class_attribute(
repo,
'assignees_url',
URITemplate
)
#: Branches URL Template. Expand with ``branch``
self.branches_urlt = self._class_attribute(
repo,
'branches_url',
URITemplate
)
#: Blobs URL Template. Expand with ``sha``
self.blobs_urlt = self._class_attribute(
repo,
'blobs_url',
URITemplate
)
#: Git tags URL Template. Expand with ``sha``
self.git_tags_urlt = self._class_attribute(
repo,
'git_tags_url',
URITemplate
)
#: Git refs URL Template. Expand with ``sha``
self.git_refs_urlt = self._class_attribute(
repo,
'git_refs_url',
URITemplate
)
#: Trres URL Template. Expand with ``sha``
self.trees_urlt = self._class_attribute(
repo,
'trees_url',
URITemplate
)
#: Statuses URL Template. Expand with ``sha``
self.statuses_urlt = self._class_attribute(
repo,
'statuses_url',
URITemplate
)
#: Commits URL Template. Expand with ``sha``
self.commits_urlt = self._class_attribute(
repo,
'commits_url',
URITemplate
)
#: Git commits URL Template. Expand with ``sha``
self.git_commits_urlt = self._class_attribute(
repo,
'git_commits_url',
URITemplate
)
#: Comments URL Template. Expand with ``number``
self.comments_urlt = self._class_attribute(
repo,
'comments_url',
URITemplate
)
#: Pull Request Review Comments URL
self.review_comments_url = self._class_attribute(
repo,
'review_comments_url',
URITemplate
)
#: Pull Request Review Comments URL Template. Expand with ``number``
self.issue_events_urlt = self._class_attribute(
repo,
'review_comment_url',
URITemplate
)
#: Issue comment URL Template. Expand with ``number``
self.issue_comment_urlt = self._class_attribute(
repo,
'issue_comment_url',
URITemplate
)
#: Contents URL Template. Expand with ``path``
self.contents_urlt = self._class_attribute(
repo,
'contents_url',
URITemplate
)
#: Comparison URL Template. Expand with ``base`` and ``head``
self.compare_urlt = self._class_attribute(
repo,
'compare_url',
URITemplate
)
#: Archive URL Template. Expand with ``archive_format`` and ``ref``
self.archive_urlt = self._class_attribute(
repo,
'archive_url',
URITemplate
)
#: Issues URL Template. Expand with ``number``
self.issues_urlt = self._class_attribute(
repo,
'issues_url',
URITemplate
)
#: Pull Requests URL Template. Expand with ``number``
self.pulls_urlt = self._class_attribute(
repo,
'pulls_url',
URITemplate
)
#: Milestones URL Template. Expand with ``number``
self.milestones_urlt = self._class_attribute(
repo,
'milestones_url',
URITemplate
)
#: Notifications URL Template. Expand with ``since``, ``all``,
#: ``participating``
self.notifications_urlt = self._class_attribute(
repo,
'notifications_url',
URITemplate
)
#: Labels URL Template. Expand with ``name``
self.labels_urlt = self._class_attribute(
repo,
'labels_url',
URITemplate
)
def _repr(self):
return '<Repository [{0}]>'.format(self)
def __str__(self):
return self.full_name
def _create_pull(self, data):
self._remove_none(data)
json = None
if data:
url = self._build_url('pulls', base_url=self._api)
json = self._json(self._post(url, data=data), 201)
return self._instance_or_null(PullRequest, json)
@requires_auth
def add_collaborator(self, username):
"""Add ``username`` as a collaborator to a repository.
:param username: (required), username of the user
:type username: str or :class:`User <github3.users.User>`
:returns: bool -- True if successful, False otherwise
"""
if not username:
return False
url = self._build_url('collaborators', str(username),
base_url=self._api)
return self._boolean(self._put(url), 204, 404)
def archive(self, format, path='', ref='master'):
"""Get the tarball or zipball archive for this repo at ref.
See: http://developer.github.com/v3/repos/contents/#get-archive-link
:param str format: (required), accepted values: ('tarball',
'zipball')
:param path: (optional), path where the file should be saved
to, default is the filename provided in the headers and will be
written in the current directory.
it can take a file-like object as well
:type path: str, file
:param str ref: (optional)
:returns: bool -- True if successful, False otherwise
"""
resp = None
if format in ('tarball', 'zipball'):
url = self._build_url(format, ref, base_url=self._api)
resp = self._get(url, allow_redirects=True, stream=True)
if resp and self._boolean(resp, 200, 404):
stream_response_to_file(resp, path)
return True
return False
def asset(self, id):
"""Return a single asset.
:param int id: (required), id of the asset
:returns: :class:`Asset <github3.repos.release.Asset>`
"""
data = None
if int(id) > 0:
url = self._build_url('releases', 'assets', str(id),
base_url=self._api)
data = self._json(self._get(url, headers=Release.CUSTOM_HEADERS),
200)
return self._instance_or_null(Asset, data)
def assignees(self, number=-1, etag=None):
r"""Iterate over all assignees to which an issue may be assigned.
:param int number: (optional), number of assignees to return. Default:
-1 returns all available assignees
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`~github3.users.User`\ s
"""
url = self._build_url('assignees', base_url=self._api)
return self._iter(int(number), url, users.ShortUser, etag=etag)
def blob(self, sha):
"""Get the blob indicated by ``sha``.
:param str sha: (required), sha of the blob
:returns: :class:`Blob <github3.git.Blob>` if successful, otherwise
None
"""
url = self._build_url('git', 'blobs', sha, base_url=self._api)
json = self._json(self._get(url), 200)
return self._instance_or_null(Blob, json)
def branch(self, name):
"""Get the branch ``name`` of this repository.
:param str name: (required), branch name
:type name: str
:returns: :class:`Branch <github3.repos.branch.Branch>`
"""
json = None
if name:
url = self._build_url('branches', name, base_url=self._api)
json = self._json(self._get(url, headers=Branch.PREVIEW_HEADERS),
200)
return self._instance_or_null(Branch, json)
def branches(self, number=-1, protected=False, etag=None):
r"""Iterate over the branches in this repository.
:param int number: (optional), number of branches to return. Default:
-1 returns all branches
:param bool protected: (optional), True lists only protected branches.
Default: False
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of
:class:`Branch <github3.repos.branch.Branch>`\ es
"""
url = self._build_url('branches', base_url=self._api)
params = {'protected': '1'} if protected else None
return self._iter(int(number), url, Branch, params, etag=etag,
headers=Branch.PREVIEW_HEADERS)
def code_frequency(self, number=-1, etag=None):
"""Iterate over the code frequency per week.
Returns a weekly aggregate of the number of additions and deletions
pushed to this repository.
:param int number: (optional), number of weeks to return. Default: -1
returns all weeks
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of lists ``[seconds_from_epoch, additions,
deletions]``
.. note:: All statistics methods may return a 202. On those occasions,
you will not receive any objects. You should store your
iterator and check the new ``last_status`` attribute. If it
is a 202 you should wait before re-requesting.
.. versionadded:: 0.7
"""
url = self._build_url('stats', 'code_frequency', base_url=self._api)
return self._iter(int(number), url, list, etag=etag)
def collaborators(self, number=-1, etag=None):
r"""Iterate over the collaborators of this repository.
:param int number: (optional), number of collaborators to return.
Default: -1 returns all comments
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`~github3.users.ShortUser`\ s
"""
url = self._build_url('collaborators', base_url=self._api)
return self._iter(int(number), url, users.ShortUser, etag=etag)
def comments(self, number=-1, etag=None):
r"""Iterate over comments on all commits in the repository.
:param int number: (optional), number of comments to return. Default:
-1 returns all comments
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of
:class:`RepoComment <github3.repos.comment.RepoComment>`\ s
"""
url = self._build_url('comments', base_url=self._api)
return self._iter(int(number), url, RepoComment, etag=etag)
def commit(self, sha):
"""Get a single (repo) commit.
See :func:`git_commit` for the Git Data Commit.
:param str sha: (required), sha of the commit
:returns: :class:`RepoCommit <github3.repos.commit.RepoCommit>` if
successful, otherwise None
"""
url = self._build_url('commits', sha, base_url=self._api)
json = self._json(self._get(url), 200)
return self._instance_or_null(RepoCommit, json)
def commit_activity(self, number=-1, etag=None):
"""Iterate over last year of commit activity by week.
See: http://developer.github.com/v3/repos/statistics/
.. note:: All statistics methods may return a 202. On those occasions,
you will not receive any objects. You should store your
iterator and check the new ``last_status`` attribute. If it
is a 202 you should wait before re-requesting.
.. versionadded:: 0.7
:param int number: (optional), number of weeks to return. Default -1
will return all of the weeks.
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of dictionaries
"""
url = self._build_url('stats', 'commit_activity', base_url=self._api)
return self._iter(int(number), url, dict, etag=etag)
def commit_comment(self, comment_id):
"""Get a single commit comment.
:param int comment_id: (required), id of the comment used by GitHub
:returns: :class:`RepoComment <github3.repos.comment.RepoComment>` if
successful, otherwise None
"""
url = self._build_url('comments', str(comment_id), base_url=self._api)
json = self._json(self._get(url), 200)
return self._instance_or_null(RepoComment, json)
def commits(self, sha=None, path=None, author=None, number=-1, etag=None,
since=None, until=None, per_page=None):
r"""Iterate over commits in this repository.
:param str sha: (optional), sha or branch to start listing commits
from
:param str path: (optional), commits containing this path will be
listed
:param str author: (optional), GitHub login, real name, or email to
filter commits by (using commit author)
:param int number: (optional), number of comments to return. Default:
-1 returns all comments
:param str etag: (optional), ETag from a previous request to the same
endpoint
:param since: (optional), Only commits after this date will
be returned. This can be a ``datetime`` or an ``ISO8601`` formatted
date string.
:type since: datetime or string
:param until: (optional), Only commits before this date will
be returned. This can be a ``datetime`` or an ``ISO8601`` formatted
date string.
:type until: datetime or string
:param int per_page: (optional), commits listing page size
:returns: generator of
:class:`RepoCommit <github3.repos.commit.RepoCommit>`\ s
"""
params = {'sha': sha, 'path': path, 'author': author,
'since': timestamp_parameter(since),
'until': timestamp_parameter(until),
'per_page': per_page}
self._remove_none(params)
url = self._build_url('commits', base_url=self._api)
return self._iter(int(number), url, RepoCommit, params, etag)
def compare_commits(self, base, head):
"""Compare two commits.
:param str base: (required), base for the comparison
:param str head: (required), compare this against base
:returns: :class:`Comparison <github3.repos.comparison.Comparison>` if
successful, else None
"""
url = self._build_url('compare', base + '...' + head,
base_url=self._api)
json = self._json(self._get(url), 200)
return self._instance_or_null(Comparison, json)
def contributor_statistics(self, number=-1, etag=None):
"""Iterate over the contributors list.
See also: http://developer.github.com/v3/repos/statistics/
.. note:: All statistics methods may return a 202. On those occasions,
you will not receive any objects. You should store your
iterator and check the new ``last_status`` attribute. If it
is a 202 you should wait before re-requesting.
.. versionadded:: 0.7
:param int number: (optional), number of weeks to return. Default -1
will return all of the weeks.
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of
:class:`ContributorStats <github3.repos.stats.ContributorStats>`
"""
url = self._build_url('stats', 'contributors', base_url=self._api)
return self._iter(int(number), url, ContributorStats, etag=etag)
def contributors(self, anon=False, number=-1, etag=None):
r"""Iterate over the contributors to this repository.
:param bool anon: (optional), True lists anonymous contributors as
well
:param int number: (optional), number of contributors to return.
Default: -1 returns all contributors
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`~github3.users.ShortUser`\ s
"""
url = self._build_url('contributors', base_url=self._api)
params = {}
if anon:
params = {'anon': 'true'}
return self._iter(int(number), url, users.ShortUser, params, etag)
@requires_auth
def create_blob(self, content, encoding):
"""Create a blob with ``content``.
:param str content: (required), content of the blob
:param str encoding: (required), ('base64', 'utf-8')
:returns: string of the SHA returned
"""
sha = ''
if encoding in ('base64', 'utf-8'):
url = self._build_url('git', 'blobs', base_url=self._api)
data = {'content': content, 'encoding': encoding}
json = self._json(self._post(url, data=data), 201)
if json:
sha = json.get('sha')
return sha
@requires_auth
def create_comment(self, body, sha, path=None, position=None, line=1):
"""Create a comment on a commit.
:param str body: (required), body of the message
:param str sha: (required), commit id
:param str path: (optional), relative path of the file to comment
on
:param str position: (optional), line index in the diff to comment on
:param int line: (optional), line number of the file to comment on,
default: 1
:returns: :class:`RepoComment <github3.repos.comment.RepoComment>` if
successful, otherwise None
"""
json = None
if body and sha and (line and int(line) > 0):
data = {'body': body, 'line': line, 'path': path,
'position': position}
self._remove_none(data)
url = self._build_url('commits', sha, 'comments',
base_url=self._api)
json = self._json(self._post(url, data=data), 201)
return self._instance_or_null(RepoComment, json)
@requires_auth
def create_commit(self, message, tree, parents, author=None,
committer=None):
"""Create a commit on this repository.
:param str message: (required), commit message
:param str tree: (required), SHA of the tree object this
commit points to
:param list parents: (required), SHAs of the commits that were parents
of this commit. If empty, the commit will be written as the root
commit. Even if there is only one parent, this should be an
array.
:param dict author: (optional), if omitted, GitHub will
use the authenticated user's credentials and the current
time. Format: {'name': 'Committer Name', 'email':
'name@example.com', 'date': 'YYYY-MM-DDTHH:MM:SS+HH:00'}
:param dict committer: (optional), if ommitted, GitHub will use the
author parameters. Should be the same format as the author
parameter.
:returns: :class:`Commit <github3.git.Commit>` if successful, else
None
"""
json = None
if message and tree and isinstance(parents, list):
url = self._build_url('git', 'commits', base_url=self._api)
data = {'message': message, 'tree': tree, 'parents': parents,
'author': author, 'committer': committer}
self._remove_none(data)
json = self._json(self._post(url, data=data), 201)
return self._instance_or_null(Commit, json)
@requires_auth
def create_deployment(self, ref, required_contexts=None, payload='',
auto_merge=False, description='', environment=None):
"""Create a deployment.
:param str ref: (required), The ref to deploy. This can be a branch,
tag, or sha.
:param list required_contexts: Optional array of status contexts
verified against commit status checks. To bypass checking
entirely pass an empty array. Default: []
:param str payload: Optional JSON payload with extra information about
the deployment. Default: ""
:param bool auto_merge: Optional parameter to merge the default branch
into the requested deployment branch if necessary. Default: False
:param str description: Optional short description. Default: ""
:param str environment: Optional name for the target deployment
environment (e.g., production, staging, qa). Default: "production"
:returns: :class:`Deployment <github3.repos.deployment.Deployment>`
"""
json = None
if ref:
if required_contexts is None:
required_contexts = []
url = self._build_url('deployments', base_url=self._api)
data = {'ref': ref, 'required_contexts': required_contexts,
'payload': payload, 'auto_merge': auto_merge,
'description': description, 'environment': environment}
self._remove_none(data)
json = self._json(self._post(url, data=data),
201)
return self._instance_or_null(Deployment, json)
@requires_auth
def create_file(self, path, message, content, branch=None,
committer=None, author=None):
"""Create a file in this repository.
See also: http://developer.github.com/v3/repos/contents/#create-a-file
:param str path: (required), path of the file in the repository
:param str message: (required), commit message
:param bytes content: (required), the actual data in the file
:param str branch: (optional), branch to create the commit on.
Defaults to the default branch of the repository
:param dict committer: (optional), if no information is given the
authenticated user's information will be used. You must specify
both a name and email.
:param dict author: (optional), if omitted this will be filled in with
committer information. If passed, you must specify both a name and
email.
:returns: {
'content': :class:`Contents <github3.repos.contents.Contents>`:,
'commit': :class:`Commit <github3.git.Commit>`}
"""
if content and not isinstance(content, bytes):
raise ValueError( # (No coverage)
'content must be a bytes object') # (No coverage)
json = None
if path and message and content:
url = self._build_url('contents', path, base_url=self._api)
content = b64encode(content).decode('utf-8')
data = {'message': message, 'content': content, 'branch': branch,
'committer': validate_commmitter(committer),
'author': validate_commmitter(author)}
self._remove_none(data)
json = self._json(self._put(url, data=dumps(data)), 201)
if json and 'content' in json and 'commit' in json:
json['content'] = Contents(json['content'], self)
json['commit'] = Commit(json['commit'], self)
return json
@requires_auth
def create_fork(self, organization=None):
"""Create a fork of this repository.
:param str organization: (required), login for organization to create
the fork under
:returns: :class:`Repository <Repository>` if successful, else None
"""
url = self._build_url('forks', base_url=self._api)
if organization:
resp = self._post(url, data={'organization': organization})
else:
resp = self._post(url)
json = self._json(resp, 202)
return self._instance_or_null(Repository, json)
@requires_auth
def create_hook(self, name, config, events=['push'], active=True):
"""Create a hook on this repository.
:param str name: (required), name of the hook
:param dict config: (required), key-value pairs which act as settings
for this hook
:param list events: (optional), events the hook is triggered for
:param bool active: (optional), whether the hook is actually
triggered
:returns: :class:`Hook <github3.repos.hook.Hook>` if successful,
otherwise None
"""
json = None
if name and config and isinstance(config, dict):
url = self._build_url('hooks', base_url=self._api)
data = {'name': name, 'config': config, 'events': events,
'active': active}
json = self._json(self._post(url, data=data), 201)
return Hook(json, self) if json else None
@requires_auth
def create_issue(self,
title,
body=None,
assignee=None,
milestone=None,
labels=None,
assignees=None):
"""Create an issue on this repository.
:param str title: (required), title of the issue
:param str body: (optional), body of the issue
:param str assignee: (optional), login of the user to assign the
issue to
:param int milestone: (optional), id number of the milestone to
attribute this issue to (e.g. ``m`` is a :class:`Milestone
<github3.issues.milestone.Milestone>` object, ``m.number`` is
what you pass here.)
:param labels: (optional), labels to apply to this
issue
:type labels: list of strings
:param assignees: (optional), login of the users to assign the
issue to
:type assignees: list of strings
:returns: :class:`Issue <github3.issues.issue.Issue>` if successful,
otherwise None
"""
issue = {'title': title, 'body': body, 'assignee': assignee,
'milestone': milestone, 'labels': labels,
'assignees': assignees}
self._remove_none(issue)
json = None
if issue:
url = self._build_url('issues', base_url=self._api)
json = self._json(self._post(url, data=issue), 201)
return self._instance_or_null(Issue, json)
@requires_auth
def create_key(self, title, key, read_only=False):
"""Create a deploy key.
:param str title: (required), title of key
:param str key: (required), key text
:param bool read_only: (optional), restrict key access to read-only,
default is False
:returns: :class:`~github3.users.Key` if successful, else None
"""
json = None
if title and key:
data = {'title': title, 'key': key, 'read_only': read_only}
url = self._build_url('keys', base_url=self._api)
json = self._json(self._post(url, data=data), 201)
return self._instance_or_null(users.Key, json)
@requires_auth
def create_label(self, name, color):
"""Create a label for this repository.
:param str name: (required), name to give to the label
:param str color: (required), value of the color to assign to the
label, e.g., '#fafafa' or 'fafafa' (the latter is what is sent)
:returns: :class:`Label <github3.issues.label.Label>` if successful,
else None
"""
json = None
if name and color:
data = {'name': name, 'color': color.strip('#')}
url = self._build_url('labels', base_url=self._api)
json = self._json(self._post(url, data=data), 201)
return self._instance_or_null(Label, json)
@requires_auth
def create_milestone(self, title, state=None, description=None,
due_on=None):
"""Create a milestone for this repository.
:param str title: (required), title of the milestone
:param str state: (optional), state of the milestone, accepted
values: ('open', 'closed'), default: 'open'
:param str description: (optional), description of the milestone
:param str due_on: (optional), ISO 8601 formatted due date
:returns: :class:`Milestone <github3.issues.milestone.Milestone>` if
successful, otherwise None
"""
url = self._build_url('milestones', base_url=self._api)
if state not in ('open', 'closed'):
state = None
data = {'title': title, 'state': state,
'description': description, 'due_on': due_on}
self._remove_none(data)
json = None
if data:
json = self._json(self._post(url, data=data), 201)
return self._instance_or_null(Milestone, json)
@requires_auth
def create_pull(self, title, base, head, body=None):
"""Create a pull request of ``head`` onto ``base`` branch in this repo.
:param str title: (required)
:param str base: (required), e.g., 'master'
:param str head: (required), e.g., 'username:branch'
:param str body: (optional), markdown formatted description
:returns: :class:`PullRequest <github3.pulls.PullRequest>` if
successful, else None
"""
data = {'title': title, 'body': body, 'base': base,
'head': head}
return self._create_pull(data)
@requires_auth
def create_pull_from_issue(self, issue, base, head):
"""Create a pull request from issue #``issue``.
:param int issue: (required), issue number
:param str base: (required), e.g., 'master'
:param str head: (required), e.g., 'username:branch'
:returns: :class:`PullRequest <github3.pulls.PullRequest>` if
successful, else None
"""
if int(issue) > 0:
data = {'issue': issue, 'base': base, 'head': head}
return self._create_pull(data)
return None
@requires_auth
def create_ref(self, ref, sha):
"""Create a reference in this repository.
:param str ref: (required), fully qualified name of the reference,
You can’t perform that action at this time.
