This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
forked from bistaastha/badging-bot
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
103 lines (90 loc) · 2.63 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
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
const { App } = require("octokit");
require("dotenv").config();
const githubBot = require("./githubBot");
const express = require("express");
const logger = require("./utils/logger");
const path = require("path");
const fs = require("fs");
const app = express();
app.use(express.json());
// instantiate Github App
const bot = new App({
appId: process.env.appId,
privateKey: process.env.privateKey,
oauth: {
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
},
webhooks: { secret: process.env.webhookSecret },
});
// Middleware function to log response details
app.use((req, res, next) => {
const oldSend = res.send;
res.send = function (data) {
logger.info(`Response for ${req.method} ${req.url}:`);
logger.info(data);
oldSend.apply(res, arguments);
};
next();
});
app.get("/status", (req, res) => {
try {
res.json({ message: "Event Badging Bot server up and running" });
} catch (error) {
console.error(error);
if (error.statusCode && error.statusCode !== 200) {
res.status(error.statusCode).json({
error: "Error",
message: "our bad, something is wrong with the server configuration",
});
} else {
res.status(500).json({
error: "Internal Server Error",
message: "An unexpected error occurred at our end",
});
}
}
});
app.post("/", async (req, res) => {
const {
headers: { "x-github-event": name },
body: payload,
} = req;
const octokit = await bot.getInstallationOctokit(payload.installation.id);
githubBot(name, octokit, payload);
logger.info(`Received ${name} event from Github`);
res.send("ok");
});
app.get("/logs", (req, res) => {
const logsDir = path.join(__dirname, "logs");
fs.readdir(logsDir, (err, files) => {
if (err) {
logger.error(err);
return res.status(500).send("Error reading logs directory");
}
const logFiles = files.filter((file) => file.startsWith("app.log"));
if (logFiles.length === 0) {
return res.status(404).send("No log files found");
}
const logFile = path.join(logsDir, logFiles[0]);
fs.readFile(logFile, "utf8", (err, data) => {
if (err) {
logger.error(err);
return res.status(500).send("Error reading log file");
}
res.send(`<pre>${data}</pre>`);
});
});
});
app.listen(process.env.PORT, () =>
logger.info(`App listening on PORT:${process.env.PORT}`)
);
if (process.env.NODE_ENV === "development") {
const SmeeClient = require("smee-client");
const smee = new SmeeClient({
source: process.env.source,
target: `http://localhost:${process.env.PORT}`,
logger: console,
});
smee.start();
}