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: redundant CLI downloads [HEAD-450][HEAD-504][HEAD-505] #163

Merged
merged 13 commits into from
Jul 20, 2023
1 change: 1 addition & 0 deletions snykTask/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ async function runSnykTest(
.argIf(taskArgs.organization, `--org=${taskArgs.organization}`)
.argIf(taskArgs.projectName, `--project-name=${projectNameArg}`)
.arg(`--json-file-output=${jsonReportOutputPath}`)
.argIf(isDebugMode(), '-d')
.line(taskArgs.additionalArguments);

const options = getOptionsToExecuteSnykCLICommand(
Expand Down
58 changes: 46 additions & 12 deletions snykTask/src/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,51 @@ export async function downloadExecutable(
targetDirectory: string,
executable: Executable,
) {
const fileWriter = fs.createWriteStream(
path.join(targetDirectory, executable.filename),
{
mode: 0o766,
},
);
return new Promise<void>((resolve, reject) => {
https.get(executable.downloadUrl, (response) => {
response.on('end', () => resolve());
response.on('error', (err) => reject(err));
response.pipe(fileWriter);
});
const filePath = path.join(targetDirectory, executable.filename);

// Check if the file already exists
if (fs.existsSync(filePath)) {
asaf92 marked this conversation as resolved.
Show resolved Hide resolved
console.log(
`File ${executable.filename} already exists, skipping download.`,
);
return;
}

const fileWriter = fs.createWriteStream(filePath, {
mode: 0o766,
});

// Wrapping the download in a function for easy retrying
const doDownload = () =>
new Promise<void>((resolve, reject) => {
https.get(executable.downloadUrl, (response) => {
response.on('end', () => resolve());
response.on('error', (err) => {
console.error(
PeterSchafer marked this conversation as resolved.
Show resolved Hide resolved
`Download of ${executable.filename} failed: ${err.message}`,
);
reject(err);
});
response.pipe(fileWriter);
});
});

// Try to download the file, retry once after 5 seconds if the first attempt fails
try {
asaf92 marked this conversation as resolved.
Show resolved Hide resolved
await doDownload();
} catch (err) {
console.error(`Download of ${executable.filename} failed: ${err.message}`);
console.log(
`Retrying download of ${executable.filename} after 5 seconds...`,
);
await new Promise((resolve) => setTimeout(resolve, 5000));
try {
await doDownload();
PeterSchafer marked this conversation as resolved.
Show resolved Hide resolved
console.log(`Retry successful for ${executable.filename}`);
} catch (retryErr) {
console.error(
`Retry failed for ${executable.filename}: ${retryErr.message}`,
);
}
}
}