Skip to content

Commit 947615f

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

File tree

6 files changed

+78
-38
lines changed

6 files changed

+78
-38
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

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)