forked from calcom/synclinear.com
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
67 lines (59 loc) · 1.98 KB
/
index.ts
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
import { NextApiRequest, NextApiResponse } from "next";
import { linearWebhookHandler } from "../../utils/webhook/linear.handler";
import { githubWebhookHandler } from "../../utils/webhook/github.handler";
export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method !== "POST") {
return res.status(405).send({
success: false,
message: "Only POST requests are accepted."
});
}
try {
/**
* Linear webhook consumer
*/
if (req.headers["user-agent"] === "Linear-Webhook") {
let originIp = req.headers["x-forwarded-for"];
if (Array.isArray(originIp)) {
originIp = originIp[0];
}
if (originIp.includes(",")) {
originIp = originIp.split(",")[0].trim();
}
const result = await linearWebhookHandler(req.body, originIp);
if (result) {
console.log(result);
return res.status(200).send({
success: true,
message: result
});
}
} else {
/**
* GitHub webhook consumer
*/
const result = await githubWebhookHandler(
req.body,
req.headers["x-hub-signature-256"] as string,
req.headers["x-github-event"] as string
);
if (result) {
console.log(result);
return res.status(200).send({
success: true,
message: result
});
}
}
} catch (e) {
return res.status(e.statusCode || 500).send({
success: false,
message: e.message
});
}
console.log("Webhook received.");
return res.status(200).send({
success: true,
message: "Webhook received."
});
};