-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathwebhook-github.ts
More file actions
230 lines (225 loc) · 8.31 KB
/
Copy pathwebhook-github.ts
File metadata and controls
230 lines (225 loc) · 8.31 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* eslint-disable no-console */
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
import i18n from 'i18n'
const dateFormat = require('dateformat')
import moment from 'moment'
const models = require('../../../models')
const constants = require('../../../modules/mail/constants')
const TaskMail = require('../../../modules/mail/task')
const SendMail = require('../../../modules/mail/mail')
const IssueClosedMail = require('../../../modules/mail/issueClosed')
import type { Request, Response } from 'express'
export const github = async (req: Request, res: Response) => {
const response = req.body || (res as any).body
const labels = response && response.issue && response.issue.labels
if (req.headers.authorization === `Bearer ${process.env.GITHUB_WEBHOOK_APP_TOKEN}`) {
// below would update issue status if someone updates it on Github
if (
response.action === 'reopened' ||
response.action === 'opened' ||
response.action === 'closed'
) {
const status = response.issue.state
const dbUrl = response.issue.html_url
const updated = await models.Task.update(
{ status: status },
{
where: {
url: dbUrl
},
returning: true
}
)
const updatedTask = updated[1][0].dataValues
const user = await models.User.findOne({
where: {
id: updatedTask.userId
}
})
if (updated) {
if (updatedTask.status === 'closed') {
IssueClosedMail.success(user.dataValues, {
name: user.dataValues.name,
url: updatedTask.url,
title: updatedTask.title
})
}
return res.json({
...response,
task: updatedTask
})
} else return res.status(500).json({})
}
if (response.action === 'labeled') {
try {
const totalLabelResponse: any[] = []
await Promise.all(
labels.map(async (label: any) => {
let persistedLabel = await models.Label.findOne({
where: {
name: label.name
}
})
if (persistedLabel === null) {
persistedLabel = await models.Label.create({
name: label.name
})
}
const labelId = persistedLabel.dataValues.id
if (label.name === 'notify') {
let finalResponse: any = {}
try {
console.log('it is labeled notify')
const user = await models.User.findOne({
where: {
username: response.issue.user.login
}
})
const userData = user && user.dataValues
const task = await models.Task.findOne({
where: {
url: response.issue.html_url
}
})
const taskData = task.dataValues
const taskUrl = `${process.env.FRONTEND_HOST}/#/task/${taskData.id}`
if (userData && !taskData.notified) {
const language = user.language || 'en'
i18n.setLocale(language)
SendMail.success(
userData,
i18n.__('mail.webhook.github.issue.new.subject', {
title: response.issue.title
}),
i18n.__('mail.webhook.github.issue.new.message', {
task: taskUrl,
issue: response.issue.html_url,
repo: response.repository.html_url
})
)
await task.addLabels(labelId)
}
TaskMail.notify(userData, {
task: {
title: taskData.title,
issue_url: taskData.url,
url: constants.taskUrl(taskData.id),
value: taskData.value > 0 ? taskData.value : null,
deadline: taskData.deadline
? `${dateFormat(taskData.deadline, constants.dateFormat)} (${moment(taskData.deadline).fromNow()})`
: null
}
})
const taskUpdate = await models.Task.update(
{
notified: true
},
{
where: {
url: response.issue.html_url
}
}
)
if (!taskUpdate) {
SendMail.error(
'notifications@gitpay.me',
'Error to update task',
`An error occurred to update the task ${task}`
)
}
finalResponse = {
task: {
id: taskData.id,
url: taskUrl,
title: taskData.title,
value: taskData.value > 0 ? taskData.value : null,
deadline: taskData.deadline
? `${dateFormat(taskData.deadline, constants.dateFormat)} (${moment(taskData.deadline).fromNow()})`
: null,
userId: userData ? userData.id : null,
label: label.name,
status: !taskUpdate ? 404 : 200
}
}
} catch (e) {
finalResponse = {}
}
totalLabelResponse.push(finalResponse.task)
}
if (label.name === 'gitpay') {
// eslint-disable-next-line no-console
console.log('it is labeled Gitpay')
let finalResponse: any = {}
try {
const user = await models.User.findOne({
where: {
username: response.issue.user.login
}
})
const userData = user && user.dataValues
const taskExist = await models.Task.findOne({
where: {
url: response.issue.html_url
}
})
const task =
taskExist ||
(await models.Task.build({
title: response.issue.title,
provider: 'github',
url: response.issue.html_url,
userId: userData ? userData.id : null
}).save())
await task.addLabels(labelId)
const taskData = task.dataValues
const taskUrl = `${process.env.FRONTEND_HOST}/#/task/${taskData.id}`
if (userData) {
const language = user.language || 'en'
i18n.setLocale(language)
SendMail.success(
userData,
i18n.__('mail.webhook.github.issue.new.subject', {
title: response.issue.title
}),
i18n.__('mail.webhook.github.issue.new.message', {
task: taskUrl,
issue: response.issue.html_url,
repo: response.repository.html_url
})
)
}
finalResponse = {
task: {
id: taskData.id,
url: taskUrl,
title: taskData.title,
userId: userData ? userData.id : null,
label: label.name,
status: 200
}
}
} catch (e) {
// eslint-disable-next-line no-console
console.log('error to build task from github webhook on label gitpay', e)
finalResponse = {}
}
totalLabelResponse.push(finalResponse.task)
}
})
)
const allResponse = { ...response, totalLabelResponse }
return res.json({ ...allResponse })
} catch (e) {
// eslint-disable-next-line no-console
console.log('error to build task from github webhook on label gitpay', e)
return res.json({})
}
}
} else {
console.log('send req body that as it is.....')
return res.status(200).json(req.body)
}
// eslint-disable-next-line no-console
}