Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ async function main() {
const outputFormats = process.env.INPUT_OUTPUT_FORMATS;
const exitCode = process.env.KICS_EXIT_CODE

// the scan verdict comes first so a reporting failure can never hide it
setWorkflowStatus(exitCode);

try {
const octokit = github.getOctokit(githubToken);
let context = {};
Expand All @@ -80,7 +83,13 @@ async function main() {
enableComments = enableComments ? enableComments : "false"
enableJobsSummary = enableJobsSummary ? enableJobsSummary : "false"

const parsedResults = readJSON(outputPath.resultsJSONFile);
let parsedResults;
try {
parsedResults = readJSON(outputPath.resultsJSONFile);
} catch (e) {
core.setFailed(`Could not read the KICS results: ${e.message}`);
return;
}
if (enableAnnotations.toLocaleLowerCase() === "true") {
annotator.annotateChangesWithResults(parsedResults);
}
Expand All @@ -91,7 +100,6 @@ async function main() {
await commenter.postJobSummary(parsedResults, commentsWithQueries.toLocaleLowerCase() === "true", excludedColumnsForCommentsWithQueries);
}

setWorkflowStatus(exitCode);
await cleanupOutput(outputPath.resultsJSONFile, outputFormats);
} catch (e) {
console.error(e);
Expand Down
10 changes: 3 additions & 7 deletions test/e2e/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ const assert = require('node:assert/strict')
const { runAction } = require('../helpers/run-action')
const { startFakeGitHub, humanComment } = require('../helpers/fake-github')
const { kicsResults, query, finding } = require('../helpers/fixtures')
const { knownBug } = require('../helpers/known-bug')

const KICS_FOUND_RESULTS = '50'

const SILENT_FAILURE =
'main() swallows every exception, so a KICS failure is never reported when GitHub or results.json cannot be used'

test('pull request comment', async (t) => {
await t.test('is not posted unless enable_comments is on (the default)', async (t) => {
const fake = await startFakeGitHub(t)
Expand Down Expand Up @@ -171,19 +167,19 @@ test('workflow status follows the KICS exit code', async (t) => {
assert.equal(run.code, 0)
})

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

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

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