generated from actions/typescript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathannotator.ts
207 lines (185 loc) · 6.57 KB
/
annotator.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as core from '@actions/core'
import {Annotation, TestResult} from './testParser.js'
import * as github from '@actions/github'
// eslint-disable-next-line import/extensions
import {SummaryTableRow} from '@actions/core/lib/summary.js'
// eslint-disable-next-line import/extensions
import {context, GitHub} from '@actions/github/lib/utils.js'
import {buildTable} from './utils.js'
export async function annotateTestResult(
testResult: TestResult,
token: string,
headSha: string,
checkAnnotations: boolean,
annotateOnly: boolean,
updateCheck: boolean,
annotateNotice: boolean,
jobName: string
): Promise<void> {
const annotations = testResult.globalAnnotations.filter(
annotation => annotateNotice || annotation.annotation_level !== 'notice'
)
const foundResults = testResult.totalCount > 0 || testResult.skipped > 0
let title = 'No test results found!'
if (foundResults) {
title = `${testResult.totalCount} tests run, ${testResult.passed} passed, ${testResult.skipped} skipped, ${testResult.failed} failed.`
}
core.info(`ℹ️ - ${testResult.checkName} - ${title}`)
const conclusion: 'success' | 'failure' = testResult.failed <= 0 ? 'success' : 'failure'
for (const annotation of annotations) {
core.info(` 🧪 - ${annotation.path} | ${annotation.message.split('\n', 1)[0]}`)
}
const octokit = github.getOctokit(token)
if (annotateOnly) {
// only create annotaitons, no check
for (const annotation of annotations) {
const properties: core.AnnotationProperties = {
title: annotation.title,
file: annotation.path,
startLine: annotation.start_line,
endLine: annotation.end_line,
startColumn: annotation.start_column,
endColumn: annotation.end_column
}
if (annotation.annotation_level === 'failure') {
core.error(annotation.message, properties)
} else if (annotation.annotation_level === 'warning') {
core.warning(annotation.message, properties)
} else if (annotateNotice) {
core.notice(annotation.message, properties)
}
}
} else {
// check status is being created, annotations are included in this (if not diasbled by "checkAnnotations")
if (updateCheck) {
const checks = await octokit.rest.checks.listForRef({
...github.context.repo,
ref: headSha,
check_name: jobName,
status: 'in_progress',
filter: 'latest'
})
core.debug(JSON.stringify(checks, null, 2))
const check_run_id = checks.data.check_runs[0].id
if (checkAnnotations) {
core.info(`ℹ️ - ${testResult.checkName} - Updating checks (Annotations: ${annotations.length})`)
for (let i = 0; i < annotations.length; i = i + 50) {
const sliced = annotations.slice(i, i + 50)
await updateChecks(octokit, check_run_id, title, testResult.summary, sliced)
}
} else {
core.info(`ℹ️ - ${testResult.checkName} - Updating checks (disabled annotations)`)
await updateChecks(octokit, check_run_id, title, testResult.summary, [])
}
} else {
const status: 'completed' | 'in_progress' | 'queued' | undefined = 'completed'
// don't send annotations if disabled
const adjustedAnnotations = checkAnnotations ? annotations : []
const createCheckRequest = {
...github.context.repo,
name: testResult.checkName,
head_sha: headSha,
status,
conclusion,
output: {
title,
summary: testResult.summary,
annotations: adjustedAnnotations.slice(0, 50)
}
}
core.debug(JSON.stringify(createCheckRequest, null, 2))
core.info(`ℹ️ - ${testResult.checkName} - Creating check (Annotations: ${adjustedAnnotations.length})`)
await octokit.rest.checks.create(createCheckRequest)
}
}
}
async function updateChecks(
octokit: InstanceType<typeof GitHub>,
check_run_id: number,
title: string,
summary: string,
annotations: Annotation[]
): Promise<void> {
const updateCheckRequest = {
...github.context.repo,
check_run_id,
output: {
title,
summary,
annotations
}
}
core.debug(JSON.stringify(updateCheckRequest, null, 2))
await octokit.rest.checks.update(updateCheckRequest)
}
export async function attachSummary(
table: SummaryTableRow[],
detailsTable: SummaryTableRow[],
flakySummary: SummaryTableRow[]
): Promise<void> {
if (table.length > 0) {
await core.summary.addTable(table).write()
}
if (detailsTable.length > 1) {
await core.summary.addTable(detailsTable).write()
}
if (flakySummary.length > 1) {
await core.summary.addTable(flakySummary).write()
}
}
export function buildCommentIdentifier(checkName: string[]): string {
return `<!-- Summary comment for ${JSON.stringify(checkName)} by mikepenz/action-junit-report -->`
}
export async function attachComment(
octokit: InstanceType<typeof GitHub>,
checkName: string[],
updateComment: boolean,
table: SummaryTableRow[],
detailsTable: SummaryTableRow[],
flakySummary: SummaryTableRow[]
): Promise<void> {
if (!context.issue.number) {
core.warning(`⚠️ Action requires a valid issue number (PR reference) to be able to attach a comment..`)
return
}
if (table.length === 0 && detailsTable.length === 0 && flakySummary.length === 0) {
core.debug(`Tables for comment were empty. 'skip_success_summary' enabled?`)
return
}
const identifier = buildCommentIdentifier(checkName)
let comment = buildTable(table)
if (detailsTable.length > 1) {
comment += '\n\n'
comment += buildTable(detailsTable)
}
if (flakySummary.length > 1) {
comment += '\n\n'
comment += buildTable(flakySummary)
}
comment += `\n\n${identifier}`
const priorComment = updateComment ? await findPriorComment(octokit, identifier) : undefined
if (priorComment) {
await octokit.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: priorComment,
body: comment
})
} else {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
})
}
}
async function findPriorComment(octokit: InstanceType<typeof GitHub>, identifier: string): Promise<number | undefined> {
const comments = await octokit.paginate(octokit.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
})
const foundComment = comments.find(comment => comment.body?.endsWith(identifier))
return foundComment?.id
}