-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
112 lines (94 loc) · 4.03 KB
/
main.js
File metadata and controls
112 lines (94 loc) · 4.03 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
const commenter = require("./commenter");
const annotator = require("./annotator");
const filter = require("./filter");
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) {
console.log(`KICS scan status code: ${statusCode}`);
if (statusCode === "0") {
return;
}
core.setFailed(`KICS scan failed with exit code ${statusCode}`);
}
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;
let enableDiffAwareReporting = process.env.INPUT_ENABLE_DIFF_AWARE_REPORTING;
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
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"
enableDiffAwareReporting = enableDiffAwareReporting ? enableDiffAwareReporting : "false"
let parsedResults = readJSON(outputPath.resultsJSONFile);
let finalExitCode = exitCode;
if (enableDiffAwareReporting.toLocaleLowerCase() === "true" && prNumber) {
parsedResults = await filter.applyDiffAwareFiltering(parsedResults, octokit, repo, prNumber);
// If diff-aware filtering resulted in zero findings, override exit code to success
if (parsedResults.total_counter === 0) {
console.log("Diff-aware filtering resulted in zero findings. Setting workflow status to success.");
finalExitCode = "0";
}
}
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(finalExitCode);
cleanupOutput(outputPath.resultsJSONFile, outputFormats);
} catch (e) {
console.error(e);
}
}
main();