import type { TriggerAccountRequirement, TriggerDefinition } from "../types" import * as z from "zod/mini" const AUTOMATE_PROJECT_READ_ACCOUNT = { connectionOptions: [ { connectionMethodId: "organization-api-key", requiredScopes: ["project:read"], }, ], planDefaultBinding: false, serviceId: "automate", } as const satisfies TriggerAccountRequirement const AUTOMATE_DEPLOYMENT_READ_ACCOUNT = { connectionOptions: [ { connectionMethodId: "organization-api-key", requiredScopes: ["deployment:read"], }, ], planDefaultBinding: false, serviceId: "automate", } as const satisfies TriggerAccountRequirement const AUTOMATION_CONFIG_SCHEMA = z.object({ automation: z.string() }) const DEPLOYMENT_CONFIG_SCHEMA = z.object({ projectId: z.string() }) export const automateTriggerDefinitions = { automateActionFailed: automationTrigger("automate.action.failed"), automateActionIndeterminate: automationTrigger( "automate.action.indeterminate", ), automateActionRetryScheduled: automationTrigger( "automate.action.retryScheduled", ), automateActionAttemptEvent: automationTrigger("automate.actionAttempt.event"), automateAutomationDisabled: automationTrigger("automate.automation.disabled"), automateAutomationEnabled: automationTrigger("automate.automation.enabled"), automateAutomationStateChanged: automationTrigger( "automate.automation.stateChanged", ), automateDeploymentAwaitingAuthorization: deploymentTrigger( "automate.deployment.awaitingAuthorization", ), automateDeploymentCancelled: deploymentTrigger( "automate.deployment.cancelled", ), automateDeploymentEvent: deploymentTrigger("automate.deployment.event"), automateDeploymentFailed: deploymentTrigger("automate.deployment.failed"), automateDeploymentSucceeded: deploymentTrigger( "automate.deployment.succeeded", ), automateExecutionEvent: automationTrigger("automate.execution.event"), automateExecutionFailed: automationTrigger("automate.execution.failed"), automateExecutionSettled: automationTrigger("automate.execution.settled"), } as const satisfies Record /** * Defines an automation-scoped lifecycle trigger. * * @param type - Registered trigger type. */ function automationTrigger(type: TType) { return { account: AUTOMATE_PROJECT_READ_ACCOUNT, getDetails: ({ config }) => [ { label: "Automation", value: AUTOMATION_CONFIG_SCHEMA.parse(config).automation, }, ], type, } as const satisfies TriggerDefinition } /** * Defines a project-scoped deployment lifecycle trigger. * * @param type - Registered trigger type. */ function deploymentTrigger(type: TType) { return { account: AUTOMATE_DEPLOYMENT_READ_ACCOUNT, getDetails: ({ config }) => [ { label: "Project ID", value: DEPLOYMENT_CONFIG_SCHEMA.parse(config).projectId, }, ], type, } as const satisfies TriggerDefinition }