forked from github/github-app-js-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (119 loc) · 3.84 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import dotenv from "dotenv";
import fs from "fs";
import http from "http";
import { Octokit, App } from "octokit";
import { createNodeMiddleware } from "@octokit/webhooks";
import { getErrorInfo } from "@pretendonetwork/error-codes";
function getErrorCode(supportCode) {
const [moduleId, descriptionId] = supportCode.split("-");
const mod = getErrorInfo(moduleId, descriptionId, "en_US");
// TODO: cleanup and formatting
return (
"Error code detected: " +
moduleId +
"-" +
descriptionId +
"\n" +
"System: " +
(mod.module.system || "Unknown") +
"\n\n" +
"Module: " +
(mod.module.name || "Unknown") +
"(" +
(mod.module.description || "Unknown") +
")\n" +
"Error: " +
(mod.name || "Unknown") +
"\n" +
"Message: " +
(mod.message || "Unknown") +
"\n" +
"Description: " +
(mod.short_description || "Unknown") +
"\n" +
"Long Description: " +
(mod.long_description || "Unknown") +
"\n\n" +
"Short-term solution: " +
(mod.short_solution || "Unknown") +
"\n" +
"Long-term solution: " +
(mod.long_solution || "Unknown") +
"\n\nSupport Link: " +
(mod.support_link || "Unknown")
);
}
// Load environment variables from .env file
dotenv.config();
// Set configured values
const appId = process.env.APP_ID;
const privateKeyPath = process.env.PRIVATE_KEY_PATH;
const privateKey = fs.readFileSync(privateKeyPath, "utf8");
const secret = process.env.WEBHOOK_SECRET;
const enterpriseHostname = process.env.ENTERPRISE_HOSTNAME;
const messageForNewPRs = fs.readFileSync("./message.md", "utf8");
// taken from https://github.com/PretendoNetwork/Bandwidth/blob/master/src/utils/errorCode.js
// then thrown into chatgpt for a generic regex
const ANY_SUPPORT_CODE_REGEX = /\b\d{3}-\d{4}\b/gm;
// Create an authenticated Octokit client authenticated as a GitHub App
const app = new App({
appId,
privateKey,
webhooks: {
secret,
},
...(enterpriseHostname && {
Octokit: Octokit.defaults({
baseUrl: `https://${enterpriseHostname}/api/v3`,
}),
}),
});
// Optional: Get & log the authenticated app's name
const { data } = await app.octokit.request("/app");
// Read more about custom logging: https://github.com/octokit/core.js#logging
app.octokit.log.debug(`Authenticated as '${data.name}'`);
// Subscribe to the "pull_request.opened" webhook event
app.webhooks.on("pull_request.opened", async ({ octokit, payload }) => {
console.log(
`Received a pull request event for #${payload.pull_request.number}`,
);
if (ANY_SUPPORT_CODE_REGEX.test(payload.pull_request.body)) {
try {
await octokit.rest.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
body: getErrorCode(
payload.pull_request.body.match(ANY_SUPPORT_CODE_REGEX)[0],
),
});
} catch (error) {
if (error.response) {
console.error(
`Error! Status: ${error.response.status}. Message: ${error.response.data.message}`,
);
} else {
console.error(error);
}
}
}
});
// Optional: Handle errors
app.webhooks.onError((error) => {
if (error.name === "AggregateError") {
// Log Secret verification errors
console.log(`Error processing request: ${error.event}`);
} else {
console.log(error);
}
});
// Launch a web server to listen for GitHub webhooks
const port = process.env.PORT || 3000;
const path = "/api/webhook";
const localWebhookUrl = `http://localhost:${port}${path}`;
// See https://github.com/octokit/webhooks.js/#createnodemiddleware for all options
const middleware = createNodeMiddleware(app.webhooks, { path });
http.createServer(middleware).listen(port, () => {
console.log(`Server is listening for events at: ${localWebhookUrl}`);
console.log("Press Ctrl + C to quit.");
});