-
Notifications
You must be signed in to change notification settings - Fork 929
/
probot-receive.ts
144 lines (131 loc) 路 3.86 KB
/
probot-receive.ts
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
134
135
136
137
138
139
140
141
142
143
144
// Usage: probot receive -e push -p path/to/payload app.js
import fs from "node:fs";
import path from "node:path";
import { randomUUID as uuidv4 } from "node:crypto";
import express, { Router } from "express";
import { config as dotenvConfig } from "dotenv";
dotenvConfig();
import { program } from "commander";
import { getPrivateKey } from "@probot/get-private-key";
import { getLog } from "../helpers/get-log.js";
import { Probot, type ApplicationFunctionOptions } from "../index.js";
import { resolveAppFunction } from "../helpers/resolve-app-function.js";
async function main() {
program
.usage("[options] [path/to/app.js...]")
.option(
"-e, --event <event-name>",
"Event name",
process.env.GITHUB_EVENT_NAME,
)
.option(
"-p, --payload-path <payload-path>",
"Path to the event payload",
process.env.GITHUB_EVENT_PATH,
)
.option(
"-t, --token <access-token>",
"Access token",
process.env.GITHUB_TOKEN,
)
.option("-a, --app <id>", "ID of the GitHub App", process.env.APP_ID)
.option(
"-P, --private-key <file>",
"Path to private key file (.pem) for the GitHub App",
process.env.PRIVATE_KEY_PATH,
)
.option(
"-L, --log-level <level>",
'One of: "trace" | "debug" | "info" | "warn" | "error" | "fatal"',
process.env.LOG_LEVEL,
)
.option(
"--log-format <format>",
'One of: "pretty", "json"',
process.env.LOG_LEVEL || "pretty",
)
.option(
"--log-level-in-string",
"Set to log levels (trace, debug, info, ...) as words instead of numbers (10, 20, 30, ...)",
process.env.LOG_LEVEL_IN_STRING === "true",
)
.option(
"--log-message-key",
"Set to the string key for the 'message' in the log JSON object",
process.env.LOG_MESSAGE_KEY || "msg",
)
.option(
"--sentry-dsn <dsn>",
'Set to your Sentry DSN, e.g. "https://1234abcd@sentry.io/12345"',
process.env.SENTRY_DSN,
)
.option(
"--base-url <url>",
'GitHub API base URL. If you use GitHub Enterprise Server, and your hostname is "https://github.acme-inc.com", then the root URL is "https://github.acme-inc.com/api/v3"',
process.env.GHE_HOST
? `${process.env.GHE_PROTOCOL || "https"}://${
process.env.GHE_HOST
}/api/v3`
: "https://api.github.com",
)
.parse(process.argv);
const {
app: appId,
baseUrl,
token: githubToken,
event,
payloadPath,
logLevel,
logFormat,
logLevelInString,
logMessageKey,
sentryDsn,
} = program.opts();
if (!event || !payloadPath) {
program.help();
}
const privateKey = getPrivateKey();
if (!githubToken && (!appId || !privateKey)) {
console.warn(
"No token specified and no certificate found, which means you will not be able to do authenticated requests to GitHub",
);
}
const payload = JSON.parse(
fs.readFileSync(path.resolve(payloadPath), "utf8"),
);
const log = getLog({
level: logLevel,
logFormat,
logLevelInString,
logMessageKey,
sentryDsn,
});
const probot = new Probot({
appId,
privateKey: String(privateKey),
githubToken: githubToken,
log,
baseUrl: baseUrl,
});
const expressApp = express();
const options: ApplicationFunctionOptions = {
getRouter: (path: string = "/") => {
const newRouter = Router();
expressApp.use(path, newRouter);
return newRouter;
},
};
const appFn = await resolveAppFunction(
path.resolve(process.cwd(), program.args[0]),
);
await probot.load(appFn, options);
probot.log.debug("Receiving event", event);
probot.receive({ name: event, payload, id: uuidv4() }).catch(() => {
// Process must exist non-zero to indicate that the action failed to run
process.exit(1);
});
}
main().catch((error) => {
console.error(error);
process.exit(1);
});