forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrepos.py
More file actions
1683 lines (1434 loc) · 60.3 KB
/
Copy pathrepos.py
File metadata and controls
1683 lines (1434 loc) · 60.3 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
"""
github3.repos
=============
This module contains the class relating to repositories.
"""
from base64 import b64decode
from json import dumps
from github3.events import Event
from github3.issues import Issue, IssueEvent, Label, Milestone, issue_params
from github3.git import Blob, Commit, Reference, Tag, Tree
from github3.models import GitHubObject, GitHubCore, BaseComment, BaseCommit
from github3.pulls import PullRequest
from github3.users import User, Key
from github3.decorators import requires_auth
class Repository(GitHubCore):
"""The :class:`Repository <Repository>` object. It represents how GitHub
sends information about repositories.
"""
def __init__(self, repo, session=None):
super(Repository, self).__init__(repo, session)
#: URL used to clone via HTTPS.
self.clone_url = repo.get('clone_url')
#: ``datetime`` object representing when the Repository was created.
self.created_at = self._strptime(repo.get('created_at'))
#: Description of the repository.
self.description = repo.get('description')
# The number of forks
#: The number of forks made of this repository.
self.forks = repo.get('forks')
# Is this repository a fork?
self._is_fork = repo.get('fork')
# Clone url using git, e.g. git://github.com/sigmavirus24/github3.py
#: Plain git url for an anonymous clone.
self.git_url = repo.get('git_url')
self._has_dl = repo.get('has_downloads')
self._has_issues = repo.get('has_issues')
self._has_wiki = repo.get('has_wiki')
# e.g. https://sigmavirus24.github.com/github3.py
#: URL of the home page for the project.
self.homepage = repo.get('homepage')
# e.g. https://github.com/sigmavirus24/github3.py
#: URL of the project at GitHub.
self.html_url = repo.get('html_url')
#: Unique id of the repository.
self.id = repo.get('id')
#: Language property.
self.language = repo.get('language')
#: Mirror property.
self.mirror_url = repo.get('mirror_url')
# Repository name, e.g. github3.py
#: Name of the repository.
self.name = repo.get('name')
# Number of open issues
#: Number of open issues on the repository.
self.open_issues = repo.get('open_issues')
# Repository owner's name
#: :class:`User <github3.users.User>` object representing the
# repository owner.
self.owner = User(repo.get('owner'), self._session)
# Is this repository private?
self._priv = repo.get('private')
#: ``datetime`` object representing the last time commits were pushed
# to the repository.
self.pushed_at = self._strptime(repo.get('pushed_at'))
#: Size of the repository.
self.size = repo.get('size')
# SSH url e.g. git@github.com/sigmavirus24/github3.py
#: URL to clone the repository via SSH.
self.ssh_url = repo.get('ssh_url')
#: If it exists, url to clone the repository via SVN.
self.svn_url = repo.get('svn_url')
#: ``datetime`` object representing the last time the repository was
# updated.
self.updated_at = self._strptime(repo.get('updated_at'))
self._api = repo.get('url', '')
# The number of watchers
#: Number of users watching the repository.
self.watchers = repo.get('watchers')
def __repr__(self):
return '<Repository [{0}/{1}]>'.format(self.owner.login, self.name)
def _update_(self, repo):
self.__init__(repo, self._session)
def _create_pull(self, data):
json = None
if data:
url = self._build_url('pulls', self._api)
json = self._json(self._post(url, data), 201)
return PullRequest(json, self._session) if json else None
@requires_auth
def add_collaborator(self, login):
"""Add ``login`` as a collaborator to a repository.
:param login: (required), login of the user
:type login: str
:returns: bool -- True if successful, False otherwise
"""
resp = False
if login:
url = self._build_url('collaborators', login, base_url=self._api)
resp = self._boolean(self._put(url), 204, 404)
return resp
def archive(self, format, path='', ref='master'):
"""Get the tarball or zipball archive for this repo at ref.
:param format: (required), accepted values: ('tarball',
'zipball')
:type format: str
: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
:type path: str
:param ref: (optional)
:type ref: str
:returns: bool -- True if successful, False otherwise
"""
resp = None
written = False
if format in ('tarball', 'zipball'):
url = self._build_url(format, ref, base_url=self._api)
resp = self._get(url, allow_redirects=True)
if resp.ok and path:
with open(path, 'wb') as fd:
fd.write(resp.content)
written = True
elif resp:
header = resp.headers['content-disposition']
i = header.find('filename=') + len('filename=')
with open(header[i:], 'wb') as fd:
fd.write(resp.content)
written = True
return written
def blob(self, sha):
"""Get the blob indicated by ``sha``.
:param sha: (required), sha of the blob
:type sha: str
: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 Blob(json) if json else None
def branch(self, name):
"""Get the branch ``name`` of this repository.
:param name: (required), branch name
:type name: str
:returns: :class:`Branch <Branch>`
"""
json = None
if name:
url = self._build_url('branches', name, base_url=self._api)
json = self._json(self._get(url), 200)
return Branch(json, self) if json else None
def commit(self, sha):
"""Get a single (repo) commit. See :func:`git_commit` for the Git Data
Commit.
:param sha: (required), sha of the commit
:type sha: str
:returns: :class:`RepoCommit <RepoCommit>` if successful, otherwise
None
"""
url = self._build_url('commits', sha, base_url=self._api)
json = self._json(self._get(url), 200)
return RepoCommit(json, self) if json else None
def commit_comment(self, comment_id):
"""Get a single commit comment.
:param comment_id: (required), id of the comment used by GitHub
:type comment_id: int
:returns: :class:`RepoComment <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 RepoComment(json, self) if json else None
def compare_commits(self, base, head):
"""Compare two commits.
:param base: (required), base for the comparison
:type base: str
:param head: (required), compare this against base
:type head: str
:returns: :class:`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 Comparison(json) if json else None
def contents(self, path):
"""Get the contents of the file pointed to by ``path``.
:param path: (required), path to file, e.g.
github3/repo.py
:type path: str
:returns: :class:`Contents <Contents>` if successful, else None
"""
url = self._build_url('contents', path, base_url=self._api)
json = self._json(self._get(url), 200)
return Contents(json) if json else None
@requires_auth
def create_blob(self, content, encoding):
"""Create a blob with ``content``.
:param content: (required), content of the blob
:type content: str
:param encoding: (required), ('base64', 'utf-8')
:type encoding: str
:returns: string of the SHA returned
"""
sha = ''
if encoding in ('base64', 'utf-8') and content:
url = self._build_url('git', 'blobs', base_url=self._api)
data = dumps({'content': content, 'encoding': encoding})
json = self._json(self._post(url, data), 201)
if json:
sha = json.get('sha')
return sha
@requires_auth
def create_comment(self, body, sha, path, position, line=1):
"""Create a comment on a commit.
:param body: (required), body of the message
:type body: str
:param sha: (required), commit id
:type sha: str
:param path: (required), relative path of the file to comment
on
:type path: str
:param position: (required), line index in the diff to comment on
:type position: int
:param line: (optional), line number of the file to comment on,
default: 1
:type line: int
:returns: :class:`RepoComment <RepoComment>` if successful else None
"""
line = int(line)
position = int(position)
json = None
if body and sha and line > 0 and path and position > 0:
data = dumps({'body': body, 'commit_id': sha, 'line': line,
'path': path, 'position': position})
url = self._build_url('commits', sha, 'comments',
base_url=self._api)
json = self._json(self._post(url, data), 201)
return RepoComment(json, self) if json else None
@requires_auth
def create_commit(self, message, tree, parents, author={}, committer={}):
"""Create a commit on this repository.
:param message: (required), commit message
:type message: str
:param tree: (required), SHA of the tree object this
commit points to
:type tree: str
:param 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.
:type parents: list
:param 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'}
:type author: dict
:param committer: (optional), if ommitted, GitHub will use the author
parameters. Should be the same format as the author parameter.
:type commiter: dict
: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 = dumps({'message': message, 'tree': tree, 'parents': parents,
'author': author, 'committer': committer})
json = self._json(self._post(url, data), 201)
return Commit(json, self) if json else None
@requires_auth
def create_download(self, name, path, description='',
content_type='text/plain'):
"""Create a new download on this repository.
I do not require you provide the size in bytes because it can be
determined by the operating system.
:param str name: (required), name of the file as it will appear
:param path: (required), path to the file
:type path: str
:param description: (optional), description of the file
:type description: str
:param content_type: (optional), e.g. 'text/plain'
:type content_type: str
:returns: :class:`Download <Download>` if successful, else None
"""
json = None
if name and path:
url = self._build_url('downloads', base_url=self._api)
from os import stat
info = stat(path)
data = dumps({'name': name, 'size': info.st_size,
'description': description, 'content_type': content_type})
json = self._json(self._post(url, data), 201)
if not json:
return None
form = [('key', json.get('path')),
('acl', json.get('acl')),
('success_action_status', '201'),
('Filename', json.get('name')),
('AWSAccessKeyId', json.get('accesskeyid')),
('Policy', json.get('policy')),
('Signature', json.get('signature')),
('Content-Type', json.get('mime_type'))]
file = [('file', open(path, 'rb').read())]
resp = self._post(json.get('s3_url'), data=form, files=file,
auth=tuple())
return Download(json, self) if self._boolean(resp, 201, 404) else None
@requires_auth
def create_fork(self, organization=None):
"""Create a fork of this repository.
:param organization: (required), login for organization to create the
fork under
:type organization: str
:returns: :class:`Repository <Repository>` if successful, else None
"""
url = self._build_url('forks', base_url=self._api)
if organization:
resp = self._post(url, params={'org': organization})
else:
resp = self._post(url)
json = self._json(resp, 202)
return Repository(json, self) if json else None
@requires_auth
def create_hook(self, name, config, events=['push'], active=True):
"""Create a hook on this repository.
:param name: (required), name of the hook
:type name: str
:param config: (required), key-value pairs which act as settings
for this hook
:type config: dict
:param events: (optional), events the hook is triggered for
:type events: list
:param active: (optional), whether the hook is actually
triggered
:type active: bool
:returns: :class:`Hook <Hook>` if successful, else None
"""
json = None
if name and config and isinstance(config, dict):
url = self._build_url('hooks', base_url=self._api)
data = dumps({'name': name, 'config': config, 'events': events,
'active': active})
json = self._json(self._post(url, data), 201)
return Hook(json, self) if json else None
@requires_auth
def create_issue(self,
title,
body=None,
assignee=None,
milestone=None,
labels=[]):
"""Creates an issue on this repository.
:param title: (required), title of the issue
:type title: str
:param body: (optional), body of the issue
:type body: str
:param assignee: (optional), login of the user to assign the
issue to
:type assignee: str
:param milestone: (optional), milestone to attribute this issue
to
:type milestone: str
:param labels: (optional), labels to apply to this
issue
:type labels: list of strings
:returns: :class:`Issue <github3.issues.Issue>` if successful, else
None
"""
issue = dumps({'title': title, 'body': body, 'assignee': assignee,
'milestone': milestone, 'labels': labels})
url = self._build_url('issues', base_url=self._api)
json = self._json(self._post(url, issue), 201)
return Issue(json, self) if json else None
@requires_auth
def create_key(self, title, key):
"""Create a deploy key.
:param title: (required), title of key
:type title: str
:param key: (required), key text
:type key: str
:returns: :class:`Key <github3.users.Key>` if successful, else None
"""
data = dumps({'title': title, 'key': key})
url = self._build_url('keys', base_url=self._api)
json = self._json(self._post(url, data), 201)
return Key(json, self) if json else None
@requires_auth
def create_label(self, name, color):
"""Create a label for this repository.
:param name: (required), name to give to the label
:type name: str
:param color: (required), value of the color to assign to the
label
:type color: str
:returns: :class:`Label <github3.issues.Label>` if successful, else
None
"""
data = dumps({'name': name, 'color': color.strip('#')})
url = self._build_url('labels', base_url=self._api)
json = self._json(self._post(url, data), 201)
return Label(json, self) if json else None
@requires_auth
def create_milestone(self, title, state=None, description=None,
due_on=None):
"""Create a milestone for this repository.
:param title: (required), title of the milestone
:type title: str
:param state: (optional), state of the milestone, accepted
values: ('open', 'closed'), default: 'open'
:type state: str
:param description: (optional), description of the milestone
:type description: str
:param due_on: (optional), ISO 8601 formatted due date
:type due_on: str
:returns: :class:`Milestone <github3.issues.Milestone>` if successful,
else None
"""
url = self._build_url('milestones', base_url=self._api)
if state not in ('open', 'closed'):
state = 'open'
data = dumps({'title': title, 'state': state,
'description': description, 'due_on': due_on})
json = self._json(self._post(url, data), 201)
return Milestone(json, self) if json else None
@requires_auth
def create_pull(self, title, base, head, body=''):
"""Create a pull request using commits from ``head`` and comparing
against ``base``.
:param title: (required)
:type title: str
:param base: (required), e.g., 'username:branch', or a sha
:type base: str
:param head: (required), e.g., 'master', or a sha
:type head: str
:param body: (optional), markdown formatted description
:type body: str
:returns: :class:`PullRequest <github3.pulls.PullRequest>` if
successful, else None
"""
data = dumps({'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 issue: (required), issue number
:type issue: int
:param base: (required), e.g., 'username:branch', or a sha
:type base: str
:param head: (required), e.g., 'master', or a sha
:type head: str
:returns: :class:`PullRequest <github3.pulls.PullRequest>` if
successful, else None
"""
data = dumps({'issue': issue, 'base': base, 'head': head})
return self._create_pull(data)
@requires_auth
def create_ref(self, ref, sha):
"""Create a reference in this repository.
:param ref: (required), fully qualified name of the reference,
e.g. ``refs/heads/master``. If it doesn't start with ``refs`` and
contain at least two slashes, GitHub's API will reject it.
:type ref: str
:param sha: (required), SHA1 value to set the reference to
:type sha: str
:returns: :class:`Reference <github3.git.Reference>` if successful
else None
"""
data = dumps({'ref': ref, 'sha': sha})
url = self._build_url('git', 'refs', base_url=self._api)
json = self._json(self._post(url, data), 201)
return Reference(json, self) if json else None
@requires_auth
def create_status(self, sha, state, target_url='', description=''):
"""Create a status object on a commit.
:param str sha: (required), SHA of the commit to create the status on
:param str state: (required), state of the test; only the following
are accepted: 'pending', 'success', 'error', 'failure'
:param str target_url: (optional), URL to associate with this status.
:param str description: (optional), short description of the status
"""
json = {}
if sha and state:
data = dumps({'state': state, 'target_url': target_url,
'description': description})
url = self._build_url('statuses', sha, base_url=self._api)
json = self._json(self._post(url, data=data), 201)
return Status(json) if json else None
@requires_auth
def create_tag(self, tag, message, sha, obj_type, tagger,
lightweight=False):
"""Create a tag in this repository.
:param tag: (required), name of the tag
:type tag: str
:param message: (required), tag message
:type message: str
:param sha: (required), SHA of the git object this is tagging
:type sha: str
:param obj_type: (required), type of object being tagged, e.g.,
'commit', 'tree', 'blob'
:type obj_type: str
:param tagger: (required), containing the name, email of the
tagger and the date it was tagged
:type tagger: dict
:param lightweight: (optional), if False, create an annotated
tag, otherwise create a lightweight tag (a Reference).
:type lightweight: bool
:returns: If lightweight == False: :class:`Tag <github3.git.Tag>` if
successful, else None. If lightweight == True: :class:`Reference
<Reference>`
"""
if lightweight and tag and sha:
return self.create_ref('refs/tags/' + tag, sha)
json = None
if tag and message and sha and obj_type and len(tagger) == 3:
data = dumps({'tag': tag, 'message': message, 'object': sha,
'type': obj_type, 'tagger': tagger})
url = self._build_url('git', 'tags', base_url=self._api)
json = self._json(self._post(url, data), 201)
if json:
self.create_ref('refs/tags/' + tag, sha)
return Tag(json) if json else None
@requires_auth
def create_tree(self, tree, base_tree=''):
"""Create a tree on this repository.
:param tree: (required), specifies the tree structure.
Format: [{'path': 'path/file', 'mode':
'filemode', 'type': 'blob or tree', 'sha': '44bfc6d...'}]
:type tree: list of dicts
:param base_tree: (optional), SHA1 of the tree you want
to update with new data
:type base_tree: str
:returns: :class:`Tree <github3.git.Tree>` if successful, else None
"""
json = None
if tree and isinstance(tree, list):
data = dumps({'tree': tree, 'base_tree': base_tree})
url = self._build_url('git', 'trees', base_url=self._api)
json = self._json(self._post(url, data), 201)
return Tree(json) if json else None
@requires_auth
def delete(self):
"""Delete this repository.
:returns: bool -- True if successful, False otherwise
"""
return self._boolean(self._delete(self._api), 204, 404)
@requires_auth
def delete_key(self, key_id):
"""Delete the key with the specified id from your deploy keys list.
:returns: bool -- True if successful, False otherwise
"""
if int(key_id) <= 0:
return False
url = self._build_url('keys', str(key_id), base_url=self._api)
return self._boolean(self._delete(url), 204, 404)
def download(self, id_num):
"""Get a single download object by its id.
:param id_num: (required), id of the download
:type id_num: int
:returns: :class:`Download <Download>` if successful, else None
"""
json = None
if int(id_num) > 0:
url = self._build_url('downloads', str(id_num),
base_url=self._api)
json = self._json(self._get(url), 200)
return Download(json, self) if json else None
@requires_auth
def edit(self,
name,
description='',
homepage='',
private=False,
has_issues=True,
has_wiki=True,
has_downloads=True):
"""Edit this repository.
:param name: (required), name of the repository
:type name: str
:param description: (optional)
:type description: str
:param homepage: (optional)
:type homepage: str
:param private: (optional), If ``True``, create a
private repository. API default: ``False``
:type private: bool
:param has_issues: (optional), If ``True``, enable
issues for this repository. API default: ``True``
:type has_issues: bool
:param has_wiki: (optional), If ``True``, enable the
wiki for this repository. API default: ``True``
:type has_wiki: bool
:param has_downloads: (optional), If ``True``, enable
downloads for this repository. API default: ``True``
:type has_downloads: bool
:returns: bool -- True if successful, False otherwise
"""
data = dumps({'name': name, 'description': description,
'homepage': homepage, 'private': private,
'has_issues': has_issues, 'has_wiki': has_wiki,
'has_downloads': has_downloads})
json = self._json(self._patch(self._api, data=data), 200)
if json:
self._update_(json)
return True
return False
def is_collaborator(self, login):
"""Check to see if ``login`` is a collaborator on this repository.
:param login: (required), login for the user
:type login: str
:returns: bool -- True if successful, False otherwise
"""
if login:
url = self._build_url('collaborators', login, base_url=self._api)
return self._boolean(self._get(url), 204, 404)
return False
def is_fork(self):
"""Checks if this repository is a fork.
:returns: bool
"""
return self._is_fork
def is_private(self):
"""Checks if this repository is private.
:returns: bool
"""
return self._priv
def git_commit(self, sha):
"""Get a single (git) commit.
:param sha: (required), sha of the commit
:type sha: str
:returns: :class:`Commit <github3.git.Commit>` if successful,
otherwise None
"""
url = self._build_url('git', 'commits', sha, base_url=self._api)
json = self._json(self._get(url), 200)
return Commit(json, self) if json else None
def has_downloads(self):
"""Checks if this repository has downloads.
:returns: bool
"""
return self._has_dl
def has_issues(self):
"""Checks if this repository has issues enabled.
:returns: bool
"""
return self._has_issues
def has_wiki(self):
"""Checks if this repository has a wiki.
:returns: bool
"""
return self._has_wiki
@requires_auth
def hook(self, id_num):
"""Get a single hook.
:param id_num: (required), id of the hook
:type id_num: int
:returns: :class:`Hook <Hook>` if successful, else None
"""
json = None
if int(id_num) > 0:
url = self._build_url('hooks', str(id_num), base_url=self._api)
json = self._json(self._get(url), 200)
return Hook(json, self) if json else None
def is_assignee(self, login):
"""Check if the user is a possible assignee for an issue on this
repository.
:returns: :class:`bool`
"""
url = self._build_url('assignees', login, base_url=self._api)
return self._boolean(self._get(url), 204, 404)
def issue(self, number):
"""Get the issue specified by ``number``.
:param number: (required), number of the issue on this repository
:type number: int
:returns: :class:`Issue <github3.issues.Issue>` if successful, else
None
"""
json = None
if int(number) > 0:
url = self._build_url('issues', str(number), base_url=self._api)
json = self._json(self._get(url), 200)
return Issue(json, self) if json else None
@requires_auth
def key(self, id_num):
"""Get the specified deploy key.
:param id_num: (required), id of the key
:type id_num: int
:returns: :class:`Key <Key>` if successful, else None
"""
json = None
if int(id_num) > 0:
url = self._build_url('keys', str(id_num), base_url=self._api)
json = self._json(self._get(url), 200)
return Key(json, self) if json else None
def label(self, name):
"""Get the label specified by ``name``
:param name: (required), name of the label
:type name: str
:returns: :class:`Label <github3.issues.Label>` if successful, else
None
"""
json = None
if name:
url = self._build_url('labels', name, base_url=self._api)
json = self._json(self._get(url), 200)
return Label(json, self) if json else None
def list_assignees(self):
"""List all available assignees to which an issue may be assigned.
:returns: list of :class:`User <github3.users.User>`\ s
"""
url = self._build_url('assignees', base_url=self._api)
json = self._json(self._get(url), 200)
return [User(u, self) for u in json]
def list_branches(self):
"""List the branches in this repository.
:returns: list of :class:`Branch <Branch>`\ es
"""
# Paginate?
url = self._build_url('branches', base_url=self._api)
json = self._json(self._get(url), 200)
return [Branch(b, self) for b in json]
def list_comments(self):
"""List comments on all commits in the repository.
:returns: list of :class:`RepoComment <RepoComment>`\ s
"""
# Paginate?
url = self._build_url('comments', base_url=self._api)
json = self._json(self._get(url), 200)
return [RepoComment(comment, self) for comment in json]
def list_comments_on_commit(self, sha):
"""List comments for a single commit.
:param sha: (required), sha of the commit to list comments on
:type sha: str
:returns: list of :class:`RepoComment <RepoComment>`\ s
"""
# Paginate?
json = []
if sha:
url = self._build_url('commits', sha, 'comments',
base_url=self._api)
json = self._json(self._get(url), 200)
return [RepoComment(comm, self) for comm in json]
def list_commits(self, sha='', path='', author=''):
"""List 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)
:returns: list of :class:`RepoCommit <RepoCommit>`\ s
"""
# Paginate
url = self._build_url('commits', base_url=self._api)
json = self._json(self._get(url), 200)
return [RepoCommit(commit, self) for commit in json]
def list_contributors(self, anon=False):
"""List the contributors to this repository.
:param anon: (optional), True lists anonymous contributors as well
:type anon: bool
:returns: list of :class:`User <github3.users.User>`\ s
"""
# Paginate
url = self._build_url('contributors', base_url=self._api)
params = {}
if anon:
params = {'anon': anon}
json = self._json(self._get(url, params=params), 200)
return [User(c, self) for c in json]
def list_downloads(self):
"""List available downloads for this repository.
:returns: list of :class:`Download <Download>`\ s
"""
# Paginate?
url = self._build_url('downloads', base_url=self._api)
json = self._json(self._get(url), 200)
return [Download(dl, self) for dl in json]
def list_events(self):
"""List events on this repository.
:returns: list of :class:`Event <github3.events.Event>`\ s
"""
# Paginate
url = self._build_url('events', base_url=self._api)
json = self._json(self._get(url), 200)
return [Event(e, self) for e in json]
def list_forks(self, sort=''):
"""List forks of this repository.
:param sort: (optional), accepted values:
('newest', 'oldest', 'watchers'), API default: 'newest'
:type sort: str
:returns: list of :class:`Repository <Repository>`
"""
# Paginate?
url = self._build_url('forks', base_url=self._api)
params = {}
if sort in ('newest', 'oldest', 'watchers'):
params = {'sort': sort}
json = self._json(self._get(url, params=params), 200)
return [Repository(r, self) for r in json]
@requires_auth
def list_hooks(self):
"""List hooks registered on this repository.
:returns: list of :class:`Hook <Hook>`\ s
"""
# Paginate?
url = self._build_url('hooks', base_url=self._api)
json = self._json(self._get(url), 200)
return [Hook(h, self) for h in json]
def list_issues(self,
milestone=None,
state=None,
assignee=None,
mentioned=None,
labels=None,
sort=None,
direction=None,
since=None):
"""List issues on this repo based upon parameters passed.
:param milestone: (optional), 'none', or '*'
:type milestone: int
:param state: (optional), accepted values: ('open', 'closed')
:type state: str
:param assignee: (optional), 'none', '*', or login name
:type assignee: str
:param mentioned: (optional), user's login name
:type mentioned: str
:param labels: (optional), comma-separated list of labels, e.g.
'bug,ui,@high' :param sort: accepted values:
('created', 'updated', 'comments', 'created')
:type labels: str
:param direction: (optional), accepted values: ('open', 'closed')
:type direction: str
:param since: (optional), ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ
:type since: str
:returns: list of :class:`Issue <github3.issues.Issue>`\ s
"""
# Paginate
url = self._build_url('issues', base_url=self._api)
params = {}
if milestone in ('*', 'none') or isinstance(milestone, int):
params['milestone'] = str(milestone).lower()
# str(None) = 'None' which is invalid, so .lower() it to make it
# work.
if assignee:
params['assignee'] = assignee
if mentioned:
params['mentioned'] = mentioned
params.update(issue_params(None, state, labels, sort, direction,
since))
request = self._get(url, params=params)
json = self._json(request, 200)
return [Issue(i, self) for i in json]
def list_issue_events(self):
"""List issue events on this repository.
:returns: list of :class:`IssueEvent <github3.issues.IssueEvent>`\ s
"""
# Paginate
url = self._build_url('issues', 'events', base_url=self._api)
json = self._json(self._get(url), 200)
return [IssueEvent(e, self) for e in json]
@requires_auth
def list_keys(self):
"""List deploy keys on this repository.