From 68e711968d5eb9113969c78b6cb1e6662eb2edee Mon Sep 17 00:00:00 2001 From: km-anthropic Date: Tue, 29 Jul 2025 14:32:28 -0700 Subject: [PATCH] some structural simplification --- src/github/context.ts | 24 +++++++++++++----------- src/modes/agent/index.ts | 6 ++---- src/modes/registry.ts | 7 ++----- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/github/context.ts b/src/github/context.ts index 8f0ad81..5eb3258 100644 --- a/src/github/context.ts +++ b/src/github/context.ts @@ -37,6 +37,17 @@ export type ScheduleEvent = { import type { ModeName } from "../modes/types"; 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 type BaseContext = { runId: string; @@ -265,21 +276,12 @@ export function isIssuesAssignedEvent( export function isEntityContext( context: GitHubContext, ): context is ParsedGitHubContext { - return ( - context.eventName === "issues" || - context.eventName === "issue_comment" || - context.eventName === "pull_request" || - context.eventName === "pull_request_review" || - context.eventName === "pull_request_review_comment" - ); + return ENTITY_EVENT_NAMES.includes(context.eventName as any); } // Type guard to check if context is an automation context export function isAutomationContext( context: GitHubContext, ): context is AutomationContext { - return ( - context.eventName === "workflow_dispatch" || - context.eventName === "schedule" - ); + return AUTOMATION_EVENT_NAMES.includes(context.eventName as any); } diff --git a/src/modes/agent/index.ts b/src/modes/agent/index.ts index a32260a..15a8d0c 100644 --- a/src/modes/agent/index.ts +++ b/src/modes/agent/index.ts @@ -1,6 +1,7 @@ import * as core from "@actions/core"; import { mkdir, writeFile } from "fs/promises"; import type { Mode, ModeOptions, ModeResult } from "../types"; +import { isAutomationContext } from "../../github/context"; /** * Agent mode implementation. @@ -15,10 +16,7 @@ export const agentMode: Mode = { shouldTrigger(context) { // Only trigger for automation events - return ( - context.eventName === "workflow_dispatch" || - context.eventName === "schedule" - ); + return isAutomationContext(context); }, prepareContext(context) { diff --git a/src/modes/registry.ts b/src/modes/registry.ts index 829e1d4..83ce7ab 100644 --- a/src/modes/registry.ts +++ b/src/modes/registry.ts @@ -14,6 +14,7 @@ import type { Mode, ModeName } from "./types"; import { tagMode } from "./tag"; import { agentMode } from "./agent"; import type { GitHubContext } from "../github/context"; +import { isAutomationContext } from "../github/context"; export const DEFAULT_MODE = "tag" 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 - if ( - name === "tag" && - (context.eventName === "workflow_dispatch" || - context.eventName === "schedule") - ) { + if (name === "tag" && isAutomationContext(context)) { throw new Error( `Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events.`, );