some structural simplification

This commit is contained in:
km-anthropic
2025-07-29 14:32:28 -07:00
parent 5bdfd090e0
commit 68e711968d
3 changed files with 17 additions and 20 deletions

View File

@@ -37,6 +37,17 @@ export type ScheduleEvent = {
import type { ModeName } from "../modes/types"; import type { ModeName } from "../modes/types";
import { DEFAULT_MODE, isValidMode } from "../modes/registry"; import { DEFAULT_MODE, isValidMode } from "../modes/registry";
// Event name constants for better maintainability
const ENTITY_EVENT_NAMES = [
"issues",
"issue_comment",
"pull_request",
"pull_request_review",
"pull_request_review_comment",
] as const;
const AUTOMATION_EVENT_NAMES = ["workflow_dispatch", "schedule"] as const;
// Common fields shared by all context types // Common fields shared by all context types
type BaseContext = { type BaseContext = {
runId: string; runId: string;
@@ -265,21 +276,12 @@ export function isIssuesAssignedEvent(
export function isEntityContext( export function isEntityContext(
context: GitHubContext, context: GitHubContext,
): context is ParsedGitHubContext { ): context is ParsedGitHubContext {
return ( return ENTITY_EVENT_NAMES.includes(context.eventName as any);
context.eventName === "issues" ||
context.eventName === "issue_comment" ||
context.eventName === "pull_request" ||
context.eventName === "pull_request_review" ||
context.eventName === "pull_request_review_comment"
);
} }
// Type guard to check if context is an automation context // Type guard to check if context is an automation context
export function isAutomationContext( export function isAutomationContext(
context: GitHubContext, context: GitHubContext,
): context is AutomationContext { ): context is AutomationContext {
return ( return AUTOMATION_EVENT_NAMES.includes(context.eventName as any);
context.eventName === "workflow_dispatch" ||
context.eventName === "schedule"
);
} }

View File

@@ -1,6 +1,7 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import { mkdir, writeFile } from "fs/promises"; import { mkdir, writeFile } from "fs/promises";
import type { Mode, ModeOptions, ModeResult } from "../types"; import type { Mode, ModeOptions, ModeResult } from "../types";
import { isAutomationContext } from "../../github/context";
/** /**
* Agent mode implementation. * Agent mode implementation.
@@ -15,10 +16,7 @@ export const agentMode: Mode = {
shouldTrigger(context) { shouldTrigger(context) {
// Only trigger for automation events // Only trigger for automation events
return ( return isAutomationContext(context);
context.eventName === "workflow_dispatch" ||
context.eventName === "schedule"
);
}, },
prepareContext(context) { prepareContext(context) {

View File

@@ -14,6 +14,7 @@ import type { Mode, ModeName } from "./types";
import { tagMode } from "./tag"; import { tagMode } from "./tag";
import { agentMode } from "./agent"; import { agentMode } from "./agent";
import type { GitHubContext } from "../github/context"; import type { GitHubContext } from "../github/context";
import { isAutomationContext } from "../github/context";
export const DEFAULT_MODE = "tag" as const; export const DEFAULT_MODE = "tag" as const;
export const VALID_MODES = ["tag", "agent"] as const; export const VALID_MODES = ["tag", "agent"] as const;
@@ -44,11 +45,7 @@ export function getMode(name: ModeName, context: GitHubContext): Mode {
} }
// Validate mode can handle the event type // Validate mode can handle the event type
if ( if (name === "tag" && isAutomationContext(context)) {
name === "tag" &&
(context.eventName === "workflow_dispatch" ||
context.eventName === "schedule")
) {
throw new Error( throw new Error(
`Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events.`, `Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events.`,
); );