Skip to content

Commit

Permalink
fix(post): fix post processing
Browse files Browse the repository at this point in the history
  • Loading branch information
swarit-pandey committed Jul 30, 2024
1 parent 85e92cc commit c63efcd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ async function runKnoxctlScan(): Promise<void> {
}
}

// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
log(`Creating output directory: ${outputDir}`);
fs.mkdirSync(outputDir, { recursive: true });
} else {
log(`Output directory already exists: ${outputDir}`);
}

const commandString = command.join(" ");
Expand Down
63 changes: 47 additions & 16 deletions src/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,53 @@ function stopKnoxctlScan(): void {
const pidFile = getPidFilePath();

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

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

setTimeout(() => {
try {
process.kill(Number(pid), 0);
log("Process is still running. Attempting to force kill...");
process.kill(Number(pid), "SIGKILL");
} catch (error) {
log("knoxctl scan process has been terminated");
}

fs.unlinkSync(pidFile);
log("Removed PID file");
}, 5000);
} catch (error) {
log("Failed to stop knoxctl scan process:", "error");
log(error instanceof Error ? error.message : String(error), "error");
log(
`Failed to stop knoxctl scan process: ${error instanceof Error ? error.message : String(error)}`,
"error",
);
}

fs.unlinkSync(pidFile);
} else {
log("No knoxctl scan PID file found");
log(
"No knoxctl scan PID file found. The process may have already completed.",
);
}
}

function getLatestFile(directory: string, prefix: string): string | null {
const files = fs
.readdirSync(directory)
log(`Searching for files with prefix "${prefix}" in directory: ${directory}`);
const files = fs.readdirSync(directory);
log(`Files in directory: ${files.join(", ")}`);

const matchingFiles = files
.filter((file) => file.startsWith(prefix) && file.endsWith(".md"))
.map((file) => ({
name: file,
time: fs.statSync(path.join(directory, file)).mtime.getTime(),
}))
.sort((a, b) => b.time - a.time);

return files.length > 0 ? files[0].name : null;
log(`Matching files: ${matchingFiles.map((f) => f.name).join(", ")}`);
return matchingFiles.length > 0 ? matchingFiles[0].name : null;
}

function addToSummary(content: string): void {
Expand All @@ -61,21 +80,25 @@ function processResultFile(
): void {
const file = getLatestFile(outputDir, prefix);
if (file) {
const content = fs.readFileSync(path.join(outputDir, file), ENCODING);
const filePath = path.join(outputDir, file);
log(`Processing ${title} file: ${filePath}`);
const content = fs.readFileSync(filePath, ENCODING);
addToSummary(`## ${title}\n\n${content}`);
} else {
log(`No ${title.toLowerCase()} file found`);
log(
`No ${title.toLowerCase()} file found with prefix ${prefix} in ${outputDir}`,
);
}
}

function processResults(): void {
const outputDir = getOutputDir();
const outputDir = path.resolve(getOutputDir());

if (!outputDir) {
throw new Error("Output directory is not defined");
}

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

if (!IS_GITHUB_ACTIONS) {
log(
Expand All @@ -84,6 +107,11 @@ function processResults(): void {
);
}

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 @@ -97,6 +125,9 @@ 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

0 comments on commit c63efcd

Please sign in to comment.