-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (33 loc) · 1.51 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
const { Comment, CommentError } = require('./lib/comment')
const { newPullRequest, GithubError } = require('./lib/github-utils')
const { spamCheck, SpamError } = require('./lib/spam-filter')
/**
* This is the main entrypoint of static-comments app
* @param {import('probot').Application} app
*/
module.exports = (app, { getRouter }) => {
const router = getRouter('/static-comments')
router.use(require('express').urlencoded({ extended: true }))
router.post('/new', (req, res) => {
app.log.debug(`Processing new request from IP ${req.ip}`)
const logMessageAndReturn = (message, code, redirect) => {
if (code === 200 || code === 400) app.log.debug(message)
else app.log.error(message)
if (redirect) res.redirect(301, redirect)
else res.status(code).send(message + '\n')
}
const handleError = error => {
if (error instanceof GithubError || error instanceof CommentError) logMessageAndReturn(error.message, 502)
else if (error instanceof SpamError) logMessageAndReturn(error.message, 400)
else logMessageAndReturn(error.message, 500)
}
try {
const comment = new Comment(req.body)
spamCheck(comment, req.ip, app)
.then(comment => newPullRequest(comment, `staticComments-${comment.id}`, app))
.then(message => logMessageAndReturn(message, 200, comment.redirect))
.catch(error => handleError(error))
} catch (error) { handleError(error) }
})
app.log.info('Listening for new comment requests at `POST /static-comments/new`')
}