Skip to content

Commit

Permalink
gh-actions/jq: Use fs by default, improve sanitization, and add yam…
Browse files Browse the repository at this point in the history
…l input (#972)

Signed-off-by: Ryan Northey <[email protected]>
  • Loading branch information
phlax authored Nov 7, 2023
1 parent e9a1ce5 commit e9fcb8a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 79 deletions.
8 changes: 7 additions & 1 deletion gh-actions/jq/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ inputs:
default: false
use-tmp-file:
type: boolean
default: false
default: true
input-format:
type: string
default: json
sanitize-input:
type: boolean
default: true
runs:
using: 'node16'
main: 'dist/index.js'
4 changes: 2 additions & 2 deletions gh-actions/jq/dist/index.js

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions gh-actions/jq/jq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import fs from 'fs'
import os from 'os'
import tmp from 'tmp'
import {exec} from 'child_process'
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import * as yaml from 'js-yaml'

const run = async (): Promise<void> => {
try {
Expand All @@ -16,6 +19,8 @@ const run = async (): Promise<void> => {
const envVar = core.getInput('env_var')
const printResult = core.getBooleanInput('print-result')
const filter = core.getInput('filter')
const sanitize = core.getBooleanInput('sanitize-input')
const inputFormat = core.getInput('input-format')

if (!filter || filter === '') return
core.debug(`input: ${input}`)
Expand All @@ -31,15 +36,22 @@ const run = async (): Promise<void> => {
if (decode) {
decodePipe = '| base64 -d'
}
let sanitizedInput
if (sanitize && inputFormat === 'json') {
sanitizedInput = JSON.stringify(JSON.parse(input), null, 2)
} else if (inputFormat === 'yaml') {
const yamlObject = yaml.load(input)
sanitizedInput = JSON.stringify(yamlObject, null, 2)
} else {
sanitizedInput = input
}

let tmpFile: tmp.FileResult
let shellCommand = `printf '%s' '${input}' ${decodePipe} | jq ${options} '${filter}' ${encodePipe}`
let shellCommand = `printf '%s' '${sanitizedInput}' ${decodePipe} | jq ${options} '${filter}' ${encodePipe}`
if (os.platform() === 'win32' || useTmpFile) {
const script = `#!/bin/bash -e
${shellCommand}
`
tmpFile = tmp.fileSync()
fs.writeFileSync(tmpFile.name, script)
shellCommand = `bash ${tmpFile.name}`
fs.writeFileSync(tmpFile.name, sanitizedInput)
shellCommand = `cat ${tmpFile.name} ${decodePipe} | jq ${options} '${filter}' ${encodePipe}`
}
core.debug(`Running shell command: ${shellCommand}`)
const proc = exec(shellCommand, (error, stdout, stderr) => {
Expand Down
Loading

0 comments on commit e9fcb8a

Please sign in to comment.