-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
taskFetch.js
293 lines (279 loc) · 11.2 KB
/
taskFetch.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const Promise = require('bluebird')
const models = require('../../models')
const secrets = require('../../config/secrets')
const url = require('url')
const requestPromise = require('request-promise')
const roleExists = require('../roles').roleExists
const { userExists } = require('../users')
const memberExists = require('../members').memberExists
module.exports = Promise.method(function taskFetch (taskParams) {
return models.Task.findOne({
where: {
id: taskParams.id
},
include: [
models.User,
{
model: models.Project,
include: [models.Organization]
},
{
model: models.Order,
include: [models.User]
},
{
model: models.Assign,
include: [models.User]
},
{
model: models.Member,
include: [models.User, models.Role]
},
{
model: models.Offer,
include: [models.User, models.Task]
},
{
model: models.History
},
{
model: models.Label
},
{
model: models.Transfer
}
]
})
.then(data => {
const githubClientId = secrets.github.id
const githubClientSecret = secrets.github.secret
const issueUrl = data?.dataValues?.url
const splitIssueUrl = url.parse(issueUrl).path.split('/')
const userOrCompany = splitIssueUrl[1]
const projectName = splitIssueUrl[2]
const issueId = splitIssueUrl[4]
if (data.dataValues.private) return data.dataValues
switch (data.dataValues.provider) {
case 'github':
return requestPromise({
uri: `https://api.github.com/repos/${userOrCompany}/${projectName}/issues/${issueId}?client_id=${githubClientId}&client_secret=${githubClientSecret}`,
headers: {
'User-Agent':
'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
}
})
.then(async response => {
const issueDataJsonGithub = JSON.parse(response)
const assigned = await models.Assign.findOne(
{
where: {
id: data.assigned
},
include: [models.User]
}
).catch(e => {})
const role = await roleExists({ name: 'company_owner' })
if (role.dataValues && role.dataValues.id) {
const userInfo = await requestPromise({
uri: `https://api.github.com/users/${userOrCompany}?client_id=${githubClientId}&client_secret=${githubClientSecret}`,
headers: {
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
}
})
const userInfoJSON = JSON.parse(userInfo)
const userExist = userExists && await userExists({ email: userInfoJSON.email })
if (userExist && userExist.dataValues && userExist.dataValues.id) {
const memberExist = await memberExists({ userId: userExist.dataValues.id, taskId: data.id })
if (memberExist.dataValues && memberExist.dataValues.id) {
// alerady member
}
else {
// add member
const task = await models.Task.findOne({
where: {
id: data.id
}
})
await task.createMember({ userId: userExist.dataValues.id, roleId: role.dataValues.id })
}
}
else {
// send an email
}
}
const repoInfo = await requestPromise({
uri: `${issueDataJsonGithub.repository_url}?client_id=${githubClientId}&client_secret=${githubClientSecret}`,
headers: {
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0'
}
})
const repoInfoJSON = JSON.parse(repoInfo)
const repoUrl = repoInfoJSON.html_url
const ownerUrl = repoInfoJSON.owner.html_url
let responseGithub = {
id: data.dataValues.id,
url: issueUrl,
private: data.dataValues.private,
title: data.dataValues.title,
description: data.dataValues.description,
value: data.dataValues.value || 0,
deadline: data.dataValues.deadline,
level: data.dataValues.level,
status: data.dataValues.status,
assigned: data.dataValues.assigned,
assignedUser: assigned && assigned.dataValues.User.dataValues,
User: data.dataValues && data.dataValues.User && data.dataValues.User.dataValues,
paid: data.dataValues.paid,
transfer_id: data.dataValues.transfer_id,
provider: data.dataValues.provider,
metadata: {
id: issueId,
user: userOrCompany,
company: userOrCompany,
projectName: projectName,
repoUrl: repoUrl,
ownerUrl: ownerUrl,
labels: issueDataJsonGithub.labels,
issue: issueDataJsonGithub
},
orders: data.dataValues.Orders,
Transfer: data.dataValues.Transfer,
Assigns: data.dataValues.Assigns,
members: data.dataValues.Members,
Offers: data.dataValues.Offers,
histories: data.dataValues.Histories,
Project: data.dataValues.Project && { ...data.dataValues.Project.dataValues, organization: data.dataValues.Project.dataValues.Organization.dataValues }
}
if (!data.title || data.title !== issueDataJsonGithub.title) {
const dataTitleUpdate = await
// eslint-disable no-unused-vars
data
.update({ title: issueDataJsonGithub.title }, {
where: {
id: data.id
}
})
responseGithub.title = dataTitleUpdate.title
}
if (data.status !== 'in_progress' && data.status !== issueDataJsonGithub.state) {
// eslint-disable no-unused-vars
const dataStatusUpdateInProgress = await data
.update({ status: issueDataJsonGithub.state }, {
where: {
id: data.id
},
returning: true,
})
responseGithub.status = dataStatusUpdateInProgress.status
}
if (issueDataJsonGithub.state === 'closed' && data.status !== 'closed') {
// eslint-disable no-unused-vars
const dataStatusUpdatedClosed = await data
.update({ status: issueDataJsonGithub.state }, {
where: {
id: data.id
},
returning: true,
})
responseGithub.status = dataStatusUpdatedClosed.status
}
if (data.Labels.length !== issueDataJsonGithub.labels.length) {
// eslint-disable no-unused-vars
try {
const githubLabels = issueDataJsonGithub.labels
const taskLabels = data.Labels
githubLabels.forEach(async l => {
if (!taskLabels.includes(l['name'])) {
const label = await models.Label.findOrCreate({
where: {
name: l['name']
},
include: [models.Task],
})
data.addLabel(label[0])
}
})
}
catch (e) {
console.log('error', e)
}
}
return responseGithub
})
.catch(e => {
// eslint-disable-next-line no-console
console.log('github response error')
// eslint-disable-next-line no-console
console.log(e)
return data.dataValues
})
case 'bitbucket':
return requestPromise({
uri: `https://api.bitbucket.org/2.0/repositories/${userOrCompany}/${projectName}/issues/${issueId}`
})
.then(response => {
const issueDataJsonBitbucket = JSON.parse(response)
const responseBitbucket = {
id: data.dataValues.id,
url: issueUrl,
private: data.dataValues.private,
title: data.dataValues.title,
value: data.dataValues.value || 0,
deadline: data.dataValues.deadline,
status: data.dataValues.status,
assigned: data.dataValues.assigned,
userId: data.dataValues.userId,
paid: data.dataValues.paid,
transfer_id: data.dataValues.transfer_id,
provider: data.dataValues.provider,
User: data.dataValues && data.dataValues.User && data.dataValues.User.dataValues,
Project: data.dataValues.Project && { ...data.dataValues.Project.dataValues, organization: data.dataValues.Project.dataValues.Organization.dataValues },
metadata: {
id: issueId,
user: userOrCompany,
company: userOrCompany,
projectName: projectName,
provider: data.provider,
issue: {
state: issueDataJsonBitbucket.status,
body: issueDataJsonBitbucket.content.raw,
user: {
login: issueDataJsonBitbucket.reporter.username,
avatar_url: issueDataJsonBitbucket.reporter.links.avatar.href
}
}
},
orders: data.dataValues.Orders,
Transfer: data.dataValues.Transfer,
assigns: data.dataValues.Assigns,
members: data.dataValues.Members,
Offers: data.dataValues.Offers
}
/*
dont try to sync here too
if (!data.title && data.title !== issueDataJsonBitbucket.title) {
// eslint-disable no-unused-vars
data
.updateAttributes({ title: issueDataJsonBitbucket.title })
.then(task => responseBitbucket)
}
*/
return responseBitbucket
})
.catch(e => {
// eslint-disable-next-line no-console
console.log('Bitbucket response error')
// eslint-disable-next-line no-console
console.log(e)
return data.dataValues
})
default:
return data.dataValues
}
})
.catch(error => {
// eslint-disable-next-line no-console
console.log(error)
return false
})
})