forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpulls.py
More file actions
391 lines (320 loc) · 11.3 KB
/
Copy pathpulls.py
File metadata and controls
391 lines (320 loc) · 11.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
"""
github3.pulls
=============
This module contains all the classes relating to pull requests.
"""
from json import dumps
from .git import Commit
from .models import GitHubObject, GitHubCore, BaseComment
from .users import User
class PullDestination(GitHubCore):
"""The :class:`PullDestination <PullDestination>` object."""
def __init__(self, dest, direction):
super(PullDestination, self).__init__(None)
self._dir = direction
self._ref = dest.get('ref')
self._label = dest.get('label')
self._user = None
if dest.get('user'):
self._user = User(dest.get('user'), None)
self._sha = dest.get('sha')
self._repo_name = ''
self._repo_owner = ''
if dest.get('repo'):
self._repo_name = dest['repo'].get('name')
self._repo_owner = dest['repo']['owner'].get('login')
def __repr__(self):
return '<{0} [{1}]>'.format(self._dir, self._label)
@property
def label(self):
"""label of the destination"""
return self._label
@property
def sha(self):
"""SHA of the commit at the head"""
return self._sha
@property
def ref(self):
"""Full reference string of the object"""
return self._ref
@property
def repo(self):
"""(owner, name) representing the repository this is on"""
return (self._repo_owner, self._repo_name)
@property
def user(self):
""":class:`User <github3.user.User>` representing the owner"""
return self._user
class PullFile(GitHubObject):
"""The :class:`PullFile <PullFile>` object."""
def __init__(self, pfile):
super(PullFile, self).__init__(pfile)
self._sha = pfile.get('sha')
self._name = pfile.get('filename')
self._status = pfile.get('status')
self._add = pfile.get('additions')
self._del = pfile.get('deletions')
self._changes = pfile.get('changes')
self._blob = pfile.get('blob_url')
self._raw = pfile.get('raw_url')
self._patch = pfile.get('patch')
def __repr__(self):
return '<Pull Request File [{0}]>'.format(self._name)
@property
def additions(self):
"""Number of additions on this file"""
return self._add
@property
def blob_url(self):
"""URL to view the blob for this file"""
return self._blob
@property
def changes(self):
"""Number of changes made to this file"""
return self._changes
@property
def deletions(self):
"""Number of deletions on this file"""
return self._del
@property
def filename(self):
"""Name of the file"""
return self._name
@property
def patch(self):
"""URL to view the patch"""
return self._patch
@property
def raw_url(self):
"""URL to view the raw diff of this file"""
return self._raw
@property
def sha(self):
"""SHA of the commit"""
return self._sha
@property
def status(self):
"""Status of the file, e.g., 'added'"""
return self._status
class PullRequest(GitHubCore):
"""The :class:`PullRequest <PullRequest>` object."""
def __init__(self, pull, session=None):
super(PullRequest, self).__init__(pull, session)
self._update_(pull)
def __repr__(self):
return '<Pull Request [#{0}]>'.format(self._num)
def _update_(self, pull):
self._json_data = pull
self._api = pull.get('url')
self._base = PullDestination(pull.get('base'), 'Base')
self._body = pull.get('body')
self._closed = None
# If the pull request has been closed
if pull.get('closed_at'):
self._closed = self._strptime(pull.get('closed_at'))
self._created = self._strptime(pull.get('created_at'))
self._diff = pull.get('diff_url')
self._head = PullDestination(pull.get('head'), 'Head')
self._url = pull.get('html_url')
self._id = pull.get('id')
self._issue = pull.get('issue_url')
# These are the links provided by the dictionary in the json called
# '_links'. It's structure is horrific, so to make this look a lot
# cleaner, I reconstructed what the links would be:
# - ``self`` is just the api url, e.g.,
# https://api.github.com/repos/:user/:repo/pulls/:number
# - ``comments`` is just the api url for comments on the issue, e.g.,
# https://api.github.com/repos/:user/:repo/issues/:number/comments
# - ``issue`` is the api url for the issue, e.g.,
# https://api.github.com/repos/:user/:repo/issues/:number
# - ``html`` is just the html_url attribute
# - ``review_comments`` is just the api url for the pull, e.g.,
# https://api.github.com/repos/:user/:repo/pulls/:number/comments
self._links = {
'self': self._api,
'comments': '/'.join([self._api.replace('pulls', 'issues'),
'comments']),
'issue': self._api.replace('pulls', 'issues'),
'html': self._url,
'review_comments': self._api + '/comments'
}
self._merged = None
# If the pull request has been merged
if pull.get('merged_at'):
self._merged = self._strptime(pull.get('merged_at'))
self._mergeable = pull.get('mergeable')
self._mergedby = None
if pull.get('merged_by'):
self._mergedby = User(pull.get('merged_by'), self)
self._num = pull.get('number')
self._patch_url = pull.get('patch_url')
self._state = pull.get('state')
self._title = pull.get('title')
self._updated = self._strptime(pull.get('updated_at'))
self._user = None
if pull.get('user'):
self._user = User(pull.get('user'), self)
@property
def base(self):
"""Base of the merge"""
return self._base
@property
def body(self):
"""Body of the pull request message"""
return self._body
@property
def closed_at(self):
"""datetime object representing when the pull was closed"""
return self._closed
@property
def created_at(self):
"""datetime object representing when the pull was created"""
return self._created
@property
def diff_url(self):
"""URL to view the diff associated with the pull"""
return self._diff
@property
def head(self):
"""The new head after the pull request"""
return self._head
@property
def html_url(self):
"""The URL of the pull request"""
return self._url
@property
def id(self):
"""The unique id of the pull request"""
return self._id
def is_mergeable(self):
"""Checks to see if the pull request can be merged by GitHub.
:returns: bool
"""
return self._mergeable
def is_merged(self):
"""Checks to see if the pull request was merged.
:returns: bool
"""
url = self._build_url('merge', base_url=self._api)
return self._boolean(self._get(url), 204, 404)
@property
def issue_url(self):
"""The URL of the associated issue"""
return self._issue
@property
def links(self):
"""Dictionary of _links"""
return self._links
def list_comments(self):
"""List the comments on this pull request.
:returns: list of :class:`ReviewComment <ReviewComment>`\ s
"""
url = self._build_url('comments', base_url=self._api)
json = self._json(self._get(url), 200)
return [ReviewComment(comment, self) for comment in json]
def list_commits(self):
"""List the commits on this pull request.
:returns: list of :class:`Commit <github3.git.Commit>`\ s
"""
url = self._build_url('commits', base_url=self._api)
json = self._json(self._get(url), 200)
return [Commit(commit, self) for commit in json]
def list_files(self):
"""List the files associated with this pull request.
:returns: list of :class:`PullFile <PullFile>`\ s
"""
url = self._build_url('files', base_url=self._api)
json = self._json(self._get(url), 200)
return [PullFile(f) for f in json]
@GitHubCore.requires_auth
def merge(self, commit_message=''):
"""Merge this pull request.
:param commit_message: (optional), message to be used for the merge
commit
:type commit_message: str
:returns: bool
"""
data = None
if commit_message:
data = dumps({'commit_message': commit_message})
url = self._build_url('merge', base_url=self._api)
resp = self._put(url, data)
return resp.json['merged']
@property
def merged_at(self):
"""datetime object representing when the pull was merged"""
return self._merged
@property
def merged_by(self):
""":class:`User <github3.user.User>` who merged this pull"""
return self._mergedby
@property
def number(self):
"""Number of the pull/issue on the repository"""
return self._num
@property
def patch_url(self):
"""The URL of the patch"""
return self._patch_url
@property
def state(self):
"""The state of the pull"""
return self._state
@property
def title(self):
"""The title of the request"""
return self._title
@GitHubCore.requires_auth
def update(self, title='', body='', state=''):
"""Update this pull request.
:param title: (optional), title of the pull
:type title: str
:param body: (optional), body of the pull request
:type body: str
:param state: (optional), ('open', 'closed')
:type state: str
:returns: bool
"""
data = dumps({'title': title, 'body': body, 'state': state})
json = self._json(self._patch(self._api, data=data), 200)
if json:
self._update_(json)
return True
return False
@property
def user(self):
""":class:`User <github3.user.User>` object representing the creator of
the pull request"""
return self._user
class ReviewComment(BaseComment):
"""The :class:`ReviewComment <ReviewComment>` object. This is used to
represent comments on pull requests.
"""
def __init__(self, comment, session=None):
super(ReviewComment, self).__init__(comment, session)
self._user = None
if comment.get('user'):
self._user = User(comment.get('user'), self)
def __repr__(self):
return '<Review Comment [{0}]>'.format(self._user.login)
@property
def commit_id(self):
"""SHA of the commit the comment is on"""
return self._cid
@property
def html_url(self):
"""URL of the comment"""
return self._url
@property
def path(self):
"""Path to the file"""
return self._path
@property
def position(self):
"""Position within the commit"""
return self._pos
@property
def updated_at(self):
"""datetime object representing the last time the object was
updated."""
return self._updated