forked from ScaCap/action-surefire-report
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.js
174 lines (134 loc) · 6.11 KB
/
action.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const { retry } = require("@octokit/plugin-retry");
const { Octokit } = require("@octokit/core");
const RetryingOctokit = Octokit.plugin(retry);
const { parseTestReports, formatMilliseconds, partition, unique } = require('./utils.js');
const MAX_ANNOTATIONS_PER_REQUEST = 50;
const addAnnotations = async (check, title, partitions) => {
const octokit = new RetryingOctokit({auth: core.getInput('github_token'), request: { retries: 3 }});
let total = 0;
for (let i = 0; i < partitions.length; i++) {
const updateCheckRequest = {
...github.context.repo,
check_run_id: check.id,
output: {
title: check.output.title || title,
summary: check.output.summary || title,
text: check.output.text || title,
annotations: partitions[i],
}
};
await octokit.rest.checks.update(updateCheckRequest);
total += partitions[i].length;
core.debug(`Update request for check ${check.id} is ${JSON.stringify(updateCheckRequest)}`);
}
core.info(`Added ${total} annotations to ${check.id}`);
}
const action = async () => {
const reportPaths = core.getInput('report_paths');
core.info(`Going to parse results form ${reportPaths}`);
const check_name = core.getInput('check_name');
const commit = core.getInput('commit');
const failIfEmpty = (core.getInput('fail_if_empty') || "false") === "true";
const showSkipped = (core.getInput('show_skipped') || "false") === "true";
const updateExistingCheck = (core.getInput('update_existing_check') || "false") === "true";
const removeDuplicates = (core.getInput('remove_duplicates') || "true") === "true";
const skipPublishing = core.getInput('skip_publishing') === 'true';
core.info(`Running action with failIfEmpty: ${failIfEmpty}, showSkipped: ${showSkipped}, updateExistingCheck: ${updateExistingCheck}`)
let { total, passed, failed, ignored, skipped, annotations, durationMs, errors } = await parseTestReports(reportPaths, showSkipped);
const foundResults = total > 0 || !failIfEmpty;
if (errors.length > 0) {
errors.forEach(error => core.debug(`Could not fully parse ${error.file}: ${error.error}`));
}
if (skipPublishing) {
core.info('Not publishing test result due to skip_publishing=true');
for (const annotation of annotations) {
const properties = {
title: annotation.title,
file: annotation.path,
startLine: annotation.start_line,
endLine: annotation.end_line,
startColumn: annotation.start_column,
endColumn: annotation.end_column
};
core.error(annotation.message, properties);
}
return;
}
const octokit = github.getOctokit(core.getInput('github_token'), {request: fetch});
let stats = [
`${passed} passed`,
`${failed} failed`
];
if (showSkipped) {
stats.push(`${ignored} ignored`);
stats.push(`${skipped} skipped`);
}
const title = foundResults
? `${total} tests: ${stats.join(", ")}. Elapsed ${formatMilliseconds(durationMs)}.`
: 'No TestNG reports found!';
core.info(`Result: ${title}`);
const pullRequest = github.context.payload.pull_request;
const link = pullRequest && pullRequest.html_url || github.context.ref;
const conclusion = (foundResults && annotations.length === 0) ? 'success' : 'failure';
const status = 'completed';
const head_sha = commit || pullRequest && pullRequest.head.sha || github.context.sha;
let checkToUpdate;
if (removeDuplicates) {
const count = annotations.length;
annotations = unique(annotations, annotation => annotation.title);
core.info(`Removed ${annotations.length - count} duplicates.`)
}
let annotationsPartitioned = partition(annotations, MAX_ANNOTATIONS_PER_REQUEST);
console.debug(`Partitioned ${annotations.length} annotations into ${annotationsPartitioned.length} partitions`);
if (updateExistingCheck) {
core.info(`Updating check '${check_name}' with status '${status}' and conclusion '${conclusion}' to ${link} (sha: ${head_sha})`);
const listExistingChecks = {
...github.context.repo,
ref: head_sha,
check_name
};
const existingChecks = await octokit.rest.checks.listForRef(listExistingChecks);
if (!(existingChecks.data && existingChecks.data.check_runs.length == 1)) {
core.setFailed(`Could not find existing check '${check_name}'`);
}
const existingCheck = existingChecks.data.check_runs.slice(-1)[0];
core.info(`Found existing check with id ${existingCheck.id}`)
const output = {
title: title,
summary: existingCheck.output.summary || title,
text: existingCheck.output.text || title,
annotations: annotationsPartitioned[0]
};
const updateCheckRequest = {
...github.context.repo,
check_run_id: existingCheck.id,
output
};
core.debug(`Updating check ${existingCheck.id} with ${JSON.stringify(updateCheckRequest)}`);
await octokit.rest.checks.update(updateCheckRequest);
checkToUpdate = existingCheck;
}
else {
core.info(`Creating check '${check_name}' with status '${status}' and conclusion '${conclusion}' to ${link} (sha: ${head_sha})`);
const createCheckRequest = {
...github.context.repo,
name: check_name,
head_sha,
status,
conclusion,
output: {
title,
summary: '',
annotations: annotationsPartitioned[0]
}
};
let response = await octokit.rest.checks.create(createCheckRequest);
checkToUpdate = response.data;
}
if (annotationsPartitioned.length > 1) {
addAnnotations(checkToUpdate, annotationsPartitioned.slice(1));
}
};
module.exports = action;