Skip to content

Commit 75f1a51

Browse files
committed
Allow tags to be yaml-object both key: value pair and {key: name, value: value} object list
1 parent 40be365 commit 75f1a51

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

__tests__/utils.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@ describe('Parse Tags', () => {
3737
const json = parseTags(JSON.stringify([{ Key: 'Test', Value: 'Value' }]))
3838
expect(json).toEqual([{ Key: 'Test', Value: 'Value' }])
3939
})
40+
41+
test('returns valid Array from YAML key-value object format', async () => {
42+
const yaml = `
43+
Key1: Value1
44+
Key2: Value2
45+
`
46+
const result = parseTags(yaml)
47+
expect(result).toEqual([
48+
{ Key: 'Key1', Value: 'Value1' },
49+
{ Key: 'Key2', Value: 'Value2' }
50+
])
51+
})
52+
53+
test('returns valid Array from YAML array format', async () => {
54+
const yaml = `
55+
- Key: keyname1
56+
Value: value1
57+
- Key: keyname2
58+
Value: value2
59+
`
60+
const result = parseTags(yaml)
61+
expect(result).toEqual([
62+
{ Key: 'keyname1', Value: 'value1' },
63+
{ Key: 'keyname2', Value: 'value2' }
64+
])
65+
})
66+
67+
test('returns undefined for invalid YAML', async () => {
68+
const invalidYaml = `
69+
Key1: 'Value1
70+
Key2: Value2
71+
`
72+
const result = parseTags(invalidYaml)
73+
expect(result).toBeUndefined()
74+
})
4075
})
4176

4277
describe('Parse Parameters', () => {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"@actions/core": "^1.10.0",
3737
"@aws-sdk/client-cloudformation": "^3.474.0",
3838
"@smithy/node-http-handler": "3.0.0",
39-
"https-proxy-agent": "^5.0.1"
39+
"https-proxy-agent": "^5.0.1",
40+
"js-yaml": "^4.1.0"
4041
},
4142
"devDependencies": {
4243
"@types/jest": "^29.2.3",

src/utils.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,43 @@ export function isUrl(s: string): boolean {
1616
}
1717

1818
export function parseTags(s: string): Tag[] | undefined {
19-
let json
19+
if (!s || s.trim() === '') {
20+
return undefined;
21+
}
22+
23+
let tags;
24+
25+
// Try to parse as JSON first (backward compatibility)
26+
try {
27+
tags = JSON.parse(s);
28+
return tags;
29+
} catch (_) {
30+
// JSON parsing failed, try to parse as YAML
31+
}
2032

33+
// If JSON parsing fails, try to handle as YAML
2134
try {
22-
json = JSON.parse(s)
23-
} catch (_) {}
35+
const yaml = require('js-yaml');
36+
const parsed = yaml.load(s);
37+
38+
if (!parsed) {
39+
return undefined;
40+
}
41+
42+
// Handle the two YAML structure formats
43+
if (Array.isArray(parsed)) {
44+
// Already in the format [{Key: 'key', Value: 'value'}, ...]
45+
return parsed;
46+
} else if (typeof parsed === 'object') {
47+
// Convert from {Key1: 'Value1', Key2: 'Value2'} format
48+
return Object.entries(parsed).map(([Key, Value]) => ({ Key, Value }));
49+
}
50+
} catch (_) {
51+
// YAML parsing failed
52+
return undefined;
53+
}
2454

25-
return json
55+
return undefined;
2656
}
2757

2858
export function parseARNs(s: string): string[] | undefined {

0 commit comments

Comments
 (0)