Skip to content

Commit 88f7996

Browse files
committed
fix: handle non-JSON responses
Signed-off-by: Avi Miller <[email protected]>
1 parent 9106be3 commit 88f7996

File tree

7 files changed

+84
-42
lines changed

7 files changed

+84
-42
lines changed

.github/workflows/sanity-test.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ permissions:
1212
contents: read
1313

1414
jobs:
15-
1615
test-action:
17-
name: OCI CLI GitHub Action Test
16+
name: Sanity test run-oci-cli-command action
1817
runs-on: ubuntu-latest
1918
env:
2019
OCI_CLI_USER: ${{ secrets.OCI_CLI_USER }}
@@ -37,3 +36,10 @@ jobs:
3736
- name: Output object storage namespace
3837
id: output-os-ns
3938
run: echo "${{ steps.test-action-get-os-ns.outputs.output }}"
39+
40+
- name: Test non-JSON output
41+
id: test-non-json-output
42+
uses: ./
43+
with:
44+
command: --output=table iam region list
45+
silent: false

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ purposes only:
3434
3535
## Outputs
3636

37-
- `output`: will contain the results of the command in JSON format.
37+
- `output`: will contain the results of the command.
3838
- `raw_output`: if the output of a given query is a single string value, `raw_output` will return the string without
3939
surrounding quotes.
4040

4141
> **Note:** filtering the `output` or `raw_output` by piping it through another tool like `jq` may result in the
42-
> filtered output being visible in the job logs. We recommend using the `query` parameter instead.
42+
> filtered output being visible in the job logs. We recommend using the `query` parameter whenever possible.
43+
44+
If the result of the command is not valid JSON, it will not be visible unless `silent` is set to _false_.
4345

4446
## Sample workflow
4547

@@ -63,14 +65,14 @@ jobs:
6365
OCI_CLI_REGION: ${{ secrets.OCI_CLI_REGION }}
6466
steps:
6567
- name: Retrieve the OCID of a named compartment in tenancy
66-
uses: oracle-actions/run-oci-cli-command@v1.2.0
68+
uses: oracle-actions/run-oci-cli-command@v1.3.0
6769
id: find-compartment-id
6870
with:
6971
command: 'iam compartment list --compartment-id-in-subtree=true'
7072
query: "data[?name=='testing'].id"
7173

7274
- name: Retrieve the display name and shape of the instances in my compartment
73-
uses: oracle-actions/run-oci-cli-command@v1.2.0
75+
uses: oracle-actions/run-oci-cli-command@v1.3.0
7476
id: find-instances
7577
with:
7678
command: 'compute instance list --compartment-id ${{ steps.find-compartment-id.outputs.raw_output }}'

dist/index.js

Lines changed: 33 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@oracle-actions/run-oci-cli-command",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"author": {
55
"name": "Oracle Cloud Infrastructure",
66
"email": "[email protected]"

src/main.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ import * as fs from 'fs'
1111
import * as os from 'os'
1212
import * as path from 'path'
1313

14+
/**
15+
* Test if the content of a variable has a valid JSON structure
16+
*/
17+
function isJson(item: string): boolean {
18+
let value = typeof item !== 'string' ? JSON.stringify(item) : item
19+
try {
20+
value = JSON.parse(value)
21+
} catch (e) {
22+
return false
23+
}
24+
25+
return typeof value === 'object' && value !== null
26+
}
27+
1428
/**
1529
* Install the OCI CLI (if ncessary) and then run the command specified by
1630
* the user workflow. By default, the action suppresses/masks the command
@@ -42,27 +56,30 @@ async function runOciCliCommand(): Promise<void> {
4256
if (silent) core.setSecret(cliCommand)
4357

4458
const cliResult = await exec.getExecOutput(cliCommand, [], { silent: silent })
59+
let stdout = {}
60+
let output = ''
61+
let raw_output = ''
4562

46-
if (cliResult) {
47-
const stdout = cliResult.stdout ? JSON.parse(cliResult.stdout) : {}
48-
const stderr = cliResult.stderr ? JSON.stringify(cliResult.stderr) : ''
49-
50-
if (cliResult.exitCode == 0) {
51-
const output = JSON.stringify(JSON.stringify(stdout))
52-
53-
if (silent && output) core.setSecret(output)
54-
core.setOutput('output', output)
55-
63+
if (cliResult && cliResult.exitCode == 0) {
64+
if (cliResult.stdout && !isJson(cliResult.stdout)) {
65+
output = cliResult.stdout
66+
raw_output = cliResult.stdout
67+
} else {
68+
stdout = JSON.parse(cliResult.stdout)
69+
output = JSON.stringify(JSON.stringify(stdout))
5670
if (Object.keys(stdout).length == 1) {
57-
const raw_output = stdout[0]
58-
if (silent && raw_output) core.setSecret(raw_output)
59-
core.setOutput('raw_output', raw_output)
71+
raw_output = Object.keys(stdout)[0]
6072
}
61-
} else {
62-
core.setFailed(`Failed: ${JSON.stringify(stderr)}`)
6373
}
74+
75+
if (silent && output) core.setSecret(output)
76+
core.setOutput('output', output)
77+
78+
if (silent && raw_output) core.setSecret(raw_output)
79+
core.setOutput('raw_output', raw_output)
6480
} else {
65-
core.setFailed('Failed to execute OCI CLI command.')
81+
const stderr = cliResult.stderr ? JSON.stringify(cliResult.stderr) : ''
82+
core.setFailed(`Failed: ${JSON.stringify(stderr)}`)
6683
}
6784
}
6885

0 commit comments

Comments
 (0)