Skip to content

Commit e64c8a6

Browse files
feat: clean empty arrays and objects from the task def file (#52)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent b3ad7ca commit e64c8a6

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

index.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,43 @@ function findAppSpecKey(obj, keyName) {
6464
throw new Error(`AppSpec file must include property '${keyName}'`);
6565
}
6666

67-
function undefinedOrNullReplacer(_, value) {
68-
if (value === null || value === undefined) {
67+
function isEmptyValue(value) {
68+
if (value === null || value === undefined || value === '') {
69+
return true;
70+
}
71+
72+
if (Array.isArray(value) && value.length === 0) {
73+
return true;
74+
}
75+
76+
if (typeof value === 'object' && Object.values(value).length === 0) {
77+
return true;
78+
}
79+
80+
return false;
81+
}
82+
83+
function emptyValueReplacer(_, value) {
84+
if (isEmptyValue(value)) {
85+
return undefined;
86+
}
87+
88+
if (typeof value === 'object') {
89+
for (var childValue of Object.values(value)) {
90+
if (!isEmptyValue(childValue)) {
91+
// the object has at least one non-empty property
92+
return value;
93+
}
94+
}
95+
// the object has no non-empty property
6996
return undefined;
7097
}
7198

7299
return value;
73100
}
74101

75102
function cleanNullKeys(obj) {
76-
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
103+
return JSON.parse(JSON.stringify(obj, emptyValueReplacer));
77104
}
78105

79106
function removeIgnoredAttributes(taskDef) {

index.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,44 @@ describe('Deploy to ECS', () => {
174174
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
175175
});
176176

177+
test('cleans empty arrays out of the task definition contents', async () => {
178+
fs.readFileSync.mockImplementation((pathInput, encoding) => {
179+
if (encoding != 'utf8') {
180+
throw new Error(`Wrong encoding ${encoding}`);
181+
}
182+
183+
return '{ "tags": [], "family": "task-def-family" }';
184+
});
185+
186+
await run();
187+
expect(core.setFailed).toHaveBeenCalledTimes(0);
188+
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
189+
});
190+
191+
test('cleans empty strings and objects out of the task definition contents', async () => {
192+
fs.readFileSync.mockImplementation((pathInput, encoding) => {
193+
if (encoding != 'utf8') {
194+
throw new Error(`Wrong encoding ${encoding}`);
195+
}
196+
197+
return '{ "memory": "", "containerDefinitions": [ { "name": "sample-container", "logConfiguration": {}, "repositoryCredentials": { "credentialsParameter": "" }, "cpu": 0, "essential": false } ], "requiresCompatibilities": [ "EC2" ], "family": "task-def-family" }';
198+
});
199+
200+
await run();
201+
expect(core.setFailed).toHaveBeenCalledTimes(0);
202+
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, {
203+
family: 'task-def-family',
204+
containerDefinitions: [
205+
{
206+
name: 'sample-container',
207+
cpu: 0,
208+
essential: false
209+
}
210+
],
211+
requiresCompatibilities: [ 'EC2' ]
212+
});
213+
});
214+
177215
test('cleans invalid keys out of the task definition contents', async () => {
178216
fs.readFileSync.mockImplementation((pathInput, encoding) => {
179217
if (encoding != 'utf8') {

0 commit comments

Comments
 (0)