Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Converts parameters into a format ready for API use #1070

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/components/pipeline/parameters/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
49 changes: 49 additions & 0 deletions app/components/pipeline/parameters/util.js
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
})
);
});
Expand Down Expand Up @@ -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' }
})
);
});
Expand Down Expand Up @@ -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'
})
);
});
Expand Down
76 changes: 76 additions & 0 deletions tests/unit/components/pipeline/parameters/util-test.js
Original file line number Diff line number Diff line change
@@ -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 }
}
);
});
});