-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
152 lines (135 loc) · 5.59 KB
/
main.js
File metadata and controls
152 lines (135 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const commenter = require("./commenter");
const annotator = require("./annotator");
const core = require("@actions/core");
const github = require("@actions/github");
const io = require("@actions/io");
const filepath = require('path');
const fs = require("fs");
function readJSON(filename) {
const rawdata = fs.readFileSync(filename);
const parsedJSON = JSON.parse(rawdata.toString());
return parsedJSON;
}
function cleanupOutput(resultsJSONFile, outputFormats) {
if (!outputFormats.toLowerCase().includes('json') || outputFormats === '') {
io.rmRF(resultsJSONFile);
}
}
function processOutputPath(output) {
if (output === '') {
return {
path: "./",
resultsJSONFile: "./results.json"
}
}
return {
path: output,
resultsJSONFile: filepath.join(output, "/results.json")
}
}
function setWorkflowStatus(statusCode, results, failOnThreshold) {
console.log(`KICS scan status code: ${statusCode}`);
// If failOnThreshold is provided, check severity counters
if (failOnThreshold && typeof results === 'object' && results.severity_counters) {
let failed = false;
for (const [severity, rule] of Object.entries(failOnThreshold)) {
const count = results.severity_counters[severity.toUpperCase()] || 0;
if (rule && rule.op && typeof rule.value === 'number') {
if ((rule.op === 'gt' && count > rule.value) ||
(rule.op === 'lt' && count < rule.value) ||
(rule.op === 'gte' && count >= rule.value) ||
(rule.op === 'lte' && count <= rule.value)) {
console.log(`Failing workflow: ${severity} issues (${count}) ${rule.op} ${rule.value}`);
failed = true;
}
}
}
if (failed) {
core.setFailed(`KICS scan failed due to fail_on_threshold: ${JSON.stringify(failOnThreshold)}`);
return;
} else {
// If fail_on_threshold is set and not triggered, always pass
return;
}
}
// Default behavior
if (statusCode === "0") {
return;
}
core.setFailed(`KICS scan failed with exit code ${statusCode}`);
}
function parseFailOnThreshold(input) {
if (!input) return undefined;
// Accept JSON or comma-separated string (e.g., "high>5,low<20")
try {
if (input.trim().startsWith('{')) {
return JSON.parse(input);
}
const map = {};
input.split(',').forEach(pair => {
const match = pair.trim().match(/^(\w+)\s*([><]=?)\s*(\d+)$/);
if (match) {
const [, key, op, value] = match;
let opName = '';
if (op === '>') opName = 'gt';
else if (op === '>=') opName = 'gte';
else if (op === '<') opName = 'lt';
else if (op === '<=') opName = 'lte';
map[key.toLowerCase()] = { op: opName, value: Number(value) };
}
});
return Object.keys(map).length ? map : undefined;
} catch (e) {
console.error('Could not parse INPUT_FAIL_ON_THRESHOLD:', e);
return undefined;
}
}
async function main() {
console.log("Running KICS action...");
// Get ENV variables
const githubToken = process.env.INPUT_TOKEN;
let enableAnnotations = process.env.INPUT_ENABLE_ANNOTATIONS;
let enableComments = process.env.INPUT_ENABLE_COMMENTS;
let enableJobsSummary = process.env.INPUT_ENABLE_JOBS_SUMMARY;
const commentsWithQueries = process.env.INPUT_COMMENTS_WITH_QUERIES;
const excludedColumnsForCommentsWithQueries = process.env.INPUT_EXCLUDED_COLUMNS_FOR_COMMENTS_WITH_QUERIES.split(',');
const outputPath = processOutputPath(process.env.INPUT_OUTPUT_PATH);
const outputFormats = process.env.INPUT_OUTPUT_FORMATS;
const exitCode = process.env.KICS_EXIT_CODE
// Read fail_on_threshold using core.getInput for consistency
const failOnThresholdRaw = core.getInput('fail_on_threshold');
const failOnThreshold = parseFailOnThreshold(failOnThresholdRaw);
try {
const octokit = github.getOctokit(githubToken);
let context = {};
let repo = '';
let prNumber = '';
if (github.context) {
context = github.context;
if (context.repo) {
repo = context.repo;
}
if (context.payload && context.payload.pull_request) {
prNumber = context.payload.pull_request.number;
}
}
enableAnnotations = enableAnnotations ? enableAnnotations : "false"
enableComments = enableComments ? enableComments : "false"
enableJobsSummary = enableJobsSummary ? enableJobsSummary : "false"
const parsedResults = readJSON(outputPath.resultsJSONFile);
if (enableAnnotations.toLocaleLowerCase() === "true") {
annotator.annotateChangesWithResults(parsedResults);
}
if (enableComments.toLocaleLowerCase() === "true") {
await commenter.postPRComment(parsedResults, repo, prNumber, octokit, commentsWithQueries.toLocaleLowerCase() === "true", excludedColumnsForCommentsWithQueries);
}
if (enableJobsSummary.toLocaleLowerCase() === "true") {
await commenter.postJobSummary(parsedResults, commentsWithQueries.toLocaleLowerCase() === "true", excludedColumnsForCommentsWithQueries);
}
setWorkflowStatus(exitCode, parsedResults, failOnThreshold);
cleanupOutput(outputPath.resultsJSONFile, outputFormats);
} catch (e) {
console.error(e);
}
}
main();