-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (63 loc) · 1.84 KB
/
index.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
"use strict";
const GITHUB_WEBHOOK_SECRET = process.env.GITHUB_WEBHOOK_SECRET;
const ZULIP_BOT_EMAIL = process.env.ZULIP_BOT_EMAIL;
const ZULIP_BOT_API_KEY = process.env.ZULIP_BOT_API_KEY;
const ZULIP_REALM = process.env.ZULIP_REALM;
const zulip = require("zulip-js");
const { Webhooks } = require("@octokit/webhooks");
const {
onGollum,
onIssues,
onIssueComment,
onPullRequest,
onPullRequestReview,
onPullRequestReviewComment,
} = require("./messages");
const zulipConfig = {
username: ZULIP_BOT_EMAIL,
apiKey: ZULIP_BOT_API_KEY,
realm: ZULIP_REALM,
};
exports.zulipGitHubEvents = async (req, res) => {
const payload = req.body;
const webhooks = new Webhooks({
secret: GITHUB_WEBHOOK_SECRET,
});
if (!webhooks.verify(payload, req.get("x-hub-signature"))) {
return res.sendStatus(403);
}
const stream = req.query.stream;
const topic = req.query.topic;
if (!stream) {
return res.status(400).send("Missing `stream` parameter");
}
if (!topic) {
return res.status(400).send("Missing `topic` parameter");
}
const maybeSend = (handler) => async ({ payload }) => {
const content = handler(payload);
if (!content) return;
const client = await zulip(zulipConfig);
return await client.messages.send({
type: "stream",
to: stream,
topic: topic,
content,
});
};
webhooks.on("gollum", maybeSend(onGollum));
webhooks.on("issues", maybeSend(onIssues));
webhooks.on("issue_comment", maybeSend(onIssueComment));
webhooks.on("pull_request", maybeSend(onPullRequest));
webhooks.on("pull_request_review", maybeSend(onPullRequestReview));
webhooks.on(
"pull_request_review_comment",
maybeSend(onPullRequestReviewComment)
);
await webhooks.receive({
id: req.get("x-github-delivery"),
name: req.get("x-github-event"),
payload,
});
res.sendStatus(200);
};