-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexecuteStepWithDependencies.ts
More file actions
85 lines (73 loc) · 3.02 KB
/
executeStepWithDependencies.ts
File metadata and controls
85 lines (73 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { IntegrationExecutionConfig } from '@jupiterone/integration-sdk-core';
import { buildStepDependencyGraph } from '@jupiterone/integration-sdk-runtime';
import { StepTestConfig } from './config';
import {
createMockStepExecutionContext,
MockIntegrationStepExecutionContext,
} from './context';
import { getStepExecutionOrder } from './getStepExecutionOrder';
export async function executeStepWithDependencies(params: StepTestConfig) {
const { stepId, invocationConfig, instanceConfig, dependencyStepIds } =
params;
const stepDependencyGraph = buildStepDependencyGraph(
invocationConfig.integrationSteps,
);
const step = stepDependencyGraph.getNodeData(stepId);
if (step.dependencyGraphId && !dependencyStepIds) {
throw new Error(
`stepId: "${stepId}" has dependencyGraphId: "${step.dependencyGraphId}" but dependencyStepIds is undefined.`,
);
}
// Only separate steps by dependencyGraphId if the step has a dependencyGraphId
// and the dependencyGraphOrder is defined
const useDependencyGraphOrder =
!!step.dependencyGraphId && !!invocationConfig.dependencyGraphOrder;
// Get the execution order of the dependencyStepIds
const dependencySteps = useDependencyGraphOrder
? getStepExecutionOrder({
integrationSteps: invocationConfig.integrationSteps,
dependencyStepIds: dependencyStepIds!,
dependencyGraphOrder: invocationConfig.dependencyGraphOrder!,
})
: new Set<string>();
// Add the dependencies of the stepId
stepDependencyGraph
.dependenciesOf(stepId)
.forEach((stepId) => dependencySteps.add(stepId));
const executionConfig = invocationConfig.loadExecutionConfig
? invocationConfig.loadExecutionConfig({ config: instanceConfig })
: {};
const preContext: MockIntegrationStepExecutionContext & {
executionConfig: IntegrationExecutionConfig;
} = {
...createMockStepExecutionContext({ instanceConfig }),
executionConfig,
};
for (const dependencyStepId of dependencySteps) {
const dependencyStep = stepDependencyGraph.getNodeData(dependencyStepId);
await dependencyStep.executionHandler(preContext);
}
const context: MockIntegrationStepExecutionContext & {
executionConfig: IntegrationExecutionConfig;
} = {
...createMockStepExecutionContext({
instanceConfig,
entities: preContext.jobState.collectedEntities,
relationships: preContext.jobState.collectedRelationships,
setData: preContext.jobState.collectedData,
normalizeGraphObjectKey: invocationConfig.normalizeGraphObjectKey,
}),
executionConfig,
};
await step.executionHandler(context);
return {
collectedEntities: context.jobState.collectedEntities,
collectedRelationships: context.jobState.collectedRelationships,
collectedData: context.jobState.collectedData,
encounteredTypes: context.jobState.encounteredTypes,
encounteredEntityKeys: new Set<string>([
...context.jobState.collectedEntities.map((e) => e._key),
...preContext.jobState.collectedEntities.map((e) => e._key),
]),
};
}