-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·137 lines (125 loc) · 3.88 KB
/
index.js
File metadata and controls
executable file
·137 lines (125 loc) · 3.88 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
const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const bodyParser = require('koa-bodyparser');
const rawBody = require('raw-body');
const inflate = require('inflation');
const crypto = require('crypto');
const cheerio = require('cheerio');
const GitHubApi = require("github");
const Promise = require('bluebird');
const watchUrl = process.env.URL || '/discourse-webhooks';
const port = process.env.PORT || 4000;
const secret = process.env.SECRET_KEY || '';
const discourseUrl = process.env.DISCOURSE_URL || '';
const githubRepo = process.env.GITHUB_REPO || '';
const githubUsername = process.env.GITHUB_USERNAME || 'discourse';
const githubAccessToken = process.env.GITHUB_ACCESS_TOKEN || '';
const githubPullRequestRegexp = new RegExp(`github\\.com/${githubUsername}/${githubRepo}/pull/(\\d+)`, 'i')
const githubCustomHeaders = {
'User-Agent': "discourse-webhook-bot"
}
router.post(watchUrl, (ctx, next) => {
const headers = ctx.request.headers;
const body = ctx.request.body;
const rawBody = ctx.request.rawBody;
if (headers['x-discourse-event-type'] !== 'post') {
ctx.body = 'No interests.';
return next();
}
const hmac = crypto.createHmac('sha256', secret);
const hash = `sha256=${hmac.update(rawBody).digest('hex')}`;
if (hash !== headers['x-discourse-event-signature']) {
ctx.body = 'HMAC mismatched. Malformed payload.\n';
ctx.body += `Signature: ${headers['x-discourse-event-signature']}\n`;
ctx.body += `Computed: ${hash}\n`;
ctx.body += `Body: ${rawBody}`;
return next();
}
// get url from the post
let urlSet = new Set();
const $ = cheerio.load(body.post.cooked);
$('a').each((_, element) => {
const url = $(element).attr('href');
if (githubPullRequestRegexp.test(url)) {
urlSet.add(url);
}
});
if (urlSet.size === 0) {
ctx.body = 'OK. No url matched in the post.';
return next();
}
// github setup
const github = new GitHubApi({
protocol: 'https',
host: "api.github.com",
headers: githubCustomHeaders,
Promise: Promise,
timeout: 5000
});
github.authenticate({
type: 'oauth',
token: githubAccessToken
});
// check existing comments on target pull requests
let postPath = `/t/${body.post.topic_slug}/${body.post.topic_id}`;
const postNumber = body.post.post_number;
if (postNumber > 1) {
postPath += `/${postNumber}`
}
let promises = [];
urlSet.forEach(url => {
let existLink = false;
const found = url.match(githubPullRequestRegexp);
const pullRequestId = found[1];
promises.push(github.issues.getComments({
user: githubUsername,
repo: githubRepo,
number: pullRequestId
}).then(json => {
json.forEach(comment => {
if (comment.body.indexOf(postPath) !== -1) {
existLink = true;
}
});
while (github.hasNextPage(json)) {
github.getNextPage(json, githubCustomHeaders).then(json => {
json.forEach(comment => {
if (comment.body.indexOf(postPath) !== -1) {
existLink = true;
}
});
});
}
}).then(() => {
if (existLink) {
ctx.body = 'OK. Skip, found existing link.'
} else {
github.issues.createComment({
user: githubUsername,
repo: githubRepo,
number: pullRequestId,
body: `\`@${body.user.username}\` mentioned this pull request. See ${discourseUrl}${postPath}.`
});
ctx.body = 'OK. New link created.';
}
}));
});
return Promise.all(promises).then(() => { return next(); });
});
app
.use((ctx, next) => {
let req = ctx.req || ctx;
let opts = {
encoding: 'utf8',
limit: '1mb'
}
rawBody(inflate(req), opts).then(str => {
ctx.request.rawBody = str;
});
return next();
})
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());
app.listen(port);