Skip to content

Commit f3125e8

Browse files
author
Asaf Agami
authored
fix: redundant CLI downloads (#163)
1 parent 6a8eb7a commit f3125e8

File tree

5 files changed

+83
-18
lines changed

5 files changed

+83
-18
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: 2.1
22

33
orbs:
44
prodsec: snyk/[email protected]
5-
5+
66
jobs:
77
test:
88
docker:

.snyk

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
2+
version: v1.25.0
3+
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
4+
ignore:
5+
SNYK-JS-MOCKERY-3043117:
6+
- '*':
7+
reason: No upgrade available
8+
expires: 2024-07-18T00:00:00.000Z
9+
created: 2023-07-18T12:52:43.840Z
10+
SNYK-JS-SEMVER-3247795:
11+
- '*':
12+
reason: No upgrade available
13+
expires: 2024-07-18T00:00:00.000Z
14+
created: 2023-07-18T12:52:32.034Z
15+
patch: {}

ops/deploy/run-test-pipelines.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ async function runBuild(
9595
): Promise<void> {
9696
let success = false;
9797

98+
console.log(
99+
`Starting build for project: ${testProjectName} with build definition ID: ${testBuildDefinitionId}`,
100+
);
101+
98102
try {
99103
const launchPipelineResult = await launchBuildPipeline(
100104
webApi,
@@ -123,21 +127,23 @@ async function runBuild(
123127
}
124128

125129
if (status === BuildStatus.Completed) {
126-
console.log('build is complete');
130+
console.log(`build is complete for ${testProjectName}`);
127131
const result = checkBuildStatusRes.result;
128132
console.log(`build result: ${result}`);
129133
if (result) {
130134
if (result === BuildResult.Succeeded) {
131-
console.log('build succeeded');
135+
console.log(`build succeeded for ${testProjectName}`);
132136
success = true;
133137
} else {
134-
console.log(`build did not succeed. BuildResult code: ${result}`);
138+
console.log(
139+
`build did not succeed for ${testProjectName}. BuildResult code: ${result}`,
140+
);
135141
}
136142
}
137143
break;
138144
} else {
139145
console.log(
140-
`Still waiting for build ${buildId} to complete. Status: ${status}. Time: ${new Date().getTime()}`,
146+
`Still waiting for build ${buildId} (${testProjectName}) to complete. Status: ${status}. Time: ${new Date().getTime()}`,
141147
);
142148
await asyncSleep(10000);
143149
}
@@ -150,7 +156,9 @@ async function runBuild(
150156
return Promise.reject();
151157
}
152158
} catch (err) {
153-
console.log('failed to launching / checking build');
159+
console.log(
160+
`Failed to launch/check build for project: ${testProjectName} with build definition ID: ${testBuildDefinitionId}`,
161+
);
154162
console.log(err);
155163
console.log('\nrejecting - not successful');
156164
return Promise.reject();

snykTask/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ async function runSnykTest(
154154
.argIf(taskArgs.organization, `--org=${taskArgs.organization}`)
155155
.argIf(taskArgs.projectName, `--project-name=${projectNameArg}`)
156156
.arg(`--json-file-output=${jsonReportOutputPath}`)
157+
.argIf(isDebugMode(), '-d')
157158
.line(taskArgs.additionalArguments);
158159

159160
const options = getOptionsToExecuteSnykCLICommand(

snykTask/src/install/index.ts

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,59 @@ export function getSnykDownloadInfo(platform: Platform): SnykDownloads {
5353
export async function downloadExecutable(
5454
targetDirectory: string,
5555
executable: Executable,
56+
maxRetries = 5,
5657
) {
57-
const fileWriter = fs.createWriteStream(
58-
path.join(targetDirectory, executable.filename),
59-
{
60-
mode: 0o766,
61-
},
62-
);
63-
return new Promise<void>((resolve, reject) => {
64-
https.get(executable.downloadUrl, (response) => {
65-
response.on('end', () => resolve());
66-
response.on('error', (err) => reject(err));
67-
response.pipe(fileWriter);
68-
});
58+
const filePath = path.join(targetDirectory, executable.filename);
59+
60+
// Check if the file already exists
61+
if (fs.existsSync(filePath)) {
62+
console.log(
63+
`File ${executable.filename} already exists, skipping download.`,
64+
);
65+
return;
66+
}
67+
68+
const fileWriter = fs.createWriteStream(filePath, {
69+
mode: 0o766,
6970
});
71+
72+
// Wrapping the download in a function for easy retrying
73+
const doDownload = () =>
74+
new Promise<void>((resolve, reject) => {
75+
https.get(executable.downloadUrl, (response) => {
76+
response.on('end', () => resolve());
77+
response.on('error', (err) => {
78+
console.error(
79+
`Download of ${executable.filename} failed: ${err.message}`,
80+
);
81+
reject(err);
82+
});
83+
response.pipe(fileWriter);
84+
});
85+
});
86+
87+
// Try to download the file, retry up to `maxRetries` times if the attempt fails
88+
for (let attempt = 0; attempt < maxRetries; attempt++) {
89+
try {
90+
await doDownload();
91+
console.log(`Download successful for ${executable.filename}`);
92+
break;
93+
} catch (err) {
94+
console.error(
95+
`Download of ${executable.filename} failed: ${err.message}`,
96+
);
97+
98+
// Don't wait before retrying the last attempt
99+
if (attempt < maxRetries - 1) {
100+
console.log(
101+
`Retrying download of ${executable.filename} after 5 seconds...`,
102+
);
103+
await new Promise((resolve) => setTimeout(resolve, 5000));
104+
} else {
105+
console.error(
106+
`All retries failed for ${executable.filename}: ${err.message}`,
107+
);
108+
}
109+
}
110+
}
70111
}

0 commit comments

Comments
 (0)