-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
155 lines (150 loc) · 7.66 KB
/
server.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
const express = require('express')
const bodyParser = require('body-parser')
const { WebhookClient } = require('discord.js')
express()
.use(express.static('public'))
.use(bodyParser.json())
.get('/webhook', (request, response) => response.sendStatus(200))
.post('/webhook', async (request, response) => {
if (request.body) {
const discordWebhook = new WebhookClient(process.env.WEBHOOKID, process.env.WEBHOOKTOKEN)
const body = request.body
if (body.type === 'test') {
await discordWebhook.send({
username: 'Taiga',
avatarURL: 'https://cdn.discordapp.com/attachments/596130529129005056/596406037859401738/favicon.png',
embeds: [{
'description': 'just a test',
'timestamp': body.date,
'footer': { icon_url: body.by.photo, text: body.by.full_name },
'color': 0x5000
}]
})
} else if (body.type === 'task' && body.action === 'create') {
await discordWebhook.send({
username: 'Taiga',
avatarURL: 'https://cdn.discordapp.com/attachments/596130529129005056/596406037859401738/favicon.png',
embeds: [{
'author': {
name: `Created subtask #${body.data.ref} on ${body.data.project.name}`,
url: `${body.data.project.permalink}/task/${body.data.ref}`
},
'description': [
`**Subject**: ${body.data.subject}`,
`**Instance**: [${body.data.user_story.subject}](https://tree.taiga.io/project/naedian-hythelia/task/${body.data.ref})`,
`**Description**: ${body.data.description ? body.data.description : 'Nothing'}`,
`**Assigned to**: ${body.data.assigned_to ? body.data.assigned_to.username : 'Nobody'}`,
`**Status**: ${body.data.status.name}`
].join('\n'),
'timestamp': body.date,
'footer': { icon_url: body.by.photo, text: body.by.username },
'color': parseInt(body.data.status.color.replace('#', ''), 16)
}]
})
} else if (body.type === 'userstory' && body.action === 'create') {
await discordWebhook.send({
username: 'Taiga',
avatarURL: 'https://cdn.discordapp.com/attachments/596130529129005056/596406037859401738/favicon.png',
embeds: [{
'author': {
name: `Created task #${body.data.ref} on ${body.data.project.name}`,
url: `${body.data.project.permalink}/us/${body.data.ref}`
},
'description': [
`**Subject**: ${body.data.subject}`,
`**Description**: ${body.data.description ? body.data.description : 'Nothing'}`,
`**Assigned to**: ${body.data.assigned_to ? body.data.assigned_to.username : 'Nobody'}`,
`**Status**: ${body.data.status.name}`
].join('\n'),
'timestamp': body.date,
'footer': { icon_url: body.by.photo, text: body.by.username },
'color': parseInt(body.data.status.color.replace('#', ''), 16)
}]
})
} else if (body.type === 'task' && body.action === 'delete') {
await discordWebhook.send({
username: 'Taiga',
avatarURL: 'https://cdn.discordapp.com/attachments/596130529129005056/596406037859401738/favicon.png',
embeds: [{
'author': {
name: `Deleted subtask #${body.data.ref} on ${body.data.project.name}`,
url: body.data.project.permalink
},
'description': [
`**Subject**: ${body.data.subject}`,
`**Instance**: [${body.data.user_story.subject}](${body.data.project.permalink}/task/${body.data.ref})`,
`**Description**: ${body.data.description ? body.data.description : 'Nothing'}`,
`**Old status**: ${body.data.status.name}`
].join('\n'),
'timestamp': body.date,
'footer': { icon_url: body.by.photo, text: body.by.username },
'color': parseInt(body.data.status.color.replace('#', ''), 16)
}]
})
} else if (body.type === 'userstory' && body.action === 'delete') {
await discordWebhook.send({
username: 'Taiga',
avatarURL: 'https://cdn.discordapp.com/attachments/596130529129005056/596406037859401738/favicon.png',
embeds: [{
'author': {
name: `Deleted task #${body.data.ref} on ${body.data.project.name}`,
url: body.data.project.permalink
},
'description': [
`**Subject**: ${body.data.subject}`,
`**Description**: ${body.data.description ? body.data.description : 'Nothing'}`,
`**Old status**: ${body.data.status.name}`
].join('\n'),
'timestamp': body.date,
'footer': { icon_url: body.by.photo, text: body.by.username },
'color': parseInt(body.data.status.color.replace('#', ''), 16)
}]
})
} else if ((body.type === 'task' || body.type === 'userstory') && body.action === 'change') {
const description = [ '' ]
if (body.change.diff.status) description.push(`**Old status**: ${body.change.diff.status.from}`)
if (body.change.diff.subject) description.push(`**Old subject**: ${body.change.diff.subject.from}`)
if (body.change.diff.assigned_to) description.push(`**Old assigned**: ${body.change.diff.assigned_to.from ? body.change.diff.assigned_to.from : 'Nobody'}`)
if (body.change.diff.description_diff) description.push(`**Old description**: ${body.change.diff.description_diff.from ? body.change.diff.description_diff.from : 'Nothing'}`)
if (body.change.diff.assigned_users) description.push(`**Old assigned**: ${body.change.diff.assigned_users.from ? body.change.diff.assigned_users.from : 'Nobody'}`)
Object.keys(body.change.diff).map(d => {
switch (d) {
case 'subject':
description.push(`**New subject**: ${body.change.diff.subject.to}`)
break
case 'status':
description.push(`**New status**: ${body.change.diff.status.to}`)
break
case 'assigned_to':
description.push(`**New assigned**: ${body.change.diff.assigned_to.to ? body.change.diff.assigned_to.to : 'Nobody'} `)
break
case 'description_diff':
description.push(`**New description**: ${body.change.diff.description_diff.to ? body.change.diff.description_diff.to : 'Nothing'}`)
break
case 'assigned_users':
description.push(`**New assigned**: ${body.change.diff.assigned_users.to ? body.change.diff.assigned_users.to : 'Nobody'}`)
}
})
if (description.length !== 0) {
await discordWebhook.send({
username: 'Taiga',
avatarURL: 'https://cdn.discordapp.com/attachments/596130529129005056/596406037859401738/favicon.png',
embeds: [{
'author': {
name: `Updated ${body.type === 'task' ? 'subtask' : 'task'} #${body.data.ref} on ${body.data.project.name}`,
url: `${body.data.project.permalink}/task/${body.data.ref}`
},
'description': description.join('\n'),
'timestamp': body.date,
'footer': { icon_url: body.by.photo, text: body.by.username },
'color': parseInt(body.data.status.color.replace('#', ''), 16)
}]
})
}
}
response.status(200).send('Event received!')
} else {
response.status(404).send('Something went wrong!')
}
})
.listen(process.env.PORT, () => console.log(`Webhook listening on port ${process.env.PORT}!`))