Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(configs): add propper support for output name on config file #106

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
493ae3d
update main
cxMiguelSilva Mar 8, 2024
0ca0adb
update main
cxMiguelSilva Mar 8, 2024
dcba255
add missing packages
cxMiguelSilva Mar 8, 2024
a5bdc0e
update config parsing
cxMiguelSilva Mar 11, 2024
243572c
update config parsing
cxMiguelSilva Mar 11, 2024
62cbbeb
update config parsing
cxMiguelSilva Mar 11, 2024
2987e8e
update config parsing
cxMiguelSilva Mar 11, 2024
096f765
update config parsing
cxMiguelSilva Mar 11, 2024
9228d58
update config parsing
cxMiguelSilva Mar 11, 2024
8c29deb
update config parsing
cxMiguelSilva Mar 11, 2024
703fbdb
update config parsing
cxMiguelSilva Mar 11, 2024
c661cc0
update config parsing
cxMiguelSilva Mar 11, 2024
53d2530
update config parsing
cxMiguelSilva Mar 11, 2024
5239572
update config parsing
cxMiguelSilva Mar 11, 2024
d103479
update config parsing
cxMiguelSilva Mar 11, 2024
25bae9c
update config parsing
cxMiguelSilva Mar 11, 2024
f37baf7
update config parsing
cxMiguelSilva Mar 11, 2024
8c905f5
update config parsing
cxMiguelSilva Mar 11, 2024
ecd9603
update config parsing
cxMiguelSilva Mar 11, 2024
2db4dc2
update config parsing
cxMiguelSilva Mar 12, 2024
26f7422
update config parsing
cxMiguelSilva Mar 12, 2024
b6aeac0
update config parsing
cxMiguelSilva Mar 12, 2024
b1e0c2e
update config parsing
cxMiguelSilva Mar 12, 2024
7693a77
update config parsing
cxMiguelSilva Mar 12, 2024
e922013
update config parsing
cxMiguelSilva Mar 12, 2024
3a377e3
update config parsing
cxMiguelSilva Mar 12, 2024
3aa84fc
remove test configs
cxMiguelSilva Mar 12, 2024
c2a286a
reset entrypoint
cxMiguelSilva Mar 12, 2024
eb07474
reset entrypoint
cxMiguelSilva Mar 12, 2024
0fe772c
add case for no extension
cxMiguelSilva Mar 13, 2024
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
100 changes: 87 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
"@actions/github": "^5.0.0",
"@actions/io": "^1.1.1",
"@actions/tool-cache": "^2.0.1",
"@iarna/toml": "^2.2.5",
"hcl-parser": "^0.1.1",
"hcl2-parser": "^1.0.3",
"js-hcl-parser": "^1.0.1",
"js-yaml": "^4.1.0",
"moment": "^2.29.4",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/uuid": "^8.3.4",
"@vercel/ncc": "^0.36.1",
"prettier": "^2.4.1",
"@types/uuid": "^8.3.4"
"prettier": "^2.4.1"
}
}
94 changes: 84 additions & 10 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const github = require("@actions/github");
const io = require("@actions/io");
const filepath = require('path');
const fs = require("fs");
const yaml = require('js-yaml');
const HCL = require("js-hcl-parser")
const toml = require('@iarna/toml');

function readJSON(filename) {
const rawdata = fs.readFileSync(filename);
const parsedJSON = JSON.parse(rawdata.toString());
return parsedJSON;
const rawData = fs.readFileSync(filename);
return JSON.parse(rawData.toString());
}

function cleanupOutput(resultsJSONFile, outputFormats) {
Expand All @@ -18,17 +20,88 @@ function cleanupOutput(resultsJSONFile, outputFormats) {
}
}

function processOutputPath(output) {
if (output === '') {
return {
path: "./",
resultsJSONFile: "./results.json"
async function processOutputPath(output, configPath) {
const workspace = "/github/workspace"
let resultsFileName = '';
if (configPath !== '' ) {

[config_type, content] = await fileAnalyzer(configPath, workspace);

if (config_type !== '') {
resultsFileName = content["output-name"] || '';
if (resultsFileName !== '') {
// if the output file name does not have an extension, add .json
if (!resultsFileName.includes('.')) {
resultsFileName = resultsFileName + ".json";
}
}
}
}

if (output === '') {
output = "./";
}

if (resultsFileName === '') {
resultsFileName = filepath.join(output, "/results.json")
} else {
resultsFileName = filepath.join(workspace, resultsFileName);
}

return {
path: output,
resultsJSONFile: filepath.join(output, "/results.json")
resultsJSONFile: resultsFileName
}
}

function readFileContent(filePath, workspace) {
try {
// read file content
console.log(`Reading file: ${filePath}`);
console.log(`Workspace: ${workspace}`);
const path = filepath.join(workspace, filePath);
const stats = fs.statSync(path); // Use fs.statSync to get file stats synchronously
if (!stats.isFile()) {
throw new Error('Provided path is not a file.');
}
const data = fs.readFileSync(path, 'utf8'); // Use fs.readFileSync to read file content synchronously
return data;
} catch (error) {
console.error('Error reading file:', error);
return ''; // Return empty string or handle the error as needed
}
}
async function fileAnalyzer(filePath, workspace) {
const fileContent = await readFileContent(filePath, workspace);
let temp = {};

if (fileContent === '') {
console.log('Error analyzing file: Empty file content');
return ['', {}];
}

try {
const jsonData = JSON.parse(fileContent);
return ['json', jsonData];
} catch (jsonError) {
try {
const parsed = HCL.parse(fileContent);
const jsonData = JSON.parse(parsed);
return ['hcl', jsonData];
} catch (hclErr) {
try {
temp = toml.parse(fileContent);
return ['toml', temp];
} catch (tomlErr) {
try {
temp = yaml.load(fileContent);
return ['yaml', temp];
} catch (yamlErr) {
console.log(`Error analyzing file: Invalid configuration file format`);
return ['', {}];
}
}
}
}
}

Expand All @@ -52,10 +125,11 @@ async function main() {
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 outputPath = await processOutputPath(process.env.INPUT_OUTPUT_PATH, process.env.INPUT_CONFIG_PATH);
const outputFormats = process.env.INPUT_OUTPUT_FORMATS;
const exitCode = process.env.KICS_EXIT_CODE

console.log("Output Path: ", outputPath);
try {
const octokit = github.getOctokit(githubToken);
let context = {};
Expand Down
Loading