-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolveAndTestFacts w/ async polling(c/c++)
Introduces async request for C/C++ with polling, where its Facts are resolved/computed in our platform
- Loading branch information
Showing
4 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import * as config from '../config'; | ||
import { isCI } from '../is-ci'; | ||
import { makeRequest } from '../request/promise'; | ||
import { Options } from '../types'; | ||
|
||
import { ResolveAndTestFactsResponse } from '../snyk-test/legacy'; | ||
import { assembleQueryString } from '../snyk-test/common'; | ||
import { getAuthHeader } from '../api-token'; | ||
import { ScanResult } from './types'; | ||
|
||
export async function requestPollingToken( | ||
options: Options, | ||
isAsync: boolean, | ||
scanResult: ScanResult, | ||
): Promise<ResolveAndTestFactsResponse> { | ||
const payload = { | ||
method: 'POST', | ||
url: `${config.API}/test-dependencies`, | ||
json: true, | ||
headers: { | ||
'x-is-ci': isCI(), | ||
authorization: getAuthHeader(), | ||
}, | ||
body: { | ||
isAsync, | ||
scanResult, | ||
}, | ||
qs: assembleQueryString(options), | ||
}; | ||
return await makeRequest<ResolveAndTestFactsResponse>(payload); | ||
} | ||
|
||
export async function pollingWithTokenUntilDone( | ||
token: string, | ||
type: string, | ||
options: Options, | ||
pollInterval: number, | ||
attemptsCount: number, | ||
maxAttempts = Infinity, | ||
): Promise<ResolveAndTestFactsResponse> { | ||
const payload = { | ||
method: 'GET', | ||
url: `${config.API}/test-dependencies/${token}`, | ||
json: true, | ||
headers: { | ||
'x-is-ci': isCI(), | ||
authorization: getAuthHeader(), | ||
}, | ||
qs: { ...assembleQueryString(options), type }, | ||
}; | ||
|
||
const response = await makeRequest<ResolveAndTestFactsResponse>(payload); | ||
|
||
if (response.result && response.meta) { | ||
return response; | ||
} | ||
|
||
if (pollingRequestHasFailed(response)) { | ||
throw new Error('polling request has failed.'); | ||
} | ||
|
||
//TODO: (@snyk/tundra): move to backend | ||
checkPollingAttempts(maxAttempts)(attemptsCount); | ||
|
||
return await retryWithPollInterval( | ||
token, | ||
type, | ||
options, | ||
pollInterval, | ||
attemptsCount, | ||
maxAttempts, | ||
); | ||
} | ||
|
||
async function retryWithPollInterval( | ||
token: string, | ||
type: string, | ||
options: Options, | ||
pollInterval: number, | ||
attemptsCount: number, | ||
maxAttempts: number, | ||
): Promise<ResolveAndTestFactsResponse> { | ||
return new Promise((resolve, reject) => | ||
setTimeout(async () => { | ||
const res = await pollingWithTokenUntilDone( | ||
token, | ||
type, | ||
options, | ||
pollInterval, | ||
attemptsCount, | ||
maxAttempts, | ||
); | ||
|
||
if (pollingRequestHasFailed(res)) { | ||
return reject(res); | ||
} | ||
|
||
if (res.result && res.meta) { | ||
return resolve(res); | ||
} | ||
}, pollInterval), | ||
); | ||
} | ||
|
||
function checkPollingAttempts(maxAttempts: number) { | ||
return (attemptsCount: number) => { | ||
if (attemptsCount > maxAttempts) { | ||
throw new Error('Exceeded Polling maxAttempts'); | ||
} | ||
}; | ||
} | ||
function pollingRequestHasFailed( | ||
response: ResolveAndTestFactsResponse, | ||
): boolean { | ||
const { token, result, meta, status, error, code, message } = response; | ||
const hasError = !!error && !!code && !!message; | ||
const hasPollingContext = token && result && meta && status; | ||
|
||
return !hasPollingContext || hasError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { Options } from '../../../../../src/lib/types'; | ||
import * as polling from '../../../../../src/lib/ecosystems/polling'; | ||
import { resolveAndTestFacts } from '../../../../../src/lib/ecosystems/test'; | ||
|
||
describe('test ecosystems', () => { | ||
it('resolveAndTestFacts', async () => { | ||
/* eslint-disable @typescript-eslint/camelcase */ | ||
const scanResults = { | ||
path: [ | ||
{ | ||
name: 'my-unmanaged-c-project', | ||
facts: [ | ||
{ | ||
type: 'fileSignatures', | ||
data: [ | ||
{ | ||
path: 'fastlz_example/fastlz.h', | ||
hashes_ffm: [ | ||
{ | ||
format: 1, | ||
data: 'ucMc383nMM/wkFRM4iOo5Q', | ||
}, | ||
{ | ||
format: 1, | ||
data: 'k+DxEmslFQWuJsZFXvSoYw', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
identity: { | ||
type: 'cpp', | ||
}, | ||
target: { | ||
remoteUrl: 'https://github.com/some-org/some-unmanaged-project.git', | ||
branch: 'master', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const depGraphData = { | ||
schemaVersion: '1.2.0', | ||
pkgManager: { name: 'cpp' }, | ||
pkgs: [ | ||
{ id: '_root@0.0.0', info: { name: '_root', version: '0.0.0' } }, | ||
{ | ||
id: 'fastlz|https://github.com/ariya/fastlz/archive/0.5.0.zip@0.5.0', | ||
info: { | ||
name: 'fastlz|https://github.com/ariya/fastlz/archive/0.5.0.zip', | ||
version: '0.5.0', | ||
}, | ||
}, | ||
], | ||
graph: { | ||
rootNodeId: 'root-node', | ||
nodes: [ | ||
{ | ||
nodeId: 'root-node', | ||
pkgId: '_root@0.0.0', | ||
deps: [ | ||
{ | ||
nodeId: | ||
'fastlz|https://github.com/ariya/fastlz/archive/0.5.0.zip@0.5.0', | ||
}, | ||
], | ||
}, | ||
{ | ||
nodeId: | ||
'fastlz|https://github.com/ariya/fastlz/archive/0.5.0.zip@0.5.0', | ||
pkgId: | ||
'fastlz|https://github.com/ariya/fastlz/archive/0.5.0.zip@0.5.0', | ||
deps: [], | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
// temporary values, we trully want depGraph & issuesData/affectedPkgs | ||
const pollingWithTokenUntilDoneSpy = jest.spyOn( | ||
polling, | ||
'pollingWithTokenUntilDone', | ||
); | ||
|
||
pollingWithTokenUntilDoneSpy.mockResolvedValueOnce({ | ||
result: { | ||
issuesData: {}, | ||
issues: [], | ||
depGraphData, | ||
meta: { | ||
isPrivate: true, | ||
isLicensesEnabled: false, | ||
ignoreSettings: null, | ||
org: expect.any(String), | ||
}, | ||
}, | ||
}); | ||
|
||
const [testResults, errors] = await resolveAndTestFacts( | ||
'cpp', | ||
scanResults, | ||
{} as Options, | ||
); | ||
|
||
expect(testResults).toEqual([ | ||
{ | ||
result: { | ||
issuesData: {}, | ||
issues: [], | ||
depGraphData, | ||
meta: { | ||
isPrivate: true, | ||
isLicensesEnabled: false, | ||
ignoreSettings: null, | ||
org: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
]); | ||
expect(errors).toEqual([]); | ||
}); | ||
}); |