forked from ashley-taylor/junit-report-annotations-action
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (141 loc) · 5.2 KB
/
Copy pathindex.js
File metadata and controls
160 lines (141 loc) · 5.2 KB
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
const core = require('@actions/core');
const github = require('@actions/github');
const glob = require('@actions/glob');
const parser = require('xml-js');
const axios = require('axios');
const fs = require('fs');
(async () => {
try {
const path = core.getInput('path');
const includeSummary = core.getInput('includeSummary');
const numFailures = core.getInput('numFailures');
const accessToken = core.getInput('access-token');
const testSrcPath = core.getInput('testSrcPath');
const slackBotToken = core.getInput('slack-bot-token');
const slackChannelId = core.getInput('slack-channel-id');
const globber = await glob.create(path, {followSymbolicLinks: false});
let numTests = 0;
let numSkipped = 0;
let numFailed = 0;
let numErrored = 0;
let testDuration = 0;
let annotations = [];
const octokit = new github.GitHub(accessToken);
const req = {
...github.context.repo,
ref: github.context.sha
}
const res = await octokit.checks.listForRef(req);
const check_run_id = res.data.check_runs.filter(check => check.name === 'build')[0].id
core.debug(`Run id: ${check_run_id}`);
for await (const file of globber.globGenerator()) {
const data = await fs.promises.readFile(file);
var json = JSON.parse(parser.xml2json(data, {compact: true}));
// core.debug(`json: ${JSON.stringify(json)}`);
if (json.testsuite) {
const testsuite = json.testsuite;
testDuration += Number(testsuite._attributes.time);
numTests += Number(testsuite._attributes.tests);
numErrored += Number(testsuite._attributes.errors);
numFailed += Number(testsuite._attributes.failures);
numSkipped += Number(testsuite._attributes.skipped);
testFunction = async testcase => {
const problem = testcase.failure || testcase.error;
// core.debug(`Testcase: ${JSON.stringify(testcase)}, numFailures: ${numFailures}`);
if (problem) {
if (numFailures === "0" || annotations.length < numFailures) {
const name = testcase._attributes.classname;
const klass = name.replace(/$.*/g, '').replace(/\./g, '/');
const path = `${testSrcPath}${klass}.java`
const file = await fs.promises.readFile(path, {encoding: 'utf-8'});
//TODO: make this better won't deal with methods with arguments etc
let line = 0;
const lines = file.split('\n')
for (let i = 0; i < lines.length; i++) {
if (lines[i].indexOf(testcase._attributes.name) >= 0) {
line = i;
break;
}
}
const descriptor = testcase.failure ? 'Failure' : 'Error';
annotations.push({
path: path,
start_line: line,
end_line: line,
start_column: 0,
end_column: 1,
annotation_level: 'failure',
message: `Test ${testcase._attributes.name} ${descriptor} ${problem._attributes.message}`,
});
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
const arr = /:(\d+)\)/g.exec(problem._attributes.$t);
const lineNum = (arr && arr.length > 1) ? arr[1] : 0;
const branch = github.context.ref.replace("refs/heads/", "");
const message = problem._attributes.message
? problem._attributes.message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
: "No message found";
const slackMessage = "*Test " + descriptor
+ "* | <https://github.com/" + owner + "/" + repo + "/runs/" + check_run_id + "|" + owner + ":" + repo + "@" + branch + ">"
+ " - `" + testcase._attributes.name + "`:\n```" + message
+ "``` <https://github.com/" + owner + "/" + repo
+ "/blob/" + branch + "/" + path + "#L" + lineNum + "|" + path + ">";
core.debug(slackMessage);
await axios({
url: "https://slack.com/api/chat.postMessage",
method: "post",
headers: {
"Authorization": `Bearer ${slackBotToken}`,
"Content-type": "application/json"
},
data: {
"channel": slackChannelId,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": slackMessage
}
}
]
}
});
}
//add
}
}
if (Array.isArray(testsuite.testcase)) {
for (const testcase of testsuite.testcase) {
await testFunction(testcase)
}
} else {
//single test
await testFunction(testsuite.testcase)
}
}
}
const annotation_level = numFailed + numErrored > 0 ? 'failure' : 'notice';
const annotation = {
path: 'test',
start_line: 0,
end_line: 0,
start_column: 0,
end_column: 0,
annotation_level,
message: `Junit Results ran ${numTests} in ${testDuration} seconds ${numErrored} Errored, ${numFailed} Failed, ${numSkipped} Skipped`,
};
const update_req = {
...github.context.repo,
check_run_id,
output: {
title: "Junit Results",
summary: `Num passed etc`,
annotations: [annotation, ...annotations]
}
}
await octokit.checks.update(update_req);
} catch (error) {
core.setFailed(error.message);
}
})();