-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.85 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
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const { Octokit } = require("@octokit/rest");
const { createAppAuth } = require("@octokit/auth-app");
require("dotenv").config();
const app = express();
app.use(bodyParser.json());
app.post("/webhook", async (req, res) => {
const event = req.headers["x-github-event"];
const payload = req.body;
if (event === "pull_request" && payload.action === "opened") {
const pullRequest = payload.pull_request;
const repo = payload.repository;
const octokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: process.env.GITHUB_APP_ID,
privateKey: process.env.GITHUB_PRIVATE_KEY,
installationId: payload.installation.id,
},
});
const files = await octokit.pulls.listFiles({
owner: repo.owner.login,
repo: repo.name,
pull_number: pullRequest.number,
});
for (const file of files.data) {
if (file.filename.endsWith(".js")) {
const content = await axios.get(file.raw_url);
// Perform ESLint check
const { CLIEngine } = require("eslint");
const cli = new CLIEngine();
const report = cli.executeOnText(content.data, file.filename);
const formatter = cli.getFormatter();
const resultText = formatter(report.results);
if (report.errorCount > 0 || report.warningCount > 0) {
await octokit.issues.createComment({
owner: repo.owner.login,
repo: repo.name,
issue_number: pullRequest.number,
body: `ESLint found issues in \`${file.filename}\`:\n\`\`\`\n${resultText}\n\`\`\``,
});
}
}
}
}
res.status(200).send("OK");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));