diff --git a/src/main/wrapper/CxConstants.ts b/src/main/wrapper/CxConstants.ts index 479cb5e4..4f41ea0e 100644 --- a/src/main/wrapper/CxConstants.ts +++ b/src/main/wrapper/CxConstants.ts @@ -36,6 +36,8 @@ export enum CxConstants { SUB_CMD_CREATE = "create", CMD_TRIAGE = "triage", SUB_CMD_UPDATE = "update", + SUB_CMD_GET_STATES = "get-states", + ALL_STATES_FLAG = "--all", CMD_RESULT = "results", SUB_CMD_BFL = "bfl", CMD_CODE_BASHING = "codebashing", @@ -72,6 +74,7 @@ export enum CxConstants { SIMILARITY_ID = "--similarity-id", QUERY_ID = "--query-id", STATE = "--state", + STATE_ID = "--state-id", COMMENT = "--comment", SEVERITY = "--severity", REPORT_FORMAT = "--report-format", diff --git a/src/main/wrapper/CxWrapper.ts b/src/main/wrapper/CxWrapper.ts index 869ce902..ccd61842 100644 --- a/src/main/wrapper/CxWrapper.ts +++ b/src/main/wrapper/CxWrapper.ts @@ -200,8 +200,20 @@ export class CxWrapper { return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.PREDICATE_TYPE); } - async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string): Promise { + async triageUpdate(projectId: string, similarityId: string, scanType: string, state: string, comment: string, severity: string, stateId = ""): Promise { 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]; + if(stateId) { + commands.push(CxConstants.STATE_ID) + commands.push(stateId) + } + commands.push(...this.initializeCommands(false)); + const exec = new ExecutionService(); + return await exec.executeCommands(this.config.pathToExecutable, commands); + } + + async triageGetStates(all: boolean): Promise { + const commands: string[] = [CxConstants.CMD_TRIAGE, CxConstants.SUB_CMD_GET_STATES]; + if (all) commands.push(CxConstants.ALL_STATES_FLAG) commands.push(...this.initializeCommands(false)); const exec = new ExecutionService(); return await exec.executeCommands(this.config.pathToExecutable, commands); diff --git a/src/tests/PredicateTest.test.ts b/src/tests/PredicateTest.test.ts index 68cdf99a..3f8bbad5 100644 --- a/src/tests/PredicateTest.test.ts +++ b/src/tests/PredicateTest.test.ts @@ -6,36 +6,87 @@ import {CxConstants} from '../main/wrapper/CxConstants'; describe("Triage cases", () => { const cxScanConfig = new BaseTest(); - - it('Triage Successful case', async () => { - const auth = new CxWrapper(cxScanConfig); - + const auth = new CxWrapper(cxScanConfig); + const getScanAndResult = async (): Promise<{ scan: any, result: CxResult }> => { const scanList: CxCommandOutput = await auth.scanList("statuses=Completed,limit=100"); - let result: CxResult; - let scan, output; - while (!output && scanList && scanList.payload && scanList.payload.length > 0) { - scan = scanList.payload.pop() - console.log("Triage Successful case - ScanId " + scan.id) - output = await auth.getResultsList(scan.id) - if (output.status == "Error in the json file.") { + let scan, output, result; + while (!output && scanList?.payload?.length > 0) { + scan = scanList.payload.pop(); + console.log("Triage case - ScanId " + scan.id); + output = await auth.getResultsList(scan.id); + if (output.status === "Error in the json file.") { output = undefined; } else { - result = output.payload.find(res => res.type == CxConstants.SAST) - if (!result || !result.similarityId) { + result = output.payload.find(res => res.type === CxConstants.SAST); + if (!result?.similarityId) { output = undefined; } } } + return { scan, result }; + }; + const handleTriageShow = async (scan: any, result: CxResult) => { const cxShow: CxCommandOutput = await auth.triageShow(scan.projectID, result.similarityId, result.type); - expect(cxShow.exitCode).toEqual(0); + } - const cxUpdate: CxCommandOutput = await - auth.triageUpdate(scan.projectID, result.similarityId, result.type, result.state, - "Edited via JavascriptWrapper", - result.severity.toLowerCase() == "high" ? CxConstants.SEVERITY_MEDIUM : CxConstants.SEVERITY_HIGH); - + const handleTriageUpdate = async (scan: any, result: CxResult, newState: string, newSeverity: string, newStateId = "") => { + const cxUpdate: CxCommandOutput = await auth.triageUpdate( + scan.projectID, result.similarityId, result.type, newState, + "Edited via JavascriptWrapper", + newSeverity, newStateId + ); expect(cxUpdate.exitCode).toEqual(0); + }; + const handlegetStates = async () => { + const cxCommandOutput: CxCommandOutput = await auth.triageGetStates(false); + console.log("Json object from states successful case: " + JSON.stringify(cxCommandOutput)); + expect(cxCommandOutput.payload.length).toBeGreaterThanOrEqual(1); + expect(cxCommandOutput.exitCode).toBe(0); + return cxCommandOutput + }; + + it('Triage Successful case', async () => { + const { scan, result } = await getScanAndResult(); + await handleTriageShow(scan, result); + await handleTriageUpdate(scan, result, result.state, result.severity.toLowerCase() === "high" ? CxConstants.SEVERITY_MEDIUM : CxConstants.SEVERITY_HIGH); + }); + + it.skip('Triage with custom state Successful case', async () => { + const { scan, result } = await getScanAndResult(); + + const cxCommandOutput = await handlegetStates(); + + let customState = cxCommandOutput.payload[0].name + + if (result.state == customState) { + if (cxCommandOutput.payload.length > 1) { + customState = cxCommandOutput.payload[1].name + } else { + await handleTriageUpdate(scan, result, CxConstants.STATE_CONFIRMED, CxConstants.SEVERITY_MEDIUM); + } + } + await handleTriageUpdate(scan, result, customState, CxConstants.SEVERITY_MEDIUM); + + }); + + it.skip('Triage with custom state id Successful case', async () => { + const { scan, result } = await getScanAndResult(); + + const cxCommandOutput = await handlegetStates(); + + const allStates = cxCommandOutput.payload; + let customStateId = allStates[0].id + const customStateName = allStates[0].name + + if (result.state == customStateName) { + if (allStates.length > 1) { + customStateId = allStates[1].id + } else { + await handleTriageUpdate(scan, result, CxConstants.STATE_CONFIRMED, CxConstants.SEVERITY_MEDIUM); + } + } + await handleTriageUpdate(scan, result, "", CxConstants.SEVERITY_MEDIUM, customStateId.toString()); }); }); \ No newline at end of file