forked from msolomonTMG/jira-comment-slack-notification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
349 lines (307 loc) · 10.9 KB
/
app.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
'use strict';
const
express = require('express'),
exphbs = require('express-handlebars'),
bodyParser = require('body-parser'),
slack = require('./slack'),
user = require('./user'),
jira = require('./jira'),
utils = require('./utils'),
passport = require('passport'),
AtlassianOAuthStrategy = require('passport-atlassian-oauth').Strategy,
request = require('request'),
mongoose = require('mongoose'),
APP_URL = process.env.APP_URL || `http://localhost:5000/`,
JIRA_URL = process.env.JIRA_URL,
MONGO_URI = process.env.MONGODB_URI || "mongodb://localhost:27017/mongo_test";
let privateKey = Buffer.from(process.env.RSA_PRIVATE_KEY, 'base64').toString();
mongoose.connect(MONGO_URI, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + MONGO_URI + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + MONGO_URI);
}
});
var app = express();
app.set('port', process.env.PORT || 5000);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.get('/signup', function(req, res) {
res.render('signup');
})
// passport setup for atlassian
// called from route: /auth/atlassian-oauth
passport.use(new AtlassianOAuthStrategy({
applicationURL: `${JIRA_URL}`,
callbackURL:`${APP_URL}auth/atlassian-oauth/callback`,
passReqToCallback: true,
consumerKey:"neptune-the-dodle",
consumerSecret:privateKey
}, function(req, token, tokenSecret, profile, done) {
console.log('HELLO')
process.nextTick(function() {
console.log(token)
console.log(tokenSecret)
console.log(req.session.slackUsername)
// check if this user is just adding a jira token
// or if they are a brand new user
user.getBySlackUsername(req.session.slackUsername).then(thisUser => {
if (!thisUser) {
user.create({
slackUsername: req.session.slackUsername,
jiraToken: token,
jiraUsername: profile.username,
jiraTokenSecret: tokenSecret
}).then(createdUser => {
return done(null, createdUser)
})
} else {
console.log('updating user')
user.getBySlackUsername(req.session.slackUsername).then(thisUser => {
user.update(thisUser._id, {
jiraToken: token,
jiraTokenSecret: tokenSecret,
jiraUsername: utils.addJiraMarkupToUsername(profile.username)
}).then(updatedUser => {
console.log(updatedUser)
return done(null, updatedUser)
})
})
}
})
})
}
));
app.get('/auth', function(req, res) {
console.log('AUTH')
console.log(privateKey)
user.getBySlackUsername(req.query.slackUsername)
.then(thisUser => {
if (thisUser) {
// save slack username to session to use when saving user after auth
req.session.slackUsername = req.query.slackUsername
// send to auth route
res.redirect('/auth/atlassian-oauth')
} else {
// this user already signed up
res.send(JSON.stringify({user: thisUser}))
}
})
})
// auth route uses passport
app.get('/auth/atlassian-oauth',
passport.authenticate('atlassian-oauth'),
function (req, res) {
console.log('ATLASSIAN AUTH')
res.render('message', {
successMsg: 'yay!'
})
// The request will be redirected to the Atlassian app for authentication, so this
// function will not be called.
})
app.get('/auth/atlassian-oauth/callback',
passport.authenticate('atlassian-oauth', { failureRedirect:'/fail' }),
function (req, res) {
console.log('req')
console.log(req)
console.log("ATLASSIAN AUTH CALLBACK")
if (req.session.passport.user.jiraToken && req.session.passport.user.jiraTokenSecret) {
slack.sendMessageToUser(req.session.passport.user.slackUsername, `:+1: Nice work, you're all set. Going forward, you'll have the option to respond to Jira comments from here!`)
}
res.redirect('/?success=true');
})
app.get('/auth/atlassian-oauth/authorize', function(req, res) {
console.log('AUTH URL')
console.log(req.body)
res.sendStatus(200)
})
app.get('/', function(req, res) {
if (req.query.success) {
res.render('message', {
successMsg: 'You can now receive and respond to Jira comments from within Slack!'
})
} else {
res.render('home')
}
})
app.get('/settings', function(req, res) {
if (!req.query.slackUsername) {
res.send(403)
}
user.getBySlackUsername(req.query.slackUsername).then(thisUser => {
console.log(thisUser)
if (!thisUser) {
res.sendStatus(403)
}
res.render('settings', {
slackUsername: thisUser.slackUsername,
jiraUsername: utils.stripJiraMarkupFromUsername(thisUser.jiraUsername)
})
}).catch(err => {
res.sendStatus(403)
})
})
app.post('/response-from-slack', function(req, res) {
console.log(req.body)
if (req.body.challenge) {
res.send(req.body.challenge)
} else if (req.body.payload) {
let payload = JSON.parse(req.body.payload)
console.log("PAYLOAD")
console.log(payload)
if (payload.callback_id == 'pop_comment_dialog') {
// give slack a response right away
res.setHeader('Content-Type', 'application/json')
res.status(200).send({
"replace_original": false,
"response_type": "ephemeral",
"text": "Responding..."
})
console.log(payload.user.name)
user.getBySlackUsername(payload.user.name).then(thisUser => {
console.log(thisUser)
if (!thisUser) {
console.log('there is no user')
slack.sendSettingsToUser(thisUser)
} else if (!thisUser.jiraToken || !thisUser.jiraTokenSecret) {
console.log('no tokens!!')
// this shouldnt happen because we pop auth buttons instead
// of popping respond to comment buttons if no tokens
} else {
slack.openCommentDialog(payload).then(success => {
console.log(success)
})
}
//slack.popDialog(thisUser)
})
} else if (payload.callback_id.match(/create_comment/)) {
user.getBySlackUsername(payload.user.name).then(thisUser => {
console.log(thisUser)
console.log(payload)
console.log(payload.callback_id)
let issueKey = payload.callback_id.split('|')[1]
let comment = payload.submission.comment
jira.createComment(thisUser, issueKey, comment).then(success => {
console.log('SUCCESS')
console.log(success)
// slack will post OK in the channel if you just return 200
slack.sendMessageToUser(thisUser.slackUsername, ':white_check_mark: Your comment has been made in Jira')
res.setHeader('Content-Type', 'application/json');
res.status(200).send()
})
})
}
}
// user.getBySlackUserId(req.body.event.user).then(thisUser => {
//
// res.send(200)
//
// })
})
app.get('/user/create', function(req, res) {
user.getBySlackUsername(req.query.slackUsername).then(thisUser => {
if(thisUser == null) {
user.create({
slackUsername: req.query.slackUsername
}).then(createdUser => {
console.log('CREATED A USER')
console.log(createdUser)
res.redirect(`/auth?slackUsername=${createdUser.slackUsername}`)
})
} else {
console.log('there is a user')
console.log(thisUser)
res.redirect(`/auth?slackUsername=${thisUser.slackUsername}`)
}
}).catch(err => {
console.log(err)
})
})
app.post('/user/create', function(req, res) {
let newUser = {
slackUsername: req.body.slack.username,
slackUserId: req.body.slackUserId,
jiraUsername: req.body.jira.username
}
user.create(newUser).then(createdUser => {
return res.render('settings', {
slackUsername: createdUser.slackUsername,
slackUserId: createdUser.slackUserId,
jiraUsername: utils.stripJiraMarkupFromUsername(createdUser.jiraUsername),
signUpSuccessMsg: 'Signup Successful!'
})
})
})
app.post('/msg-wake-up', function(req, res) {
if (req.body.challenge) {
res.send(req.body.challenge)
} else {
//wake up!
console.log('Im up!')
res.send(200)
}
})
app.post('/comment-created', function(req, res) {
let webhookReason = req.body.webhookEvent,
webhookData = req.body,
commentBody = req.body.comment.body;
// continue if the webhook was sent to us because an issue was commented on
// by someone other than our GitHub Integration
if (webhookReason === "comment_created" && webhookData.comment.author.displayName != "GitHub Integration") {
// look for a user mention in the comment
utils.getUserMentionsFromComment(commentBody).then(userMentions => {
// for each mentioned user thats signed up for this app, send slack msg
userMentions.forEach(userMention => {
// find if there is a user with that jira username in this app's DB
user.getByJiraUsername(userMention).then((thisUser, index) => {
// check if this webhook contains a jira issue in payload
// https://github.com/msolomonTMG/jira-comment-slack-notification/issues/17
// TODO: we can clean this up with async/await
if (!webhookData.issue) {
const issueUrl = webhookData.comment.self.split('/comment')[0]
jira.getTicketInfo(thisUser, issueUrl).then(issueData => {
webhookData.issue = issueData
// send a slack message to the user
slack.sendCommentToUser(thisUser, webhookData).then(result => {
// if this is the last user to msg, send 200 status
if (userMentions.length === index + 1) {
res.sendStatus(200)
}
})
.catch(err => { return res.sendStatus(500) })
})
} else {
// send a slack message to the user
slack.sendCommentToUser(thisUser, webhookData).then(result => {
// if this is the last user to msg, send 200 status
if (userMentions.length === index + 1) {
res.sendStatus(200)
}
})
.catch(err => { return res.sendStatus(500) })
}
})
.catch(noUser => { return res.sendStatus(200) })
})
})
.catch(noMentions => { return res.sendStatus(200) })
}
})
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
module.exports = app;