-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathindex.js
65 lines (50 loc) · 1.9 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
import fs from "fs";
import { Webhooks, createNodeMiddleware } from "@octokit/webhooks";
import express from "express";
import client from "./client.js";
const app = express();
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Website is running on http://localhost:${port}`);
});
app.get("/", (request, response) => {
response.redirect("https://github.com/zulip/zulipbot");
});
const webhooks = new Webhooks({ secret: client.cfg.auth.webhookSecret });
webhooks.onAny(async ({ name, payload }) => {
const repo = payload.repository ? payload.repository.full_name : null;
const check = client.cfg.activity.check.repositories.includes(repo);
const eventHandler = client.events.get(name);
if (!check || !eventHandler) return;
await eventHandler(payload);
});
app.use("/github", createNodeMiddleware(webhooks, { path: "/" }));
process.on("unhandledRejection", (error) => {
console.log(`Unhandled promise rejection:\n${error.stack}`);
});
if (client.cfg.activity.check.interval) {
setInterval(() => {
client.events.get("activity")();
}, client.cfg.activity.check.interval * 3600000);
}
for (const pair of Object.entries(client.cfg.auth)) {
const [key, value] = pair;
if (typeof value === "string") {
console.log(`Using environment variable value for \`${key}\`...`);
continue;
}
try {
const secretsPath = new URL("../config/secrets.json", import.meta.url);
console.log(`Using value from \`${secretsPath}\` for \`${key}\`...`);
const secrets = JSON.parse(fs.readFileSync(secretsPath));
if (typeof secrets[key] !== "string") {
throw new TypeError(`Expected string for \`${key}\``);
}
client.cfg.auth[key] = secrets[key];
} catch {
console.log(`\`${key}\` value was not set. Please fix your configuration.`);
process.exit(1);
}
}
const response = await client.users.getAuthenticated();
client.cfg.auth.username = response.data.login;