Skip to content

Commit fee6ac5

Browse files
committed
Fix
1 parent 72eed27 commit fee6ac5

File tree

2 files changed

+97
-85
lines changed

2 files changed

+97
-85
lines changed

apps/sim/lib/copilot/tools/handlers/deployment/manage.test.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import { beforeEach, describe, expect, it, vi } from 'vitest'
6+
import type { ExecutionContext } from '@/lib/copilot/request/types'
67

78
const { ensureWorkflowAccessMock, performRevertToVersionMock } = vi.hoisted(() => ({
89
ensureWorkflowAccessMock: vi.fn(),
@@ -23,17 +24,6 @@ vi.mock('@sim/db', () => ({
2324
workflowMcpTool: {},
2425
}))
2526

26-
vi.mock('drizzle-orm', async () => {
27-
const actual = await vi.importActual<typeof import('drizzle-orm')>('drizzle-orm')
28-
return {
29-
...actual,
30-
and: vi.fn(),
31-
eq: vi.fn(),
32-
inArray: vi.fn(),
33-
isNull: vi.fn(),
34-
}
35-
})
36-
3727
vi.mock('@/lib/audit/log', () => ({
3828
AuditAction: {},
3929
AuditResourceType: {},
@@ -86,7 +76,8 @@ describe('executeRevertToVersion', () => {
8676

8777
const result = await executeRevertToVersion({ workflowId: 'wf-1', version: 7 }, {
8878
userId: 'user-1',
89-
} as any)
79+
workflowId: 'wf-1',
80+
} as ExecutionContext)
9081

9182
expect(ensureWorkflowAccessMock).toHaveBeenCalledWith('wf-1', 'user-1', 'admin')
9283
expect(performRevertToVersionMock).toHaveBeenCalledWith({
@@ -113,7 +104,8 @@ describe('executeRevertToVersion', () => {
113104

114105
const result = await executeRevertToVersion({ workflowId: 'wf-1', version: 7 }, {
115106
userId: 'user-1',
116-
} as any)
107+
workflowId: 'wf-1',
108+
} as ExecutionContext)
117109

118110
expect(result).toEqual({
119111
success: false,

apps/sim/lib/copilot/tools/server/user/set-environment-variables.ts

Lines changed: 92 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,35 @@ import {
99
import type { BaseServerTool, ServerToolContext } from '@/lib/copilot/tools/server/base-tool'
1010
import { upsertPersonalEnvVars, upsertWorkspaceEnvVars } from '@/lib/environment/utils'
1111

12+
type EnvironmentVariableInputValue = string | number | boolean | null | undefined
13+
14+
interface EnvironmentVariableInput {
15+
name: string
16+
value: EnvironmentVariableInputValue
17+
}
18+
1219
interface SetEnvironmentVariablesParams {
13-
variables: Record<string, any> | Array<{ name: string; value: string }>
20+
variables: Record<string, EnvironmentVariableInputValue> | EnvironmentVariableInput[]
1421
scope?: 'personal' | 'workspace'
1522
workflowId?: string
1623
workspaceId?: string
1724
}
1825

26+
interface SetEnvironmentVariablesResult {
27+
message: string
28+
scope: 'personal' | 'workspace'
29+
workspaceId?: string
30+
variableCount: number
31+
variableNames: string[]
32+
addedVariables: string[]
33+
updatedVariables: string[]
34+
workspaceUpdatedVariables: string[]
35+
}
36+
1937
const EnvVarSchema = z.object({ variables: z.record(z.string()) })
2038

2139
function normalizeVariables(
22-
input: Record<string, any> | Array<{ name: string; value: string }>
40+
input: Record<string, EnvironmentVariableInputValue> | EnvironmentVariableInput[]
2341
): Record<string, string> {
2442
if (Array.isArray(input)) {
2543
return input.reduce(
@@ -59,73 +77,75 @@ async function resolveWorkspaceId(
5977
return getDefaultWorkspaceId(userId)
6078
}
6179

62-
export const setEnvironmentVariablesServerTool: BaseServerTool<SetEnvironmentVariablesParams, any> =
63-
{
64-
name: SetEnvironmentVariables.id,
65-
async execute(
66-
params: SetEnvironmentVariablesParams,
67-
context?: ServerToolContext
68-
): Promise<any> {
69-
const logger = createLogger('SetEnvironmentVariablesServerTool')
70-
71-
if (!context?.userId) {
72-
logger.error(
73-
'Unauthorized attempt to set environment variables - no authenticated user context'
74-
)
75-
throw new Error('Authentication required')
76-
}
77-
78-
const authenticatedUserId = context.userId
79-
const { variables } = params || ({} as SetEnvironmentVariablesParams)
80-
const scope = params.scope === 'personal' ? 'personal' : 'workspace'
81-
82-
const normalized = normalizeVariables(variables || {})
83-
const { variables: validatedVariables } = EnvVarSchema.parse({ variables: normalized })
84-
const variableNames = Object.keys(validatedVariables)
85-
const added: string[] = []
86-
const updated: string[] = []
87-
let workspaceUpdated: string[] = []
88-
89-
let resolvedWorkspaceId: string | undefined
90-
if (scope === 'workspace') {
91-
resolvedWorkspaceId = await resolveWorkspaceId(params, context, authenticatedUserId)
92-
workspaceUpdated = await upsertWorkspaceEnvVars(
93-
resolvedWorkspaceId,
94-
validatedVariables,
95-
authenticatedUserId
96-
)
97-
} else {
98-
const result = await upsertPersonalEnvVars(authenticatedUserId, validatedVariables)
99-
added.push(...result.added)
100-
updated.push(...result.updated)
101-
}
102-
103-
const totalProcessed = added.length + updated.length + workspaceUpdated.length
104-
105-
logger.info('Saved environment variables', {
106-
userId: authenticatedUserId,
107-
scope,
108-
addedCount: added.length,
109-
updatedCount: updated.length,
110-
workspaceUpdatedCount: workspaceUpdated.length,
111-
workspaceId: resolvedWorkspaceId,
112-
})
113-
114-
const parts: string[] = []
115-
if (added.length > 0) parts.push(`${added.length} personal secret(s) added`)
116-
if (updated.length > 0) parts.push(`${updated.length} personal secret(s) updated`)
117-
if (workspaceUpdated.length > 0)
118-
parts.push(`${workspaceUpdated.length} workspace secret(s) updated`)
119-
120-
return {
121-
message: `Successfully processed ${totalProcessed} secret(s): ${parts.join(', ')}`,
122-
scope,
123-
workspaceId: resolvedWorkspaceId,
124-
variableCount: variableNames.length,
125-
variableNames,
126-
addedVariables: added,
127-
updatedVariables: updated,
128-
workspaceUpdatedVariables: workspaceUpdated,
129-
}
130-
},
131-
}
80+
export const setEnvironmentVariablesServerTool: BaseServerTool<
81+
SetEnvironmentVariablesParams,
82+
SetEnvironmentVariablesResult
83+
> = {
84+
name: SetEnvironmentVariables.id,
85+
async execute(
86+
params: SetEnvironmentVariablesParams,
87+
context?: ServerToolContext
88+
): Promise<SetEnvironmentVariablesResult> {
89+
const logger = createLogger('SetEnvironmentVariablesServerTool')
90+
91+
if (!context?.userId) {
92+
logger.error(
93+
'Unauthorized attempt to set environment variables - no authenticated user context'
94+
)
95+
throw new Error('Authentication required')
96+
}
97+
98+
const authenticatedUserId = context.userId
99+
const { variables } = params || ({} as SetEnvironmentVariablesParams)
100+
const scope = params.scope === 'personal' ? 'personal' : 'workspace'
101+
102+
const normalized = normalizeVariables(variables || {})
103+
const { variables: validatedVariables } = EnvVarSchema.parse({ variables: normalized })
104+
const variableNames = Object.keys(validatedVariables)
105+
const added: string[] = []
106+
const updated: string[] = []
107+
let workspaceUpdated: string[] = []
108+
109+
let resolvedWorkspaceId: string | undefined
110+
if (scope === 'workspace') {
111+
resolvedWorkspaceId = await resolveWorkspaceId(params, context, authenticatedUserId)
112+
workspaceUpdated = await upsertWorkspaceEnvVars(
113+
resolvedWorkspaceId,
114+
validatedVariables,
115+
authenticatedUserId
116+
)
117+
} else {
118+
const result = await upsertPersonalEnvVars(authenticatedUserId, validatedVariables)
119+
added.push(...result.added)
120+
updated.push(...result.updated)
121+
}
122+
123+
const totalProcessed = added.length + updated.length + workspaceUpdated.length
124+
125+
logger.info('Saved environment variables', {
126+
userId: authenticatedUserId,
127+
scope,
128+
addedCount: added.length,
129+
updatedCount: updated.length,
130+
workspaceUpdatedCount: workspaceUpdated.length,
131+
workspaceId: resolvedWorkspaceId,
132+
})
133+
134+
const parts: string[] = []
135+
if (added.length > 0) parts.push(`${added.length} personal secret(s) added`)
136+
if (updated.length > 0) parts.push(`${updated.length} personal secret(s) updated`)
137+
if (workspaceUpdated.length > 0)
138+
parts.push(`${workspaceUpdated.length} workspace secret(s) updated`)
139+
140+
return {
141+
message: `Successfully processed ${totalProcessed} secret(s): ${parts.join(', ')}`,
142+
scope,
143+
workspaceId: resolvedWorkspaceId,
144+
variableCount: variableNames.length,
145+
variableNames,
146+
addedVariables: added,
147+
updatedVariables: updated,
148+
workspaceUpdatedVariables: workspaceUpdated,
149+
}
150+
},
151+
}

0 commit comments

Comments
 (0)