Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 127 additions & 58 deletions apps/docs/openapi-v2-workflows.json

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions apps/sim/app/api/v2/workflows/[workflowId]/execute/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,43 @@ describe('POST /api/v2/workflows/[workflowId]/execute', () => {
expect(mockExecuteWorkflowCore).not.toHaveBeenCalled()
})

describe('names used before the draft-run rename', () => {
it('keeps the draft-run limits for source manual: no async, no input beside the sample', async () => {
authenticatePersonalKey()

const asyncResponse = await callExecute({ run: { source: 'manual' }, async: true })
expect(asyncResponse.status).toBe(400)
expect((await asyncResponse.json()).error.message).toBe(
'A draft run does not support async mode'
)

const withInput = await callExecute({
input: { event: 'created' },
run: { source: 'manual', entry: { type: 'trigger', useMockPayload: true } },
})
expect(withInput.status).toBe(400)
expect((await withInput.json()).error.message).toBe(
'input and run.entry.sample cannot be combined'
)
expect(mockExecuteManualTrigger).not.toHaveBeenCalled()
expect(mockExecuteWorkflowCore).not.toHaveBeenCalled()
})

it('refuses useMockPayload and sample that disagree, before dispatch', async () => {
authenticatePersonalKey()

const response = await callExecute({
run: { source: 'draft', entry: { type: 'trigger', sample: true, useMockPayload: false } },
})

expect(response.status).toBe(400)
expect(JSON.stringify(await response.json())).toContain(
'useMockPayload is the deprecated name of sample'
)
expect(mockExecuteManualTrigger).not.toHaveBeenCalled()
})
})

it('dispatches an OAuth manual trigger run through the same manual operation', async () => {
mockAuthenticateV2ApiKey.mockResolvedValue({
principal: {
Expand Down Expand Up @@ -1045,6 +1082,17 @@ describe('POST /api/v2/workflows/[workflowId]/execute', () => {
expect(mockExecuteManualTrigger).not.toHaveBeenCalled()
})

it('never permits a run named source manual on the anonymous public path', async () => {
dbChainMockFns.limit.mockReset()
queuePublicWorkflowReads()

const response = await callPublicExecute({ run: { source: 'manual' } })

expect(response.status).toBe(401)
expect(mockExecuteManualTrigger).not.toHaveBeenCalled()
expect(mockExecuteWorkflowCore).not.toHaveBeenCalled()
})

it('returns not found when a public workflow disappears before authorization', async () => {
dbChainMockFns.limit.mockReset()
queuePublicWorkflowReads()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { presentDry } from '@/lib/api/contracts/v2/deprecated-names'
import { v2ApplyWorkflowOperationsContract } from '@/lib/api/contracts/v2/workflows'
import { defineV2JsonRoute, v2ApiKeyAuth, v2RateLimits } from '@/lib/api/server/routes'
import { v2WorkflowErrorPolicies } from '@/lib/workflows/api'
Expand Down Expand Up @@ -53,7 +54,7 @@ export const POST = defineV2JsonRoute({
lint: presentWorkflowLint(result.lint),
warnings: result.warnings,
needsRedeployment: result.needsRedeployment,
dry: result.dryRun,
...presentDry(result.dryRun),
},
}),
})
3 changes: 2 additions & 1 deletion apps/sim/app/api/v2/workflows/[workflowId]/state/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BlockState, WorkflowState } from '@sim/workflow-types/workflow'
import { presentDry } from '@/lib/api/contracts/v2/deprecated-names'
import {
v2GetWorkflowStateContract,
v2ReplaceWorkflowStateContract,
Expand Down Expand Up @@ -71,7 +72,7 @@ export const PUT = defineV2JsonRoute({
warnings,
needsRedeployment,
lint: presentWorkflowLint(lint),
dry: dryRun,
...presentDry(dryRun),
},
}),
})
23 changes: 23 additions & 0 deletions apps/sim/app/api/v2/workspaces/[workspaceId]/source/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ describe('POST /api/v2/workspaces/{workspaceId}/source', () => {
expect(charges()).toEqual(['workflows.source.write 1', 'workflows.source.write 39'])
})

it('runs the pre-rename ?dryRun as a dry run, in the plan bucket, answering under both names', async () => {
v2RouteMocks.operationRate.mockClear()
mocks.apply.mockClear()
mocks.apply.mockResolvedValue({
workspaceId: WORKSPACE_ID,
dryRun: true,
plan: [],
applied: [],
needBlobs: [],
files: {},
base: { version: 1, workspaceId: WORKSPACE_ID, resources: {} },
diagnostics: [],
})

const response = await apply('?dryRun=true')

expect(response.status).toBe(200)
expect(mocks.apply.mock.calls[0][0].input).toMatchObject({ dryRun: true })
const buckets = v2RouteMocks.operationRate.mock.calls.map(([key]) => String(key).split(':')[1])
expect(new Set(buckets)).toEqual(new Set(['source.plan']))
expect((await response.json()).data).toMatchObject({ dry: true, dryRun: true })
})

it('runs dry runs in plan slots, never in an apply’s: past the plans this server runs at once a dry run answers 429 and a real apply still runs', async () => {
const result = {
workspaceId: WORKSPACE_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,14 @@ describe('POST /api/v2/workspaces/{workspaceId}/workflows/source', () => {
expect(response.status).toBe(200)
expect(mocks.create.mock.calls[0][0].input).toMatchObject({ dryRun: true })
})

it('treats the pre-rename ?dryRun as dry and answers under both names', async () => {
mocks.create.mockResolvedValue({ ...created, dryRun: true, applied: false })

const response = await POST(createRequest({ files }, '?dryRun=true'), context)

expect(response.status).toBe(200)
expect(mocks.create.mock.calls[0][0].input).toMatchObject({ dryRun: true })
expect((await response.json()).data).toMatchObject({ dry: true, dryRun: true })
})
})
58 changes: 58 additions & 0 deletions apps/sim/lib/api/contracts/v2/deprecated-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { z } from 'zod'

/**
* Deprecated names on the public v2 surface. A field, query parameter or value renamed after it
* shipped keeps its old name as an alias, so a client written against the earlier name keeps
* working: a request alias is folded into the current name at parse time (routes and use cases
* only ever see the current one), and a renamed response field is sent under both names.
*/

/** A request field kept under its old name: same meaning as `current`, marked deprecated. */
export function deprecatedRequestName<S extends z.ZodType>(current: string, schema: S): S {
return schema.meta({
deprecated: true,
description: `Deprecated name of \`${current}\`, accepted with the same meaning. Send \`${current}\`; a request that sends both with different values is refused.`,
})
}

/** A response field still sent under its old name: always equal to `current`, marked deprecated. */
function deprecatedResponseName<S extends z.ZodType>(current: string, schema: S): S {
return schema.meta({
deprecated: true,
description: `Deprecated name of \`${current}\`, always sent with the same value. Read \`${current}\`.`,
})
}

/**
* Folds the deprecated `old` field of an object into `current` and drops it from the parsed
* value. Sending only `old` acts exactly as sending `current`; sending both is refused unless
* they agree.
*/
export function foldDeprecatedName<
S extends z.ZodType<Record<string, unknown>>,
Old extends keyof z.output<S> & string,
Current extends keyof z.output<S> & string,
>(schema: S, old: Old, current: Current) {
return schema.transform((value, ctx): Omit<z.output<S>, Old> => {
const { [old]: legacy, ...rest } = value
if (legacy === undefined) return rest
const given: unknown = value[current]
if (given !== undefined && given !== legacy) {
ctx.addIssue({
code: 'custom',
path: [old],
message: `${old} is the deprecated name of ${current}; send only ${current}`,
})
return z.NEVER
}
return { ...rest, [current]: legacy }
})
}

/** Both names of a dry-run flag in a response: `dry`, and the deprecated `dryRun`. */
export function presentDry(dry: boolean): { dry: boolean; dryRun: boolean } {
return { dry, dryRun: dry }
}

/** The deprecated `dryRun` response field beside `dry`. */
export const deprecatedDryRunResponseField = deprecatedResponseName('dry', z.boolean())
3 changes: 3 additions & 0 deletions apps/sim/lib/api/contracts/v2/openapi/workflow-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const workflowSourceOpenApiRoutes = [
workflowId: WORKFLOW_SOURCE_EXAMPLE.workflowId,
workspaceId: WORKFLOW_SOURCE_EXAMPLE.workspaceId,
dry: false,
dryRun: false,
applied: false,
diff: {
noop: true,
Expand Down Expand Up @@ -180,6 +181,7 @@ export const workflowSourceOpenApiRoutes = [
workflowId: WORKFLOW_SOURCE_EXAMPLE.workflowId,
workspaceId: WORKFLOW_SOURCE_EXAMPLE.workspaceId,
dry: true,
dryRun: true,
applied: false,
diff: {
noop: true,
Expand Down Expand Up @@ -243,6 +245,7 @@ export const workflowSourceOpenApiRoutes = [
workflowId: WORKFLOW_SOURCE_EXAMPLE.workflowId,
workspaceId: WORKFLOW_SOURCE_EXAMPLE.workspaceId,
dry: false,
dryRun: false,
applied: true,
diff: {
noop: false,
Expand Down
2 changes: 2 additions & 0 deletions apps/sim/lib/api/contracts/v2/openapi/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ const declaredRoutes = [
warnings: [],
needsRedeployment: true,
dry: false,
dryRun: false,
lint: EMPTY_LINT_EXAMPLE,
},
},
Expand Down Expand Up @@ -507,6 +508,7 @@ const declaredRoutes = [
],
needsRedeployment: true,
dry: false,
dryRun: false,
},
},
]
Expand Down
2 changes: 2 additions & 0 deletions apps/sim/lib/api/contracts/v2/openapi/workspace-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const workspaceSourceOpenApiRoutes = [
data: {
workspaceId: WORKSPACE_ID,
dry: false,
dryRun: false,
plan: [{ kind: 'table', key: 'leads', action: 'noop', id: TABLE_ID }],
applied: [],
needBlobs: [],
Expand Down Expand Up @@ -192,6 +193,7 @@ export const workspaceSourceOpenApiRoutes = [
data: {
workspaceId: WORKSPACE_ID,
dry: true,
dryRun: true,
plan: [{ kind: 'table', key: 'leads', action: 'noop', id: TABLE_ID }],
applied: [],
needBlobs: [],
Expand Down
2 changes: 2 additions & 0 deletions apps/sim/lib/api/contracts/v2/workflow-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe('v2ApplyWorkflowOperationsDataSchema', () => {
mintedBlockIds: {},
lint: EMPTY_LINT,
dry: false,
dryRun: false,
})
expect(result.success, `skip type ${type} is not published`).toBe(true)
}
Expand All @@ -211,6 +212,7 @@ describe('v2ApplyWorkflowOperationsDataSchema', () => {
mintedBlockIds: {},
lint: FULL_LINT,
dry: false,
dryRun: false,
})

expect(result.error?.issues ?? []).toEqual([])
Expand Down
39 changes: 25 additions & 14 deletions apps/sim/lib/api/contracts/v2/workflow-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { SOURCE_ENTRY_FILE, SOURCE_LIMITS } from '@sim/workflow-code/limits'
import { z } from 'zod'
import { booleanQueryFlagSchema, workspaceIdSchema } from '@/lib/api/contracts/primitives'
import { defineRouteContract } from '@/lib/api/contracts/types'
import {
deprecatedDryRunResponseField,
deprecatedRequestName,
foldDeprecatedName,
} from '@/lib/api/contracts/v2/deprecated-names'
import { v2DataResponse, v2FolderPathInputSchema } from '@/lib/api/contracts/v2/shared'
import { v2WorkflowIdParamsSchema } from '@/lib/api/contracts/v2/workflows'
import {
Expand All @@ -22,20 +27,25 @@ export const v2GetWorkflowSourceContract = defineRouteContract({
response: { mode: 'json', schema: v2DataResponse(workflowSourceSchema) },
})

const applyWorkflowSourceQuerySchema = z
.object({
dry: booleanQueryFlagSchema
.optional()
.describe(
'Run every check without writing, auditing, or notifying collaborators. Findings, errors included, come back in `diagnostics` with `applied: false` and a `200`, and the files, lock and fingerprint show what a committed apply would store. A request refused before the source is evaluated (an invalid or oversized body, a locked workflow or folder) gets the same status as without `dry`, and a create under a taken name answers `409`.'
),
allowDelete: booleanQueryFlagSchema
.optional()
.describe(
'Allow the apply to delete blocks and variables the source no longer declares. Without it, an apply that would delete any block is refused with `E_APPLY_BLOCK_DELETE`, and one that would delete any variable with `E_APPLY_VARIABLE_DELETE`, naming them. Ignored when creating a workflow.'
),
})
.strict()
const applyWorkflowSourceQuerySchema = foldDeprecatedName(
z
.object({
dry: booleanQueryFlagSchema
.optional()
.describe(
'Run every check without writing, auditing, or notifying collaborators. Findings, errors included, come back in `diagnostics` with `applied: false` and a `200`, and the files, lock and fingerprint show what a committed apply would store. A request refused before the source is evaluated (an invalid or oversized body, a locked workflow or folder) gets the same status as without `dry`, and a create under a taken name answers `409`.'
),
allowDelete: booleanQueryFlagSchema
.optional()
.describe(
'Allow the apply to delete blocks and variables the source no longer declares. Without it, an apply that would delete any block is refused with `E_APPLY_BLOCK_DELETE`, and one that would delete any variable with `E_APPLY_VARIABLE_DELETE`, naming them. Ignored when creating a workflow.'
),
dryRun: deprecatedRequestName('dry', booleanQueryFlagSchema.optional()),
})
.strict(),
'dryRun',
'dry'
)

/** Longest path a source file may have, in a workflow's source and in a workspace's alike. */
export const SOURCE_PATH_MAX_LENGTH = 1024
Expand Down Expand Up @@ -231,6 +241,7 @@ const applyWorkflowSourceDataSchema = z
workflowId: z.string().describe('Workflow id.'),
workspaceId: z.string().describe('Workspace id.'),
dry: z.boolean().describe('True when nothing was written because `dry` was set.'),
dryRun: deprecatedDryRunResponseField,
applied: z
.boolean()
.describe(
Expand Down
Loading
Loading