diff --git a/.github/workflows/stackql-assert.yml b/.github/workflows/stackql-assert.yml index e35cc98..0473d19 100644 --- a/.github/workflows/stackql-assert.yml +++ b/.github/workflows/stackql-assert.yml @@ -60,7 +60,7 @@ jobs: # # Example `test_query_file_path` with `expected_rows` supplying `vars` using `jsonnet` config provided using `data_file_path` # - - name: Use test query string and expected results file, with auth object + - name: Use test query file with a jsonnet data file sourcing external vars and an expected row count uses: ./ with: test_query_file_path: './.github/workflows/workflow_scripts/github-example.iql' diff --git a/action.yml b/action.yml index f316e48..f19148c 100644 --- a/action.yml +++ b/action.yml @@ -33,7 +33,7 @@ inputs: runs: using: "composite" steps: - - name: Check StackQL is installed and set output + - name: check if stackql is installed and set output id: check-stackql shell: bash run: | @@ -42,15 +42,15 @@ runs: else echo "stackql_installed=false" >> $GITHUB_OUTPUT fi - - - name: Setup StackQL + - name: setup stackql uses: stackql/setup-stackql@v1.2.0 if: ${{steps.check-stackql.outputs.stackql_installed == 'false'}} with: use_wrapper: true - - name: Setup auth + - name: setup auth + if: (inputs.auth_obj_path != '') || (inputs.auth_str != '') id: setup-auth uses: actions/github-script@v6 with: @@ -77,7 +77,24 @@ runs: DATA_FILE_PATH: ${{inputs.data_file_path}} VARS: ${{inputs.vars}} OUTPUT: 'json' - + + - name: dryrun stackql command + id: dryrun-query + shell: bash + run: | + ${{ env.STACKQL_DRYRUN_COMMAND }} + + - name: show rendered stackql query + uses: actions/github-script@v6 + with: + script: | + const path = require('path'); + const utilsPath = path.join(process.env.GITHUB_ACTION_PATH, 'lib', 'utils.js') + const {showStackQLQuery} = require(utilsPath) + showStackQLQuery(core) + env: + DRYRUN_RESULT: ${{steps.dryrun-query.outputs.stdout}} + - name: execute stackql command id: exec-query shell: bash @@ -97,9 +114,6 @@ runs: EXPECTED_RESULTS_STR: ${{ inputs.expected_results_str }} EXPECTED_RESULTS_FILE_PATH: ${{inputs.expected_results_file_path}} EXPECTED_ROWS: ${{inputs.expected_rows}} - - - branding: icon: 'terminal' diff --git a/lib/assert.js b/lib/assert.js index d1d1825..f498458 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -10,7 +10,7 @@ const parseResult = (resultStr, varName) => { const jsonObj = JSON.parse(jsonStr); // parse the JSON string into an object return jsonObj; } catch (error) { - throw(`Failed to parse ${varName} JSON + throw(`❌ Failed to parse ${varName} JSON \nvalue: ${resultStr} \nerror: ${error}`); } @@ -34,18 +34,21 @@ const checkResult = (expectedResult, expectedRows, actualResult) => { let equality; let message; expectedRows = parseInt(expectedRows); + const successMessage = `✅ StackQL Assert Successful`; + const failureMessage = `❌ StackQL Assert Failed`; + // if only passed expectedRows, check expectedRows // if only passed expected result, only check expected result // if both passed, check both if (expectedRows) { equality = actualResult.length === expectedRows; if(equality) { - message = `============ StackQL Assert Successful (row count) ============ \n + message = `============ ${successMessage} (row count) ============ \n Expected Number of Rows: ${expectedRows} \n Actual Number of Rows: ${actualResult.length} \n `; } else { - message = `============ StackQL Assert Failed (row count) ============ \n + message = `============ ${failureMessage} (row count) ============ \n Expected Number of Rows: ${expectedRows} \n Actual Number of Rows: ${actualResult.length} \n Execution Result: ${JSON.stringify(actualResult)} \n @@ -56,9 +59,9 @@ const checkResult = (expectedResult, expectedRows, actualResult) => { if (expectedResult) { equality = JSON.stringify(expectedResult) === JSON.stringify(actualResult); if(equality) { - message = `============ StackQL Assert Successful (expected results) ============`; + message = `============ ${successMessage} (expected results) ============`; } else { - message = `============ StackQL Assert Failed (expected results) ============ \n + message = `============ ${failureMessage} (expected results) ============ \n Expected: ${JSON.stringify(expectedResult)}\n Actual: ${JSON.stringify(actualResult)} `; @@ -80,7 +83,7 @@ function checkParameters(expectedResultStr, expectedResultFilePath, expectedRows .map(param => param.name) if (missingParams.length === 3) { - const errorMessage = "Cannot find expected result, file path or expected rows"; + const errorMessage = "❌ Cannot find expected result, file path or expected rows"; throw errorMessage; } } @@ -111,7 +114,7 @@ const assertResult = (coreObj) =>{ const actualResult = parseResult(execResultStr); if (!actualResult) { - core.setFailed("No Output from executing query"); + core.setFailed(`❌ No Output from executing query`); } const {equality, message} = checkResult(expectedResult, expectedRows, actualResult); diff --git a/lib/tests/assert.test.js b/lib/tests/assert.test.js index d3d1956..b9841c9 100644 --- a/lib/tests/assert.test.js +++ b/lib/tests/assert.test.js @@ -133,7 +133,7 @@ describe('getExpectedResult', ()=>{ assertResult(coreObj) - expect(coreObj.setFailed).toHaveBeenCalledWith('Cannot find expected result, file path or expected rows') + expect(coreObj.setFailed).toHaveBeenCalledWith('❌ Cannot find expected result, file path or expected rows') }); it('it should setFailed when actual result is not equal to expected result', () => { diff --git a/lib/utils.js b/lib/utils.js index c9c6990..0a6f4b0 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -28,6 +28,27 @@ function setupAuth(core) { core.exportVariable("AUTH", auth); } +async function showStackQLQuery(core) { + try { + let [ + dryRunCommand, + dryRunResult, + ] = [ + process.env.STACKQL_DRYRUN_COMMAND, + process.env.DRYRUN_RESULT, + ]; + + if (!dryRunResult) { + core.setFailed("No Dryrun Output from stackql command"); + } + + core.info(`\n🚀 rendered stackql query:\n${dryRunResult}`); + + } catch (e) { + core.setFailed(e); + } +} + async function getStackqlCommand(core) { const [query, queryFilePath, dataFilePath, vars, auth, output = "json"] = [ @@ -45,32 +66,43 @@ async function getStackqlCommand(core) { } let args = ["exec"]; + let dryRunArgs = ["exec", "-H"]; if (query) { args.push(`"${query}"`); + dryRunArgs.push(`"${query}"`); } else { - args.push("-i", queryFilePath); + args.push("-i", queryFilePath); + dryRunArgs.push("-i", queryFilePath); } - + if (checkEnvVarValid(dataFilePath)) { args.push(`--iqldata='${dataFilePath}'`); + dryRunArgs.push(`--iqldata='${dataFilePath}'`); } - + if (checkEnvVarValid(vars)) { args.push(`--var='${vars}'`); + dryRunArgs.push(`--var='${vars}'`); } if (checkEnvVarValid(auth)) { args.push(`--auth='${auth}'`); + dryRunArgs.push(`--auth='${auth}'`); } args.push(`--output='${output}'`); + dryRunArgs.push(`--output='text'`); + dryRunArgs.push(`--dryrun`); try { - core.exportVariable('STACKQL_COMMAND', `stackql ${args.join(" ")}`) + const stackqlQuery = `stackql ${args.join(" ")}`; + const stackqlDryRunQuery = `stackql ${dryRunArgs.join(" ")}`; + core.exportVariable('STACKQL_COMMAND', stackqlQuery); + core.exportVariable('STACKQL_DRYRUN_COMMAND', stackqlDryRunQuery); } catch (error) { core.error(error); - core.setFailed("Error when executing stackql"); + core.setFailed("Error exporting stackql command"); } } @@ -88,4 +120,5 @@ const checkEnvVarValid = (variable) => { module.exports = { setupAuth, getStackqlCommand, + showStackQLQuery, };