Skip to content

Commit

Permalink
Use JMESPath queries
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
joshuanapoli committed Dec 15, 2022
1 parent 5a287a0 commit 83b52b2
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

This action reads a YAML file and outputs selected properties.

The action uses lodash.get to access properties at the provided paths. The `file` input is the only required input. All other inputs are mapped into equally named outputs with the value at the given paths.
The action uses a [JMESPath](https://jmespath.org/) query to access properties at the provided paths. The `file` is the only input required. All other inputs are mapped into equally named outputs with the value at the given paths.

## Inputs

### `file`

**Required** The name of the file to load.

### `name: path`
### `name: query`

Give each path to look-up as a `name: path` input pair.
Give each path to look-up as a `name: query` input pair.

## Outputs

Expand All @@ -29,15 +29,32 @@ provider:
```

A step definition like this:
```yml
uses: CumulusDS/get-yaml-paths-action@v0.1.0
```yaml
uses: CumulusDS/get-yaml-paths-action@v0.2.0
with:
file: file.yml
bar: foo.bar
providerStage: provider.stage
```
sets the `bar` output to `baz` and sets the `providerstage` output (note all lower-case) to `green`.

Object and array outputs are JSON-serialized. For example, given an input file `qux.yml`:
```yaml
qux:
- bar: hello
- bar: world
```

A step definition like this:
```yaml
uses: CumulusDS/get-yaml-paths-action@v0.2.0
with:
file: qux.yml
bars: qux[].bar
```

Sets the `bars` output to `["hello","world"]`.

## CloudFormation

The CloudFormation YAML schema is supported. The input file can contain the CloudFormation template tags:
Expand Down
3 changes: 3 additions & 0 deletions env/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ foo:
bar: baz
provider:
stage: green
qux:
- bar: hello
- bar: world
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
},
"dependencies": {
"@actions/core": "^1.10.0",
"js-yaml": "^3.13.1",
"lodash.get": "^4.4.2"
"jmespath": "^0.16.0",
"js-yaml": "^3.13.1"
},
"devDependencies": {
"@babel/core": "^7.17.8",
Expand Down
8 changes: 5 additions & 3 deletions src/generateEachPath.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// @flow

import get from "lodash.get";
// $FlowFixMe[untyped-import]
import jmespath from "jmespath";

export default function* generateEachPath(
document: {},
paths: { [string]: string }
): Iterable<{ name: string, value: string }> {
for (const name of Object.keys(paths)) {
const path = paths[name];
const value = get(document, path);
const query = paths[name];
const result = jmespath.search(document, query);
const value = typeof result === "object" ? JSON.stringify(result) : result;
yield { name, value };
}
}
11 changes: 10 additions & 1 deletion test/unit/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ describe("main", () => {
const OLD_ENV = process.env;

it("calls setOutput when successful", async () => {
process.env = { INPUT_FILE: "env/test.yml", INPUT_A: "foo.bar", INPUT_B: "provider.stage", NOT_INPUT_C: "c" };
process.env = {
INPUT_FILE: "env/test.yml",
INPUT_A: "foo.bar",
INPUT_B: "provider.stage",
NOT_INPUT_C: "c",
INPUT_D: "qux[].bar",
INPUT_E: "foo"
};
await main();
expect(core.setOutput).toHaveBeenCalledWith("a", "baz");
expect(core.setOutput).toHaveBeenCalledWith("b", "green");
expect(core.setOutput).toHaveBeenCalledWith("d", '["hello","world"]');
expect(core.setOutput).toHaveBeenCalledWith("e", '{"bar":"baz"}');
});

it("calls setFailed when unsuccessful", async () => {
Expand Down
19 changes: 18 additions & 1 deletion test/unit/readYaml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@ import readYaml from "../../src/readYaml";

describe("readYaml", () => {
it("reads a yaml file", () =>
expect(readYaml("env/test.yml")).resolves.toEqual({ foo: { bar: "baz" }, provider: { stage: "green" } }));
expect(readYaml("env/test.yml")).resolves.toMatchInlineSnapshot(`
Object {
"foo": Object {
"bar": "baz",
},
"provider": Object {
"stage": "green",
},
"qux": Array [
Object {
"bar": "hello",
},
Object {
"bar": "world",
},
],
}
`));

it("reads a CloudFormation yaml file", () =>
expect(readYaml("env/cfn.yml")).resolves.toMatchObject({
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4988,6 +4988,11 @@ jest@^26.0.0:
import-local "^3.0.2"
jest-cli "^26.6.3"

jmespath@^0.16.0:
version "0.16.0"
resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.16.0.tgz#b15b0a85dfd4d930d43e69ed605943c802785076"
integrity sha512-9FzQjJ7MATs1tSpnco1K6ayiYE3figslrXA72G2HQ/n76RzvYlofyi5QM+iX4YRs/pu3yzxlVQSST23+dMDknw==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down Expand Up @@ -5216,11 +5221,6 @@ locate-path@^5.0.0:
dependencies:
p-locate "^4.1.0"

lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
Expand Down

0 comments on commit 83b52b2

Please sign in to comment.