Skip to content
Open
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
14 changes: 12 additions & 2 deletions src/github/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
PullRequestEvent,
PullRequestReviewEvent,
PullRequestReviewCommentEvent,
PushEvent,
WorkflowRunEvent,
} from "@octokit/webhooks-types";
import { CLAUDE_APP_BOT_ID, CLAUDE_BOT_LOGIN } from "./constants";
Expand Down Expand Up @@ -65,6 +66,7 @@ const AUTOMATION_EVENT_NAMES = [
"repository_dispatch",
"schedule",
"workflow_run",
"push",
] as const;

// Derive types from constants for better maintainability
Expand Down Expand Up @@ -118,14 +120,15 @@ export type ParsedGitHubContext = BaseContext & {
isPR: boolean;
};

// Context for automation events (workflow_dispatch, repository_dispatch, schedule, workflow_run)
// Context for automation events (workflow_dispatch, repository_dispatch, schedule, workflow_run, push)
export type AutomationContext = BaseContext & {
eventName: AutomationEventName;
payload:
| WorkflowDispatchEvent
| RepositoryDispatchEvent
| ScheduleEvent
| WorkflowRunEvent;
| WorkflowRunEvent
| PushEvent;
};

// Union type for all contexts
Expand Down Expand Up @@ -247,6 +250,13 @@ export function parseGitHubContext(): GitHubContext {
payload: context.payload as unknown as WorkflowRunEvent,
};
}
case "push": {
return {
...commonFields,
eventName: "push",
payload: context.payload as unknown as PushEvent,
};
}
default:
throw new Error(`Unsupported event type: ${context.eventName}`);
}
Expand Down
24 changes: 23 additions & 1 deletion test/github-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ describe("parseGitHubContext", () => {
expect(context.eventName).toBe("workflow_run");
expect(isAutomationContext(context)).toBe(true);
});

test("push produces an automation context", () => {
setEvent("push", {
ref: "refs/heads/main",
before: "0000000000000000000000000000000000000000",
after: "1111111111111111111111111111111111111111",
repository: repositoryPayload,
pusher: { name: "test-actor", email: "test-actor@example.com" },
});

const context = parseGitHubContext();

expect(context.eventName).toBe("push");
expect(isAutomationContext(context)).toBe(true);
expect("entityNumber" in context).toBe(false);
expect("isPR" in context).toBe(false);
});
});

describe("invalid partition", () => {
Expand Down Expand Up @@ -442,6 +459,9 @@ describe("type guards", () => {
const workflowDispatchContext = createMockAutomationContext({
eventName: "workflow_dispatch",
});
const pushContext = createMockAutomationContext({
eventName: "push",
});

test("isIssuesEvent accepts only issues events", () => {
expect(isIssuesEvent(issuesContext)).toBe(true);
Expand Down Expand Up @@ -496,9 +516,10 @@ describe("type guards", () => {
expect(isEntityContext(reviewContext)).toBe(true);
expect(isEntityContext(reviewCommentContext)).toBe(true);
expect(isEntityContext(workflowDispatchContext)).toBe(false);
expect(isEntityContext(pushContext)).toBe(false);
});

test("isAutomationContext accepts the four automation events", () => {
test("isAutomationContext accepts the five automation events", () => {
expect(isAutomationContext(workflowDispatchContext)).toBe(true);
expect(
isAutomationContext(
Expand All @@ -515,6 +536,7 @@ describe("type guards", () => {
createMockAutomationContext({ eventName: "workflow_run" }),
),
).toBe(true);
expect(isAutomationContext(pushContext)).toBe(true);
expect(isAutomationContext(issuesContext)).toBe(false);
});
});