-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
304 lines (270 loc) · 8.7 KB
/
index.js
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
const https = require('https')
const _ = require('underscore')
class GHProjectManager {
// @TODO The user/token logic will have to be replaced with oAuth tokens with an expiration
constructor(user, token) {
this.credentials = {
username: user,
token: token
}
// If this becomes a dedicated Integration we'll replace 'User-Agent' with the application name
var header = {
// Required for projects as of now
'Accept': 'application/vnd.github.inertia-preview+json',
'User-Agent': this.credentials.username
}
// Options that should be consistent every time (other than the credentials).
this.defaultOptions = {
host: 'api.github.com',
headers: header,
auth: this.credentials.username + ':' + this.credentials.token
}
// Initialize cache object to be empty.
// @TODO eventually save and load this from memory.
this.cache = {}
}
/*
* Gets the list of projects associated with the given owner and repo.
*
* @param string owner Owner username
* @param string repo Repository name
* @param boolean (optional) debug Display extra data?
*/
GetProjects(owner, repo, debug = false) {
var options = JSON.parse(JSON.stringify(this.defaultOptions))
options.path = '/repos/' + owner + '/' + repo + '/projects'
options.method = 'GET'
return this.Request(options, debug)
}
/*
* Gets the list of columns associated with the given owner, repo, and projectID.
*
* @param string owner Owner username
* @param string repo Repository name
* @param number projectID ID associated with the column
* @param boolean (optional) debug Display extra data?
*/
GetColumns(owner, repo, projectID, debug = false) {
var options = JSON.parse(JSON.stringify(this.defaultOptions))
options.path = '/repos/' + owner + '/' + repo + '/projects/' + projectID + '/columns'
options.method = 'GET'
return this.Request(options, debug)
}
ClearCache() {
this.cache = {}
}
/*
* Gets the list of cards associated with the given owner, repo, and columnID.
*
* @param string owner Owner username
* @param string repo Repository name
* @param number columnID ID associated with the cards
* @param boolean (optional) debug Display extra data?
*/
GetCards(owner, repo, columnID, debug = false) {
var options = JSON.parse(JSON.stringify(this.defaultOptions))
options.path = '/repos/' + owner + '/' + repo + '/projects/columns/' + columnID + '/cards'
options.method = 'GET'
return this.Request(options, debug)
}
/*
* Gets the list of issues associated with the given owner and repo.
*
* @param string owner Owner username
* @param string repo Repository name
* @param boolean (optional) debug Display extra data?
*/
GetIssues(owner, repo, debug = false) {
var options = JSON.parse(JSON.stringify(this.defaultOptions))
options.path = '/repos/' + owner + '/' + repo + '/issues'
options.method = 'GET'
return this.Request(options, debug)
}
/*
* Gets the list of labels associated with the given owner, repo, and issueID.
*
* @param string owner Owner username
* @param string repo Repository name
* @param number issueID
* @param boolean (optional) debug Display extra data?
*/
GetLabels(owner, repo, issueID, debug = false) {
return new Promise((resolve, reject) => {
this.GetIssue(owner, repo, issueID, debug).then(
(data) => {
data.response = data.response.labels
resolve(data)
})
})
}
/*
* Gets the issue associated with the given owner, repo, and issueID.
*
* @param string owner Owner username
* @param string repo Repository name
* @param number issueID
* @param boolean (optional) debug Display extra data?
*/
GetIssue(owner, repo, issueID, debug = false) {
return new Promise((resolve, reject) => {
this.GetIssues(owner, repo, debug).then(
(data) => {
let issueMatch = _.find(data.response, function(issue) {
return issue.id === issueID
})
data.response = issueMatch
// Maybe reject if there is no good data...
resolve(data)
})
})
}
/*
* Creates a card at the specified column
*
* @param string owner Owner username
* @param string repo Repository name
* @param number columnID
* @param number|string contentID issue or PR to associate with the card
* @param string contentType "Issue" or "PullRequest"
* @param boolean (optional) debug Display extra data?
*
* Return codes
* 200 - success
*/
CreateCard(owner, repo, columnID, contentID, contentType = '', debug = false) {
var options = JSON.parse(JSON.stringify(this.defaultOptions))
options.path = `/repos/${owner}/${repo}/projects/columns/${columnID}/cards`
options.method = 'POST'
options.headers['Content-Type'] = 'application/x-www-form-urlencoded'
var payload
if (typeof (contentID) !== 'number') {
payload = {
note: contentID
}
} else {
payload = {
content_id: contentID,
content_type: contentType
}
}
payload = JSON.stringify(payload)
options.headers['Content-Length'] = Buffer.byteLength(payload)
return this.Request(options, debug, payload)
}
/*
* Moves a card over to the specified Column
*
* @param string owner Owner username
* @param string repo Repository name
* @param number cardID
* @param number columnID
* @param string position [can be top, bottom, or after:<card-id>]
* @param boolean (optional) debug Display extra data?
*/
MoveCard(owner, repo, cardID, columnID, position = 'top', debug = false) {
var options = JSON.parse(JSON.stringify(this.defaultOptions))
options.path = `/repos/${owner}/${repo}/projects/columns/cards/${cardID}/moves`
options.method = 'POST'
options.headers['Content-Type'] = 'application/x-www-form-urlencoded'
var payload = {
position,
column_id: columnID
}
payload = JSON.stringify(payload)
options.headers['Content-Length'] = Buffer.byteLength(payload)
return this.Request(options, debug, payload)
}
/*
* Deletes a specified card
*
* @param string owner Owner username
* @param string repo Repository name
* @param number cardID
* @param boolean (optional) debug Display extra data?
*/
DeleteCard(owner, repo, cardID, debug = false) {
var options = JSON.parse(JSON.stringify(this.defaultOptions))
options.path = `/repos/${owner}/${repo}/projects/columns/cards/${cardID}`
options.method = 'DELETE'
return this.Request(options, debug)
}
/*
* Makes the web API request to GitHub with the given parameters.
*
* @param object options
* @param boolean (optional) debug Display extra data?
* @param object (optional) payload The arguments passed via POST
*/
Request(options, debug = false, payload = {}) {
const CODES = {
OK: 200,
CREATED: 201,
NO_CONTENT: 204,
NOT_MODIFIED: 304,
UNAUTHORIZED: 401,
FORBIDDEN: 403,
NOT_FOUND: 404
}
const ERROR_CODES = [CODES.UNAUTHORIZED, 402, CODES.FORBIDDEN, CODES.NOT_FOUND]
return new Promise((resolve, reject) => {
if (!_.isUndefined(this.cache[options.path])) {
options.headers['If-None-Match'] = this.cache[options.path].etag
}
var req = https.request(options, (res) => {
let response = ''
if (debug) {
console.log('status:', res.statusCode)
console.log('headers:', res.headers)
}
if (_.contains(ERROR_CODES, res.statusCode)) {
reject({header: res.headers, statusCode: res.statusCode})
}
if (options.method === 'DELETE') {
if (res.statusCode === CODES.NO_CONTENT) {
resolve({response: {}, statusCode: res.statusCode})
} else {
reject({header: res.headers, statusCode: res.statusCode})
}
}
res.on('data', (d) => {
response += d
})
req.on('error', (e) => {
if (debug) {
console.error(e)
}
reject({error: e, statusCode: res.statusCode})
})
res.on('end', () => {
var key = options.path
var formattedResponse = {}
// @TODO Put this into it's own function, possibly make the cache itself its own object.
// Mainly since caches should only work with GET requests, nothing else.
if (res.statusCode === CODES.OK) {
// Successful get
var etag = res.headers.etag
formattedResponse = JSON.parse(response)
this.cache[key] = {
etag: etag,
value: formattedResponse
}
resolve({response: formattedResponse, statusCode: res.statusCode})
} else if (res.statusCode === CODES.NOT_MODIFIED) {
// No change
resolve({response: this.cache[key].value, statusCode: res.statusCode})
} else if (res.statusCode === CODES.CREATED) {
// For creation
formattedResponse = JSON.parse(response)
resolve({response: formattedResponse, statusCode: res.statusCode})
}
})
})
if (options.method === 'POST') {
req.write(payload)
}
req.end()
})
}
}
// Export this class as our package
module.exports = GHProjectManager