Skip to content

Commit 9f025aa

Browse files
authored
Add tablesDatabaseId and tablesDatabaseToken to block context to be later used by openops-tables (#1647)
Part of OPS-3021.
1 parent 4c8fca3 commit 9f025aa

File tree

8 files changed

+63
-25
lines changed

8 files changed

+63
-25
lines changed

packages/blocks/framework/src/lib/context.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
AppConnectionValue,
3+
EncryptedObject,
34
ExecutionType,
45
FlowRunId,
56
PauseMetadata,
@@ -125,6 +126,8 @@ export type ServerContext = {
125126
apiUrl: string;
126127
publicUrl: string;
127128
token: string;
129+
tablesDatabaseId: number;
130+
tablesDatabaseToken: EncryptedObject;
128131
};
129132
export type BaseActionContext<
130133
ET extends ExecutionType,

packages/engine/src/lib/handler/block-executor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ const executeAction: ActionHandler<BlockAction> = async ({
189189
token: constants.engineToken,
190190
apiUrl: constants.internalApiUrl,
191191
publicUrl: constants.publicUrl,
192+
tablesDatabaseId: constants.tablesDatabaseId,
193+
tablesDatabaseToken: constants.tablesDatabaseToken,
192194
},
193195
propsValue: processedInput,
194196
tags: createTagsManager(hookResponse),

packages/engine/src/lib/handler/context/engine-constants.ts

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
EncryptedObject,
23
ExecuteFlowOperation,
34
ExecutePropsOptions,
45
ExecuteStepOperation,
@@ -38,8 +39,6 @@ export class EngineConstants {
3839
public static readonly BLOCK_SOURCES =
3940
process.env.OPS_BLOCKS_SOURCE ?? 'FILE';
4041

41-
private project: Project | null = null;
42-
4342
public get baseCodeDirectory(): string {
4443
return EngineConstants.BASE_CODE_DIRECTORY;
4544
}
@@ -67,12 +66,19 @@ export class EngineConstants {
6766
public readonly serverHandlerId: string | null,
6867
public readonly testRunActionLimits: TestRunLimitSettings,
6968
public readonly isTestRun: boolean,
69+
public readonly tablesDatabaseId: number,
70+
public readonly tablesDatabaseToken: EncryptedObject,
7071
public readonly resumePayload?: ResumePayload,
7172
) {}
7273

73-
public static fromExecuteFlowInput(
74+
public static async fromExecuteFlowInput(
7475
input: ExecuteFlowOperation,
75-
): EngineConstants {
76+
): Promise<EngineConstants> {
77+
const project = await EngineConstants.fetchProject(
78+
input.internalApiUrl,
79+
input.engineToken,
80+
);
81+
7682
return new EngineConstants(
7783
input.executionCorrelationId,
7884
input.flowVersion.flowId,
@@ -96,15 +102,22 @@ export class EngineConstants {
96102
input.serverHandlerId ?? null,
97103
input.flowVersion.testRunActionLimits,
98104
input.runEnvironment === 'TESTING',
105+
project.tablesDatabaseId,
106+
project.tablesDatabaseToken,
99107
input.executionType === ExecutionType.RESUME
100108
? input.resumePayload
101109
: undefined,
102110
);
103111
}
104112

105-
public static fromExecuteStepInput(
113+
public static async fromExecuteStepInput(
106114
input: ExecuteStepOperation,
107-
): EngineConstants {
115+
): Promise<EngineConstants> {
116+
const project = await EngineConstants.fetchProject(
117+
input.internalApiUrl,
118+
input.engineToken,
119+
);
120+
108121
return new EngineConstants(
109122
null,
110123
input.flowVersion.flowId,
@@ -128,12 +141,19 @@ export class EngineConstants {
128141
null,
129142
input.flowVersion.testRunActionLimits,
130143
true,
144+
project.tablesDatabaseId,
145+
project.tablesDatabaseToken,
131146
);
132147
}
133148

134-
public static fromExecutePropertyInput(
149+
public static async fromExecutePropertyInput(
135150
input: ExecutePropsOptions,
136-
): EngineConstants {
151+
): Promise<EngineConstants> {
152+
const project = await EngineConstants.fetchProject(
153+
input.internalApiUrl,
154+
input.engineToken,
155+
);
156+
137157
return new EngineConstants(
138158
null,
139159
input.flowVersion.flowId,
@@ -157,12 +177,19 @@ export class EngineConstants {
157177
null,
158178
input.flowVersion.testRunActionLimits,
159179
true,
180+
project.tablesDatabaseId,
181+
project.tablesDatabaseToken,
160182
);
161183
}
162184

163-
public static fromExecuteTriggerInput(
185+
public static async fromExecuteTriggerInput(
164186
input: ExecuteTriggerOperation<TriggerHookType>,
165-
): EngineConstants {
187+
): Promise<EngineConstants> {
188+
const project = await EngineConstants.fetchProject(
189+
input.internalApiUrl,
190+
input.engineToken,
191+
);
192+
166193
return new EngineConstants(
167194
null,
168195
input.flowVersion.flowId,
@@ -186,24 +213,26 @@ export class EngineConstants {
186213
null,
187214
input.flowVersion.testRunActionLimits,
188215
input.test,
216+
project.tablesDatabaseId,
217+
project.tablesDatabaseToken,
189218
);
190219
}
191220

192-
private async getProject(): Promise<Project> {
193-
if (this.project) {
194-
return this.project;
195-
}
196-
197-
const getWorkerProjectEndpoint = `${this.internalApiUrl}v1/worker/project`;
221+
private static async fetchProject(
222+
internalApiUrl: string,
223+
engineToken: string,
224+
): Promise<Project> {
225+
const getWorkerProjectEndpoint = `${addTrailingSlashIfMissing(
226+
internalApiUrl,
227+
)}v1/worker/project`;
198228

199229
const response = await fetch(getWorkerProjectEndpoint, {
200230
headers: {
201-
Authorization: `Bearer ${this.engineToken}`,
231+
Authorization: `Bearer ${engineToken}`,
202232
},
203233
});
204234

205-
this.project = (await response.json()) as Project;
206-
return this.project;
235+
return (await response.json()) as Project;
207236
}
208237
}
209238

packages/engine/src/lib/helper/block-helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ export const blockHelper = {
124124
token: params.engineToken,
125125
apiUrl: constants.internalApiUrl,
126126
publicUrl: params.publicUrl,
127+
tablesDatabaseId: constants.tablesDatabaseId,
128+
tablesDatabaseToken: constants.tablesDatabaseToken,
127129
},
128130
project: {
129131
id: params.projectId,

packages/engine/src/lib/operations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const executeFlow = async (
4545
context: FlowExecutorContext,
4646
): Promise<EngineResponse<Pick<FlowRunResponse, 'status' | 'error'>>> => {
4747
try {
48-
const constants = EngineConstants.fromExecuteFlowInput(input);
48+
const constants = await EngineConstants.fromExecuteFlowInput(input);
4949

5050
const response = await flowExecutor.triggerFlowExecutor({
5151
trigger: input.flowVersion.trigger,
@@ -92,7 +92,7 @@ async function executeStep(
9292
engineToken: input.engineToken,
9393
stepTestOutputs: input.stepTestOutputs,
9494
}),
95-
constants: EngineConstants.fromExecuteStepInput(input),
95+
constants: await EngineConstants.fromExecuteStepInput(input),
9696
});
9797

9898
const stepResult = output.steps[step.name];
@@ -203,7 +203,7 @@ export async function execute(
203203
stepTestOutputs: input.stepTestOutputs,
204204
}),
205205
searchValue: input.searchValue,
206-
constants: EngineConstants.fromExecutePropertyInput(input),
206+
constants: await EngineConstants.fromExecutePropertyInput(input),
207207
});
208208

209209
return {
@@ -222,7 +222,7 @@ export async function execute(
222222

223223
const output = await triggerHelper.executeTrigger({
224224
params: input,
225-
constants: EngineConstants.fromExecuteTriggerInput(input),
225+
constants: await EngineConstants.fromExecuteTriggerInput(input),
226226
});
227227

228228
return {

packages/engine/src/lib/resolve-variable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function resolveVariable(
99
input: ResolveVariableOperation,
1010
): Promise<ResolveVariableResponse> {
1111
try {
12-
const constants = EngineConstants.fromExecuteStepInput({
12+
const constants = await EngineConstants.fromExecuteStepInput({
1313
projectId: input.projectId,
1414
engineToken: input.engineToken,
1515
internalApiUrl: input.internalApiUrl,

packages/engine/test/handler/test-helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const generateMockEngineConstants = (params?: Partial<EngineConstants>):
3030
params?.serverHandlerId ?? null,
3131
params?.testRunActionLimits ?? { isEnabled: false, limits: [] },
3232
params?.isTestRun ?? false,
33+
params?.tablesDatabaseId ?? 1,
34+
params?.tablesDatabaseToken ?? { iv: 'test-iv', data: 'test-data' },
3335
params?.resumePayload,
3436
)
3537
}

packages/engine/test/resolve-variable.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('resolveVariable', () => {
6060

6161
beforeEach(() => {
6262
jest.clearAllMocks();
63-
mockEngineConstants.fromExecuteStepInput = jest.fn().mockReturnValue(mockConstants);
63+
mockEngineConstants.fromExecuteStepInput = jest.fn().mockResolvedValue(mockConstants);
6464
mockTestExecutionContext.stateFromFlowVersion = jest.fn().mockResolvedValue(mockExecutionState);
6565
});
6666

0 commit comments

Comments
 (0)