Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/probot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ export class Probot {
return this;
}

public async recover(): Promise<void> {
if (this.#state.initializationState === INITIALIZED) {
return;
}
this.#state.initializationState = UNINITIALIZED;
this.#state.initializedPromise = createDeferredPromise<void>();
await this.#initialize();
}

public async receive(event: WebhookEvent): Promise<void> {
await this.#state.initializedPromise.promise;

Expand Down
37 changes: 37 additions & 0 deletions test/probot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,4 +772,41 @@ describe("Probot", () => {
);
});
});

describe("recover", () => {
it("allows re-initialization after a failed initialization", async () => {
let constructorCallCount = 0;

class FailOnFirstConstruct extends ProbotOctokit {
constructor(options?: ConstructorParameters<typeof ProbotOctokit>[0]) {
super(options);
constructorCallCount++;
if (constructorCallCount === 1) {
throw new Error("Simulated initialization failure");
}
}
}

const probot = new Probot({
appId,
privateKey,
Octokit: FailOnFirstConstruct,
});

let firstError: Error | undefined;
try {
await probot.auth();
} catch (e) {
firstError = e as Error;
}

expect(firstError).toBeDefined();
expect(firstError?.message).toContain("Simulated initialization failure");

await probot.recover();

const octokit = await probot.auth();
expect(octokit).toBeDefined();
});
});
});