-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
353 lines (325 loc) · 11 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
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
'use strict';
/* jshint node:true */
var request = require('request'),
_ = require('underscore'),
querystring = require('querystring'),
fs = require('fs'),
path = require('path'),
Pivotal;
/**
* Pivotal API Interface
* @constructor
* @param {string} apiToken Pivotal API Token
*/
Pivotal = function Pivotal(apiToken) {
this.apiToken = apiToken;
this.baseUrl = 'https://www.pivotaltracker.com/services/v5/';
};
/**
* Update a story
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {Object} [params] Extra parameters
* @param {Function} [callback] function(error, response)
*/
Pivotal.prototype.updateStory = function updateStory(projectId, storyId, params, callback) {
this.api('put', 'projects/' + projectId + '/stories/' + storyId, {
json: params
}, callback);
};
/**
* Delete a story
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {Function} [callback] function(error, response)
*/
Pivotal.prototype.deleteStory = function deleteStory(projectId, storyId, callback) {
this.api('delete', 'projects/' + projectId + '/stories/' + storyId, {}, callback);
};
/**
* Obtains a story
* @param {String} storyId Pivotal story id
* @param {Function} [callback] function(error, response)
*/
Pivotal.prototype.getStory = function getStory(storyId, callback) {
this.api('get', '/stories/' + storyId, {}, callback);
};
/**
* Obtains list of projects
* @param {Function} [callback] function(error, projects)
*/
Pivotal.prototype.getProjects = function getProjects(callback) {
this.api('get', '/projects', {}, callback);
};
/**
* Get paginated stories from project
* @param {String} projectId Pivotal project id
* @param {Object} [options] Extra options
* @param {Function} [callback] function(stories, pagination, callback(error or true to stop pagination))
* @param {Function} [completed] function(error)
* @param {Integer} [offset] Initial pagination offset
* @param {Integer} [limit] Pagination limit for each response
*/
Pivotal.prototype.getStories = function getStories(projectId, options, callback, completed, offset, limit) {
this.paginated('projects/' + projectId + '/stories', offset || 0, limit || 128, options, callback, completed);
};
/**
* Get paginated activity from project
* @param {String} projectId Pivotal project id
* @param {Object} [options] Extra options
* @param {Function} [callback] function(events, pagination, callback(error or true to stop pagination))
* @param {Function} [completed] function(error)
* @param {Integer} [offset] Initial pagination offset
* @param {Integer} [limit] Pagination limit for each response
*/
Pivotal.prototype.getActivity = function getActivity(projectId, options, callback, completed, offset, limit) {
this.paginated('projects/' + projectId + '/activity', offset || 0, limit || 128, options, callback, completed);
};
/**
* Get activity for story
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {Function} [callback] function(error, events)
* @param {Object} [options] Extra options
*/
Pivotal.prototype.getStoryActivity = function getStoryActivity(projectId, storyId, callback, options) {
this.api('get', 'projects/' + projectId + '/stories/' + storyId + '/activity', {
qs: options || {}
}, callback);
};
/**
* Get API token owner activity
* @param {Function} [callback] function(error, events)
* @param {Object} [options] Extra options
*/
Pivotal.prototype.getMyActivity = function getMyActivity(callback, options) {
this.api('get', 'my/activity', {
qs: options || {}
}, callback);
};
/**
* Get tasks for story
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {Function} [callback] function(error, tasks)
*/
Pivotal.prototype.getTasks = function getTasks(projectId, storyId, callback) {
this.api('get', 'projects/' + projectId + '/stories/' + storyId + '/tasks', {}, callback);
};
/**
* Add story task
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {String} description Description
* @param {int} position Position
* @param {Function} [callback] function(error, label)
*/
Pivotal.prototype.addTask = function addTask(projectId, storyId, description, position, callback) {
this.api('post', 'projects/' + projectId + '/stories/' + storyId + '/tasks', {
body: {
description: description,
position: position
}
}, callback);
};
/**
* Get paginated iterations for project
* @param {String} projectId Pivotal project id
* @param {Object} [options] Extra options
* @param {Function} [callback] function(iterations, pagination, callback(error or true to stop pagination))
* @param {Function} [completed] function(error)
* @param {Integer} [offset] Initial pagination offset
* @param {Integer} [limit] Pagination limit for each response
*/
Pivotal.prototype.getIterations = function getIterations(projectId, options, callback, completed, offset, limit) {
this.paginated('projects/' + projectId + '/iterations', offset || 0, limit || 128, options, callback, completed);
};
/**
* Get current iteration stories from project
* @param {String} projectId Pivotal project id
* @param {Function} [callback] function(error, iterations)
*/
Pivotal.prototype.getCurrentIterations = function getCurrentIterations(projectId, callback) {
this.api('get', 'projects/' + projectId + '/iterations', {
qs: {
scope: 'current',
date_format: 'millis'
}
}, function(err, iterations) {
if (_.isFunction(callback)) {
if (err || !iterations) {
callback(err);
} else {
callback(false, iterations);
}
}
});
};
/**
* Get memberships for project
* @param {String} projectId Pivotal project id
* @param {Function} [callback] function(error, memberships)
*/
Pivotal.prototype.getMemberships = function getMemberships(projectId, callback) {
this.api('get', 'projects/' + projectId + '/memberships', {}, callback);
};
/**
* Get comments from story
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {Function} [callback] function(error, comments)
*/
Pivotal.prototype.getComments = function getComments(projectId, storyId, callback) {
this.api('get', 'projects/' + projectId + '/stories/' + storyId + '/comments', {}, callback);
};
/**
* Export stories from Pivotal
* @param {String[]} stories List of story id's
* @param {Function} [callback] function(error, response)
*/
Pivotal.prototype.exportStories = function exportStories(stories, callback) {
this.api('post', 'stories/export', {
body: querystring.stringify({
'ids[]': stories
})
}, callback);
};
/**
* Post attachment to story
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {String} filepath Path of attachment
* @param {String} type Content type of attachment
* @param {String} comment Comment text
* @param {Function} [callback] function(error, response)
*/
Pivotal.prototype.postAttachment = function postAttachment(projectId, storyId, filepath, type, comment, callback) {
var that = this;
fs.readFile(filepath, function(err, contents) {
request.post({
url: "https://www.pivotaltracker.com/services/v5/projects/" + projectId + "/uploads",
multipart: [{
"Content-Disposition": "form-data; name=\"file\"; filename=\"" + path.basename(filepath) + "\"",
"Content-Type": type,
"body": contents
}],
headers: {
"X-TrackerToken": that.apiToken
}
}, function(err, res, upload) {
if (err || upload.kind == 'error') {
callback(err, {
success: false,
error: upload ? upload.error + ' (' + upload.general_problem + ')' : err
});
} else {
that.api('post', 'projects/' + projectId + '/stories/' + storyId + '/comments', {
json: {
text: comment,
file_attachments: [JSON.parse(upload)]
}
}, callback);
}
});
});
};
/**
* Get all labels in project
* @param {String} projectId Pivotal project id
* @param {Function} [callback] function(error, labels)
*/
Pivotal.prototype.getLabels = function getLabels(projectId, callback) {
this.api('get', 'projects/' + projectId + '/labels', {}, callback);
};
/**
* Get labels on story
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {Function} [callback] function(error, labels)
*/
Pivotal.prototype.getStoryLabels = function getStoryLabels(projectId, storyId, callback) {
this.api('get', 'projects/' + projectId + '/stories/' + storyId + '/labels', {}, callback);
};
/**
* Add story label
* @param {String} projectId Pivotal project id
* @param {String} storyId Pivotal story id
* @param {String} name Name of label
* @param {Function} [callback] function(error, label)
*/
Pivotal.prototype.addStoryLabel = function addStoryLabel(projectId, storyId, name, callback) {
this.api('post', 'projects/' + projectId + '/stories/' + storyId + '/labels', {
body: {
name: name
}
}, callback);
};
/**
* Create label
* @param {String} projectId Pivotal project id
* @param {String} name Name of label
* @param {Function} [callback] function(error, label)
*/
Pivotal.prototype.createLabel = function createLabel(projectId, name, callback) {
this.api('post', 'projects/' + projectId + '/labels', {
body: {
name: name
}
}, callback);
};
/**
* Create new Pivotal story
* @param {String} projectId Pivotal project id
* @param {Object} [params] Story parameters
* @param {Function} [callback] function(error, story)
*/
Pivotal.prototype.createStory = function createStory(projectId, params, callback) {
this.api('post', 'projects/' + projectId + '/stories', {
body: params
}, callback);
};
Pivotal.prototype.paginated = function paginated(path, offset, limit, options, callback, completed) {
var that = this;
var _options = {
qs: _.extend({
offset: offset,
limit: limit,
envelope: true
}, options)
};
this.api('get', path, _options, function(err, res) {
if (err || !res.pagination) {
completed && completed(err || res);
} else {
offset += res.pagination.returned;
callback && callback(res.data, res.pagination, function(err) {
if (!err) {
var left = res.pagination.total - offset;
if (left > 0) {
that.paginated(path, offset, Math.min(left, limit), options, callback, completed);
} else {
completed && completed();
}
} else {
completed && completed(err);
}
});
}
});
};
Pivotal.prototype.api = function api(method, path, options, callback) {
var opts = _.extend({
method: method,
url: this.baseUrl + path,
json: true,
headers: {
'X-TrackerToken': this.apiToken
}
}, options);
request(opts, function(err, response, result) {
if (_.isFunction(callback)) {
callback(err, result);
}
});
};
module.exports = Pivotal;