-
Notifications
You must be signed in to change notification settings - Fork 407
/
Copy pathprojects.py
564 lines (454 loc) · 16.6 KB
/
projects.py
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
"""This module contains all the classes relating to projects."""
from json import dumps
from . import exceptions
from . import models
from . import pulls
from . import users
from .decorators import requires_auth
from .issues import issue
class Project(models.GitHubCore):
"""Object representing a single project from the API.
See http://developer.github.com/v3/projects/ for more details.
.. attribute:: body
The Markdown formatted text describing the project.
.. attribute:: created_at
A :class:`~datetime.datetime` representing the date and time when
this project was created.
.. attribute:: creator
A :class:`~github3.users.ShortUser` instance representing the user who
created this project.
.. attribute:: id
The unique identifier for this project on GitHub.
.. attribute:: name
The name given to this project.
.. attribute:: number
The repository-local identifier of this project.
.. attribute:: owner_url
The URL of the resource in the API of the owning resource - either
a repository or an organization.
.. attribute:: updated_at
A :class:`~datetime.datetime` representing the date and time when
this project was last updated.
"""
CUSTOM_HEADERS = {"Accept": "application/vnd.github.inertia-preview+json"}
def _update_attributes(self, project):
self._api = project["url"]
self.body = project["body"]
self.created_at = self._strptime(project["created_at"])
self.creator = users.ShortUser(project["creator"], self)
self.id = project["id"]
self.name = project["name"]
self.number = project["number"]
self.owner_url = project["owner_url"]
self.updated_at = self._strptime(project["updated_at"])
def _repr(self):
return f"<Project [#{self.id}]>"
def column(self, id):
"""Get a project column with the given ID.
:param int id:
(required), the column ID
:returns:
the desired column in the project
:rtype:
:class:`~github3.projects.ProjectColumn`
"""
url = self._build_url("projects", "columns", str(id))
json = self._json(self._get(url, headers=Project.CUSTOM_HEADERS), 200)
return self._instance_or_null(ProjectColumn, json)
def columns(self, number=-1, etag=None):
"""Iterate over the columns in this project.
:param int number:
(optional), number of columns to return. Default: -1 returns all
available columns.
:param str etag:
(optional), ETag from a previous request to the same endpoint
:returns:
generator of columns
:rtype:
:class:`~github3.project.ProjectColumn`
"""
# TODO(sigmaviurs24): Determine if we need to construct from scratch
# or if we can use `self._api` with 'columns' to build the URL
url = self._build_url("projects", str(self.id), "columns")
return self._iter(
int(number),
url,
ProjectColumn,
headers=Project.CUSTOM_HEADERS,
etag=etag,
)
@requires_auth
def create_column(self, name):
"""Create a column in this project.
:param str name:
(required), name of the column
:returns:
the created project column
:rtype:
:class:`~github3.projects.ProjectColumn`
"""
url = self._build_url("columns", base_url=self._api)
json = None
if name:
json = self._json(
self._post(
url, data={"name": name}, headers=Project.CUSTOM_HEADERS
),
201,
)
return self._instance_or_null(ProjectColumn, json)
@requires_auth
def delete(self):
"""Delete this project.
:returns:
True if successfully deleted, False otherwise
:rtype:
bool
"""
return self._boolean(
self._delete(self._api, headers=self.CUSTOM_HEADERS), 204, 404
)
@requires_auth
def update(self, name=None, body=None):
"""Update this project.
:param str name:
(optional), new name of the project
:param str body:
(optional), new body of the project
:returns:
True if successfully updated, False otherwise
:rtype:
bool
"""
data = {"name": name, "body": body}
json = None
self._remove_none(data)
if data:
json = self._json(
self._patch(
self._api, data=dumps(data), headers=self.CUSTOM_HEADERS
),
200,
)
if json:
self._update_attributes(json)
return True
return False
class ProjectColumn(models.GitHubCore):
"""Object representing a column in a project.
See http://developer.github.com/v3/projects/columns/
.. attribute:: created_at
A :class:`~datetime.datetime` object representing the date and time
when the column was created.
.. attribute:: id
The unique identifier for this column across GitHub.
.. attribute:: name
The name given to this column.
.. attribute:: project_url
The URL used to retrieve the project that owns this column via the API.
.. attribute:: updated_at
A :class:`~datetime.datetime` object representing the date and time
when the column was last updated.
"""
def _update_attributes(self, project_column):
self.created_at = self._strptime(project_column["created_at"])
self.id = project_column["id"]
self.name = project_column["name"]
self.project_url = project_column["project_url"]
self.updated_at = self._strptime(project_column["updated_at"])
def _repr(self):
return f"<ProjectColumn [#{self.id}]>"
def card(self, id):
"""Get a project card with the given ID.
:param int id:
(required), the card ID
:returns:
the card identified by the provided id
:rtype:
:class:`~github3.projects.ProjectCard`
"""
url = self._build_url("projects", "columns", "cards", str(id))
json = self._json(self._get(url, headers=Project.CUSTOM_HEADERS), 200)
return self._instance_or_null(ProjectCard, json)
def cards(self, number=-1, etag=None):
"""Iterate over the cards in this column.
:param int number:
(optional), number of cards to return. Default: -1 returns all
available cards.
:param str etag:
(optional), ETag from a previous request to the same endpoint
:returns:
generator of cards
:rtype:
:class:`~github3.project.ProjectCard`
"""
url = self._build_url("projects", "columns", str(self.id), "cards")
return self._iter(
int(number),
url,
ProjectCard,
headers=Project.CUSTOM_HEADERS,
etag=etag,
)
@requires_auth
def create_card_with_content_id(self, content_id, content_type):
"""Create a content card in this project column.
:param int content_id:
(required), the ID of the content
:param str content_type:
(required), the type of the content
:returns:
the created card
:rtype:
:class:`~github3.projects.ProjectCard`
"""
if not content_id or not content_type:
return None
url = self._build_url("projects", "columns", str(self.id), "cards")
json = None
data = {"content_id": content_id, "content_type": content_type}
json = self._json(
self._post(url, data=data, headers=Project.CUSTOM_HEADERS), 201
)
return self._instance_or_null(ProjectCard, json)
@requires_auth
def create_card_with_issue(self, issue):
"""Create a card in this project column linked with an Issue.
:param issue:
(required), an issue with which to link the card. Can also be
:class:`~github3.issues.ShortIssue`.
:type issue:
:class:`~github3.issues.Issue`
:returns:
the created card
:rtype:
:class:`~github3.projects.ProjectCard`
"""
if not issue:
return None
return self.create_card_with_content_id(issue.id, "Issue")
@requires_auth
def create_card_with_note(self, note):
"""Create a note card in this project column.
:param str note:
(required), the note content
:returns:
the created card
:rtype:
:class:`~github3.projects.ProjectCard`
"""
url = self._build_url("projects", "columns", str(self.id), "cards")
json = None
if note:
json = self._json(
self._post(
url, data={"note": note}, headers=Project.CUSTOM_HEADERS
),
201,
)
return self._instance_or_null(ProjectCard, json)
@requires_auth
def delete(self):
"""Delete this column.
:returns:
True if successful, False otherwise
:rtype:
bool
"""
url = self._build_url("projects", "columns", str(self.id))
return self._boolean(
self._delete(url, headers=Project.CUSTOM_HEADERS), 204, 404
)
@requires_auth
def move(self, position):
"""Move this column.
:param str position:
(required), can be one of `first`, `last`, or `after:<column-id>`,
where `<column-id>` is the id value of a column in the same
project.
:returns:
True if successful, False otherwise
:rtype:
bool
"""
if not position:
return False
url = self._build_url("projects", "columns", str(self.id), "moves")
data = {"position": position}
return self._boolean(
self._post(url, data=data, headers=Project.CUSTOM_HEADERS),
201,
404,
)
@requires_auth
def update(self, name=None):
"""Update this column.
:param str name:
(optional), name of the column
:returns:
True if successful, False otherwise
:rtype:
bool
"""
data = {"name": name}
json = None
self._remove_none(data)
if data:
url = self._build_url("projects", "columns", str(self.id))
json = self._json(
self._patch(
url, data=dumps(data), headers=Project.CUSTOM_HEADERS
),
200,
)
if json:
self._update_attributes(json)
return True
return False
class ProjectCard(models.GitHubCore):
"""Object representing a "card" on a project.
See http://developer.github.com/v3/projects/cards/
.. attribute:: column_url
The URL to retrieve this card's column via the API.
.. attribute:: content_url
The URl to retrieve this card's body content via the API.
.. attribute:: created_at
A :class:`~datetime.datetime` object representing the date and time
when the column was created.
.. attribute:: id
The globally unique identifier for this card.
.. attribute:: note
The body of the note attached to this card.
.. attribute:: updated_at
A :class:`~datetime.datetime` object representing the date and time
when the column was last updated.
"""
def _update_attributes(self, project_card):
#: The URL of this card's parent column
self.column_url = project_card["column_url"]
#: The URL of this card's associated content
self.content_url = project_card.get("content_url")
#: datetime object representing the last time the object was created
self.created_at = project_card["created_at"]
#: The ID of this card
self.id = project_card["id"]
#: The note attached to the card
self.note = project_card["note"]
#: datetime object representing the last time the object was changed
self.updated_at = project_card["updated_at"]
def _repr(self):
return f"<ProjectCard [#{self.id}]>"
@requires_auth
def delete(self):
"""Delete this card.
:returns:
True if successfully deleted, False otherwise
:rtype:
bool
"""
url = self._build_url("projects", "columns", "cards", str(self.id))
return self._boolean(
self._delete(url, headers=Project.CUSTOM_HEADERS), 204, 404
)
@requires_auth
def move(self, position, column_id):
"""Move this card.
:param str position:
(required), can be one of `top`, `bottom`, or `after:<card-id>`,
where `<card-id>` is the id value of a card in the same column, or
in the new column specified by `column_id`.
:param int column_id:
(required), the id value of a column in the same project.
:returns:
True if successfully moved, False
:rtype:
bool
"""
if not position or not column_id:
return False
url = self._build_url(
"projects", "columns", "cards", str(self.id), "moves"
)
data = {"position": position, "column_id": column_id}
return self._boolean(
self._post(url, data=data, headers=Project.CUSTOM_HEADERS),
201,
404,
)
@requires_auth
def update(self, note=None):
"""Update this card.
:param str note:
(optional), the card's note content. Only valid for cards without
another type of content, so this cannot be specified if the card
already has a content_id and content_type.
:returns:
True if successfully updated, False otherwise
:rtype:
bool
"""
data = {"note": note}
json = None
self._remove_none(data)
if data:
url = self._build_url(
"projects", "columns", "cards", str(self.id)
)
json = self._json(
self._patch(
url, data=dumps(data), headers=Project.CUSTOM_HEADERS
),
200,
)
if json:
self._update_attributes(json)
return True
return False
def retrieve_issue_from_content(self):
"""Attempt to retrieve an Issue from the content url.
:returns:
The issue that backs up this particular project card if the card
has a content_url.
.. note::
Cards can be created from Issues and Pull Requests. Pull
Requests are also technically Issues so this method is always
safe to call.
:rtype:
:class:`~github3.issues.issue.Issue`
:raises:
:class:`~github3.exceptions.CardHasNoContentUrl`
"""
if self.content_url is None:
raise exceptions.CardHasNoContentUrl(
f"Card {self.id} has no content_url"
)
parsed = self._uri_parse(self.content_url)
_, owner, repository, _, number = parsed.path[1:].split("/", 5)
resp = self._get(
self._build_url("repos", owner, repository, "issues", number)
)
json = self._json(resp, 200)
return self._instance_or_null(issue.Issue, json)
def retrieve_pull_request_from_content(self):
"""Attempt to retrieve an PullRequest from the content url.
:returns:
The pull request that backs this particular project card if the
card has a content_url.
.. note::
Cards can be created from Issues and Pull Requests.
:rtype:
:class:`~github3.issues.issue.Issue`
:raises:
:class:`~github3.exceptions.CardHasNoContentUrl`
"""
if self.content_url is None:
raise exceptions.CardHasNoContentUrl(
f"Card {self.id} has no content_url"
)
parsed = self._uri_parse(self.content_url)
_, owner, repository, _, number = parsed.path[1:].split("/", 5)
resp = self._get(
self._build_url("repos", owner, repository, "pulls", number)
)
json = self._json(resp, 200)
return self._instance_or_null(pulls.PullRequest, json)