-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (61 loc) · 1.79 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
require("dotenv").config();
const Asana = require("asana");
const express = require("express");
const bodyParser = require("body-parser");
const crypto = require("crypto");
const app = express();
app.use(bodyParser.json());
app.post("/webhook", (req, res) => {
const signature = req.headers["x-hub-signature-256"];
const payload = JSON.stringify(req.body);
if (verifySignature(payload, signature)) {
if (req.body.action === "opened") {
const issue = req.body.issue;
if (issue) {
createAsanaTask(issue);
}
}
res.status(200).send("OK");
} else {
res.status(401).send("Signature verification failed");
}
});
function verifySignature(payload, signature) {
const hmac = crypto.createHmac("sha256", process.env.GITHUB_SECRET);
const digest = `sha256=${hmac.update(payload).digest("hex")}`;
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest));
}
function createAsanaTask(issue) {
const data = {
gid: issue.url,
name: issue.title,
notes: issue.body,
projects: [process.env.ASANA_PROJECT_ID],
workspace: process.env.ASANA_WORKSPACE_ID,
external: {
data: issue.html_url,
},
assignee: "me",
};
let client = Asana.ApiClient.instance;
let token = client.authentications["token"];
token.accessToken = process.env.ASANA_ACCESS_TOKEN;
let tasksApiInstance = new Asana.TasksApi();
let body = { data };
let opts = {
opt_fields:
"name,assignee,workspace",
};
tasksApiInstance.createTask(body, opts).then(
(result) => {
console.log("API called successfully.",result);
},
(error) => {
console.error(error.response.body, data);
}
);
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});