-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrellorepository.js
188 lines (155 loc) · 5.45 KB
/
trellorepository.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
var Trello = require('node-trello');
var nconf = require("./config.js");
var when = require('when');
var u = require('./utils');
// https://api.trello.com/1/boards/52ea5661407c28d75e495891?fields=name,desc&key=[application_key]&token=[optional_auth_token]
var TrelloRepository = function(options) {
// Ensure new
if (!(this instanceof TrelloRepository))
return new TrelloRepository();
var self = this;
var defaults = {
publicKey: nconf.get('trello_publicKey'),
token: nconf.get('trello_token'),
boardID: nconf.get('trello_boardID'),
fields: nconf.get('trello_fields'),
debug: nconf.get('debug_trello') || false
};
options = options || {};
// public fields
this.boardID = options.boardID || defaults.boardID; // delay between writes and reads
this.fields = options.fields || defaults.fields;
this.debug = options.debug || defaults.debug;
this.cache = {};
// Private fields
var publicKey = options.publicKey || defaults.publicKey;
var token = options.token || defaults.token;
var trello = new Trello(publicKey, token);
// Get an estimate from a title
var estimatePattern = /^\(([0-9\.]+)\).*/;
this.getEstimate = function getEstimate(title) {
var match = estimatePattern.exec(title);
if (match !== null && u.isNumber(match[1])) {
return parseFloat(match[1]);
} else {
return undefined;
}
};
this.getCards = function(options) {
options = options || {};
var cards = [];
var cardFields = (self.fields || nconf.get("trello_fields")).map(function (str) {return str.toLowerCase().trim(); });
// Make sure name is in there, since we'll need it for estimates
if (cardFields.indexOf("name") === -1) {
cardFields[cardFields.length] = "name";
}
if (cardFields.indexOf("id") === -1) {
cardFields[cardFields.length] = "id";
}
var currentDate = new Date();
var currentDateAsStr = u.dateAsStr(currentDate);
// get all the cards for the board
return when.promise(function (resolve, reject) {
var url = "/1/boards/"+ self.boardID + "/lists?cards=open&actions_limit=1000&card_fields=" + cardFields.join(',') + "&labels=true";
if (self.debug) console.log("Retrieving cards from: " + url);
trello.get("/1/boards/"+ self.boardID + "/lists?cards=open&actions_limit=1000&card_fields=" + cardFields.join(',') + "&labels=true", function(err, data) {
if (err) reject(err); else resolve(data);
});
})
.then(function(lists) {
if (self.debug)
console.log(lists.length + " lists found");
// Add some more detail to the card objects
// Namely - colors, estimates, and labels
lists.forEach(function(list) {
var listName = list.name;
var listID = list.id;
if (self.debug)
console.log(listName + ": has " + list.cards.length + " cards");
list.cards.forEach(function(card) {
// Set the estimate
card.estimate = self.getEstimate(card.name);
// Set the list
card.listID = listID;
card.list = listName;
// Parse the labels
if (card.labels !== undefined) {
card.colors = card.labels.map(function(label) { return label.color; });
card.labels = card.labels.map(function(label) { return label.name; });
}
// Set the report date
card.reportDate = currentDate;
card.reportDateAsStr = currentDateAsStr;
cards[cards.length] = card;
});
});
return cards;
});
};
this.getBoards = function() {
return when.promise(function (resolve, reject) {
trello.get("/1/members/me/boards", function (err, data) {
if (err) reject(err); else resolve(data);
});
});
};
this.populateLists = function() {
if (self.cache.lists !== undefined)
return when(self.cache.lists);
return when.promise(function (resolve, reject) {
trello.get("/1/boards/"+ self.boardID + "/lists", function (err, data) {
if (err) reject(err);
self.cache.listLookup = {};
self.cache.lists = data.map(function(list) {
self.cache.listLookup[list.id] = list.name;
return { id : list.id, name: list.name};
});
resolve(self.cache.lists);
});
});
};
this.lists = function() {
// Ensure we have lists
if (self.cache.lists === undefined)
throw new Error("You need to call lists before lists");
return self.cache.lists;
};
// If listID is not populated, returns the entire mapping
this.listLookup= function(listID) {
// Ensure we have lists
if (self.cache.listLookup === undefined)
throw new Error("You need to call populateLists before listLookup");
if (listID === undefined)
return self.cache.listLookup;
else
return self.cache.listLookup[listID];
};
this.populateLabels = function() {
if (self.cache.labels !== undefined)
return when(self.cache.labels);
return when.promise(function (resolve, reject) {
trello.get("/1/boards/"+ self.boardID + "?fields=labelNames", function (err, data) {
if (err) reject(err);
self.cache.labelLookup = {};
self.cache.labels = u.properties(data.labelNames).map(function(color) {
self.cache.labelLookup[color] = data.labelNames[color];
return { id : color, name: data.labelNames[color]};
});
resolve(self.cache.labels);
});
});
};
this.labelLookup = function(labelID) {
// Ensure we have lists
if (self.cache.labelLookup === undefined)
throw new Error("You need to call populateLabels before labelLookup");
if (labelID === undefined)
return self.cache.labelLookup;
else
return self.cache.labelLookup[labelID];
};
this.clearCache = function() {
cache = {};
};
};
module.exports = TrelloRepository;