Skip to content

Commit

Permalink
feat: ref
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Feb 22, 2024
1 parent 295284a commit b02d104
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/github/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GitHubEventHandler } from "../github-event-handler";
import { getConfig } from "../utils/config";
import { issueCommentCreated } from "./issue-comment/created";
import { repositoryDispatch } from "./repository-dispatch";
import { dispatchWorkflow } from "../utils/workflow-dispatch";
import { dispatchWorkflow, getDefaultBranch } from "../utils/workflow-dispatch";

function tryCatchWrapper(fn: (event: EmitterWebhookEvent) => unknown) {
return async (event: EmitterWebhookEvent) => {
Expand Down Expand Up @@ -46,14 +46,15 @@ async function handleEvent(event: EmitterWebhookEvent, eventHandler: InstanceTyp
for (const { workflow, settings } of handler) {
console.log(`Calling handler for event ${event.name} and workflow ${workflow}`);

const ref = workflow.ref ?? (await getDefaultBranch(context, workflow.owner, workflow.repository));
const token = await eventHandler.getToken(event.payload.installation.id);
const inputs = new DelegatedComputeInputs(context.key, event, settings, token);
const inputs = new DelegatedComputeInputs(context.key, event, settings, token, ref);

await dispatchWorkflow(context, {
owner: workflow.owner,
repository: workflow.repository,
workflowId: workflow.workflowId,
ref: workflow.branch,
ref: workflow.ref,
inputs: inputs.getInputs(),
});
}
Expand All @@ -64,12 +65,14 @@ class DelegatedComputeInputs<T extends EmitterWebhookEventName = EmitterWebhookE
public event: EmitterWebhookEvent<T>;
public settings: unknown;
public authToken: string;
public ref: string;

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

public getInputs() {
Expand All @@ -78,6 +81,7 @@ class DelegatedComputeInputs<T extends EmitterWebhookEventName = EmitterWebhookE
event: JSON.stringify(this.event),
settings: JSON.stringify(this.settings),
authToken: this.authToken,
ref: this.ref,
};
}
}
2 changes: 1 addition & 1 deletion src/github/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const handlerSchema = T.Array(
owner: T.String(),
repository: T.String(),
workflowId: T.String(),
branch: T.Optional(T.String()),
ref: T.Optional(T.String()),
}),
settings: T.Optional(T.Unknown()),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/github/utils/workflow-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function dispatchWorkflow(context: GitHubContext, options: Workflow
});
}

async function getDefaultBranch(context: GitHubContext, owner: string, repository: string) {
export async function getDefaultBranch(context: GitHubContext, owner: string, repository: string) {
const repo = await context.octokit.repos.get({
owner: owner,
repo: repository,
Expand Down

0 comments on commit b02d104

Please sign in to comment.