Skip to content

dryrun to show rendered query #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/stackql-assert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
30 changes: 22 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -42,15 +42,15 @@ runs:
else
echo "stackql_installed=false" >> $GITHUB_OUTPUT
fi


- name: Setup StackQL
- name: setup stackql
uses: stackql/[email protected]
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:
Expand All @@ -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
Expand All @@ -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'
Expand Down
17 changes: 10 additions & 7 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand All @@ -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
Expand All @@ -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)}
`;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
43 changes: 38 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = [
Expand All @@ -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");
}
}

Expand All @@ -88,4 +120,5 @@ const checkEnvVarValid = (variable) => {
module.exports = {
setupAuth,
getStackqlCommand,
showStackQLQuery,
};