Skip to content

Commit

Permalink
fix: handle non-JSON responses
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Miller <[email protected]>
  • Loading branch information
Djelibeybi committed Jun 20, 2024
1 parent 9106be3 commit b496421
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/sanity-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ permissions:
contents: read

jobs:

test-action:
name: OCI CLI GitHub Action Test
name: Sanity test run-oci-cli-command action
runs-on: ubuntu-latest
env:
OCI_CLI_USER: ${{ secrets.OCI_CLI_USER }}
Expand All @@ -37,3 +36,15 @@ jobs:
- name: Output object storage namespace
id: output-os-ns
run: echo "${{ steps.test-action-get-os-ns.outputs.output }}"

- name: Test non-JSON output
id: test-non-json-output
uses: ./
with:
command: --output=table iam region list

- name: Check non-JSON outputs
id: show-non-json-output
run: |
echo "${{ steps.test-non-json-output.outputs.raw_output }}"
echo "${{ steps.test-non-json-output.outputs.output }}"
23 changes: 21 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oracle-actions/run-oci-cli-command",
"version": "1.2.0",
"version": "1.3.0",
"author": {
"name": "Oracle Cloud Infrastructure",
"email": "[email protected]"
Expand Down
24 changes: 22 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'

/**
* Test if the content of a variable has a valid JSON structure
*/
function isJson(item: string): boolean {
let value = typeof item !== 'string' ? JSON.stringify(item) : item
try {
value = JSON.parse(value)
} catch (e) {
return false
}

return typeof value === 'object' && value !== null
}

/**
* Install the OCI CLI (if ncessary) and then run the command specified by
* the user workflow. By default, the action suppresses/masks the command
Expand Down Expand Up @@ -42,9 +56,15 @@ async function runOciCliCommand(): Promise<void> {
if (silent) core.setSecret(cliCommand)

const cliResult = await exec.getExecOutput(cliCommand, [], { silent: silent })
let stdout = {}

if (cliResult) {
const stdout = cliResult.stdout ? JSON.parse(cliResult.stdout) : {}
if (cliResult.stdout && !isJson(cliResult.stdout)) {
stdout = JSON.parse(JSON.stringify({ result: cliResult.stdout }))
} else {
stdout = JSON.parse(cliResult.stdout)
}

const stderr = cliResult.stderr ? JSON.stringify(cliResult.stderr) : ''

if (cliResult.exitCode == 0) {
Expand All @@ -54,7 +74,7 @@ async function runOciCliCommand(): Promise<void> {
core.setOutput('output', output)

if (Object.keys(stdout).length == 1) {
const raw_output = stdout[0]
const raw_output = Object.keys(stdout)[0]
if (silent && raw_output) core.setSecret(raw_output)
core.setOutput('raw_output', raw_output)
}
Expand Down

0 comments on commit b496421

Please sign in to comment.