mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
refactor: further simplify discriminated union implementation
- Add event name constants to reduce duplication - Derive EntityEventName and AutomationEventName types from constants - Use isAutomationContext consistently in agent mode and registry - Simplify parseGitHubContext by removing redundant type assertions - Extract payload casts to variables for cleaner code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -48,6 +48,10 @@ const ENTITY_EVENT_NAMES = [
|
|||||||
|
|
||||||
const AUTOMATION_EVENT_NAMES = ["workflow_dispatch", "schedule"] as const;
|
const AUTOMATION_EVENT_NAMES = ["workflow_dispatch", "schedule"] as const;
|
||||||
|
|
||||||
|
// Derive types from constants for better maintainability
|
||||||
|
type EntityEventName = typeof ENTITY_EVENT_NAMES[number];
|
||||||
|
type AutomationEventName = typeof AUTOMATION_EVENT_NAMES[number];
|
||||||
|
|
||||||
// Common fields shared by all context types
|
// Common fields shared by all context types
|
||||||
type BaseContext = {
|
type BaseContext = {
|
||||||
runId: string;
|
runId: string;
|
||||||
@@ -78,12 +82,7 @@ type BaseContext = {
|
|||||||
|
|
||||||
// Context for entity-based events (issues, PRs, comments)
|
// Context for entity-based events (issues, PRs, comments)
|
||||||
export type ParsedGitHubContext = BaseContext & {
|
export type ParsedGitHubContext = BaseContext & {
|
||||||
eventName:
|
eventName: EntityEventName;
|
||||||
| "issues"
|
|
||||||
| "issue_comment"
|
|
||||||
| "pull_request"
|
|
||||||
| "pull_request_review"
|
|
||||||
| "pull_request_review_comment";
|
|
||||||
payload:
|
payload:
|
||||||
| IssuesEvent
|
| IssuesEvent
|
||||||
| IssueCommentEvent
|
| IssueCommentEvent
|
||||||
@@ -96,7 +95,7 @@ export type ParsedGitHubContext = BaseContext & {
|
|||||||
|
|
||||||
// Context for automation events (workflow_dispatch, schedule)
|
// Context for automation events (workflow_dispatch, schedule)
|
||||||
export type AutomationContext = BaseContext & {
|
export type AutomationContext = BaseContext & {
|
||||||
eventName: "workflow_dispatch" | "schedule";
|
eventName: AutomationEventName;
|
||||||
payload: WorkflowDispatchEvent | ScheduleEvent;
|
payload: WorkflowDispatchEvent | ScheduleEvent;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -142,67 +141,68 @@ export function parseGitHubContext(): GitHubContext {
|
|||||||
|
|
||||||
switch (context.eventName) {
|
switch (context.eventName) {
|
||||||
case "issues": {
|
case "issues": {
|
||||||
|
const payload = context.payload as IssuesEvent;
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
eventName: "issues" as const,
|
eventName: "issues",
|
||||||
payload: context.payload as IssuesEvent,
|
payload,
|
||||||
entityNumber: (context.payload as IssuesEvent).issue.number,
|
entityNumber: payload.issue.number,
|
||||||
isPR: false,
|
isPR: false,
|
||||||
} as ParsedGitHubContext;
|
};
|
||||||
}
|
}
|
||||||
case "issue_comment": {
|
case "issue_comment": {
|
||||||
|
const payload = context.payload as IssueCommentEvent;
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
eventName: "issue_comment" as const,
|
eventName: "issue_comment",
|
||||||
payload: context.payload as IssueCommentEvent,
|
payload,
|
||||||
entityNumber: (context.payload as IssueCommentEvent).issue.number,
|
entityNumber: payload.issue.number,
|
||||||
isPR: Boolean(
|
isPR: Boolean(payload.issue.pull_request),
|
||||||
(context.payload as IssueCommentEvent).issue.pull_request,
|
};
|
||||||
),
|
|
||||||
} as ParsedGitHubContext;
|
|
||||||
}
|
}
|
||||||
case "pull_request": {
|
case "pull_request": {
|
||||||
|
const payload = context.payload as PullRequestEvent;
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
eventName: "pull_request" as const,
|
eventName: "pull_request",
|
||||||
payload: context.payload as PullRequestEvent,
|
payload,
|
||||||
entityNumber: (context.payload as PullRequestEvent).pull_request.number,
|
entityNumber: payload.pull_request.number,
|
||||||
isPR: true,
|
isPR: true,
|
||||||
} as ParsedGitHubContext;
|
};
|
||||||
}
|
}
|
||||||
case "pull_request_review": {
|
case "pull_request_review": {
|
||||||
|
const payload = context.payload as PullRequestReviewEvent;
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
eventName: "pull_request_review" as const,
|
eventName: "pull_request_review",
|
||||||
payload: context.payload as PullRequestReviewEvent,
|
payload,
|
||||||
entityNumber: (context.payload as PullRequestReviewEvent).pull_request
|
entityNumber: payload.pull_request.number,
|
||||||
.number,
|
|
||||||
isPR: true,
|
isPR: true,
|
||||||
} as ParsedGitHubContext;
|
};
|
||||||
}
|
}
|
||||||
case "pull_request_review_comment": {
|
case "pull_request_review_comment": {
|
||||||
|
const payload = context.payload as PullRequestReviewCommentEvent;
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
eventName: "pull_request_review_comment" as const,
|
eventName: "pull_request_review_comment",
|
||||||
payload: context.payload as PullRequestReviewCommentEvent,
|
payload,
|
||||||
entityNumber: (context.payload as PullRequestReviewCommentEvent)
|
entityNumber: payload.pull_request.number,
|
||||||
.pull_request.number,
|
|
||||||
isPR: true,
|
isPR: true,
|
||||||
} as ParsedGitHubContext;
|
};
|
||||||
}
|
}
|
||||||
case "workflow_dispatch": {
|
case "workflow_dispatch": {
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
eventName: "workflow_dispatch" as const,
|
eventName: "workflow_dispatch",
|
||||||
payload: context.payload as unknown as WorkflowDispatchEvent,
|
payload: context.payload as unknown as WorkflowDispatchEvent,
|
||||||
} as AutomationContext;
|
};
|
||||||
}
|
}
|
||||||
case "schedule": {
|
case "schedule": {
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
eventName: "schedule" as const,
|
eventName: "schedule",
|
||||||
payload: context.payload as unknown as ScheduleEvent,
|
payload: context.payload as unknown as ScheduleEvent,
|
||||||
} as AutomationContext;
|
};
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported event type: ${context.eventName}`);
|
throw new Error(`Unsupported event type: ${context.eventName}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user