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: Refactor parameter flattening to generalized util #1073

Merged
merged 1 commit into from
Jun 27, 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
2 changes: 1 addition & 1 deletion app/components/pipeline/parameters/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { action } from '@ember/object';
import {
extractJobParameters,
extractEventParameters,
flattenParameters,
getNormalizedParameterGroups
} from 'screwdriver-ui/utils/pipeline/parameters';
import { flattenParameters } from './util';

export default class PipelineParametersComponent extends Component {
@tracked parameters;
Expand Down
49 changes: 0 additions & 49 deletions app/components/pipeline/parameters/util.js

This file was deleted.

50 changes: 50 additions & 0 deletions app/utils/pipeline/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,53 @@ export function getNormalizedParameterGroups(

return normalizedParameterGroups.concat(normalizedJobParameterGroups);
}

/**
* 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;
}
76 changes: 0 additions & 76 deletions tests/unit/components/pipeline/parameters/util-test.js

This file was deleted.

71 changes: 71 additions & 0 deletions tests/unit/utils/pipeline/parameters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { module, test } from 'qunit';
import {
extractEventParameters,
extractJobParameters,
flattenJobParameters,
flattenParameters,
flattenParameterGroup,
normalizeParameters,
getNormalizedParameterGroups
} from 'screwdriver-ui/utils/pipeline/parameters';
Expand Down Expand Up @@ -226,4 +229,72 @@ module('Unit | Utility | Pipeline | parameters', function () {
paramGroupTitle: 'Job: job1'
});
});

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 }
}
);
});
});