Skip to content

Commit

Permalink
fix: Make probot receive support complex Probot apps (#1714)
Browse files Browse the repository at this point in the history
The command `probot receive` fails to work when:

1. The Probot appFn is asynchronous
2. The Probot appFn requires `getRouter`

The command `probot run` works fine though with the same app.

This attempts to add support for these sort of Probot apps when calling `probot receive`.
  • Loading branch information
moltenice committed Sep 5, 2022
1 parent 78cb019 commit eff5553
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/bin/probot-receive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Usage: probot receive -e push -p path/to/payload app.js

import express, { Router } from "express";

require("dotenv").config();

import path from "path";
Expand All @@ -8,7 +10,7 @@ import program from "commander";
import { getPrivateKey } from "@probot/get-private-key";
import { getLog } from "../helpers/get-log";

import { Probot } from "../";
import { ApplicationFunctionOptions, Probot } from "../";
import { resolveAppFunction } from "../helpers/resolve-app-function";

async function main() {
Expand Down Expand Up @@ -101,10 +103,19 @@ async function main() {
baseUrl: program.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])
);
probot.load(appFn);
await probot.load(appFn, options);

probot.log.debug("Receiving event", program.event);
probot.receive({ name: program.event, payload, id: uuidv4() }).catch(() => {
Expand Down
8 changes: 6 additions & 2 deletions src/probot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ProbotOctokit } from "./octokit/probot-octokit";
import { VERSION } from "./version";
import {
ApplicationFunction,
ApplicationFunctionOptions,
DeprecatedLogger,
Options,
ProbotWebhooks,
Expand Down Expand Up @@ -104,14 +105,17 @@ export class Probot {
return this.webhooks.receive(event);
}

public async load(appFn: ApplicationFunction | ApplicationFunction[]) {
public async load(
appFn: ApplicationFunction | ApplicationFunction[],
options: ApplicationFunctionOptions = {}
) {
if (Array.isArray(appFn)) {
for (const fn of appFn) {
await this.load(fn);
}
return;
}

return appFn(this, {});
return appFn(this, options);
}
}

0 comments on commit eff5553

Please sign in to comment.