Skip to content

Commit

Permalink
feat: allow multifile yaml (#570)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
holzgeist authored Mar 26, 2024
1 parent ada2af9 commit 451fb54
Show file tree
Hide file tree
Showing 6 changed files with 1,502 additions and 1,310 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,36 @@ jobs:
}
```

### Advaned Example with an separate target repository
### Change a YAML Multifile

Yaml supports multiple documents in a single file separated by `---`.
To update such a file, start the property path with the index of the document to be changed.

```yaml
jobs:
test-multifile-changes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: fjogeleit/yaml-update-action@main
with:
valueFile: 'deployment/helm/values.yaml'
branch: deployment/v1.0.1
targetBranch: main
createPR: 'true'
description: Test GitHub Action
message: 'Update Images'
title: 'Version Updates '
changes: |
{
"__tests__/fixtures/multivalue.yaml": {
"[0].backend.version": "v1.1.0",
"[1].containers[1].image": "node:alpine"
}
}
```

### Advanced Example with an separate target repository

```yaml
env:
Expand Down
75 changes: 75 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,50 @@ test('multiple changes in one file', async () => {
console.info(content)
})

test('change in multi file', async () => {
process.env['VALUE_FILE'] = 'fixtures/multivalue.yaml'
process.env['WORK_DIR'] = '__tests__'
process.env['VALUE_PATH'] = '[0].backend.version'
process.env['VALUE'] = 'v1.1.0'
process.env['BRANCH'] = 'deployment/v1.1.0'
process.env['QUOTING_TYPE'] = '"'

type Result = {
backend: { version: string }
frontend: ContentNode
}

const [{ json, content }] = await runTest<Result>(new EnvOptions())

const jsonArray = json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual(process.env['VALUE'])
expect(jsonArray[1].backend.version).not.toEqual(process.env['VALUE'])
console.info(content)
console.info(json)
})

test('multiple changes in a multifile', async () => {
process.env['VALUE_FILE'] = 'fixtures/multivalue.yaml'
process.env['CHANGES'] =
'{"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"}'

type Result = {
backend: { version: string }
containers: { name: string; image: string }[]
}

const [{ json, content }] = await runTest<Result>(new EnvOptions())

const jsonArray = json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual('v1.1.0')
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
console.info(content)
})

test('multiple changes in multiple files', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
Expand All @@ -201,6 +245,37 @@ test('multiple changes in multiple files', async () => {
console.info(results[1].content)
})

test('multiple changes in multiple files, including multifiles', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
"fixtures/multivalue.yaml": {"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"},
"fixtures/values.prod.yaml": {"backend.version": "v1.3.0", "frontend": true}
}`

type Result = {
backend: { version: string }
fronted: boolean
containers: { name: string; image: string }[]
}

const results = await runTest<Result>(new EnvOptions())

expect(results[0].json.backend.version).toEqual('v1.1.0')
expect(results[0].json.containers[1].image).toEqual('node:alpine')
console.info(results[0].content)

const jsonArray = results[1].json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual('v1.1.0')
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
console.info(results[1].content)

expect(results[2].json.backend.version).toEqual('v1.3.0')
expect(results[2].json.frontend).toEqual(true)
console.info(results[2].content)
})
test('append array node', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {
Expand Down
26 changes: 26 additions & 0 deletions __tests__/fixtures/multivalue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
backend:
version: v1.2.0
frontend: {}
containers:
- name: nginx
image: nginx:latest
- name: node
image: node:latest
config:
prod: false,
version: 0
boolString: 'true'

---
backend:
version: v1.2.0
frontend: {}
containers:
- name: nginx
image: nginx:latest
- name: node
image: node:latest
config:
prod: false,
version: 0
boolString: 'true'
Loading

0 comments on commit 451fb54

Please sign in to comment.