Skip to content

Commit 3d37c10

Browse files
fix: apply the KICS verdict first and fail when the results cannot be read
1 parent 370f7ba commit 3d37c10

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

‎src/main.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ async function main() {
6060
const outputFormats = process.env.INPUT_OUTPUT_FORMATS;
6161
const exitCode = process.env.KICS_EXIT_CODE
6262

63+
// the scan verdict comes first so a reporting failure can never hide it
64+
setWorkflowStatus(exitCode);
65+
6366
try {
6467
const octokit = github.getOctokit(githubToken);
6568
let context = {};
@@ -80,7 +83,13 @@ async function main() {
8083
enableComments = enableComments ? enableComments : "false"
8184
enableJobsSummary = enableJobsSummary ? enableJobsSummary : "false"
8285

83-
const parsedResults = readJSON(outputPath.resultsJSONFile);
86+
let parsedResults;
87+
try {
88+
parsedResults = readJSON(outputPath.resultsJSONFile);
89+
} catch (e) {
90+
core.setFailed(`Could not read the KICS results: ${e.message}`);
91+
return;
92+
}
8493
if (enableAnnotations.toLocaleLowerCase() === "true") {
8594
annotator.annotateChangesWithResults(parsedResults);
8695
}
@@ -91,7 +100,6 @@ async function main() {
91100
await commenter.postJobSummary(parsedResults, commentsWithQueries.toLocaleLowerCase() === "true", excludedColumnsForCommentsWithQueries);
92101
}
93102

94-
setWorkflowStatus(exitCode);
95103
await cleanupOutput(outputPath.resultsJSONFile, outputFormats);
96104
} catch (e) {
97105
console.error(e);

‎test/e2e/action.test.js‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ const assert = require('node:assert/strict')
55
const { runAction } = require('../helpers/run-action')
66
const { startFakeGitHub, humanComment } = require('../helpers/fake-github')
77
const { kicsResults, query, finding } = require('../helpers/fixtures')
8-
const { knownBug } = require('../helpers/known-bug')
98

109
const KICS_FOUND_RESULTS = '50'
1110

12-
const SILENT_FAILURE =
13-
'main() swallows every exception, so a KICS failure is never reported when GitHub or results.json cannot be used'
14-
1511
test('pull request comment', async (t) => {
1612
await t.test('is not posted unless enable_comments is on (the default)', async (t) => {
1713
const fake = await startFakeGitHub(t)
@@ -171,19 +167,19 @@ test('workflow status follows the KICS exit code', async (t) => {
171167
assert.equal(run.code, 0)
172168
})
173169

174-
await t.test('fails when KICS fails and the PR comment cannot be posted', knownBug(SILENT_FAILURE), async (t) => {
170+
await t.test('fails when KICS fails and the PR comment cannot be posted', async (t) => {
175171
const fake = await startFakeGitHub(t, { failWith: 500 })
176172
const run = await runAction(t, { results: kicsResults(), kicsExitCode: KICS_FOUND_RESULTS, inputs: { enable_comments: 'true' }, apiUrl: fake.url })
177173
assert.equal(run.code, 1, 'a GitHub API outage must not turn a failed scan into a green build')
178174
})
179175

180-
await t.test('fails when KICS failed and wrote no results.json', knownBug(SILENT_FAILURE), async (t) => {
176+
await t.test('fails when KICS failed and wrote no results.json', async (t) => {
181177
const run = await runAction(t, { kicsExitCode: '126' })
182178
assert.equal(run.code, 1, run.stdout + run.stderr)
183179
assert.ok(run.errors.length > 0)
184180
})
185181

186-
await t.test('reports a missing results.json instead of passing silently', knownBug(SILENT_FAILURE), async (t) => {
182+
await t.test('reports a missing results.json instead of passing silently', async (t) => {
187183
const run = await runAction(t, { kicsExitCode: '0', inputs: { enable_comments: 'true' } })
188184
assert.notEqual(run.code, 0, 'the scan produced no report, which is not a success')
189185
})

0 commit comments

Comments
 (0)