split annotations in multiple requests

This commit is contained in:
nicolo.castro 2023-12-20 09:17:05 +01:00
parent 0a5dc1ab75
commit cf7f2a70cf
No known key found for this signature in database
GPG key ID: CD97C9B5EF7D74A8
2 changed files with 31 additions and 4 deletions

View file

@ -1,13 +1,15 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as github from '@actions/github' import * as github from '@actions/github'
import {GitHub} from '@actions/github/lib/utils' import {GitHub} from '@actions/github/lib/utils'
import { OctokitResponse } from '@octokit/types'
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
import {ArtifactProvider} from './input-providers/artifact-provider' import {ArtifactProvider} from './input-providers/artifact-provider'
import {LocalFileProvider} from './input-providers/local-file-provider' import {LocalFileProvider} from './input-providers/local-file-provider'
import {FileContent} from './input-providers/input-provider' import {FileContent} from './input-providers/input-provider'
import {ParseOptions, TestParser} from './test-parser' import {ParseOptions, TestParser} from './test-parser'
import {TestRunResult} from './test-results' import {TestRunResult} from './test-results'
import {getAnnotations} from './report/get-annotations' import {getAnnotations, Annotation} from './report/get-annotations'
import {getReport} from './report/get-report' import {getReport} from './report/get-report'
import {DartJsonParser} from './parsers/dart-json/dart-json-parser' import {DartJsonParser} from './parsers/dart-json/dart-json-parser'
@ -31,6 +33,7 @@ async function main(): Promise<void> {
} }
class TestReporter { class TestReporter {
readonly maxAnnotationsPerBatch = 50
readonly artifact = core.getInput('artifact', {required: false}) readonly artifact = core.getInput('artifact', {required: false})
readonly name = core.getInput('name', {required: true}) readonly name = core.getInput('name', {required: true})
readonly path = core.getInput('path', {required: true}) readonly path = core.getInput('path', {required: true})
@ -60,7 +63,7 @@ class TestReporter {
return return
} }
if (isNaN(this.maxAnnotations) || this.maxAnnotations < 0 || this.maxAnnotations > 50) { if (isNaN(this.maxAnnotations) || this.maxAnnotations > 50) {
core.setFailed(`Input parameter 'max-annotations' has invalid value`) core.setFailed(`Input parameter 'max-annotations' has invalid value`)
return return
} }
@ -185,7 +188,7 @@ class TestReporter {
const icon = isFailed ? Icon.fail : Icon.success const icon = isFailed ? Icon.fail : Icon.success
core.info(`Updating check run conclusion (${conclusion}) and output`) core.info(`Updating check run conclusion (${conclusion}) and output`)
const resp = await this.octokit.rest.checks.update({ const resp = await this.handleAnnotations(annotations, {
check_run_id: createResp.data.id, check_run_id: createResp.data.id,
conclusion, conclusion,
status: 'completed', status: 'completed',
@ -225,6 +228,30 @@ class TestReporter {
throw new Error(`Input variable 'reporter' is set to invalid value '${reporter}'`) throw new Error(`Input variable 'reporter' is set to invalid value '${reporter}'`)
} }
} }
private handleAnnotations = async (
annotations: Annotation[],
requestParams: RestEndpointMethodTypes['checks']['update']['parameters']
): Promise<Annotation[]> => {
const leftAnnotations = [...annotations]
let response: Promise<RestEndpointMethodTypes['checks']['update']['response']>
while (leftAnnotations.length > 0) {
const toProcess = leftAnnotations.splice(0, 50)
const status = leftAnnotations.length > 0 ? 'in_progress' : 'completed'
response = await this.updateAnnotation(toProcess, {...requestParams, status})
}
return response
}
private updateAnnotation = async (
annotations: Annotation[],
requestParams: RestEndpointMethodTypes['checks']['update']['parameters']
): Promise<RestEndpointMethodTypes['checks']['update']['response']> => {
return await this.octokit.rest.checks.update({
...requestParams,
output: {...requestParams.output, annotations}
})
}
} }
main() main()

View file

@ -2,7 +2,7 @@ import {ellipsis, fixEol} from '../utils/markdown-utils'
import {TestRunResult} from '../test-results' import {TestRunResult} from '../test-results'
import {getFirstNonEmptyLine} from '../utils/parse-utils' import {getFirstNonEmptyLine} from '../utils/parse-utils'
type Annotation = { export type Annotation = {
path: string path: string
start_line: number start_line: number
end_line: number end_line: number