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
119 changes: 82 additions & 37 deletions src/lib/ecosystems/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,52 +144,97 @@ async function monitorDependencies(
const errors: EcosystemMonitorError[] = [];
for (const [path, scanResults] of Object.entries(scans)) {
await spinner(`Monitoring dependencies in ${path}`);
for (const scanResult of scanResults) {
const monitorDependenciesRequest =
await generateMonitorDependenciesRequest(scanResult, options);
const [firstScanResult, ...remainingScanResults] = scanResults;
if (!firstScanResult) {
spinner.clearAll();
continue;
}

const firstResult = await monitorDependenciesForScanResult(
firstScanResult,
options,
path,
);
if (firstResult.monitorResult) {
results.push(firstResult.monitorResult);
}
if (firstResult.monitorError) {
errors.push(firstResult.monitorError);
}

const configOrg = config.org ? decodeURIComponent(config.org) : undefined;
const remainingResults = await Promise.all(
remainingScanResults.map((scanResult) =>
monitorDependenciesForScanResult(scanResult, options, path),
),
);

const payload = {
method: 'PUT',
url: `${config.API}/monitor-dependencies`,
json: true,
headers: {
'x-is-ci': isCI(),
authorization: getAuthHeader(),
},
body: monitorDependenciesRequest,
qs: {
org: options.org || configOrg,
},
};
try {
const response =
await makeRequest<MonitorDependenciesResponse>(payload);
results.push({
...response,
path,
scanResult,
});
} catch (error) {
if (error.code === 401) {
throw AuthFailedError();
}
if (error.code >= 400 && error.code < 500) {
throw new MonitorError(error.code, error.message);
}
errors.push({
error: 'Could not monitor dependencies in ' + path,
path,
scanResult,
});
for (const remainingResult of remainingResults) {
if (remainingResult.monitorResult) {
results.push(remainingResult.monitorResult);
}
if (remainingResult.monitorError) {
errors.push(remainingResult.monitorError);
}
}
spinner.clearAll();
}
return [results, errors];
}

async function monitorDependenciesForScanResult(
scanResult: ScanResult,
options: Options & MonitorOptions,
path: string,
): Promise<{
monitorResult?: EcosystemMonitorResult;
monitorError?: EcosystemMonitorError;
}> {
const monitorDependenciesRequest = await generateMonitorDependenciesRequest(
scanResult,
options,
);

const configOrg = config.org ? decodeURIComponent(config.org) : undefined;
const payload = {
method: 'PUT',
url: `${config.API}/monitor-dependencies`,
json: true,
headers: {
'x-is-ci': isCI(),
authorization: getAuthHeader(),
},
body: monitorDependenciesRequest,
qs: {
org: options.org || configOrg,
},
};

try {
const response = await makeRequest<MonitorDependenciesResponse>(payload);
return {
monitorResult: {
...response,
path,
scanResult,
},
};
} catch (error) {
if (error.code === 401) {
throw AuthFailedError();
}
if (error.code >= 400 && error.code < 500) {
throw new MonitorError(error.code, error.message);
}
return {
monitorError: {
error: 'Could not monitor dependencies in ' + path,
path,
scanResult,
},
};
}
}

export async function getFormattedMonitorOutput(
results: Array<GoodResult | BadResult>,
monitorResults: EcosystemMonitorResult[],
Expand Down
85 changes: 85 additions & 0 deletions test/jest/unit/ecosystems-monitor-docker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,89 @@ describe('monitorEcosystem docker/container', () => {
);
expect(parsedOutput.projectName).not.toBe('my-custom-project-name');
});

it('should monitor base scan result first and then parallelize remaining requests', async () => {
const baseScanResult = {
...readJsonFixture('container-deb-scan-result.json'),
identity: {
type: 'deb',
targetFile: 'base-os',
},
} as ScanResult;
const appScanResultOne = {
...readJsonFixture('maven-project-0-dependencies-scan-result.json'),
identity: {
type: 'maven',
targetFile: 'app-1',
},
} as ScanResult;
const appScanResultTwo = {
...readJsonFixture('maven-project-0-dependencies-scan-result.json'),
identity: {
type: 'maven',
targetFile: 'app-2',
},
} as ScanResult;

jest.spyOn(dockerPlugin, 'scan').mockResolvedValue({
scanResults: [baseScanResult, appScanResultOne, appScanResultTwo],
});

const requestsByIdentity: string[] = [];
let baseRequestResolved = false;
let appOneStartedBeforeBaseResolved = false;
let appTwoStartedBeforeBaseResolved = false;

jest.spyOn(request, 'makeRequest').mockImplementation((payload: any) => {
const identity = payload.body.scanResult.identity.targetFile as string;
requestsByIdentity.push(identity);
const baseResponse = readJsonFixture(
'monitor-dependencies-response-with-project-name.json',
) as ecosystemsTypes.MonitorDependenciesResponse;
const responseForIdentity = {
...baseResponse,
id: `${identity}-id`,
projectName: identity,
};

return new Promise((resolve) => {
if (identity === 'base-os') {
setTimeout(() => {
baseRequestResolved = true;
resolve(responseForIdentity);
}, 25);
return;
}

if (identity === 'app-1' && !baseRequestResolved) {
appOneStartedBeforeBaseResolved = true;
}
if (identity === 'app-2' && !baseRequestResolved) {
appTwoStartedBeforeBaseResolved = true;
}

resolve(responseForIdentity);
});
});

const [monitorResults, monitorErrors] = await ecosystems.monitorEcosystem(
'docker',
['/srv'],
{
path: '/srv',
docker: true,
org: 'my-org',
},
);

expect(monitorErrors).toEqual([]);
expect(requestsByIdentity).toEqual(['base-os', 'app-1', 'app-2']);
expect(appOneStartedBeforeBaseResolved).toBe(false);
expect(appTwoStartedBeforeBaseResolved).toBe(false);
expect(monitorResults.map((result) => result.projectName)).toEqual([
'base-os',
'app-1',
'app-2',
]);
});
});
Loading