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(post): output dir #20

Merged
merged 1 commit into from
Jul 30, 2024
Merged
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
4 changes: 1 addition & 3 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function getPidFilePath(): string {

export function getOutputDir(): string {
return WORKSPACE
? WORKSPACE
? path.join(WORKSPACE, RESULTS_DIR_NAME)
: path.join(__dirname, "..", "..", RESULTS_DIR_NAME);
}

Expand All @@ -51,11 +51,9 @@ export function mockCoreForLocalTesting(): void {
};
return inputs[name] || "";
};

(core as CoreMock).getBooleanInput = (name: string): boolean => {
return (core as CoreMock).getInput(name) === "true";
};

(core as CoreMock).setFailed = console.error;
}
}
Expand Down
25 changes: 10 additions & 15 deletions src/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ const PROCESS_TREE_PREFIX = "knoxctl_scan_process_tree_";

function stopKnoxctlScan(): void {
const pidFile = getPidFilePath();

if (fs.existsSync(pidFile)) {
const pid = fs.readFileSync(pidFile, ENCODING).trim();
log(`Attempting to stop knoxctl scan process with PID: ${pid}`);

try {
process.kill(Number(pid), "SIGINT");
log("Sent SIGINT signal to knoxctl scan process");

setTimeout(() => {
try {
process.kill(Number(pid), 0);
Expand All @@ -31,7 +28,6 @@ function stopKnoxctlScan(): void {
} catch (error) {
log("knoxctl scan process has been terminated");
}

fs.unlinkSync(pidFile);
log("Removed PID file");
}, 5000);
Expand All @@ -50,6 +46,12 @@ function stopKnoxctlScan(): void {

function getLatestFile(directory: string, prefix: string): string | null {
log(`Searching for files with prefix "${prefix}" in directory: ${directory}`);

if (!fs.existsSync(directory)) {
log(`Directory does not exist: ${directory}`, "error");
return null;
}

const files = fs.readdirSync(directory);
log(`Files in directory: ${files.join(", ")}`);

Expand Down Expand Up @@ -93,25 +95,20 @@ function processResultFile(

function processResults(): void {
const outputDir = path.resolve(getOutputDir());
log(`Processing knoxctl results from directory: ${outputDir}`);

if (!outputDir) {
throw new Error("Output directory is not defined");
if (!fs.existsSync(outputDir)) {
log(`Output directory does not exist: ${outputDir}`, "error");
return;
}

log(`Processing knoxctl results from directory: ${outputDir}`);

if (!IS_GITHUB_ACTIONS) {
log(
"Running in local environment. Results will be displayed in the console.",
"warning",
);
}

if (!fs.existsSync(outputDir)) {
log(`Output directory does not exist: ${outputDir}`, "error");
return;
}

processResultFile(outputDir, NETWORK_EVENTS_PREFIX, "Network Events");
processResultFile(outputDir, PROCESS_TREE_PREFIX, "Process Tree");

Expand All @@ -125,9 +122,7 @@ function processResults(): void {
async function run(): Promise<void> {
try {
stopKnoxctlScan();

await new Promise((resolve) => setTimeout(resolve, 6000));

processResults();
if (IS_GITHUB_ACTIONS) {
await core.summary.write();
Expand Down
Loading