diff --git a/cxAstScan/services/CleanUpRunner.ts b/cxAstScan/services/CleanUpRunner.ts index a6fa6ab..096acc9 100644 --- a/cxAstScan/services/CleanUpRunner.ts +++ b/cxAstScan/services/CleanUpRunner.ts @@ -7,62 +7,98 @@ export class CleanUpRunner { cxWrapperFactory= new CxWrapperFactory(); async run() { - console.log("Getting job status"); - const jobStatus = taskLib.getVariable('AGENT_JOBSTATUS'); - console.log("Job status: " + jobStatus); - if (jobStatus !== 'Canceled') { - console.log("Pipeline not cancelled, nothing to do."); - taskLib.setResult(taskLib.TaskResult.Succeeded, ""); - return; - } - - const cxScanConfig = getConfiguration(); - const wrapper = await this.cxWrapperFactory.createWrapper(cxScanConfig); - let data: string; - try { - data = await fs.readFile(getLogFilename(), 'utf8') - } catch (err: any) { - if (err.code === 'ENOENT') { - console.log("Log file not created. Task ended successfully") + console.log("Getting job status"); + const jobStatus = taskLib.getVariable('AGENT_JOBSTATUS'); + console.log("Job status: " + jobStatus); + if (jobStatus !== 'Canceled') { + console.log("Pipeline not cancelled, nothing to do."); taskLib.setResult(taskLib.TaskResult.Succeeded, ""); - } else if (err.code === 'EACCES') { - console.log('No permissions to read log file') - taskLib.setResult(taskLib.TaskResult.Failed, "") - } else { - throw err + return; } - return - } - //Regex to get the scanID ofthe logs - const regexScanId = new RegExp(/"(ID)":"((\\"|[^"])*)"/i); + const cxScanConfig = getConfiguration(); + const wrapper = await this.cxWrapperFactory.createWrapper(cxScanConfig); + let data: string; - const regexArray = regexScanId.exec(data!); + try { + data = await fs.readFile(getLogFilename(), 'utf8') + } catch (err: any) { + if (err.code === 'ENOENT') { + console.log("Log file not created. Task ended successfully") + taskLib.setResult(taskLib.TaskResult.Succeeded, ""); + } else if (err.code === 'EACCES') { + console.log('No permissions to read log file') + taskLib.setResult(taskLib.TaskResult.Failed, "") + } else { + throw err + } + return + } - try { - if (regexArray) { - //m[2] is the scanID - console.log("Canceling scan with ID: " + regexArray[2]) - await wrapper.scanCancel(regexArray[2]); - } else { - console.log("Scan not created. Terminating job.") + //Regex to get the scanID ofthe logs + const regexScanId = new RegExp(/"(ID)":"((\\"|[^"])*)"/i); + + const regexArray = regexScanId.exec(data!); + + try { + if (regexArray) { + //m[2] is the scanID + console.log("Canceling scan with ID: " + regexArray[2]) + await wrapper.scanCancel(regexArray[2]); + } else { + console.log("Scan not created. Terminating job.") + } + } catch (err) { + console.log("Error canceling scan: " + err + " " + Date.now().toString()) + taskLib.setResult(taskLib.TaskResult.Failed, ""); + return } + + taskLib.setResult(taskLib.TaskResult.Succeeded, ""); + } catch (err) { - console.log("Error canceling scan: " + err + " " + Date.now().toString()) - taskLib.setResult(taskLib.TaskResult.Failed, ""); return + } finally { + await this.deleteZipFile() + await this.deleteLogFile() } + } - taskLib.setResult(taskLib.TaskResult.Succeeded, ""); + async deleteZipFile(): Promise { + try { + const logFileName = getLogFilename(); + const data = await fs.readFile(logFileName, 'utf-8'); + const zipFilePath = this.extractZipFilePath(data); + if (zipFilePath) { + // Delete the zip file + await fs.unlink(zipFilePath); + console.log(`Deleted zip file: ${zipFilePath}`); + } else { + console.log('No zip file path found in the log file.'); + } + } catch (error: any) { + if(error.code === 'ENOENT') { + console.log('Zip file already deleted.'); + } + else { + console.error('Error deleting zip file', error); + } + } + } + async deleteLogFile(): Promise { try { - fs.unlink(getLogFilename()) - //file removed + await fs.unlink(getLogFilename()); + console.log('Log file deleted successfully.'); } catch (err) { - console.log("Unable to delete log file.", err) + console.log("Unable to delete log file.", err); } - } + extractZipFilePath(data: string): string | null { + const zipFilePattern = /Temporary zip file path:\s*(.*)$/m; + const match = data.match(zipFilePattern); + return match ? match[1].trim() : null; + } } diff --git a/cxAstScan/test/_suite.ts b/cxAstScan/test/_suite.ts index 47d9253..d1844b4 100644 --- a/cxAstScan/test/_suite.ts +++ b/cxAstScan/test/_suite.ts @@ -72,6 +72,9 @@ describe('Task runner test', function () { assert.strictEqual(tr.stdout.indexOf('Pipeline not cancelled, nothing to do.') >= 0, true, "should display cleanup message: Pipeline not cancelled, nothing to do."); + assert.strictEqual(tr.stdout.indexOf('Deleted zip file') >= 0 || tr.stdout.indexOf('Zip file already deleted.') >= 0, + true, + "should display cleanup message: Deleted zip file or Zip file already deleted."); }); it('should be success cancel scan', async function () { @@ -92,6 +95,9 @@ describe('Task runner test', function () { assert.strictEqual(tr.stdout.indexOf('Canceling scan with ID') >= 0, true, "should display cleanup message: Canceling scan with ID"); + assert.strictEqual(tr.stdout.indexOf('Deleted zip file') >= 0 || tr.stdout.indexOf('Zip file already deleted.') >= 0, + true, + "should display cleanup message: Deleted zip file or Zip file already deleted."); }); it('should be success cancel before scan start', async function () { diff --git a/package-lock.json b/package-lock.json index 7e7715e..67a9957 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,7 @@ "@typescript-eslint/parser": "^5.62.0", "eslint": "^8.57.0", "mocha": "10.7.0", - "typescript": "5.5.4" + "typescript": "^5.6.3" }, "engines": { "node": ">=16" @@ -1345,9 +1345,10 @@ "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==" }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.5", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.5.tgz", + "integrity": "sha512-ZVJrKKYunU38/76t0RMOulHOnUcbU9GbpWKAOZ0mhjr7CX6FVrH+4FrAapSOekrgFQ3f/8gwMEuIft0aKq6Hug==", + "license": "MIT", "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", @@ -2374,12 +2375,13 @@ } }, "node_modules/micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, + "license": "MIT", "dependencies": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" }, "engines": { @@ -3382,10 +3384,11 @@ } }, "node_modules/typescript": { - "version": "5.5.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz", - "integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==", + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", + "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==", "dev": true, + "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/package.json b/package.json index 4484dcf..d690980 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@typescript-eslint/parser": "^5.62.0", "eslint": "^8.57.0", "mocha": "10.7.0", - "typescript": "5.5.4" + "typescript": "^5.6.3" }, "publishConfig": { "registry": "https://npm.pkg.github.com"