Skip to content

Commit

Permalink
feat: pass auth token instead of installation id
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Feb 18, 2024
1 parent 3c0b829 commit b224246
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"open-source"
],
"dependencies": {
"@octokit/auth-app": "^6.0.3",
"@octokit/webhooks": "^12.0.10",
"@octokit/webhooks-types": "^7.3.1",
"@sinclair/typebox": "^0.32.5",
Expand Down
10 changes: 10 additions & 0 deletions src/github/github-event-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EmitterWebhookEvent, Webhooks } from "@octokit/webhooks";
import { customOctokit } from "./github-client";
import { GitHubContext, SimplifiedContext } from "./github-context";
import { createAppAuth } from "@octokit/auth-app";

export type Options = {
webhookSecret: string;
Expand Down Expand Up @@ -68,4 +69,13 @@ export class GitHubEventHandler {
},
});
}

async getToken(installationId: number) {
const auth = createAppAuth({
appId: this._appId,
privateKey: this._privateKey,
});
const token = await auth({ type: "installation", installationId });
return token.token;
}
}
21 changes: 11 additions & 10 deletions src/github/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
return;
}

if (!("installation" in event.payload) || event.payload.installation?.id === undefined) {
console.log("No installation found");
return;
}

const handler = config.handlers.events[context.key];

if (handler.length === 0) {
Expand All @@ -41,12 +46,8 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
for (const { workflow, settings } of handler) {
console.log(`Calling handler for event ${event.name} and workflow ${workflow}`);

let installationId: string | undefined = undefined;
if ("installation" in event.payload && event.payload.installation?.id !== undefined) {
installationId = event.payload.installation.id.toString();
}

const inputs = new DelegatedComputeInputs(context.key, event, settings, installationId);
const token = await eventHandler.getToken(event.payload.installation.id);
const inputs = new DelegatedComputeInputs(context.key, event, settings, token);

await dispatchWorkflow(context, {
owner: workflow.owner,
Expand All @@ -62,21 +63,21 @@ class DelegatedComputeInputs<T extends EmitterWebhookEventName = EmitterWebhookE
public eventName: T;
public event: EmitterWebhookEvent<T>;
public settings: unknown;
public installationId?: string;
public authToken: string;

constructor(eventName: T, event: EmitterWebhookEvent<T>, settings: unknown, installationId?: string) {
constructor(eventName: T, event: EmitterWebhookEvent<T>, settings: unknown, authToken: string) {
this.eventName = eventName;
this.event = event;
this.settings = settings;
this.installationId = installationId;
this.authToken = authToken;
}

public getInputs() {
return {
eventName: this.eventName,
event: JSON.stringify(this.event),
settings: JSON.stringify(this.settings),
installationId: this.installationId ?? "",
authToken: this.authToken.toString(),
};
}
}

0 comments on commit b224246

Please sign in to comment.