Skip to content

Commit e52b2e4

Browse files
fix: remove the results.json KICS wrote into the workspace when json was not requested
1 parent 13091d3 commit e52b2e4

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎src/main.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ function readJSON(filename) {
1212
return parsedJSON;
1313
}
1414

15-
function cleanupOutput(resultsJSONFile, outputFormats) {
15+
async function cleanupOutput(resultsJSONFile, outputFormats) {
1616
if (!outputFormats.toLowerCase().includes('json') || outputFormats === '') {
17-
io.rmRF(resultsJSONFile);
17+
// the entrypoint copied the report into the action dir, KICS wrote the original into the workspace
18+
const copies = new Set([filepath.resolve(resultsJSONFile), filepath.resolve(process.env.GITHUB_WORKSPACE || '', resultsJSONFile)]);
19+
for (const copy of copies) {
20+
await io.rmRF(copy);
21+
}
1822
}
1923
}
2024

@@ -88,7 +92,7 @@ async function main() {
8892
}
8993

9094
setWorkflowStatus(exitCode);
91-
cleanupOutput(outputPath.resultsJSONFile, outputFormats);
95+
await cleanupOutput(outputPath.resultsJSONFile, outputFormats);
9296
} catch (e) {
9397
console.error(e);
9498
}

‎test/e2e/entrypoint.test.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,21 @@ test('whole action: entrypoint, KICS, JavaScript stage and GitHub', async (t) =>
216216
assert.doesNotMatch(run.stdout, /^::(warning|error)/m)
217217
})
218218

219-
await t.test('the workspace is left without a results.json nobody asked for', knownBug('output_formats: sarif still leaves results.json in the workspace; main.js only removes the copy inside /app'), async (t) => {
219+
await t.test('the workspace is left without a results.json nobody asked for', async (t) => {
220220
const run = await runEntrypoint(t, { runMain: true, results: kicsResults(), inputs: { output_formats: 'sarif' } })
221221
assert.equal(fs.existsSync(path.join(run.workspace, 'results.json')), false)
222222
})
223223

224+
await t.test('the workspace is left without a results.json nobody asked for in output_path', async (t) => {
225+
const run = await runEntrypoint(t, { runMain: true, results: kicsResults(), inputs: { output_formats: 'sarif', output_path: 'reports' } })
226+
assert.equal(fs.existsSync(path.join(run.workspace, 'reports', 'results.json')), false)
227+
})
228+
229+
await t.test('the workspace keeps the results.json when json was requested', async (t) => {
230+
const run = await runEntrypoint(t, { runMain: true, results: kicsResults(), inputs: { output_formats: 'json' } })
231+
assert.equal(fs.existsSync(path.join(run.workspace, 'results.json')), true)
232+
})
233+
224234
await t.test('reports from a config file with a custom output-name are still picked up', knownBug('main.js only reads results.json, so a config with output-name makes the scan fail with ENOENT (#96, #106)'), async (t) => {
225235
const run = await runEntrypoint(t, {
226236
runMain: true,

0 commit comments

Comments
 (0)