Skip to content

Commit 027e3cb

Browse files
Triage with custom state(AST-81677) (#803)
* Add get state func and add state id for triage update. add tests * Skip on custom state tests. * Add --all flag * State id is optional --------- Co-authored-by: galactica <[email protected]>
1 parent 7ba248c commit 027e3cb

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

src/main/wrapper/CxConstants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export enum CxConstants {
3636
SUB_CMD_CREATE = "create",
3737
CMD_TRIAGE = "triage",
3838
SUB_CMD_UPDATE = "update",
39+
SUB_CMD_GET_STATES = "get-states",
40+
ALL_STATES_FLAG = "--all",
3941
CMD_RESULT = "results",
4042
SUB_CMD_BFL = "bfl",
4143
CMD_CODE_BASHING = "codebashing",
@@ -72,6 +74,7 @@ export enum CxConstants {
7274
SIMILARITY_ID = "--similarity-id",
7375
QUERY_ID = "--query-id",
7476
STATE = "--state",
77+
STATE_ID = "--state-id",
7578
COMMENT = "--comment",
7679
SEVERITY = "--severity",
7780
REPORT_FORMAT = "--report-format",

src/main/wrapper/CxWrapper.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,20 @@ 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> {
203+
async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string, stateId = ""): Promise<CxCommandOutput> {
204204
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];
205+
if(stateId) {
206+
commands.push(CxConstants.STATE_ID)
207+
commands.push(stateId)
208+
}
209+
commands.push(...this.initializeCommands(false));
210+
const exec = new ExecutionService();
211+
return await exec.executeCommands(this.config.pathToExecutable, commands);
212+
}
213+
214+
async triageGetStates(all: boolean): Promise<CxCommandOutput> {
215+
const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_GET_STATES];
216+
if (all) commands.push(CxConstants.ALL_STATES_FLAG)
205217
commands.push(...this.initializeCommands(false));
206218
const exec = new ExecutionService();
207219
return await exec.executeCommands(this.config.pathToExecutable, commands);

src/tests/PredicateTest.test.ts

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,87 @@ 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+
const handlegetStates = async () => {
43+
const cxCommandOutput: CxCommandOutput = await auth.triageGetStates(false);
44+
console.log("Json object from states successful case: " + JSON.stringify(cxCommandOutput));
45+
expect(cxCommandOutput.payload.length).toBeGreaterThanOrEqual(1);
46+
expect(cxCommandOutput.exitCode).toBe(0);
47+
return cxCommandOutput
48+
};
49+
50+
it('Triage Successful case', async () => {
51+
const { scan, result } = await getScanAndResult();
52+
await handleTriageShow(scan, result);
53+
await handleTriageUpdate(scan, result, result.state, result.severity.toLowerCase() === "high" ? CxConstants.SEVERITY_MEDIUM : CxConstants.SEVERITY_HIGH);
54+
});
55+
56+
it.skip('Triage with custom state Successful case', async () => {
57+
const { scan, result } = await getScanAndResult();
58+
59+
const cxCommandOutput = await handlegetStates();
60+
61+
let customState = cxCommandOutput.payload[0].name
62+
63+
if (result.state == customState) {
64+
if (cxCommandOutput.payload.length > 1) {
65+
customState = cxCommandOutput.payload[1].name
66+
} else {
67+
await handleTriageUpdate(scan, result, CxConstants.STATE_CONFIRMED, CxConstants.SEVERITY_MEDIUM);
68+
}
69+
}
70+
await handleTriageUpdate(scan, result, customState, CxConstants.SEVERITY_MEDIUM);
71+
72+
});
73+
74+
it.skip('Triage with custom state id Successful case', async () => {
75+
const { scan, result } = await getScanAndResult();
76+
77+
const cxCommandOutput = await handlegetStates();
78+
79+
const allStates = cxCommandOutput.payload;
80+
let customStateId = allStates[0].id
81+
const customStateName = allStates[0].name
82+
83+
if (result.state == customStateName) {
84+
if (allStates.length > 1) {
85+
customStateId = allStates[1].id
86+
} else {
87+
await handleTriageUpdate(scan, result, CxConstants.STATE_CONFIRMED, CxConstants.SEVERITY_MEDIUM);
88+
}
89+
}
90+
await handleTriageUpdate(scan, result, "", CxConstants.SEVERITY_MEDIUM, customStateId.toString());
4091
});
4192
});

0 commit comments

Comments
 (0)