forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrepos.py
More file actions
1692 lines (1438 loc) · 65.3 KB
/
Copy pathrepos.py
File metadata and controls
1692 lines (1438 loc) · 65.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 classes relating to repositories.
"""
from json import dumps
from base64 import b64decode
from collections import Callable
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
from github3.notifications import Subscription, Thread
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', 0)
#: Is this repository a fork?
self.fork = repo.get('fork')
#: Full name as login/name
self.full_name = repo.get('full_name', '')
# 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', '')
#: Whether or not this repository has downloads enabled
self.has_downloads = repo.get('has_downloads')
#: Whether or not this repository has an issue tracker
self.has_issues = repo.get('has_issues')
#: Whether or not this repository has the wiki enabled
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', 0)
#: 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', 0)
# 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.private = 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', 0)
# SSH url e.g. [email protected]/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', 0)
#: Parent of this fork, if it exists :class;`Repository`
self.source = repo.get('source')
if self.source:
self.source = Repository(self.source, self)
#: Parent of this fork, if it exists :class:`Repository`
self.parent = repo.get('parent')
if self.parent:
self.parent = Repository(self.parent, self)
#: default branch for the repository
self.master_branch = repo.get('master_branch', '')
def __repr__(self):
return '<Repository [{0}]>'.format(self)
def __str__(self):
return self.full_name
def _update_(self, repo):
self.__init__(repo, self._session)
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=dumps(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 str login: (required), login of the user
: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 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
written = False
if format in ('tarball', 'zipball'):
url = self._build_url(format, ref, base_url=self._api)
resp = self._get(url, allow_redirects=True, stream=True)
pre_opened = False
if resp and self._boolean(resp, 200, 404):
fd = None
if path:
if isinstance(getattr(path, 'write', None), Callable):
pre_opened = True
fd = path
else:
fd = open(path, 'wb')
else:
header = resp.headers['content-disposition']
i = header.find('filename=') + len('filename=')
fd = open(header[i:], 'wb')
for chunk in resp.iter_content(chunk_size=512):
fd.write(chunk)
if not pre_opened:
fd.close()
written = True
return written
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 Blob(json) if json else None
def branch(self, name):
"""Get the branch ``name`` of this repository.
:param str 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 str sha: (required), sha of the commit
: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 int comment_id: (required), id of the comment used by GitHub
: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 str base: (required), base for the comparison
:param str head: (required), compare this against base
: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 str path: (required), path to file, e.g.
github3/repo.py
: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 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') and content:
url = self._build_url('git', 'blobs', base_url=self._api)
data = {'content': content, 'encoding': encoding}
json = self._json(self._post(url, data=dumps(data)), 201)
if json:
sha = json.get('sha')
return sha
@requires_auth
def create_comment(self, body, sha, path='', position=1, 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 <RepoComment>` if successful else None
"""
line = int(line)
position = int(position)
json = None
if body and sha and line > 0:
data = {'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=dumps(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 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':
'[email protected]', '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}
json = self._json(self._post(url, data=dumps(data)), 201)
return Commit(json, self) if json else None
@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=dumps({'organization': 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 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 <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 = {'name': name, 'config': config, 'events': events,
'active': active}
json = self._json(self._post(url, data=dumps(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):
"""Creates 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), number of the milestone to attribute
this issue to (e.g. ``m`` is a Milestone object, ``m.number`` is
what you pass here.)
: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 = {'title': title, 'body': body, 'assignee': assignee,
'milestone': milestone, 'labels': labels}
self._remove_none(issue)
json = None
if issue:
url = self._build_url('issues', base_url=self._api)
json = self._json(self._post(url, data=dumps(issue)), 201)
return Issue(json, self) if json else None
@requires_auth
def create_key(self, title, key):
"""Create a deploy key.
:param str title: (required), title of key
:param str key: (required), key text
:returns: :class:`Key <github3.users.Key>` if successful, else None
"""
json = None
if title and key:
data = {'title': title, 'key': key}
url = self._build_url('keys', base_url=self._api)
json = self._json(self._post(url, data=dumps(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 str name: (required), name to give to the label
:param str color: (required), value of the color to assign to the
label
:returns: :class:`Label <github3.issues.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=dumps(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 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>` if successful,
else 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=dumps(data)), 201)
return Milestone(json, self) if json else None
@requires_auth
def create_pull(self, title, base, head, body=None):
"""Create a pull request using commits from ``head`` and comparing
against ``base``.
:param str title: (required)
:param str base: (required), e.g., 'username:branch', or a sha
:param str head: (required), e.g., 'master', or a sha
: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., 'username:branch', or a sha
:param str head: (required), e.g., 'master', or a sha
: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,
e.g. ``refs/heads/master``. If it doesn't start with ``refs`` and
contain at least two slashes, GitHub's API will reject it.
:param str sha: (required), SHA1 value to set the reference to
:returns: :class:`Reference <github3.git.Reference>` if successful
else None
"""
json = None
if ref and ref.count('/') >= 2 and sha:
data = {'ref': ref, 'sha': sha}
url = self._build_url('git', 'refs', base_url=self._api)
json = self._json(self._post(url, data=dumps(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 = {'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=dumps(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 str tag: (required), name of the tag
:param str message: (required), tag message
:param str sha: (required), SHA of the git object this is tagging
:param str obj_type: (required), type of object being tagged, e.g.,
'commit', 'tree', 'blob'
:param dict tagger: (required), containing the name, email of the
tagger and the date it was tagged
:param bool lightweight: (optional), if False, create an annotated
tag, otherwise create a lightweight tag (a Reference).
: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 = {'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=dumps(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 list tree: (required), specifies the tree structure.
Format: [{'path': 'path/file', 'mode':
'filemode', 'type': 'blob or tree', 'sha': '44bfc6d...'}]
:param str base_tree: (optional), SHA1 of the tree you want
to update with new data
:returns: :class:`Tree <github3.git.Tree>` if successful, else None
"""
json = None
if tree and isinstance(tree, list):
data = {'tree': tree, 'base_tree': base_tree}
url = self._build_url('git', 'trees', base_url=self._api)
json = self._json(self._post(url, data=dumps(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.
.. warning::
On 2012-03-11, GitHub will be deprecating the Downloads API. This
method will no longer work.
:param int id_num: (required), id of the download
: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=None,
homepage=None,
private=None,
has_issues=None,
has_wiki=None,
has_downloads=None,
default_branch=None):
"""Edit this repository.
:param str name: (required), name of the repository
:param str description: (optional), If not ``None``, change the
description for this repository. API default: ``None`` - leave
value unchanged.
:param str homepage: (optional), If not ``None``, change the homepage
for this repository. API default: ``None`` - leave value unchanged.
:param bool private: (optional), If ``True``, make the repository
private. If ``False``, make the repository public. API default:
``None`` - leave value unchanged.
:param bool has_issues: (optional), If ``True``, enable issues for
this repository. If ``False``, disable issues for this repository.
API default: ``None`` - leave value unchanged.
:param bool has_wiki: (optional), If ``True``, enable the wiki for
this repository. If ``False``, disable the wiki for this
repository. API default: ``None`` - leave value unchanged.
:param bool has_downloads: (optional), If ``True``, enable downloads
for this repository. If ``False``, disable downloads for this
repository. API default: ``None`` - leave value unchanged.
:param str default_branch: (optional), If not ``None``, change the
default branch for this repository. API default: ``None`` - leave
value unchanged.
:returns: bool -- True if successful, False otherwise
"""
edit = {'name': name, 'description': description, 'homepage': homepage,
'private': private, 'has_issues': has_issues,
'has_wiki': has_wiki, 'has_downloads': has_downloads,
'default_branch': default_branch}
self._remove_none(edit)
json = None
if edit:
json = self._json(self._patch(self._api, data=dumps(edit)), 200)
self._update_(json)
return True
return False
def is_collaborator(self, login):
"""Check to see if ``login`` is a collaborator on this repository.
:param str login: (required), login for the user
: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 git_commit(self, sha):
"""Get a single (git) commit.
:param str sha: (required), sha of the commit
:returns: :class:`Commit <github3.git.Commit>` if successful,
otherwise None
"""
json = {}
if sha:
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
@requires_auth
def hook(self, id_num):
"""Get a single hook.
:param int id_num: (required), id of the hook
: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`
"""
if not login:
return False
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 int number: (required), number of the issue on this repository
: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 int id_num: (required), id of the key
: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 str name: (required), name of the label
: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 iter_assignees(self, number=-1):
"""Iterate over all available assignees to which an issue may be
assigned.
:param int number: (optional), number of assignees to return. Default:
-1 returns all available assignees
:returns: list of :class:`User <github3.users.User>`\ s
"""
url = self._build_url('assignees', base_url=self._api)
return self._iter(int(number), url, User)
def iter_branches(self, number=-1):
"""Iterate over the branches in this repository.
:param int number: (optional), number of branches to return. Default:
-1 returns all branches
:returns: list of :class:`Branch <Branch>`\ es
"""
url = self._build_url('branches', base_url=self._api)
return self._iter(int(number), url, Branch)
def iter_comments(self, number=-1):
"""Iterate over comments on all commits in the repository.
:param int number: (optional), number of comments to return. Default:
-1 returns all comments
:returns: list of :class:`RepoComment <RepoComment>`\ s
"""
url = self._build_url('comments', base_url=self._api)
return self._iter(int(number), url, RepoComment)
def iter_comments_on_commit(self, sha, number=1):
"""Iterate over comments for a single commit.
:param sha: (required), sha of the commit to list comments on
:type sha: str
:param int number: (optional), number of comments to return. Default:
-1 returns all comments
:returns: list of :class:`RepoComment <RepoComment>`\ s
"""
url = self._build_url('commits', sha, 'comments', base_url=self._api)
return self._iter(int(number), url, RepoComment)
def iter_commits(self, sha=None, path=None, author=None, number=-1):
"""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
:returns: list of :class:`RepoCommit <RepoCommit>`\ s
"""
params = {'sha': sha, 'path': path, 'author': author}
self._remove_none(params)
url = self._build_url('commits', base_url=self._api)
return self._iter(int(number), url, RepoCommit, params=params)
def iter_contributors(self, anon=False, number=-1):
"""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
:returns: list of :class:`User <github3.users.User>`\ s
"""
url = self._build_url('contributors', base_url=self._api)
params = {}
if anon:
params = {'anon': True}
return self._iter(int(number), url, User, params=params)
def iter_downloads(self, number=-1):
"""Iterate over available downloads for this repository.
.. warning::
On 2012-03-11, GitHub will be deprecating the Downloads API. This
method will no longer work.
:param int number: (optional), number of downloads to return. Default:
-1 returns all available downloads
:returns: list of :class:`Download <Download>`\ s
"""
url = self._build_url('downloads', base_url=self._api)
return self._iter(int(number), url, Download)
def iter_events(self, number=-1):
"""Iterate over events on this repository.
:param int number: (optional), number of events to return. Default: -1
returns all available events
:returns: list of :class:`Event <github3.events.Event>`\ s
"""
url = self._build_url('events', base_url=self._api)
return self._iter(int(number), url, Event)
def iter_forks(self, sort='', number=-1):
"""Iterate over forks of this repository.
:param str sort: (optional), accepted values:
('newest', 'oldest', 'watchers'), API default: 'newest'
:param int number: (optional), number of forks to return. Default: -1
returns all forks
:returns: list of :class:`Repository <Repository>`
"""
url = self._build_url('forks', base_url=self._api)
params = {}
if sort in ('newest', 'oldest', 'watchers'):
params = {'sort': sort}
return self._iter(int(number), url, Repository, params=params)
@requires_auth
def iter_hooks(self, number=-1):
"""Iterate over hooks registered on this repository.
:param int number: (optional), number of hoks to return. Default: -1
returns all hooks
:returns: list of :class:`Hook <Hook>`\ s
"""
url = self._build_url('hooks', base_url=self._api)
return self._iter(int(number), url, Hook)
def iter_issues(self,
milestone=None,
state=None,
assignee=None,
mentioned=None,
labels=None,
sort=None,
direction=None,
since=None,
number=-1):
"""Iterate over issues on this repo based upon parameters passed.
:param int milestone: (optional), 'none', or '*'
:param str state: (optional), accepted values: ('open', 'closed')
:param str assignee: (optional), 'none', '*', or login name
:param str mentioned: (optional), user's login name
:param str labels: (optional), comma-separated list of labels, e.g.
'bug,ui,@high' :param sort: accepted values:
('created', 'updated', 'comments', 'created')
:param str direction: (optional), accepted values: ('asc', 'desc')
:param str since: (optional), ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ
:param int number: (optional), Number of issues to return.
By default all issues are returned
:returns: list of :class:`Issue <github3.issues.Issue>`\ s
"""
url = self._build_url('issues', base_url=self._api)
params = {'assignee': assignee, 'mentioned': mentioned}
if milestone in ('*', 'none') or isinstance(milestone, int):
params['milestone'] = milestone
self._remove_none(params)
params.update(issue_params(None, state, labels, sort, direction,
since)) # nopep8
return self._iter(int(number), url, Issue, params=params)
def iter_issue_events(self, number=-1):
"""Iterates over issue events on this repository.
:param int number: (optional), number of events to return. Default: -1
returns all available events
:returns: generator of
:class:`IssueEvent <github3.issues.IssueEvent>`\ s
"""
url = self._build_url('issues', 'events', base_url=self._api)
return self._iter(int(number), url, IssueEvent)
@requires_auth
def iter_keys(self, number=-1):
"""Iterates over deploy keys on this repository.
:param int number: (optional), number of keys to return. Default: -1
returns all available keys
:returns: generator of :class:`Key <github3.users.Key>`\ s
"""
url = self._build_url('keys', base_url=self._api)
return self._iter(int(number), url, Key)
def iter_labels(self, number=-1):
"""Iterates over labels on this repository.
:param int number: (optional), number of labels to return. Default: -1
returns all available labels
:returns: generator of :class:`Label <github3.issues.Label>`\ s
"""
url = self._build_url('labels', base_url=self._api)
return self._iter(int(number), url, Label)
def iter_languages(self, number=-1):
"""Iterate over the programming languages used in the repository.
:param int number: (optional), number of languages to return. Default:
-1 returns all used languages
:returns: list of tuples
"""
url = self._build_url('languages', base_url=self._api)
return self._iter(int(number), url, tuple)
def iter_milestones(self, state=None, sort=None, direction=None,
number=-1):
"""Iterates over the milestones on this repository.
:param str state: (optional), state of the milestones, accepted
values: ('open', 'closed')
:param str sort: (optional), how to sort the milestones, accepted
values: ('due_date', 'completeness')
:param str direction: (optional), direction to sort the milestones,
accepted values: ('asc', 'desc')
:param int number: (optional), number of milestones to return.
Default: -1 returns all milestones
:returns: generator of
:class:`Milestone <github3.issues.Milestone>`\ s
"""
url = self._build_url('milestones', base_url=self._api)
accepted = {'state': ('open', 'closed'),
'sort': ('due_date', 'completeness'),
'direction': ('asc', 'desc')}
params = {'state': state, 'sort': sort, 'direction': direction}
for (k, v) in list(params.items()):
if not (v and (v in accepted[k])): # e.g., '' or None
del params[k]
if not params:
params = None
return self._iter(int(number), url, Milestone, params)
def iter_network_events(self, number=-1):
"""Iterates over events on a network of repositories.
:param int number: (optional), number of events to return. Default: -1
returns all available events
:returns: generator of :class:`Event <github3.events.Event>`\ s
"""
base = self._api.replace('repos', 'networks', 1)
url = self._build_url('events', base_url=base)
return self._iter(int(number), url, Event)
@requires_auth
def iter_notifications(self, all=False, participating=False, since='',
number=-1):
"""Iterates over the notifications for this repository.
:param bool all: (optional), show all notifications, including ones
marked as read
:param bool participating: (optional), show only the notifications the
user is participating in directly
:param str since: (optional), filters out any notifications updated
before the given time. The time should be passed in as UTC in the
ISO 8601 format: ``YYYY-MM-DDTHH:MM:SSZ``. Example:
"2012-10-09T23:39:01Z".
:returns: generator of :class:`Thread <github3.notifications.Thread>`
"""