Skip to content

Commit

Permalink
feat: clean empty arrays and objects from the task def file (#52)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
clareliguori and mergify[bot] authored Apr 2, 2020
1 parent b3ad7ca commit e64c8a6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
33 changes: 30 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,43 @@ function findAppSpecKey(obj, keyName) {
throw new Error(`AppSpec file must include property '${keyName}'`);
}

function undefinedOrNullReplacer(_, value) {
if (value === null || value === undefined) {
function isEmptyValue(value) {
if (value === null || value === undefined || value === '') {
return true;
}

if (Array.isArray(value) && value.length === 0) {
return true;
}

if (typeof value === 'object' && Object.values(value).length === 0) {
return true;
}

return false;
}

function emptyValueReplacer(_, value) {
if (isEmptyValue(value)) {
return undefined;
}

if (typeof value === 'object') {
for (var childValue of Object.values(value)) {
if (!isEmptyValue(childValue)) {
// the object has at least one non-empty property
return value;
}
}
// the object has no non-empty property
return undefined;
}

return value;
}

function cleanNullKeys(obj) {
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
return JSON.parse(JSON.stringify(obj, emptyValueReplacer));
}

function removeIgnoredAttributes(taskDef) {
Expand Down
38 changes: 38 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,44 @@ describe('Deploy to ECS', () => {
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
});

test('cleans empty arrays out of the task definition contents', async () => {
fs.readFileSync.mockImplementation((pathInput, encoding) => {
if (encoding != 'utf8') {
throw new Error(`Wrong encoding ${encoding}`);
}

return '{ "tags": [], "family": "task-def-family" }';
});

await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
});

test('cleans empty strings and objects out of the task definition contents', async () => {
fs.readFileSync.mockImplementation((pathInput, encoding) => {
if (encoding != 'utf8') {
throw new Error(`Wrong encoding ${encoding}`);
}

return '{ "memory": "", "containerDefinitions": [ { "name": "sample-container", "logConfiguration": {}, "repositoryCredentials": { "credentialsParameter": "" }, "cpu": 0, "essential": false } ], "requiresCompatibilities": [ "EC2" ], "family": "task-def-family" }';
});

await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, {
family: 'task-def-family',
containerDefinitions: [
{
name: 'sample-container',
cpu: 0,
essential: false
}
],
requiresCompatibilities: [ 'EC2' ]
});
});

test('cleans invalid keys out of the task definition contents', async () => {
fs.readFileSync.mockImplementation((pathInput, encoding) => {
if (encoding != 'utf8') {
Expand Down

0 comments on commit e64c8a6

Please sign in to comment.