-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (70 loc) · 2.35 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
/**
* Trello daily reporting bot
*
* inputData - the data coming into the Zap
* inputData.channel - name of the slack channel to post to
* inputData.username - name of the bot
* inputData.date - the current day of the week
* inputData.boardId - id of the Trello board to post to
* inputData.key - Trello api key
* inputData.token - Trello api token
* inputData.watchList - Trello list to report on (single value OR comma-separated)
* inputData.slackUrl - The webhook URL for Slack
*/
const fields = ['channel', 'username', 'date', 'boardId', 'key', 'token', 'watchList', 'slackUrl'];
for (const field of fields) {
if ((inputData[field] || '').length < 1) {
throw new Error(`The input data requires that the "${field}" field is filled in.`);
}
}
const payload = {
channel: inputData.channel,
username: inputData.username,
pretext: `It's ${inputData.date}! This is what's going on right now`,
text: inputData.text,
icon_emoji: ':robot_face:',
color: 'good',
fields: []
};
const boardId = inputData.boardId;
const key = inputData.key;
const token = inputData.token;
const watchList = inputData.watchList.split(',');
// make top level await work outside of Zapier
return (async () => {
const res = await fetch(`https://api.trello.com/1/boards/${boardId}/lists?cards=open&card_fields=name,dateLastActivity&filter=open&fields=name&key=${key}&token=${token}`);
const results = await res.json();
// flatten out the cards
const cards = results.filter((obj) => watchList.indexOf(obj.id) !== -1)
.reduce((results, { name, cards }) => {
results[name] = cards;
return results;
}, {});
// build slack attachments
const cardFields = Object.entries(cards)
.map(([name, cards]) => {
return {
title: name,
value: cards.length < 1 ? '- _No tasks in the list..._': cards.map(({ name }) => `- ${name}`).join(`\n`),
short: false
};
});
payload.fields = cardFields;
// there was nothing in progress
if (payload.fields.length < 1) {
payload.fields = [{
title: 'Nothing in progress...',
short: false
}];
payload.color = 'warning';
}
const slack = await fetch(inputData.slackUrl, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
return payload;
})();