Skip to content

Commit 451fb54

Browse files
authored
feat: allow multifile yaml (#570)
* feat: allow multifile yaml * test: don't require leading $ * chore: run prettier * fix: package * fix: output multifile if input was multifile * fix: run `npm run package` * chore: run prettier * chore: run npm package * doc: add example in README.md * fix: apply linter suggestion
1 parent ada2af9 commit 451fb54

File tree

6 files changed

+1502
-1310
lines changed

6 files changed

+1502
-1310
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,36 @@ jobs:
201201
}
202202
```
203203

204-
### Advaned Example with an separate target repository
204+
### Change a YAML Multifile
205+
206+
Yaml supports multiple documents in a single file separated by `---`.
207+
To update such a file, start the property path with the index of the document to be changed.
208+
209+
```yaml
210+
jobs:
211+
test-multifile-changes:
212+
runs-on: ubuntu-latest
213+
steps:
214+
- uses: actions/checkout@v3
215+
- uses: fjogeleit/yaml-update-action@main
216+
with:
217+
valueFile: 'deployment/helm/values.yaml'
218+
branch: deployment/v1.0.1
219+
targetBranch: main
220+
createPR: 'true'
221+
description: Test GitHub Action
222+
message: 'Update Images'
223+
title: 'Version Updates '
224+
changes: |
225+
{
226+
"__tests__/fixtures/multivalue.yaml": {
227+
"[0].backend.version": "v1.1.0",
228+
"[1].containers[1].image": "node:alpine"
229+
}
230+
}
231+
```
232+
233+
### Advanced Example with an separate target repository
205234

206235
```yaml
207236
env:

__tests__/action.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,50 @@ test('multiple changes in one file', async () => {
180180
console.info(content)
181181
})
182182

183+
test('change in multi file', async () => {
184+
process.env['VALUE_FILE'] = 'fixtures/multivalue.yaml'
185+
process.env['WORK_DIR'] = '__tests__'
186+
process.env['VALUE_PATH'] = '[0].backend.version'
187+
process.env['VALUE'] = 'v1.1.0'
188+
process.env['BRANCH'] = 'deployment/v1.1.0'
189+
process.env['QUOTING_TYPE'] = '"'
190+
191+
type Result = {
192+
backend: { version: string }
193+
frontend: ContentNode
194+
}
195+
196+
const [{ json, content }] = await runTest<Result>(new EnvOptions())
197+
198+
const jsonArray = json as unknown as Result[]
199+
200+
expect(jsonArray[0].backend.version).toEqual(process.env['VALUE'])
201+
expect(jsonArray[1].backend.version).not.toEqual(process.env['VALUE'])
202+
console.info(content)
203+
console.info(json)
204+
})
205+
206+
test('multiple changes in a multifile', async () => {
207+
process.env['VALUE_FILE'] = 'fixtures/multivalue.yaml'
208+
process.env['CHANGES'] =
209+
'{"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"}'
210+
211+
type Result = {
212+
backend: { version: string }
213+
containers: { name: string; image: string }[]
214+
}
215+
216+
const [{ json, content }] = await runTest<Result>(new EnvOptions())
217+
218+
const jsonArray = json as unknown as Result[]
219+
220+
expect(jsonArray[0].backend.version).toEqual('v1.1.0')
221+
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
222+
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
223+
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
224+
console.info(content)
225+
})
226+
183227
test('multiple changes in multiple files', async () => {
184228
process.env['CHANGES'] = `{
185229
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
@@ -201,6 +245,37 @@ test('multiple changes in multiple files', async () => {
201245
console.info(results[1].content)
202246
})
203247

248+
test('multiple changes in multiple files, including multifiles', async () => {
249+
process.env['CHANGES'] = `{
250+
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
251+
"fixtures/multivalue.yaml": {"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"},
252+
"fixtures/values.prod.yaml": {"backend.version": "v1.3.0", "frontend": true}
253+
}`
254+
255+
type Result = {
256+
backend: { version: string }
257+
fronted: boolean
258+
containers: { name: string; image: string }[]
259+
}
260+
261+
const results = await runTest<Result>(new EnvOptions())
262+
263+
expect(results[0].json.backend.version).toEqual('v1.1.0')
264+
expect(results[0].json.containers[1].image).toEqual('node:alpine')
265+
console.info(results[0].content)
266+
267+
const jsonArray = results[1].json as unknown as Result[]
268+
269+
expect(jsonArray[0].backend.version).toEqual('v1.1.0')
270+
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
271+
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
272+
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
273+
console.info(results[1].content)
274+
275+
expect(results[2].json.backend.version).toEqual('v1.3.0')
276+
expect(results[2].json.frontend).toEqual(true)
277+
console.info(results[2].content)
278+
})
204279
test('append array node', async () => {
205280
process.env['CHANGES'] = `{
206281
"fixtures/values.yaml": {

__tests__/fixtures/multivalue.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
backend:
2+
version: v1.2.0
3+
frontend: {}
4+
containers:
5+
- name: nginx
6+
image: nginx:latest
7+
- name: node
8+
image: node:latest
9+
config:
10+
prod: false,
11+
version: 0
12+
boolString: 'true'
13+
14+
---
15+
backend:
16+
version: v1.2.0
17+
frontend: {}
18+
containers:
19+
- name: nginx
20+
image: nginx:latest
21+
- name: node
22+
image: node:latest
23+
config:
24+
prod: false,
25+
version: 0
26+
boolString: 'true'

0 commit comments

Comments
 (0)