-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add report upload to code test
- Loading branch information
1 parent
49cb904
commit 62e492d
Showing
10 changed files
with
330 additions
and
8 deletions.
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
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
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,89 @@ | ||
import pick from 'lodash.pick'; | ||
import { POLLING_INTERVAL } from './constants'; | ||
import { emitter } from './emitter'; | ||
import { | ||
AnalysisFailedResponse, | ||
GetAnalysisErrorCodes, | ||
UploadReportResponseDto, | ||
initReport, | ||
AnalysisStatus, | ||
Result, | ||
UploadReportOptions, | ||
getReport, | ||
} from './http'; | ||
import { ReportResult } from './interfaces/analysis-result.interface'; | ||
|
||
const sleep = (duration: number) => new Promise(resolve => setTimeout(resolve, duration)); | ||
|
||
async function pollReport( | ||
options: UploadReportOptions, | ||
): Promise<Result<AnalysisFailedResponse | ReportResult, GetAnalysisErrorCodes>> { | ||
// Return early if project name is not provided. | ||
const projectName = options.report?.projectName?.trim(); | ||
if (!projectName || projectName.length === 0) { | ||
throw new Error('"project-name" must be provided for "report"'); | ||
} | ||
|
||
emitter.analyseProgress({ | ||
status: AnalysisStatus.waiting, | ||
progress: 0, | ||
}); | ||
|
||
// First init the report | ||
const initResponse = await initReport(options); | ||
|
||
if (initResponse.type === 'error') { | ||
return initResponse; | ||
} | ||
const { reportId } = initResponse.value; | ||
|
||
let apiResponse: Result<UploadReportResponseDto, GetAnalysisErrorCodes>; | ||
let response: UploadReportResponseDto; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
// eslint-disable-next-line no-await-in-loop | ||
apiResponse = await getReport({ | ||
...pick(options, ['baseURL', 'sessionToken', 'source', 'requestId', 'org']), | ||
reportId, | ||
}); | ||
|
||
if (apiResponse.type === 'error') { | ||
return apiResponse; | ||
} | ||
|
||
response = apiResponse.value; | ||
|
||
if ( | ||
response.status === AnalysisStatus.waiting || | ||
response.status === AnalysisStatus.fetching || | ||
response.status === AnalysisStatus.analyzing || | ||
response.status === AnalysisStatus.done | ||
) { | ||
// Report progress of fetching | ||
emitter.analyseProgress(response); | ||
} else if (response.status === AnalysisStatus.complete) { | ||
// Return data of analysis | ||
return apiResponse as Result<ReportResult, GetAnalysisErrorCodes>; | ||
// deepcode ignore DuplicateIfBody: false positive it seems that interface is not taken into account | ||
} else if (response.status === AnalysisStatus.failed) { | ||
// Report failure of analysing | ||
return apiResponse as Result<AnalysisFailedResponse, GetAnalysisErrorCodes>; | ||
} | ||
|
||
// eslint-disable-next-line no-await-in-loop | ||
await sleep(POLLING_INTERVAL); | ||
} | ||
} | ||
|
||
export async function reportBundle(options: UploadReportOptions): Promise<ReportResult> { | ||
// Call remote bundle for analysis results and emit intermediate progress | ||
const response = await pollReport(options); | ||
|
||
if (response.type === 'error') { | ||
throw response.error; | ||
} else if (response.value.status === AnalysisStatus.failed) { | ||
throw new Error('Analysis has failed'); | ||
} | ||
|
||
return response.value; | ||
} |
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
Oops, something went wrong.