Skip to content

Commit ad8ad0e

Browse files
authored
Feature/results integration (#11)
* Added results list and results summary calls and test cases * updated test case * change the additional params exec logic and added test case * changed the executables to rc-21 * changed the branch back to master * removed additional test case
1 parent 8252268 commit ad8ad0e

File tree

8 files changed

+114
-3
lines changed

8 files changed

+114
-3
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@CheckmarxDev/ast-cli-javascript-wrapper",
3-
"version": "0.0.22",
3+
"version": "0.0.23",
44
"description": "AST CLI Javascript wrapper",
55
"main": "dist/CxAuth.js",
66
"typings": "dist/CxAuth.d.ts",

src/main/CxAuth.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,48 @@ export class CxAuth {
135135
return await exec.executeCommands(this.pathToExecutable, this.commands);
136136
}
137137

138+
async getResultsList(scanId: string, formatType: string) {
139+
this.commands = this.initializeCommands(false);
140+
this.commands.push("result");
141+
this.commands.push("list");
142+
if(scanId !== null && scanId !== "") {
143+
this.commands.push("--scan-id")
144+
this.commands.push(scanId)
145+
}
146+
else{
147+
console.log("Scan Id not provided")
148+
}
149+
if(formatType !== null && formatType != '') {
150+
this.commands.push("--format")
151+
this.commands.push(formatType)
152+
}
153+
let exec = new ExecutionService();
154+
return await exec.executeResultsCommands(this.pathToExecutable,this.commands)
155+
}
156+
157+
async getResultsSummary(scanId: string, formatType: string, target:string) {
158+
this.commands = this.initializeCommands(false);
159+
this.commands.push("result");
160+
this.commands.push("summary");
161+
if(scanId !== null && scanId !== "") {
162+
this.commands.push("--scan-id")
163+
this.commands.push(scanId)
164+
}
165+
else{
166+
console.log("Scan Id not provided")
167+
}
168+
if(formatType !== null && formatType != '') {
169+
this.commands.push("--format")
170+
this.commands.push(formatType)
171+
}
172+
if(target !== null && target != '') {
173+
this.commands.push("--target")
174+
this.commands.push(target)
175+
}
176+
let exec = new ExecutionService();
177+
return await exec.executeResultsCommands(this.pathToExecutable,this.commands)
178+
}
179+
138180
async getResults(scanId: string, targetPath: string, resultParam: CxResultType) {
139181
this.commands = this.initializeCommands(false);
140182
this.commands.push("result");

src/main/ExecutionService.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@ function isJsonString(s: string) {
1414
return true;
1515
}
1616

17+
function transformation(commands: string[]):string[] {
18+
const result:string[] = commands.map(transform);
19+
console.log(JSON.stringify(result))
20+
return result;
21+
}
22+
23+
function transform(n:string) {
24+
return n.replace(/["']/g, "").replace("/[, ]/g",",")
25+
}
26+
1727
export class ExecutionService {
1828
executeCommands(pathToExecutable: string, commands: string[]): Promise<CxCommandOutput> {
1929
return new Promise(function (resolve, reject) {
2030
let stderr = '';
2131
let cxCommandOutput = new CxCommandOutput();
32+
commands = transformation(commands)
2233
const cp = spawn(pathToExecutable, commands);
2334
cp.stderr.on('data', function (chunk: string) {
2435
stderr += chunk;
@@ -47,4 +58,24 @@ export class ExecutionService {
4758
});
4859
});
4960
}
61+
executeResultsCommands(pathToExecutable: string, commands: string[]): Promise<string> {
62+
return new Promise(function (resolve, reject) {
63+
let stderr = '';
64+
let results:string = '';
65+
const cp = spawn(pathToExecutable, commands);
66+
cp.stderr.on('data', function (chunk: string) {
67+
stderr += chunk;
68+
});
69+
cp.on('error', reject)
70+
.on('close', function (code: number) {
71+
logger.info("Exit code received from AST-CLI: " + code)
72+
resolve(results)
73+
logger.info(stderr)
74+
});
75+
cp.stdout.on('data', (data: any) => {
76+
logger.info(`${data}`);
77+
results += data;
78+
});
79+
});
80+
}
5081
}

src/main/resources/cx-linux

-12 KB
Binary file not shown.

src/main/resources/cx-mac

100755100644
-11.7 KB
Binary file not shown.

src/main/resources/cx.exe

-13.5 KB
Binary file not shown.

src/tests/CxAuthCall.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {CxScanConfig} from '../main/CxScanConfig';
22
import {CxAuth} from '../main/CxAuth';
33
import {CxParamType} from '../main/CxParamType';
44
import {CxCommandOutput} from "../main/CxCommandOutput";
5+
import * as fs from "fs";
56

67

78
let cxScanConfig = new CxScanConfig();
@@ -81,12 +82,49 @@ describe("ProjectList cases",() => {
8182
});
8283

8384
describe("Results cases",() => {
84-
it('Result List Successful case', async () => {
85+
it('Result Test Successful case', async () => {
8586
const data = await auth.scanList();
8687
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
8788
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
8889
const written = await auth.getResults(sampleId,"test.json",null)
8990
console.log(written)
9091
expect(cxCommandOutput.scanObjectList.length).toBeGreaterThan(0);
9192
});
93+
94+
it('Result List Successful case', async () => {
95+
const data = await auth.scanList();
96+
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
97+
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
98+
const written = await auth.getResultsList(sampleId,"json")
99+
console.log(written)
100+
expect(written.length).toBeGreaterThan(0);
101+
});
102+
103+
it('Result summary html file generation successful case', async () => {
104+
const data = await auth.scanList();
105+
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
106+
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
107+
const written = await auth.getResultsSummary(sampleId,"html","./test.html")
108+
console.log(written)
109+
const file = await fileExists("./test.html");
110+
expect(file).toBe(true);
111+
});
112+
113+
it('Result summary html string successful case', async () => {
114+
const data = await auth.scanList();
115+
const cxCommandOutput: CxCommandOutput =JSON.parse(JSON.stringify(data))
116+
let sampleId = cxCommandOutput.scanObjectList.pop().ID;
117+
const written = await auth.getResultsSummary(sampleId,"html",null)
118+
console.log(written)
119+
expect(written.length).toBeGreaterThan(0);
120+
});
121+
92122
});
123+
124+
const fileExists = (file:any) => {
125+
return new Promise((resolve) => {
126+
fs.access(file, fs.constants.F_OK, (err) => {
127+
err ? resolve(false) : resolve(true)
128+
});
129+
})
130+
}

0 commit comments

Comments
 (0)