Skip to content
Closed
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
94 changes: 68 additions & 26 deletions src/lib/ecosystems/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,35 +118,77 @@ async function testDependencies(
const errors: string[] = [];
for (const [path, scanResults] of Object.entries(scans)) {
await spinner(`Testing dependencies in ${path}`);
for (const scanResult of scanResults) {
const payload = {
method: 'POST',
url: `${config.API}/test-dependencies`,
json: true,
headers: {
'x-is-ci': isCI(),
authorization: getAuthHeader(),
},
body: {
scanResult,
},
qs: assembleQueryString(options),
};
try {
const response = await makeRequest<TestDependenciesResponse>(payload);
results.push({
issues: response.result.issues,
issuesData: response.result.issuesData,
depGraphData: response.result.depGraphData,
});
} catch (error) {
if (error.code >= 400 && error.code < 500) {
throw new Error(error.message);
}
errors.push('Could not test dependencies in ' + path);
const [firstScanResult, ...remainingScanResults] = scanResults;
if (!firstScanResult) {
continue;
}

const firstResult = await testDependenciesForScanResult(
firstScanResult,
options,
path,
);
if (firstResult.testResult) {
results.push(firstResult.testResult);
}
if (firstResult.error) {
errors.push(firstResult.error);
}

const remainingResults = await Promise.all(
remainingScanResults.map((scanResult) =>
testDependenciesForScanResult(scanResult, options, path),
),
);

for (const remainingResult of remainingResults) {
if (remainingResult.testResult) {
results.push(remainingResult.testResult);
}
if (remainingResult.error) {
errors.push(remainingResult.error);
}
}
}
spinner.clearAll();
return [results, errors];
}

async function testDependenciesForScanResult(
scanResult: ScanResult,
options: Options,
path: string,
): Promise<{ testResult?: TestResult; error?: string }> {
const payload = {
method: 'POST',
url: `${config.API}/test-dependencies`,
json: true,
headers: {
'x-is-ci': isCI(),
authorization: getAuthHeader(),
},
body: {
scanResult,
},
qs: assembleQueryString(options),
};

try {
const response = await makeRequest<TestDependenciesResponse>(payload);
return {
testResult: {
issues: response.result.issues,
issuesData: response.result.issuesData,
depGraphData: response.result.depGraphData,
},
};
} catch (error) {
if (error.code >= 400 && error.code < 500) {
throw new Error(error.message);
}

return {
error: 'Could not test dependencies in ' + path,
};
}
}
13 changes: 10 additions & 3 deletions src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ const debug = debugModule('snyk:run-test');
// Controls the number of simultaneous test requests that can be in-flight.
const MAX_CONCURRENCY = 5;

export async function sendPayloadsForTestRequests<T>(
payloads: Payload[],
sendRequest: (payload: Payload) => Promise<T>,
): Promise<T[]> {
return pMap(payloads, sendRequest, {
concurrency: MAX_CONCURRENCY,
});
}

function prepareResponseForParsing(
payload: Payload,
response: TestDependenciesResponse,
Expand Down Expand Up @@ -292,9 +301,7 @@ async function sendAndParseResults(
throw error;
};

const responses = await pMap(payloads, sendRequest, {
concurrency: MAX_CONCURRENCY,
});
const responses = await sendPayloadsForTestRequests(payloads, sendRequest);

for (const { payload, originalPayload, response } of responses) {
const {
Expand Down
Loading