generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 211
/
get-annotations.ts
107 lines (96 loc) · 2.7 KB
/
get-annotations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {ellipsis, fixEol} from '../utils/markdown-utils'
import {TestRunResult} from '../test-results'
import {getFirstNonEmptyLine} from '../utils/parse-utils'
type Annotation = {
path: string
start_line: number
end_line: number
start_column?: number
end_column?: number
annotation_level: 'notice' | 'warning' | 'failure'
message: string
title?: string
raw_details?: string
}
interface TestError {
testRunPaths: string[]
suiteName: string
testName: string
path: string
line: number
message: string
details: string
}
export function getAnnotations(results: TestRunResult[], maxCount: number): Annotation[] {
if (maxCount === 0) {
return []
}
// Collect errors from TestRunResults
// Merge duplicates if there are more test results files processed
const errors: TestError[] = []
const mergeDup = results.length > 1
for (const tr of results) {
for (const ts of tr.suites) {
for (const tg of ts.groups) {
for (const tc of tg.tests) {
const err = tc.error
if (err === undefined) {
continue
}
const path = err.path ?? tr.path
const line = err.line ?? 0
if (mergeDup) {
const dup = errors.find(e => path === e.path && line === e.line && err.details === e.details)
if (dup !== undefined) {
dup.testRunPaths.push(tr.path)
continue
}
}
errors.push({
testRunPaths: [tr.path],
suiteName: ts.name,
testName: tg.name ? `${tg.name} ► ${tc.name}` : tc.name,
details: err.details,
message: err.message ?? getFirstNonEmptyLine(err.details) ?? 'Test failed',
path,
line
})
}
}
}
}
// Limit number of created annotations
errors.splice(maxCount + 1)
const annotations = errors.map(e => {
const message = [
'Failed test found in:',
e.testRunPaths.map(p => ` ${p}`).join('\n'),
'Error:',
ident(fixEol(e.message), ' ')
].join('\n')
return enforceCheckRunLimits({
path: e.path,
start_line: e.line,
end_line: e.line,
annotation_level: 'failure',
title: `${e.suiteName} ► ${e.testName}`,
raw_details: fixEol(e.details),
message
})
})
return annotations
}
function enforceCheckRunLimits(err: Annotation): Annotation {
err.title = ellipsis(err.title || '', 255)
err.message = ellipsis(err.message, 65535)
if (err.raw_details) {
err.raw_details = ellipsis(err.raw_details, 65535)
}
return err
}
function ident(text: string, prefix: string): string {
return text
.split(/\n/g)
.map(line => prefix + line)
.join('\n')
}