Skip to content

Commit 8edff1e

Browse files
Added emojies, lines diff checker, app name (#19)
* added colors * build * added emojies * added app name * added app name to header * added app name to header added th * clean-up * extended hiddenHeader * extended hiddenHeader * review changes * changed emoji to use checkmark * changed emoji to use cross mark * changed emoji to use cross mark
1 parent df09558 commit 8edff1e

File tree

5 files changed

+131
-48
lines changed

5 files changed

+131
-48
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ inputs:
1717
lcov-base:
1818
description: The location of the lcov file for the base branch. Applicable for single repo setup.
1919
required: false
20+
app-name:
21+
description: App name to display on comment
22+
required: false
2023
runs:
2124
using: node12
2225
main: dist/main.js

dist/main.js

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6024,6 +6024,28 @@ function uncovered(file, options) {
60246024
.join(", ");
60256025
}
60266026

6027+
/**
6028+
* Compares two arrays of objects and returns with unique lines update
6029+
* @param {number} pdiff value from diff percentage
6030+
* @returns {string} emoji string for negative/positive pdiff
6031+
*/
6032+
const renderEmoji = pdiff => {
6033+
if (pdiff.toFixed(2) < 0) return "❌";
6034+
return "✅";
6035+
};
6036+
6037+
/**
6038+
* Compares two arrays of objects and returns with unique lines update
6039+
* @param {Array} otherArray
6040+
* @returns {Function} function with filtering non original lines
6041+
*/
6042+
const comparer = otherArray => current =>
6043+
otherArray.filter(
6044+
other =>
6045+
other.lines.found === current.lines.found &&
6046+
other.lines.hit === current.lines.hit,
6047+
).length === 0;
6048+
60276049
/**
60286050
* Github comment for monorepo
60296051
* @param {Array<{packageName, lcovPath}>} lcovArrayForMonorepo
@@ -6035,6 +6057,7 @@ function commentForMonorepo(
60356057
lcovBaseArrayForMonorepo,
60366058
options,
60376059
) {
6060+
const { base } = options;
60386061
const html = lcovArrayForMonorepo.map(lcovObj => {
60396062
const baseLcov = lcovBaseArrayForMonorepo.find(
60406063
el => el.packageName === lcovObj.packageName,
@@ -6046,9 +6069,14 @@ function commentForMonorepo(
60466069
const plus = pdiff > 0 ? "+" : "";
60476070
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";
60486071

6049-
const pdiffHtml = baseLcov
6050-
? th(arrow, " ", plus, pdiff.toFixed(2), "%")
6051-
: "";
6072+
const pdiffHtml = baseLcov ? th(renderEmoji(pdiff), " ", arrow, " ", plus, pdiff.toFixed(2), "%") : "";
6073+
let report = lcovObj.lcov;
6074+
6075+
if (baseLcov) {
6076+
const onlyInLcov = lcovObj.lcov.filter(comparer(baseLcov));
6077+
const onlyInBefore = baseLcov.filter(comparer(lcovObj.lcov));
6078+
report = onlyInBefore.concat(onlyInLcov);
6079+
}
60526080

60536081
return `${table(
60546082
tbody(
@@ -6060,14 +6088,13 @@ function commentForMonorepo(
60606088
),
60616089
)} \n\n ${details(
60626090
summary("Coverage Report"),
6063-
tabulate(lcovObj.lcov, options),
6091+
tabulate(report, options),
60646092
)} <br/>`;
60656093
});
60666094

6067-
return fragment(
6068-
`Coverage after merging into ${b(options.base)} <p></p>`,
6069-
html.join(""),
6070-
);
6095+
const title = `Coverage after merging into ${b(base)} <p></p>`;
6096+
6097+
return fragment(title, html.join(""));
60716098
}
60726099

60736100
/**
@@ -6076,21 +6103,31 @@ function commentForMonorepo(
60766103
* @param {*} options
60776104
*/
60786105
function comment(lcov, before, options) {
6106+
const { appName, base } = options;
60796107
const pbefore = before ? percentage(before) : 0;
60806108
const pafter = before ? percentage(lcov) : 0;
60816109
const pdiff = pafter - pbefore;
60826110
const plus = pdiff > 0 ? "+" : "";
60836111
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";
60846112

6085-
const pdiffHtml = before ? th(arrow, " ", plus, pdiff.toFixed(2), "%") : "";
6113+
const pdiffHtml = before ? th(renderEmoji(pdiff), " ", arrow, " ", plus, pdiff.toFixed(2), "%") : "";
6114+
6115+
let report = lcov;
6116+
6117+
if (before) {
6118+
const onlyInLcov = lcov.filter(comparer(before));
6119+
const onlyInBefore = before.filter(comparer(lcov));
6120+
report = onlyInBefore.concat(onlyInLcov);
6121+
}
6122+
6123+
const title = `Coverage after merging into ${b(base)} <p></p>`;
6124+
const header = appName ? tbody(tr(th(appName), th(percentage(lcov).toFixed(2), '%'), pdiffHtml)) : tbody(tr(th(percentage(lcov).toFixed(2), '%'), pdiffHtml));
60866125

60876126
return fragment(
6088-
`Coverage after merging ${b(options.head)} into ${b(
6089-
options.base,
6090-
)} <p></p>`,
6091-
table(tbody(tr(th(percentage(lcov).toFixed(2), "%"), pdiffHtml))),
6127+
title,
6128+
table(header),
60926129
"\n\n",
6093-
details(summary("Coverage Report"), tabulate(lcov, options)),
6130+
details(summary("Coverage Report"), tabulate(report, options)),
60946131
);
60956132
}
60966133

@@ -6135,11 +6172,10 @@ function diffForMonorepo(
61356172
// Every comment written by our action will have this hidden
61366173
// header on top, and will be used to identify which comments
61376174
// to update/delete etc
6138-
const hiddenHeader = `<!-- monorepo-jest-reporter-action -->`;
61396175

6140-
const appendHiddenHeaderToComment = body => hiddenHeader + body;
6176+
const appendHiddenHeaderToComment = (body, hiddenHeader) => hiddenHeader + body;
61416177

6142-
const listComments = async ({ client, context, prNumber, commentHeader }) => {
6178+
const listComments = async ({ client, context, prNumber, commentHeader, hiddenHeader }) => {
61436179
const { data: existingComments } = await client.issues.listComments({
61446180
...context.repo,
61456181
issue_number: prNumber,
@@ -6148,18 +6184,18 @@ const listComments = async ({ client, context, prNumber, commentHeader }) => {
61486184
return existingComments.filter(({ body }) => body.startsWith(hiddenHeader));
61496185
};
61506186

6151-
const insertComment = async ({ client, context, prNumber, body }) =>
6187+
const insertComment = async ({ client, context, prNumber, body }, hiddenHeader) =>
61526188
client.issues.createComment({
61536189
...context.repo,
61546190
issue_number: prNumber,
6155-
body: appendHiddenHeaderToComment(body),
6191+
body: appendHiddenHeaderToComment(body, hiddenHeader),
61566192
});
61576193

6158-
const updateComment = async ({ client, context, body, commentId }) =>
6194+
const updateComment = async ({ client, context, body, commentId }, hiddenHeader) =>
61596195
client.issues.updateComment({
61606196
...context.repo,
61616197
comment_id: commentId,
6162-
body: appendHiddenHeaderToComment(body),
6198+
body: appendHiddenHeaderToComment(body, hiddenHeader),
61636199
});
61646200

61656201
const deleteComments = async ({ client, context, comments }) =>
@@ -6172,11 +6208,12 @@ const deleteComments = async ({ client, context, comments }) =>
61726208
),
61736209
);
61746210

6175-
const upsertComment = async ({ client, context, prNumber, body }) => {
6211+
const upsertComment = async ({ client, context, prNumber, body, hiddenHeader }) => {
61766212
const existingComments = await listComments({
61776213
client,
61786214
context,
61796215
prNumber,
6216+
hiddenHeader,
61806217
});
61816218
const last = existingComments.pop();
61826219

@@ -6192,13 +6229,13 @@ const upsertComment = async ({ client, context, prNumber, body }) => {
61926229
context,
61936230
body,
61946231
commentId: last.id,
6195-
})
6232+
}, hiddenHeader)
61966233
: insertComment({
61976234
client,
61986235
context,
61996236
prNumber,
62006237
body,
6201-
});
6238+
}, hiddenHeader);
62026239
};
62036240

62046241
var github$2 = {
@@ -6256,6 +6293,7 @@ async function main() {
62566293
const token = core$1.getInput("github-token");
62576294
const lcovFile = core$1.getInput("lcov-file") || "./coverage/lcov.info";
62586295
const baseFile = core$1.getInput("lcov-base");
6296+
const appName = core$1.getInput("app-name");
62596297
// Add base path for monorepo
62606298
const monorepoBasePath = core$1.getInput("monorepo-base-path");
62616299

@@ -6309,6 +6347,7 @@ async function main() {
63096347
prefix: `${process.env.GITHUB_WORKSPACE}/`,
63106348
head: context.payload.pull_request.head.ref,
63116349
base: context.payload.pull_request.base.ref,
6350+
appName,
63126351
};
63136352

63146353
const lcov = !monorepoBasePath && (await parse$1(raw));
@@ -6327,6 +6366,7 @@ async function main() {
63276366
lcovBaseArrayForMonorepo,
63286367
options,
63296368
),
6369+
hiddenHeader: appName ? `<!-- ${appName}-code-coverage-assistant -->` : `<!-- monorepo-code-coverage-assistant -->`
63306370
});
63316371
}
63326372

src/comment.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@ import { details, summary, b, fragment, table, tbody, tr, th } from "./html";
22
import { percentage } from "./lcov";
33
import { tabulate } from "./tabulate";
44

5+
/**
6+
* Compares two arrays of objects and returns with unique lines update
7+
* @param {number} pdiff value from diff percentage
8+
* @returns {string} emoji string for negative/positive pdiff
9+
*/
10+
const renderEmoji = pdiff => {
11+
if (pdiff.toFixed(2) < 0) return "❌";
12+
return "✅";
13+
};
14+
15+
/**
16+
* Compares two arrays of objects and returns with unique lines update
17+
* @param {Array} otherArray
18+
* @returns {Function} function with filtering non original lines
19+
*/
20+
const comparer = otherArray => current =>
21+
otherArray.filter(
22+
other =>
23+
other.lines.found === current.lines.found &&
24+
other.lines.hit === current.lines.hit,
25+
).length === 0;
26+
527
/**
628
* Github comment for monorepo
729
* @param {Array<{packageName, lcovPath}>} lcovArrayForMonorepo
@@ -13,6 +35,7 @@ export function commentForMonorepo(
1335
lcovBaseArrayForMonorepo,
1436
options,
1537
) {
38+
const { base } = options;
1639
const html = lcovArrayForMonorepo.map(lcovObj => {
1740
const baseLcov = lcovBaseArrayForMonorepo.find(
1841
el => el.packageName === lcovObj.packageName,
@@ -24,9 +47,14 @@ export function commentForMonorepo(
2447
const plus = pdiff > 0 ? "+" : "";
2548
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";
2649

27-
const pdiffHtml = baseLcov
28-
? th(arrow, " ", plus, pdiff.toFixed(2), "%")
29-
: "";
50+
const pdiffHtml = baseLcov ? th(renderEmoji(pdiff), " ", arrow, " ", plus, pdiff.toFixed(2), "%") : "";
51+
let report = lcovObj.lcov;
52+
53+
if (baseLcov) {
54+
const onlyInLcov = lcovObj.lcov.filter(comparer(baseLcov));
55+
const onlyInBefore = baseLcov.filter(comparer(lcovObj.lcov));
56+
report = onlyInBefore.concat(onlyInLcov);
57+
}
3058

3159
return `${table(
3260
tbody(
@@ -38,14 +66,13 @@ export function commentForMonorepo(
3866
),
3967
)} \n\n ${details(
4068
summary("Coverage Report"),
41-
tabulate(lcovObj.lcov, options),
69+
tabulate(report, options),
4270
)} <br/>`;
4371
});
4472

45-
return fragment(
46-
`Coverage after merging into ${b(options.base)} <p></p>`,
47-
html.join(""),
48-
);
73+
const title = `Coverage after merging into ${b(base)} <p></p>`;
74+
75+
return fragment(title, html.join(""));
4976
}
5077

5178
/**
@@ -54,21 +81,31 @@ export function commentForMonorepo(
5481
* @param {*} options
5582
*/
5683
export function comment(lcov, before, options) {
84+
const { appName, base } = options;
5785
const pbefore = before ? percentage(before) : 0;
5886
const pafter = before ? percentage(lcov) : 0;
5987
const pdiff = pafter - pbefore;
6088
const plus = pdiff > 0 ? "+" : "";
6189
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";
6290

63-
const pdiffHtml = before ? th(arrow, " ", plus, pdiff.toFixed(2), "%") : "";
91+
const pdiffHtml = before ? th(renderEmoji(pdiff), " ", arrow, " ", plus, pdiff.toFixed(2), "%") : "";
92+
93+
let report = lcov;
94+
95+
if (before) {
96+
const onlyInLcov = lcov.filter(comparer(before));
97+
const onlyInBefore = before.filter(comparer(lcov));
98+
report = onlyInBefore.concat(onlyInLcov);
99+
}
100+
101+
const title = `Coverage after merging into ${b(base)} <p></p>`;
102+
const header = appName ? tbody(tr(th(appName), th(percentage(lcov).toFixed(2), '%'), pdiffHtml)) : tbody(tr(th(percentage(lcov).toFixed(2), '%'), pdiffHtml));
64103

65104
return fragment(
66-
`Coverage after merging ${b(options.head)} into ${b(
67-
options.base,
68-
)} <p></p>`,
69-
table(tbody(tr(th(percentage(lcov).toFixed(2), "%"), pdiffHtml))),
105+
title,
106+
table(header),
70107
"\n\n",
71-
details(summary("Coverage Report"), tabulate(lcov, options)),
108+
details(summary("Coverage Report"), tabulate(report, options)),
72109
);
73110
}
74111

src/github.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
// Every comment written by our action will have this hidden
1212
// header on top, and will be used to identify which comments
1313
// to update/delete etc
14-
const hiddenHeader = `<!-- monorepo-jest-reporter-action -->`;
1514

16-
const appendHiddenHeaderToComment = body => hiddenHeader + body;
15+
const appendHiddenHeaderToComment = (body, hiddenHeader) => hiddenHeader + body;
1716

18-
const listComments = async ({ client, context, prNumber, commentHeader }) => {
17+
const listComments = async ({ client, context, prNumber, commentHeader, hiddenHeader }) => {
1918
const { data: existingComments } = await client.issues.listComments({
2019
...context.repo,
2120
issue_number: prNumber,
@@ -24,18 +23,18 @@ const listComments = async ({ client, context, prNumber, commentHeader }) => {
2423
return existingComments.filter(({ body }) => body.startsWith(hiddenHeader));
2524
};
2625

27-
const insertComment = async ({ client, context, prNumber, body }) =>
26+
const insertComment = async ({ client, context, prNumber, body }, hiddenHeader) =>
2827
client.issues.createComment({
2928
...context.repo,
3029
issue_number: prNumber,
31-
body: appendHiddenHeaderToComment(body),
30+
body: appendHiddenHeaderToComment(body, hiddenHeader),
3231
});
3332

34-
const updateComment = async ({ client, context, body, commentId }) =>
33+
const updateComment = async ({ client, context, body, commentId }, hiddenHeader) =>
3534
client.issues.updateComment({
3635
...context.repo,
3736
comment_id: commentId,
38-
body: appendHiddenHeaderToComment(body),
37+
body: appendHiddenHeaderToComment(body, hiddenHeader),
3938
});
4039

4140
const deleteComments = async ({ client, context, comments }) =>
@@ -48,11 +47,12 @@ const deleteComments = async ({ client, context, comments }) =>
4847
),
4948
);
5049

51-
const upsertComment = async ({ client, context, prNumber, body }) => {
50+
const upsertComment = async ({ client, context, prNumber, body, hiddenHeader }) => {
5251
const existingComments = await listComments({
5352
client,
5453
context,
5554
prNumber,
55+
hiddenHeader,
5656
});
5757
const last = existingComments.pop();
5858

@@ -68,13 +68,13 @@ const upsertComment = async ({ client, context, prNumber, body }) => {
6868
context,
6969
body,
7070
commentId: last.id,
71-
})
71+
}, hiddenHeader)
7272
: insertComment({
7373
client,
7474
context,
7575
prNumber,
7676
body,
77-
});
77+
}, hiddenHeader);
7878
};
7979

8080
module.exports = {

0 commit comments

Comments
 (0)