-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.js
129 lines (118 loc) · 3.8 KB
/
reporter.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
const core = require('@actions/core');
const github = require('@actions/github');
async function reportMutationTesting(result, maxAnnotations) {
logResult(result);
core.setOutput('test-strength', result.testStrength);
core.setOutput('result', result.pass === true ? 'success' : 'failure');
await createCheck(result, maxAnnotations);
}
function logResult(result) {
core.info(generateResultSummary(result, false));
}
function generateResultSummary(result, markdown) {
if (markdown === true) {
return `## Mutations summary
| Measure | Value |
| :--- | ---: |
| Total | ${result.count} |
| Killed | ${result.killed} |
| Survived | ${result.survived} |
| No coverage | ${result.noCoverage} |
| Test strength | ${result.testStrength}% |
| Threshold | ${result.threshold}% |
| **Pass** | **${result.pass}** |`;
} else {
return `Mutations summary:
Total: ${result.count}
Killed: ${result.killed}
Survived: ${result.survived}
No coverage: ${result.noCoverage}
Test strength: ${result.testStrength}%
Threshold: ${result.threshold}%
Pass: ${result.pass}`;
}
}
async function createCheck(result, maxAnnotations) {
const { context } = github;
const { owner, repo } = context.repo;
const success = result.pass === true;
const conclusion = success ? 'success' : 'failure';
const summary = `Required: ${result.threshold}%, actual: ${result.testStrength}%`;
const strengthBadgeMarkDown = `![Test strength](https://img.shields.io/badge/Tests_strength-${result.testStrength}%25-lightgrey)`;
const resultBadgeMarkDown = success
? `![Mutation test success](https://img.shields.io/badge/Mutation_test-${conclusion}-brightgreen)`
: `![Mutation test failure](https://img.shields.io/badge/Mutation_test-${conclusion}-red)`;
const text = `${strengthBadgeMarkDown} ${resultBadgeMarkDown}
---
${generateResultSummary(result, true)}`;
let output;
if (success) {
output = {
title: `Test strength is good`,
summary: summary,
text: text,
};
} else {
output = {
title: `Test strength is too low`,
summary: summary,
text: text,
annotations: Array.isArray(result.mutations)
? result.mutations
.slice(0, maxAnnotations)
.map((mutation) => generateAnnotation(mutation))
: [],
};
}
try {
const octokit = github.getOctokit(core.getInput('token'));
const params = {
owner: owner,
repo: repo,
name: 'Mutation testing',
head_sha: getHeadSha(github.context),
status: 'completed',
conclusion: conclusion,
output: output,
};
const { data } = await octokit.rest.checks.create(params);
core.info(
`Check ${data.id} successfully created with conclusion ${conclusion}`
);
core.debug(`All params: ${JSON.stringify(params)}`);
core.debug(`Response: ${JSON.stringify(data)}`);
} catch (error) {
core.setFailed(`Error creating check: ${error.message}`);
}
}
function getHeadSha(context) {
const eventName = context.eventName;
const payload = context.payload;
switch (eventName) {
case 'pull_request':
return payload.pull_request.head.sha;
case 'workflow_run':
return payload.workflow_run.head_sha;
default:
return context.sha;
}
}
function generateAnnotation(mutation) {
return {
path: mutation.file,
start_line: mutation.line,
end_line: mutation.line,
annotation_level: 'warning',
message: `Mutation status: ${mutation.status}
Class: ${mutation.class}
Method: ${mutation.method}
Description: ${mutation.info}`,
};
}
module.exports = {
reportMutationTesting,
logResult,
createCheck,
getHeadSha,
generateResultSummary,
};