forked from tewen/gtd-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-trello-issue
88 lines (79 loc) · 3.38 KB
/
add-trello-issue
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
#!/usr/bin/env node
const _ = require('lodash');
const prompt = require('prompt');
const Trello = require('trello');
const colors = require('colors');
const promisify = require('es6-promisify');
const readOrPromptForKey = require('./lib/utils/file').readOrPromptForKey;
function headline(message) {
console.log(colors.cyan(message));
}
function listOfChoices(list, key = 'name') {
console.log(_.chain(list).map(_.property(key)).map((label, idx) => {
return `${idx + 1}) ${label}`;
}).value().join('\n'));
}
function rejectClosedBoards(boards) {
return _.reject(boards, _.property('closed'));
}
function promptToSelectBoard(boards) {
headline('Please select a Trello board');
listOfChoices(boards);
prompt.start();
return promisify(prompt.get)([{
name: 'choice',
description: 'Enter the number for your selection'
}]);
}
function promptToSelectList(lists, boardName) {
headline(`Please select a Trello list from board ${boardName}`);
listOfChoices(lists);
prompt.start();
return promisify(prompt.get)([{
name: 'choice',
description: 'Enter the number for your selection'
}]);
}
function promptToAddCard() {
headline('Use the prompts to add your Trello Card');
prompt.start();
return promisify(prompt.get)([{
name: 'name',
description: 'Card Name'
}, {
name: 'description',
description: 'Card Description'
}]);
}
readOrPromptForKey('keys.json', 'api.trello.apiKey', 'Trello API Key').then((trelloApiKey) => {
return readOrPromptForKey('keys.json', 'api.trello.userKey', 'Trello User Key').then((trelloUserKey) => {
const trelloApi = new Trello(trelloApiKey, trelloUserKey);
return readOrPromptForKey('keys.json', 'api.trello.memberId', 'Trello Username').then((trelloMemberId) => {
return trelloApi.getBoards(trelloMemberId).then(rejectClosedBoards).then((boards) => {
return promptToSelectBoard(boards).then((result) => {
const board = boards[parseInt(result.choice) - 1];
if (board) {
return trelloApi.getListsOnBoard(board.id).then((lists) => {
return promptToSelectList(lists, board.name).then((result) => {
const list = lists[parseInt(result.choice) - 1];
if (list) {
return promptToAddCard().then((result) => {
return trelloApi.addCard(result.name, result.description, list.id).then(() => {
console.log(colors.green(`Successfully added ${result.name} to Trello.`));
}).catch(() => {
console.log(colors.red(`Error adding ${result.name} to Trello.`));
});
});
} else {
console.error(colors.red('You must select a valid Trello list!'));
}
});
})
} else {
console.error(colors.red('You must select a valid Trello board!'));
}
})
});
});
});
}).then(process.exit);