import {z} from 'zod' export type ChangeRequestSettings = {Type?: ('ServiceNow' | 'JiraServiceManagement') | undefined}; export type RunGitRunbookRun = {ChangeRequestSettings?: Array | undefined, Comments?: string | undefined, DebugMode?: string | undefined, EnvironmentId: string, ExcludedMachineIds?: Array | undefined, ExcludedTargetTagIds?: Array | undefined, FailTargetDiscovery?: boolean | undefined, ForcePackageDownload?: boolean | undefined, FormValues?: {[key: string]: string} | undefined, Priority?: string | undefined, QueueTime?: string | undefined, QueueTimeExpiry?: string | undefined, SkipActions?: Array | undefined, SpecificMachineIds?: Array | undefined, SpecificTargetTagIds?: Array | undefined, TenantId?: string | undefined, UseGuidedFailure?: boolean | undefined}; export type GitReferenceResource = {GitCommit?: string | undefined, GitRef?: string | undefined}; export type SelectedGitResource = {ActionName: string, GitReferenceResource: GitReferenceResource, GitResourceReferenceName?: string | undefined}; export type SelectedPackage = {ActionName?: string | undefined, PackageReferenceName?: string | undefined, StepName?: string | undefined, Version?: string | undefined}; export type RunGitRunbookCommand = {GitRef: string, Notes?: string | undefined, ProjectId: string, RunbookId: string, Runs: Array, SelectedGitResources?: Array | undefined, SelectedPackages?: Array | undefined, SpaceId: string}; export type CreateApiProjectsProjectIdGitRefRunbooksRunbookIdRunV1Args = {projectId: string, gitRef: string, runbookId: string, body: RunGitRunbookCommand}; export type CreateApiProjectsProjectIdGitRefRunbooksRunbookIdRunV1Options = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const changeRequestSettings = z.object({Type: z.enum(["ServiceNow", "JiraServiceManagement"]).optional()}); export const stepLogContext = z.object({CorrelationId: z.string(), Slug: z.string().min(1)}); export const executionPlanLogContext = z.object({Steps: z.array(stepLogContext)}); export const retentionPeriod = z.object({QuantityToKeep: z.number().int().optional(), ShouldKeepForever: z.boolean().optional(), Strategy: z.string().optional(), Unit: z.enum(["Days", "Items"]).optional()}); export const runbookRunResource = z.object({ChangeRequestSettings: z.array(changeRequestSettings).optional(), Comments: z.string().optional(), Created: z.string().optional(), DebugMode: z.string().optional(), DeployedBy: z.string().optional(), DeployedByActorType: z.enum(["Unknown", "System", "User", "Agent", "ServiceAccount"]).optional(), DeployedById: z.string().optional(), DeployedToMachineIds: z.array(z.string()).optional(), EnvironmentId: z.string(), ExcludedMachineIds: z.array(z.string()).optional(), ExcludedTargetTagIds: z.array(z.string()).optional(), ExecutionPlanLogContext: executionPlanLogContext.optional(), FailTargetDiscovery: z.boolean().optional(), FailureEncountered: z.boolean().optional(), ForcePackageDownload: z.boolean().optional(), FormValues: z.record(z.string(), z.string()).optional(), FrozenRunbookProcessId: z.string().optional(), Id: z.string().optional(), LastModifiedBy: z.string().optional(), LastModifiedOn: z.string().optional(), Links: z.record(z.string(), z.string()).optional(), ManifestVariableSetId: z.string().optional(), Name: z.string().optional(), Priority: z.string().optional(), ProjectId: z.string().optional(), QueueTime: z.string().optional(), QueueTimeExpiry: z.string().optional(), RunbookId: z.string().min(1), RunbookName: z.string().optional(), RunbookSnapshotId: z.string().min(1), SkipActions: z.array(z.string()).optional(), SpaceId: z.string().optional(), SpecificMachineIds: z.array(z.string()).optional(), SpecificTargetTagIds: z.array(z.string()).optional(), TaskId: z.string().optional(), TenantId: z.string().optional(), TentacleRetentionPeriod: retentionPeriod.optional(), UseGuidedFailure: z.boolean().optional()}); export const runGitRunbookResponse = z.object({Resources: z.array(runbookRunResource)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a new Runbook Run * * POST /projects/{projectId}/{gitRef}/runbooks/{runbookId}/run/v1 * * Also accepts auth: APIKeyQuery, NugetApiKeyHeader */ export const createApiProjectsProjectIdGitRefRunbooksRunbookIdRunV1 = async (args: CreateApiProjectsProjectIdGitRefRunbooksRunbookIdRunV1Args, options?: CreateApiProjectsProjectIdGitRefRunbooksRunbookIdRunV1Options) => { const {baseUrl = "https://demo.octopus.app/api", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-Octopus-ApiKey", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/${encodeURIComponent(String(args.gitRef))}/runbooks/${encodeURIComponent(String(args.runbookId))}/run/v1${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(runGitRunbookResponse, await res.json()) };