forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojects.py
More file actions
389 lines (313 loc) · 12.1 KB
/
projects.py
File metadata and controls
389 lines (313 loc) · 12.1 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
# -*- coding: utf-8 -*-
"""
github3.projects
=============
This module contains all the classes relating to projects.
"""
from json import dumps
from . import models
from . import users
from .decorators import requires_auth
class Project(models.GitHubCore):
"""The :class:`Project <Project>` object.
See http://developer.github.com/v3/projects/
"""
CUSTOM_HEADERS = {
'Accept': 'application/vnd.github.inertia-preview+json',
}
def _update_attributes(self, project):
self._api = project['url']
#: The body of the project
self.body = project['body']
#: datetime object representing when the project was created
self.created_at = self._strptime(project['created_at'])
#: The user who created this project
self.creator = users.ShortUser(project['creator'], self)
#: The unique ID of the project
self.id = project['id']
#: The name of this project
self.name = project['name']
#: The number of the project
self.number = project['number']
#: The owner repo or organisation of this project
self.owner_url = project['owner_url']
#: datetime object representing the last time the object was changed
self.updated_at = self._strptime(project['updated_at'])
def _repr(self):
return '<Project [#{0}]>'.format(self.id)
def column(self, id):
"""Get a project column with the given ID.
:param int id: (required), the column ID
:returns: :class:`ProjectColumn <github3.projects.ProjectColumn>`
or None
"""
url = self._build_url(
'projects', 'columns', str(id), base_url=self._github_url)
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
:class:`ProjectColumn <github3.project.ProjectColumn>`
"""
url = self._build_url(
'projects', str(self.id), 'columns', base_url=self._github_url)
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: :class:`ProjectColumn <github3.projects.ProjectColumn>`
or none
"""
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: 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), name of the project
:param str body: (optional), body of the project
:returns: 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):
"""The :class:`ProjectColumn <ProjectColumn>` object.
See http://developer.github.com/v3/projects/columns/
"""
def _update_attributes(self, project_column):
#: datetime object representing the last time the object was created
self.created_at = self._strptime(project_column['created_at'])
#: The ID of this column
self.id = project_column['id']
#: The name of this column
self.name = project_column['name']
#: The URL of this column's project
self.project_url = project_column['project_url']
#: datetime object representing the last time the object was changed
self.updated_at = self._strptime(project_column['updated_at'])
def _repr(self):
return '<ProjectColumn [#{0}]>'.format(self.id)
def card(self, id):
"""Get a project card with the given ID.
:param int id: (required), the card ID
:returns: :class:`ProjectCard <github3.projects.ProjectCard>` or None
"""
url = self._build_url(
'projects/columns/cards', str(id), base_url=self._github_url)
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
:class:`ProjectCard <github3.project.ProjectCard>`
"""
url = self._build_url(
'projects/columns',
str(self.id),
'cards',
base_url=self._github_url
)
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: :class:`ProjectCard <github3.projects.ProjectCard>` or none
"""
if not content_id or not content_type:
return None
url = self._build_url(
'projects/columns',
str(self.id),
'cards',
base_url=self._github_url
)
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 :class:`Issue <github3.issues.Issue>`: (required), an issue
with which to link the card. Can also be
:class:`ShortIssue <github3.issues.ShortIssue>`.
:returns: :class:`ProjectCard <github3.projects.ProjectCard>` or none
"""
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: :class:`ProjectCard <github3.projects.ProjectCard>` or none
"""
url = self._build_url(
'projects/columns',
str(self.id),
'cards',
base_url=self._github_url
)
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: bool
"""
url = self._build_url(
'projects/columns', self.id, base_url=self._github_url)
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: bool
"""
if not position:
return False
url = self._build_url(
'projects/columns',
self.id,
'moves',
base_url=self._github_url
)
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: bool
"""
data = {'name': name}
json = None
self._remove_none(data)
if data:
url = self._build_url(
'projects/columns', self.id, base_url=self._github_url)
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):
"""The :class:`ProjectCard <ProjectCard>` object.
See http://developer.github.com/v3/projects/cards/
"""
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 '<ProjectCard [#{0}]>'.format(self.id)
@requires_auth
def delete(self):
"""Delete this card.
:returns: bool
"""
url = self._build_url(
'projects/columns/cards', self.id, base_url=self._github_url)
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: bool
"""
if not position or not column_id:
return False
url = self._build_url(
'projects/columns/cards',
self.id,
'moves',
base_url=self._github_url
)
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: bool
"""
data = {'note': note}
json = None
self._remove_none(data)
if data:
url = self._build_url(
'projects/columns/cards', self.id, base_url=self._github_url)
json = self._json(self._patch(
url, data=dumps(data), headers=Project.CUSTOM_HEADERS), 200)
if json:
self._update_attributes(json)
return True
return False