probot
Version:
A framework for building GitHub Apps to automate and improve your workflow
134 lines • 5.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Server = exports.defaultWebhooksPath = void 0;
const node_http_1 = require("node:http");
const node_path_1 = require("node:path");
const express_1 = __importStar(require("express"));
const webhooks_1 = require("@octokit/webhooks");
const logging_middleware_js_1 = require("./logging-middleware.js");
const webhook_proxy_js_1 = require("../helpers/webhook-proxy.js");
const version_js_1 = require("../version.js");
const rebind_log_js_1 = require("../helpers/rebind-log.js");
// the default path as defined in @octokit/webhooks
exports.defaultWebhooksPath = "/api/github/webhooks";
class Server {
static version = version_js_1.VERSION;
expressApp;
log;
version = version_js_1.VERSION;
probotApp;
state;
constructor(options = {}) {
this.expressApp = (0, express_1.default)();
this.probotApp = new options.Probot({
request: options.request,
});
this.log = options.log
? (0, rebind_log_js_1.rebindLog)(options.log)
: (0, rebind_log_js_1.rebindLog)(this.probotApp.log.child({ name: "server" }));
this.state = {
cwd: options.cwd || process.cwd(),
port: options.port,
host: options.host,
webhookPath: options.webhookPath || exports.defaultWebhooksPath,
webhookProxy: options.webhookProxy,
};
this.expressApp.use((0, logging_middleware_js_1.getLoggingMiddleware)(this.log, options.loggingOptions));
this.expressApp.use("/probot/static/", express_1.default.static((0, node_path_1.join)(__dirname, "..", "..", "static")));
// Wrap the webhooks middleware in a function that returns void due to changes in the types for express@v5
// Before, the express types for middleware simply had a return type of void,
// now they have a return type of `void | Promise<void>`.
this.expressApp.use(async (req, res, next) => {
await (0, webhooks_1.createNodeMiddleware)(this.probotApp.webhooks, {
path: this.state.webhookPath,
})(req, res, next);
});
this.expressApp.get("/ping", (_req, res) => {
res.end("PONG");
});
}
async load(appFn) {
await appFn(this.probotApp, {
cwd: this.state.cwd,
getRouter: (path) => this.router(path),
});
}
async start() {
this.log.info(`Running Probot v${this.version} (Node.js: ${process.version})`);
const port = this.state.port || 3000;
const { host, webhookPath, webhookProxy } = this.state;
const printableHost = host ?? "localhost";
this.state.httpServer = await new Promise((resolve, reject) => {
const server = (0, node_http_1.createServer)(this.expressApp).listen(port, ...(host ? [host] : []), async () => {
if (webhookProxy) {
this.state.eventSource = await (0, webhook_proxy_js_1.createWebhookProxy)({
logger: this.log,
path: webhookPath,
port: port,
url: webhookProxy,
});
}
this.log.info(`Listening on http://${printableHost}:${port}`);
resolve(server);
});
server.on("error", (error) => {
if (error.code === "EADDRINUSE") {
error = Object.assign(error, {
message: `Port ${port} is already in use. You can define the PORT environment variable to use a different port.`,
});
}
this.log.error(error);
reject(error);
});
});
return this.state.httpServer;
}
async stop() {
if (this.state.eventSource)
this.state.eventSource.close();
if (!this.state.httpServer)
return;
const server = this.state.httpServer;
return new Promise((resolve) => server.close(resolve));
}
router(path = "/") {
const newRouter = (0, express_1.Router)();
this.expressApp.use(path, newRouter);
return newRouter;
}
}
exports.Server = Server;
//# sourceMappingURL=server.js.map