-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
70 lines (62 loc) · 1.94 KB
/
main.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
var newTrelloCards = [],
appKey = UserProperties.getProperty("trelloApplicationKey"),
authToken = UserProperties.getProperty("trelloAuthToken");
function importMailsToTrello() {
var trelloLabels = getAllTrelloRelatedLabels();
getTrelloLists(trelloLabels, function(linkedLabels) {
for(var i = 0; i< linkedLabels.length; i++) {
var currentLabel = linkedLabels[i];
getMailsByLabel(currentLabel);
}
createAllNewCards();
});
}
function getAllTrelloRelatedLabels(label) {
var allLabels = GmailApp.getUserLabels();
var trelloLabels = [];
for(i = 0; i < allLabels.length; i++) {
var labelName = allLabels[i].getName();
if(labelName.indexOf("trello:") > -1) {
var trelloLabel = createTrelloLabelObject(allLabels[i]);
if(trelloLabel)
trelloLabels.push(trelloLabel);
}
}
return trelloLabels;
}
function createTrelloLabelObject(label) {
try {
var labelName = label.getName();
var nameWithoutTrelloPrefix = labelName.replace("trello:","");
var trelloParamaters = nameWithoutTrelloPrefix.split(",");
var boardName = trelloParamaters[0].trim(),
listName = trelloParamaters[1].trim();
var trelloLabel = {
gmailLabel: label,
boardName: boardName,
listName: listName
}
if(trelloLabel.boardName && trelloLabel.listName)
return trelloLabel;
else
return null;
} catch(ex) {
return null;
}
}
function getMailsByLabel(label) {
if(label) {
var threads = label.gmailLabel.getThreads();
Logger.log(label);
Logger.log(label.gmailLabel);
Logger.log(threads);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for(var z = 0; z < messages.length; z++) {
var trelloCard = buildTrelloCard(messages[z], label);
newTrelloCards.push(trelloCard);
threads[i].removeLabel(label.gmailLabel); // remove here, so it wont get added again.
}
}
}
}