From 4240c198a244bc4dc0026e2bc67101f117a75d24 Mon Sep 17 00:00:00 2001 From: Ming Hay Luk Date: Tue, 18 Jun 2024 20:23:31 -0700 Subject: [PATCH] Converts parameters into a format ready for API use --- .../pipeline/parameters/component.js | 5 +- app/components/pipeline/parameters/util.js | 49 ++++++++++++ .../pipeline/parameters/component-test.js | 10 +-- .../pipeline/parameters/util-test.js | 76 +++++++++++++++++++ 4 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 app/components/pipeline/parameters/util.js create mode 100644 tests/unit/components/pipeline/parameters/util-test.js diff --git a/app/components/pipeline/parameters/component.js b/app/components/pipeline/parameters/component.js index 9a9a2f009..438672c70 100644 --- a/app/components/pipeline/parameters/component.js +++ b/app/components/pipeline/parameters/component.js @@ -6,6 +6,7 @@ import { extractEventParameters, getNormalizedParameterGroups } from 'screwdriver-ui/utils/pipeline/parameters'; +import { flattenParameters } from './util'; export default class PipelineParametersComponent extends Component { @tracked parameters; @@ -91,8 +92,6 @@ export default class PipelineParametersComponent extends Component { this.selectedParameters.pipeline[parameter.name] = { value }; } - const { pipeline, job } = this.selectedParameters; - - this.args.onUpdateParameters({ ...pipeline, ...job }); + this.args.onUpdateParameters(flattenParameters(this.selectedParameters)); } } diff --git a/app/components/pipeline/parameters/util.js b/app/components/pipeline/parameters/util.js new file mode 100644 index 000000000..3af9aa1d1 --- /dev/null +++ b/app/components/pipeline/parameters/util.js @@ -0,0 +1,49 @@ +/** + * Flattens value key to the parent object + * @param parameters + * @returns {{}} + */ +export function flattenParameterGroup(parameters) { + const flattened = {}; + + Object.entries(parameters).forEach(([key, value]) => { + flattened[key] = value.value; + }); + + return flattened; +} + +/** + * Flattens the parameter group of a job + * @param parameters + * @returns {{}} + */ +export function flattenJobParameters(parameters) { + const flattened = {}; + + Object.entries(parameters).forEach(([group, groupParameters]) => { + flattened[group] = flattenParameterGroup(groupParameters); + }); + + return flattened; +} + +/** + * Flattens parameters for use in the API request body + * @param parameters + * @returns {{}} + */ +export function flattenParameters(parameters) { + let flattened = {}; + const { pipeline, job } = parameters; + + if (pipeline) { + flattened = flattenParameterGroup(pipeline); + } + + if (job) { + flattened = { ...flattened, ...flattenJobParameters(job) }; + } + + return flattened; +} diff --git a/tests/integration/components/pipeline/parameters/component-test.js b/tests/integration/components/pipeline/parameters/component-test.js index f3bd077cc..bd78fdfb0 100644 --- a/tests/integration/components/pipeline/parameters/component-test.js +++ b/tests/integration/components/pipeline/parameters/component-test.js @@ -185,8 +185,8 @@ module('Integration | Component | pipeline/parameters', function (hooks) { assert.equal(onUpdateParameters.callCount, 1); assert.true( onUpdateParameters.calledWith({ - foo: { value: 'foobar' }, - job1: { p1: { value: 'abc' } } + foo: 'foobar', + job1: { p1: 'abc' } }) ); }); @@ -233,8 +233,8 @@ module('Integration | Component | pipeline/parameters', function (hooks) { assert.equal(onUpdateParameters.callCount, 1); assert.true( onUpdateParameters.calledWith({ - foo: { value: 'foozy' }, - job1: { p1: { value: 'job123abc' } } + foo: 'foozy', + job1: { p1: 'job123abc' } }) ); }); @@ -274,7 +274,7 @@ module('Integration | Component | pipeline/parameters', function (hooks) { assert.equal(onUpdateParameters.callCount, 1); assert.true( onUpdateParameters.calledWith({ - foo: { value: 'bar' } + foo: 'bar' }) ); }); diff --git a/tests/unit/components/pipeline/parameters/util-test.js b/tests/unit/components/pipeline/parameters/util-test.js new file mode 100644 index 000000000..1190e1f01 --- /dev/null +++ b/tests/unit/components/pipeline/parameters/util-test.js @@ -0,0 +1,76 @@ +import { module, test } from 'qunit'; +import { + flattenJobParameters, + flattenParameters, + flattenParameterGroup +} from 'screwdriver-ui/components/pipeline/parameters/util'; + +module('Unit | Component | pipeline/parameters/util', function () { + test('flattenParameterGroup flattens correctly', function (assert) { + assert.deepEqual(flattenParameterGroup({ param: { value: 123 } }), { + param: 123 + }); + + assert.deepEqual( + flattenParameterGroup({ + param1: { value: 123 }, + param2: { value: 'abc' } + }), + { + param1: 123, + param2: 'abc' + } + ); + }); + + test('flattenJobParameters flattens correctly', function (assert) { + assert.deepEqual( + flattenJobParameters({ + job1: { p1: { value: 'abc' }, p2: { value: 'xyz' } }, + job2: { p1: { value: 123 }, p2: { value: 987 } } + }), + { job1: { p1: 'abc', p2: 'xyz' }, job2: { p1: 123, p2: 987 } } + ); + }); + + test('flattenParameters flattens correctly', function (assert) { + assert.deepEqual( + flattenParameters({ + pipeline: { p1: { value: 'pipeline' }, p2: { value: 'pipeline2' } } + }), + { p1: 'pipeline', p2: 'pipeline2' } + ); + + assert.deepEqual( + flattenParameters({ + job: { main: { j1: { value: 123 }, j2: { value: 'abc' } } } + }), + { main: { j1: 123, j2: 'abc' } } + ); + + assert.deepEqual( + flattenParameters({ + pipeline: { + p1: { value: 'pipeline' }, + p2: { value: 'pipeline2' } + }, + job: { + main: { + j1: { value: 123 }, + j2: { value: 'abc' } + }, + secondary: { + j1: { value: 'xyz' }, + j2: { value: 987 } + } + } + }), + { + p1: 'pipeline', + p2: 'pipeline2', + main: { j1: 123, j2: 'abc' }, + secondary: { j1: 'xyz', j2: 987 } + } + ); + }); +});