Skip to content

Commit dc4ccd2

Browse files
committed
Add get state func and add state id for triage update. add tests
1 parent 7ba248c commit dc4ccd2

File tree

3 files changed

+77
-21
lines changed

3 files changed

+77
-21
lines changed

src/main/wrapper/CxConstants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export enum CxConstants {
3636
SUB_CMD_CREATE = "create",
3737
CMD_TRIAGE = "triage",
3838
SUB_CMD_UPDATE = "update",
39+
SUB_CMD_GET_STATES = "get-states",
3940
CMD_RESULT = "results",
4041
SUB_CMD_BFL = "bfl",
4142
CMD_CODE_BASHING = "codebashing",
@@ -72,6 +73,7 @@ export enum CxConstants {
7273
SIMILARITY_ID = "--similarity-id",
7374
QUERY_ID = "--query-id",
7475
STATE = "--state",
76+
STATE_ID = "--state-id",
7577
COMMENT = "--comment",
7678
SEVERITY = "--severity",
7779
REPORT_FORMAT = "--report-format",

src/main/wrapper/CxWrapper.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,15 @@ export class CxWrapper {
200200
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PREDICATE_TYPE);
201201
}
202202

203-
async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string): Promise<CxCommandOutput> {
204-
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_UPDATE, CxConstants.PROJECT_ID, projectId, CxConstants.SIMILARITY_ID, similarityId, CxConstants.SCAN_TYPES_SUB_CMD, scanType, CxConstants.STATE, state, CxConstants.COMMENT, comment, CxConstants.SEVERITY, severity];
203+
async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string, stateId=""): Promise<CxCommandOutput> {
204+
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_UPDATE, CxConstants.PROJECT_ID, projectId, CxConstants.SIMILARITY_ID, similarityId, CxConstants.SCAN_TYPES_SUB_CMD, scanType, CxConstants.STATE, state, CxConstants.STATE_ID, stateId, CxConstants.COMMENT, comment, CxConstants.SEVERITY, severity];
205+
commands.push(...this.initializeCommands(false));
206+
const exec = new ExecutionService();
207+
return await exec.executeCommands(this.config.pathToExecutable, commands);
208+
}
209+
210+
async triageGetStates(): Promise<CxCommandOutput> {
211+
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_GET_STATES];
205212
commands.push(...this.initializeCommands(false));
206213
const exec = new ExecutionService();
207214
return await exec.executeCommands(this.config.pathToExecutable, commands);

src/tests/PredicateTest.test.ts

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,83 @@ import {CxConstants} from '../main/wrapper/CxConstants';
66

77
describe("Triage cases", () => {
88
const cxScanConfig = new BaseTest();
9-
10-
it('Triage Successful case', async () => {
11-
const auth = new CxWrapper(cxScanConfig);
12-
9+
const auth = new CxWrapper(cxScanConfig);
10+
const getScanAndResult = async (): Promise<{ scan: any, result: CxResult }> => {
1311
const scanList: CxCommandOutput = await auth.scanList("statuses=Completed,limit=100");
14-
let result: CxResult;
15-
let scan, output;
16-
while (!output && scanList && scanList.payload && scanList.payload.length > 0) {
17-
scan = scanList.payload.pop()
18-
console.log("Triage Successful case - ScanId " + scan.id)
19-
output = await auth.getResultsList(scan.id)
20-
if (output.status == "Error in the json file.") {
12+
let scan, output, result;
13+
while (!output && scanList?.payload?.length > 0) {
14+
scan = scanList.payload.pop();
15+
console.log("Triage case - ScanId " + scan.id);
16+
output = await auth.getResultsList(scan.id);
17+
if (output.status === "Error in the json file.") {
2118
output = undefined;
2219
} else {
23-
result = output.payload.find(res => res.type == CxConstants.SAST)
24-
if (!result || !result.similarityId) {
20+
result = output.payload.find(res => res.type === CxConstants.SAST);
21+
if (!result?.similarityId) {
2522
output = undefined;
2623
}
2724
}
2825
}
26+
return { scan, result };
27+
};
2928

29+
const handleTriageShow = async (scan: any, result: CxResult) => {
3030
const cxShow: CxCommandOutput = await auth.triageShow(scan.projectID, result.similarityId, result.type);
31-
3231
expect(cxShow.exitCode).toEqual(0);
32+
}
3333

34-
const cxUpdate: CxCommandOutput = await
35-
auth.triageUpdate(scan.projectID, result.similarityId, result.type, result.state,
36-
"Edited via JavascriptWrapper",
37-
result.severity.toLowerCase() == "high" ? CxConstants.SEVERITY_MEDIUM : CxConstants.SEVERITY_HIGH);
38-
34+
const handleTriageUpdate = async (scan: any, result: CxResult, newState: string, newSeverity: string, newStateId="") => {
35+
const cxUpdate: CxCommandOutput = await auth.triageUpdate(
36+
scan.projectID, result.similarityId, result.type, newState,
37+
"Edited via JavascriptWrapper",
38+
newSeverity, newStateId
39+
);
3940
expect(cxUpdate.exitCode).toEqual(0);
41+
};
42+
43+
it('Triage Successful case', async () => {
44+
const { scan, result } = await getScanAndResult();
45+
await handleTriageShow(scan, result);
46+
await handleTriageUpdate(scan, result, result.state, result.severity.toLowerCase() === "high" ? CxConstants.SEVERITY_MEDIUM : CxConstants.SEVERITY_HIGH);
47+
});
48+
49+
it('Triage with custom state Successful case', async () => {
50+
const { scan, result } = await getScanAndResult();
51+
52+
const cxCommandOutput: CxCommandOutput = await auth.triageGetStates();
53+
console.log("Json object from states successful case: " + JSON.stringify(cxCommandOutput));
54+
expect(cxCommandOutput.payload.length).toBeGreaterThan(1);
55+
expect(cxCommandOutput.exitCode).toBe(0);
56+
57+
let customState = cxCommandOutput.payload[0].name
58+
59+
if (result.state == customState) {
60+
if (cxCommandOutput.payload.length > 1) {
61+
customState = cxCommandOutput.payload[1].name
62+
} else {
63+
await handleTriageUpdate(scan, result, CxConstants.STATE_CONFIRMED, CxConstants.SEVERITY_MEDIUM);
64+
}
65+
}
66+
await handleTriageUpdate(scan, result, customState, CxConstants.SEVERITY_MEDIUM);
67+
68+
}); it('Triage with custom state id Successful case', async () => {
69+
const { scan, result } = await getScanAndResult();
70+
71+
const cxCommandOutput: CxCommandOutput = await auth.triageGetStates();
72+
console.log("Json object from states successful case: " + JSON.stringify(cxCommandOutput));
73+
expect(cxCommandOutput.payload.length).toBeGreaterThan(1);
74+
expect(cxCommandOutput.exitCode).toBe(0);
75+
const allStates = cxCommandOutput.payload;
76+
let customStateId = allStates[0].id
77+
const customStateName = allStates[0].name
78+
79+
if (result.state == customStateName) {
80+
if (allStates.length > 1) {
81+
customStateId = allStates[1].id
82+
} else {
83+
await handleTriageUpdate(scan, result, CxConstants.STATE_CONFIRMED, CxConstants.SEVERITY_MEDIUM);
84+
}
85+
}
86+
await handleTriageUpdate(scan, result, "", CxConstants.SEVERITY_MEDIUM, customStateId.toString());
4087
});
4188
});

0 commit comments

Comments
 (0)