Skip to content

Commit

Permalink
Merge pull request #521 from fjogeleit/json-support
Browse files Browse the repository at this point in the history
Add support for JSON files
  • Loading branch information
fjogeleit authored Nov 16, 2022
2 parents 847db40 + aa16cd2 commit 520bafd
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 95 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ jobs:
title: 'Version Updates on all Environments'
changes: |
{
"__tests__/fixtures/values.json": {
"backend.version": "v1.0.1",
"frontend": true
},
"__tests__/fixtures/values.dev.yaml": {
"backend.version": "v1.0.1",
"frontend": true
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# YAML Update Action

Update a single value in an existing YAML File. Push this updated YAML to an existing branch or create a new branch. Open a PullRequest to a configurable targetBranch. It is also posible to change the file locally without commiting the change.
Update valuew in an existing YAML or JSON File. Push this updated File to an existing branch or create a new branch. Open a PullRequest to a configurable targetBranch. It is also posible to change the file locally without commiting the change.


## Use Cases

### Change a local YAML file without commiting the change

By default the actual file in your workspace did not change. This Action creates an in memory copy of your YAML file and sends it to GitHub via the REST API. To achieve an actual update of your local YAML file within your workflow use the following configuration:
With the latest release, the content of your actual file will be updated by default. So, you just need to skip the commit of your change.

```yaml
name: 'workflow'
Expand All @@ -28,7 +28,6 @@ jobs:
propertyPath: 'file.version'
value: v1.0.1
commitChange: false
updateFile: true
```
### Update Helm Chart after a new Docker Image was build
Expand Down Expand Up @@ -90,8 +89,9 @@ jobs:
|value | New value for the related PropertyPath | _required_ Field |
|changes | Configure changes on multiple values and/or multiple files. Expects all changes as JSON, supported formats are `{"filepath":{"propertyPath":"value"}}` and `{"propertyPath":"value"}`. If you use the second format, it uses the filepath provided from the `valueFile` intput. ||
|labels | Comma separated list of labels, e.g. "feature, yaml-updates" | 'yaml-updates' |
|updateFile | By default the actual file is not updated, to do so set this property to 'true' | `false` |
|workDir | relative location of the configured `repository` | . | |
|updateFile | **(deprected)** the updated content will be written into the actual file by default | `false` |
|workDir | Relative location of the configured `repository` | . | |
|format | Specify the used format parser of your file. WIll be guessed by file extension if not provided and uses YAML as fallback. Supports `YAML` and `JSON` ||
|method | Configures the processing of none existing properties. Possible values: `CreateOrUpdate`, `Update`, `Create` | `CreateOrUpdate` |

#### Methods
Expand Down
55 changes: 49 additions & 6 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as process from 'process'
import * as path from 'path'
import {runTest} from '../src/action'
import {YamlNode} from '../src/types'
import {ContentNode} from '../src/types'
import {EnvOptions} from '../src/options'

afterEach(() => {
process.env['VALUE_FILE'] = ''
process.env['VALUE_PATH'] = ''
process.env['VALUE'] = ''
process.env['METHOD'] = ''
process.env['FORMAT'] = ''
process.env['CHANGES'] = ''
process.env['WORK_DIR'] = '__tests__'
})
Expand All @@ -20,7 +21,7 @@ test('test success', async () => {
process.env['VALUE'] = 'v1.1.0'
process.env['BRANCH'] = 'deployment/v1.1.0'

const [{json, content}] = await runTest<{backend: {version: string}; frontend: YamlNode}>(new EnvOptions())
const [{json, content}] = await runTest<{backend: {version: string}; frontend: ContentNode}>(new EnvOptions())

expect(json.backend.version).toEqual(process.env['VALUE'])
console.info(content)
Expand All @@ -32,7 +33,7 @@ test('test add new property', async () => {
process.env['VALUE_PATH'] = 'frontend.version'
process.env['VALUE'] = 'v1.1.0'

const [{json}] = await runTest<{backend: {version: string}; frontend: YamlNode}>(new EnvOptions())
const [{json}] = await runTest<{backend: {version: string}; frontend: ContentNode}>(new EnvOptions())

expect(json.frontend).toEqual({version: 'v1.1.0'})
})
Expand All @@ -44,7 +45,7 @@ test('test ignore not existing property for method update', async () => {
process.env['VALUE'] = 'v1.1.0'
process.env['METHOD'] = 'update'

const updatedFiles = await runTest<{backend: {version: string}; frontend: YamlNode}>(new EnvOptions())
const updatedFiles = await runTest<{backend: {version: string}; frontend: ContentNode}>(new EnvOptions())

expect(updatedFiles.length).toEqual(0)
})
Expand All @@ -56,7 +57,7 @@ test('test create not existing property for method create', async () => {
process.env['VALUE'] = 'v1.1.0'
process.env['METHOD'] = 'create'

const [{json}] = await runTest<{backend: {version: string}; frontend: YamlNode}>(new EnvOptions())
const [{json}] = await runTest<{backend: {version: string}; frontend: ContentNode}>(new EnvOptions())

expect(json.frontend).toEqual({version: 'v1.1.0'})
})
Expand All @@ -69,7 +70,7 @@ test('test ignore existing property for method create', async () => {
process.env['BRANCH'] = 'deployment/v1.1.0'
process.env['METHOD'] = 'create'

const updatedFiles = await runTest<{backend: {version: string}; frontend: YamlNode}>(new EnvOptions())
const updatedFiles = await runTest<{backend: {version: string}; frontend: ContentNode}>(new EnvOptions())

expect(updatedFiles.length).toEqual(0)
})
Expand Down Expand Up @@ -189,3 +190,45 @@ test('append array node on index', async () => {

expect(json.containers[2]).toEqual({name: 'database', image: 'postgres:alpine'})
})

test('process json', async () => {
process.env['VALUE_FILE'] = 'fixtures/values.json'
process.env['WORK_DIR'] = '__tests__'
process.env['VALUE_PATH'] = 'backend.version'
process.env['VALUE'] = 'v2.0.0'

const [{json, content}] = await runTest<{backend: {version: string}; frontend: ContentNode}>(new EnvOptions())

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

test('use configured format if it can not be guessed', async () => {
process.env['VALUE_FILE'] = 'fixtures/values'
process.env['WORK_DIR'] = '__tests__'
process.env['VALUE_PATH'] = 'backend.version'
process.env['VALUE'] = 'v2.0.0'
process.env['FORMAT'] = 'json'

const [{json, content}] = await runTest<{backend: {version: string}; frontend: ContentNode}>(new EnvOptions())

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

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

const results = await runTest<{backend: {version: string}; fronted: boolean; containers: {name: string; image: string}[]}>(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)

expect(results[1].json.backend.version).toEqual('v1.3.0')
expect(results[1].json.frontend).toEqual(true)
console.info(results[1].content)
})
8 changes: 8 additions & 0 deletions __tests__/fixtures/values
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"backend": { "version": "v1.2.0" },
"frontend": {},
"containers": [
{ "name": "nginx", "image": "nginx:latest" },
{ "name": "node", "image": "node:latest" }
]
}
8 changes: 8 additions & 0 deletions __tests__/fixtures/values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"backend": { "version": "v1.2.0" },
"frontend": {},
"containers": [
{ "name": "nginx", "image": "nginx:latest" },
{ "name": "node", "image": "node:latest" }
]
}
3 changes: 1 addition & 2 deletions __tests__/fixtures/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ containers:
image: nginx:latest
- name: node
image: node:latest
config: {
config:
prod: false,
version: 0
}
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
value:
required: true
description: 'New property value'
format:
required: false
description: 'Supported file formats, possible values are YAML and JSON, will be guessed by file extension if not provided. Falls back to YAML'
method:
required: false
description: 'Configures the processing of none existing properties. Possible values: CreateOrUpdate, Update, Create'
Expand Down
Loading

0 comments on commit 520bafd

Please sign in to comment.