Skip to content

Commit c628cf4

Browse files
Merge pull request #5 from stackql/feat/change-shell-to-js
Feat/change shell to js
2 parents 56d6a1f + d9d7e00 commit c628cf4

File tree

12 files changed

+6295
-44
lines changed

12 files changed

+6295
-44
lines changed

.github/workflows/test.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: 'Build and Test'
2+
on:
3+
pull_request:
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v3
11+
- name: Use Node.js 16
12+
uses: actions/setup-node@v3
13+
with:
14+
node-version: 16.x
15+
- run: npm ci
16+
- run: npm test
17+

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
node_modules

action.yml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,29 @@ runs:
2727
with:
2828
use_wrapper: true
2929

30-
- name: Setup auth
31-
id: setup-auth
30+
- name: Validate Stackql Version
3231
shell: bash
3332
run: |
34-
chmod +x ./action_scripts/set_auth_env.sh
35-
./action_scripts/set_auth_env.sh
33+
stackql --version
34+
35+
- name: Setup auth
36+
id: setup-auth
37+
uses: actions/github-script@v6
38+
with:
39+
script: |
40+
const {setupAuth} = require('./lib/utils.js')
41+
setupAuth(core)
3642
env:
37-
AUTH_FILE: ${{inputs.auth_obj_path}}
43+
AUTH_FILE_PATH: ${{ inputs.auth_obj_path }}
3844
AUTH_STR: ${{inputs.auth_str}}
3945

4046
- name: Execute query
41-
shell: bash
42-
run: |
43-
chmod +x ./action_scripts/execute.sh
44-
./action_scripts/execute.sh
47+
id: exec-query
48+
uses: actions/github-script@v6
49+
with:
50+
script: |
51+
const {executeStackql} = require('./lib/utils.js')
52+
await executeStackql(core)
4553
env:
4654
QUERY_FILE_PATH: ${{ inputs.query_file_path }}
4755
QUERY: ${{inputs.query}}

action_scripts/execute.sh

Lines changed: 0 additions & 14 deletions
This file was deleted.

action_scripts/set_auth_env.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/tests/failed-result.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"name":"stackql-demo-001","status":"RUNNING"}]

lib/tests/success-result.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"name":"stackql-demo-001","status":"TERMINATED"}]

lib/tests/test-auth.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}

lib/tests/utils.test.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
const { setupAuth, executeStackql } = require("../utils");
2+
const childProcess = require("child_process");
3+
4+
jest.mock("child_process", () => ({
5+
exec: jest.fn(),
6+
}));
7+
8+
describe("util", () => {
9+
let core;
10+
let exec;
11+
const expectedAuth =
12+
'{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}';
13+
14+
beforeEach(() => {
15+
core = {
16+
setFailed: jest.fn().mockImplementation((message) => {
17+
console.error(message);
18+
}),
19+
info: jest.fn().mockImplementation((message) => {
20+
console.log(message);
21+
}),
22+
exportVariable: jest.fn(),
23+
error: jest.fn().mockImplementation((message) => {
24+
console.error(message);
25+
}),
26+
};
27+
});
28+
29+
describe("setupAuth", () => {
30+
let AUTH_ENV = {
31+
AUTH_STR: expectedAuth,
32+
AUTH_FILE_PATH: "./lib/tests/test-auth.json",
33+
};
34+
35+
beforeEach(() => {
36+
jest.resetModules();
37+
process.env = { ...AUTH_ENV };
38+
});
39+
40+
afterEach(() => {
41+
process.env = AUTH_ENV;
42+
});
43+
44+
it("should throw error when neither AUTH_STR or AUTH_FILE_PATH is set", () => {
45+
process.env.AUTH_STR = undefined;
46+
process.env.AUTH_FILE_PATH = undefined;
47+
48+
setupAuth(core);
49+
expect(core.setFailed).toBeCalledWith(
50+
"Either AUTH_FILE_PATH or AUTH_STR must be set."
51+
);
52+
});
53+
54+
it("should set AUTH environment variable when AUTH_STR is set", () => {
55+
process.env.AUTH_FILE_PATH = undefined;
56+
57+
setupAuth(core);
58+
59+
expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
60+
});
61+
62+
it("should set AUTH environment variable when AUTH_FILE_PATH is set", () => {
63+
process.env.AUTH_STR = undefined;
64+
65+
setupAuth(core);
66+
67+
expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
68+
});
69+
70+
it("should throw error when AUTH_FILE_PATH is set but file does not exist", () => {
71+
process.env.AUTH_STR = undefined;
72+
process.env.AUTH_FILE_PATH = "./failed-test-auth.json";
73+
74+
setupAuth(core);
75+
expect(core.setFailed).toBeCalledWith(
76+
`Cannot find auth file ${process.env.AUTH_FILE_PATH}`
77+
);
78+
});
79+
});
80+
81+
describe("executeStackql", () => {
82+
const EXECUTE_ENV = {
83+
QUERY: "test",
84+
QUERY_FILE_PATH: "test-query.json",
85+
AUTH: "test-auth",
86+
};
87+
88+
beforeEach(() => {
89+
jest.resetModules();
90+
process.env = { ...EXECUTE_ENV };
91+
});
92+
93+
afterEach(() => {
94+
process.env = EXECUTE_ENV;
95+
jest.clearAllMocks();
96+
});
97+
98+
it("should return error when there is neither query or query file path", async () => {
99+
process.env = { ...EXECUTE_ENV };
100+
process.env.QUERY = undefined;
101+
process.env.QUERY_FILE_PATH = undefined;
102+
103+
await executeStackql(core);
104+
105+
expect(core.setFailed).toBeCalledWith(
106+
"Either query or query_file_path need to be set"
107+
);
108+
});
109+
110+
it("should return error when there is no AUTH", async () => {
111+
process.env = { ...EXECUTE_ENV };
112+
process.env.AUTH = undefined;
113+
114+
await executeStackql(core);
115+
116+
expect(core.setFailed).toHaveBeenCalledWith(
117+
"Cannot find AUTH environment variable when executing stackql"
118+
);
119+
});
120+
121+
it("should execute stackql with query file path", async () => {
122+
process.env = { ...EXECUTE_ENV };
123+
process.env.QUERY = undefined;
124+
125+
await executeStackql(core);
126+
127+
expect(childProcess.exec).toHaveBeenCalledWith(
128+
"stackql exec -i test-query.json --auth=test-auth --output=json"
129+
);
130+
});
131+
132+
it("should execute stackql with query", async () => {
133+
process.env = { ...EXECUTE_ENV };
134+
process.env.QUERY_FILE_PATH = undefined;
135+
136+
await executeStackql(core);
137+
138+
expect(childProcess.exec).toHaveBeenCalledWith(
139+
"stackql exec test --auth=test-auth --output=json"
140+
);
141+
});
142+
});
143+
});

lib/utils.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const { exec } = require("child_process");
2+
3+
function setupAuth(core) {
4+
const fs = require("fs");
5+
let auth;
6+
const fileName = process.env.AUTH_FILE_PATH;
7+
const authStr = process.env.AUTH_STR;
8+
9+
if (!checkEnvVarValid(fileName) && !checkEnvVarValid(authStr)) {
10+
core.setFailed("Either AUTH_FILE_PATH or AUTH_STR must be set.");
11+
return;
12+
}
13+
14+
if (checkEnvVarValid(fileName)) {
15+
try {
16+
// Read the contents of the JSON file into a string
17+
auth = fs.readFileSync(fileName, "utf-8");
18+
} catch (error) {
19+
core.error(error);
20+
core.setFailed(`Cannot find auth file ${fileName}`);
21+
return;
22+
}
23+
}
24+
if (checkEnvVarValid(authStr)) {
25+
auth = authStr
26+
}
27+
28+
core.info("Setting AUTH environment variable...");
29+
core.exportVariable("AUTH", auth);
30+
}
31+
32+
async function executeStackql(core) {
33+
34+
if (!checkEnvVarValid(process.env.AUTH)) {
35+
core.setFailed("Cannot find AUTH environment variable when executing stackql");
36+
return;
37+
}
38+
let [query, queryFilePath, auth, output = "json"] = [
39+
process.env.QUERY,
40+
process.env.QUERY_FILE_PATH,
41+
process.env.AUTH,
42+
process.env.OUTPUT,
43+
];
44+
45+
46+
47+
if (!checkEnvVarValid(query) && !checkEnvVarValid(queryFilePath)) {
48+
core.setFailed("Either query or query_file_path need to be set");
49+
return;
50+
}
51+
52+
let args = [];
53+
if (queryFilePath) {
54+
args = [
55+
"exec",
56+
"-i",
57+
queryFilePath,
58+
"--auth=" + auth,
59+
"--output=" + output,
60+
];
61+
}
62+
if (query) {
63+
args = ["exec", query, `--auth=${auth}`, `--output=${output}`];
64+
}
65+
try {
66+
await exec(`stackql ${args.join(" ")}`);
67+
} catch (error) {
68+
core.error(error);
69+
core.setFailed("Error when executing stackql");
70+
}
71+
}
72+
73+
/**
74+
* Checking if environment variable is not empty or undefined
75+
* @param {*} variable
76+
*/
77+
const checkEnvVarValid = (variable) => {
78+
if (!variable || variable === "" || variable === "undefined") {
79+
return false;
80+
}
81+
return true;
82+
};
83+
84+
module.exports = {
85+
setupAuth,
86+
executeStackql,
87+
};

0 commit comments

Comments
 (0)