-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
98 lines (83 loc) · 3.36 KB
/
main.js
File metadata and controls
98 lines (83 loc) · 3.36 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
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, outputName) {
const resultsFilename = outputName || "results";
if (output === '') {
return {
path: "./",
resultsJSONFile: `./${resultsFilename}.json`
}
}
return {
path: output,
resultsJSONFile: filepath.join(output, `/${resultsFilename}.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;
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, process.env.INPUT_OUTPUT_NAME);
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"
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);
cleanupOutput(outputPath.resultsJSONFile, outputFormats);
} catch (e) {
console.error(e);
}
}
main();