forked from romeovs/lcov-reporter-action
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcomment.js
More file actions
160 lines (140 loc) · 4.51 KB
/
comment.js
File metadata and controls
160 lines (140 loc) · 4.51 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
import { details, summary, b, fragment, table, tbody, tr, th } from "./html";
import { percentage } from "./lcov";
import { tabulate } from "./tabulate";
/**
* Compares two arrays of objects and returns with unique lines update
* @param {number} pdiff value from diff percentage
* @returns {string} emoji string for negative/positive pdiff
*/
const renderEmoji = pdiff => {
if (pdiff.toFixed(2) < 0) return "❌";
return "✅";
};
/**
* Compares two arrays of objects and returns with unique lines update
* @param {Array} otherArray
* @returns {Function} function with filtering non original lines
*/
const comparer = otherArray => current =>
otherArray.filter(
other =>
other.lines.found === current.lines.found &&
other.lines.hit === current.lines.hit,
).length === 0;
/**
* Github comment for monorepo
* @param {Array<{packageName, lcovPath}>} lcovArrayForMonorepo
* @param {{Array<{packageName, lcovBasePath}>}} lcovBaseArrayForMonorepo
* @param {*} options
*/
const commentForMonorepo = (
lcovArrayForMonorepo,
lcovBaseArrayForMonorepo,
options,
) => {
const { base } = options;
const html = lcovArrayForMonorepo.map(lcovObj => {
const baseLcov = lcovBaseArrayForMonorepo.find(
el => el.packageName === lcovObj.packageName,
);
const pbefore = baseLcov ? percentage(baseLcov.lcov) : 0;
const pafter = baseLcov ? percentage(lcovObj.lcov) : 0;
const pdiff = pafter - pbefore;
const plus = pdiff > 0 ? "+" : "";
let arrow = "";
if (pdiff < 0) {
arrow = "▾";
} else if (pdiff > 0) {
arrow = "▴";
}
const pdiffHtml = baseLcov
? th(
renderEmoji(pdiff),
" ",
arrow,
" ",
plus,
pdiff.toFixed(2),
"%",
)
: "";
let report = lcovObj.lcov;
if (baseLcov) {
const onlyInLcov = lcovObj.lcov.filter(comparer(baseLcov.lcov));
const onlyInBefore = baseLcov.lcov.filter(comparer(lcovObj.lcov));
report = onlyInBefore.concat(onlyInLcov);
}
return `${table(
tbody(
tr(
th(lcovObj.packageName),
th(percentage(lcovObj.lcov).toFixed(2), "%"),
pdiffHtml,
),
),
)} \n\n ${details(
summary("Coverage Report"),
tabulate(report, options),
)} <br/>`;
});
const title = `Coverage after merging into ${b(base)} <p></p>`;
return fragment(title, html.join(""));
};
/**
* Github comment for single repo
* @param {raw lcov} lcov
* @param {*} options
*/
const comment = (lcov, before, options) => {
const { appName, base } = options;
const pbefore = before ? percentage(before) : 0;
const pafter = before ? percentage(lcov) : 0;
const pdiff = pafter - pbefore;
const plus = pdiff > 0 ? "+" : "";
let arrow = "";
if (pdiff < 0) {
arrow = "▾";
} else if (pdiff > 0) {
arrow = "▴";
}
const pdiffHtml = before
? th(renderEmoji(pdiff), " ", arrow, " ", plus, pdiff.toFixed(2), "%")
: "";
let report = lcov;
if (before) {
const onlyInLcov = lcov.filter(comparer(before));
const onlyInBefore = before.filter(comparer(lcov));
report = onlyInBefore.concat(onlyInLcov);
}
const title = `Coverage after merging into ${b(base)} <p></p>`;
const header = appName
? tbody(
tr(th(appName), th(percentage(lcov).toFixed(2), "%"), pdiffHtml),
)
: tbody(tr(th(percentage(lcov).toFixed(2), "%"), pdiffHtml));
return fragment(
title,
table(header),
"\n\n",
details(summary("Coverage Report"), tabulate(report, options)),
);
};
/**
* Diff in coverage percentage for single repo
* @param {raw lcov} lcov
* @param {raw base lcov} before
* @param {*} options
*/
export const diff = (lcov, before, options) => comment(lcov, before, options);
/**
* Diff in coverage percentage for monorepo
* @param {Array<{packageName, lcovPath}>} lcovArrayForMonorepo
* @param {{Array<{packageName, lcovBasePath}>}} lcovBaseArrayForMonorepo
* @param {*} options
*/
export const diffForMonorepo = (
lcovArrayForMonorepo,
lcovBaseArrayForMonorepo,
options,
) =>
commentForMonorepo(lcovArrayForMonorepo, lcovBaseArrayForMonorepo, options);