From 2e51f3419c9bc192fb6d2646880ad019ab94ae0d Mon Sep 17 00:00:00 2001 From: Guilherme Caponetto <638737+caponetto@users.noreply.github.com> Date: Wed, 3 Apr 2024 14:42:34 -0300 Subject: [PATCH] fix(orchestrator): only inputs inherited from the assessment workflow should be disabled (#1436) --- .../service/DataInputSchemaService.test.ts | 84 ++++++++++++++++++- .../src/service/DataInputSchemaService.ts | 51 ++++++++--- 2 files changed, 124 insertions(+), 11 deletions(-) diff --git a/plugins/orchestrator-backend/src/service/DataInputSchemaService.test.ts b/plugins/orchestrator-backend/src/service/DataInputSchemaService.test.ts index e4aff97505..a9dbacccce 100644 --- a/plugins/orchestrator-backend/src/service/DataInputSchemaService.test.ts +++ b/plugins/orchestrator-backend/src/service/DataInputSchemaService.test.ts @@ -46,7 +46,7 @@ describe('workflow input schema response', () => { expect(response.schemaSteps[0].title).toEqual('Data Input Schema'); }); - it('composed schema also wihtout refs should return multiple steps', () => { + it('composed schema also without refs should return multiple steps', () => { const response = service.getWorkflowInputSchemaResponse( mockComposedGreetingWorkflowData.workflowDefinition, mockComposedGreetingWorkflowData.schema, @@ -117,4 +117,86 @@ describe('workflow input schema response', () => { }); expect(response.schemaSteps[0].readonlyKeys).toEqual(['language', 'name']); }); + + it('using assessment variables on composed schema should return read only keys', () => { + const newComponentValues = { + orgName: 'org.example', + repoName: 'example', + description: 'example description', + }; + const response = service.getWorkflowInputSchemaResponse( + mockSpringBootWorkflowData.workflowDefinition, + mockSpringBootWorkflowData.schema, + undefined, + { + workflowdata: { + newComponent: newComponentValues, + }, + }, + ); + expect(response.isComposedSchema).toEqual(true); + expect(response.schemaSteps[0].data).toEqual(newComponentValues); + expect(response.schemaSteps[0].readonlyKeys).toEqual( + Object.keys(newComponentValues), + ); + }); + + it('using initial workflow and assessment variables should return read only keys', () => { + const response = service.getWorkflowInputSchemaResponse( + mockGreetingWorkflowData.workflowDefinition, + mockGreetingWorkflowData.schema, + { + workflowdata: { + name: 'John Doe', + language: 'Spanish', + }, + }, + { + workflowdata: { + name: 'John Doe', + waitOrError: 'Error', + }, + }, + ); + expect(response.isComposedSchema).toEqual(false); + expect(response.schemaSteps[0].data).toEqual({ + language: 'Spanish', + name: 'John Doe', + }); + expect(response.schemaSteps[0].readonlyKeys).toEqual(['name']); + }); + + it('using initial workflow and assessment variables on composed schema should return read only keys', () => { + const newComponentValues = { + orgName: 'org.example', + repoName: 'example', + description: 'example description', + }; + const javaMetadataValues = { + groupId: 'org.example', + artifactId: 'example', + version: '1.0.0', + }; + const response = service.getWorkflowInputSchemaResponse( + mockSpringBootWorkflowData.workflowDefinition, + mockSpringBootWorkflowData.schema, + { + workflowdata: { + newComponent: newComponentValues, + javaMetadata: javaMetadataValues, + }, + }, + { + workflowdata: { + newComponent: newComponentValues, + }, + }, + ); + expect(response.isComposedSchema).toEqual(true); + expect(response.schemaSteps[0].data).toEqual(newComponentValues); + expect(response.schemaSteps[1].data).toEqual(javaMetadataValues); + expect(response.schemaSteps[0].readonlyKeys).toEqual( + Object.keys(newComponentValues), + ); + }); }); diff --git a/plugins/orchestrator-backend/src/service/DataInputSchemaService.ts b/plugins/orchestrator-backend/src/service/DataInputSchemaService.ts index 4bd6a6d510..c0ead3c07c 100644 --- a/plugins/orchestrator-backend/src/service/DataInputSchemaService.ts +++ b/plugins/orchestrator-backend/src/service/DataInputSchemaService.ts @@ -70,17 +70,25 @@ export class DataInputSchemaService { private getInputSchemaSteps( schema: ComposedSchema, - isAssessment: boolean, workflowData?: JsonObject, + assessmentWorkflowData?: JsonObject, ): WorkflowInputSchemaStep[] { return Object.entries(schema.properties).map(([key, curSchema]) => { - const data = (workflowData?.[key] as JsonObject) ?? {}; + const data = this.extractInnerProperty(workflowData, key); + const assessmentData = this.extractInnerProperty( + assessmentWorkflowData, + key, + ); + const assessmentInitialState = this.extractInitialStateFromWorkflowData( + assessmentData, + curSchema.properties, + ); return { title: curSchema.title || key, key, schema: curSchema, data, - readonlyKeys: isAssessment ? Object.keys(data) : [], + readonlyKeys: this.extractObjectKeys(assessmentInitialState), }; }); } @@ -91,11 +99,11 @@ export class DataInputSchemaService { instanceVariables?: ProcessInstanceVariables, assessmentInstanceVariables?: ProcessInstanceVariables, ): WorkflowInputSchemaResponse { - const variables = instanceVariables ?? assessmentInstanceVariables; - const isAssessment = !!assessmentInstanceVariables; - const workflowData = variables - ? (variables[WORKFLOW_DATA_KEY] as JsonObject) - : undefined; + const instanceWorkflowData = this.extractWorkflowData(instanceVariables); + const assessmentInstanceWorkflowData = this.extractWorkflowData( + assessmentInstanceVariables, + ); + const workflowData = instanceWorkflowData ?? assessmentInstanceWorkflowData; const res: WorkflowInputSchemaResponse = { definition, @@ -115,8 +123,8 @@ export class DataInputSchemaService { if (isComposedSchema(resolvedSchema)) { res.schemaSteps = this.getInputSchemaSteps( resolvedSchema, - isAssessment, workflowData, + assessmentInstanceWorkflowData, ); res.isComposedSchema = true; } else { @@ -126,13 +134,19 @@ export class DataInputSchemaService { resolvedSchema.properties, ) : {}; + const assessmentData = + assessmentInstanceWorkflowData && + this.extractInitialStateFromWorkflowData( + assessmentInstanceWorkflowData, + resolvedSchema.properties, + ); res.schemaSteps = [ { schema: resolvedSchema, title: resolvedSchema.title ?? SINGLE_SCHEMA_TITLE, key: SINGLE_SCHEMA_KEY, data, - readonlyKeys: isAssessment ? Object.keys(data) : [], + readonlyKeys: this.extractObjectKeys(assessmentData), }, ]; } @@ -144,4 +158,21 @@ export class DataInputSchemaService { } return res; } + + private extractWorkflowData( + variables?: ProcessInstanceVariables, + ): JsonObject | undefined { + return variables ? (variables[WORKFLOW_DATA_KEY] as JsonObject) : undefined; + } + + private extractObjectKeys(obj: JsonObject | undefined): string[] { + return obj ? Object.keys(obj) : []; + } + + private extractInnerProperty( + obj: JsonObject | undefined, + key: string, + ): JsonObject { + return (obj?.[key] as JsonObject) ?? {}; + } }