Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add descriptive check title & pull request comment summary #65

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ inputs:
description: 'Pull request number associated with the report. This property should be used when workflow trigger is different than pull_request.'
required: false
default: ''
pull_request_comment:
description: 'Enable pull request comment with coverage report'
required: true
default: true
report_name:
description: 'Use a unique name for the report and comment.'
required: false
default: ''
default: 'Coverage Report'
check_name:
description: 'Use a unique name for the commit check name.'
required: false
default: 'coverage'
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
40 changes: 26 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18168,6 +18168,9 @@ async function action(payload) {
core.error("Found no commit.");
return;
}
const pullRequestComment = JSON.parse(
core.getInput("pull_request_comment", { required: true })
);

const path = core.getInput("path", { required: true });
const skipCovered = JSON.parse(
Expand Down Expand Up @@ -18203,14 +18206,17 @@ async function action(payload) {
const onlyChangedFiles = JSON.parse(
core.getInput("only_changed_files", { required: true })
);
const reportName = core.getInput("report_name", { required: false });
const reportName =
core.getInput("report_name", { required: false }) || "Coverage Report";
const checkName =
core.getInput("check_name", { required: false }) || "coverage";

const changedFiles = onlyChangedFiles
? await listChangedFiles(pullRequestNumber)
: null;

const reports = await processCoverage(path, { skipCovered });
const comment = markdownReport(reports, commit, {
const [checkBody, comment] = markdownReport(reports, commit, {
minimumCoverage,
showLine,
showBranch,
Expand All @@ -18226,13 +18232,20 @@ async function action(payload) {
const belowThreshold = reports.some(
(report) => Math.floor(report.total) < minimumCoverage
);

if (pullRequestNumber) {
let reportTitle = reportName;
// TODO: Figure out a good title for more than one report
if (reports.length === 1) {
const _sign = belowThreshold ? "<" : ">=";
const _actual = Math.floor(reports[0].total);
reportTitle = `Coverage: ${_actual}% (actual) ${_sign} ${minimumCoverage}% (expected)`;
}
if (pullRequestNumber && pullRequestComment) {
await addComment(pullRequestNumber, comment, reportName);
}
await addCheck(
comment,
reportName,
checkName,
checkBody,
reportTitle,
commit,
failBelowThreshold ? (belowThreshold ? "failure" : "success") : "neutral"
);
Expand Down Expand Up @@ -18320,6 +18333,7 @@ function markdownReport(reports, commit, options) {
// Setup files
const files = [];
let output = "";
let structuredOutput = "";
for (const report of reports) {
const folder = reports.length <= 1 ? "" : ` ${report.folder}`;
for (const file of report.files.filter(
Expand Down Expand Up @@ -18390,23 +18404,23 @@ function markdownReport(reports, commit, options) {
return `| ${row.filter(Boolean).join(" | ")} |`;
})
.join("\n");
const titleText = `<strong>${reportName}${folder}</strong>`;
const titleText = `<strong>${reportName}${folder} - ${total}%</strong>`;
output += `${titleText}\n\n${table}\n\n`;
structuredOutput += `<details><summary>${titleText}</summary>\n\n${table}\n\n</details>\n\n`;
}
const minimumCoverageText = `_Minimum allowed coverage is \`${minimumCoverage}%\`_`;
const footerText = `<p align="right">${credits} against ${commit} </p>`;
output += `${minimumCoverageText}\n\n${footerText}`;
return output;
return [output, structuredOutput];
}

async function addComment(pullRequestNumber, body, reportName) {
const comments = await client.rest.issues.listComments({
issue_number: pullRequestNumber,
...github.context.repo,
});
const commentFilter = reportName ? reportName : credits;
const comment = comments.data.find((comment) =>
comment.body.includes(commentFilter)
comment.body.includes(reportName)
);
if (comment != null) {
await client.rest.issues.updateComment({
Expand All @@ -18423,16 +18437,14 @@ async function addComment(pullRequestNumber, body, reportName) {
}
}

async function addCheck(body, reportName, sha, conclusion) {
const checkName = reportName ? reportName : "coverage";

async function addCheck(checkName, body, checkTitle, sha, conclusion) {
await client.rest.checks.create({
name: checkName,
head_sha: sha,
status: "completed",
conclusion: conclusion,
output: {
title: checkName,
title: checkTitle,
summary: body,
},
...github.context.repo,
Expand Down
40 changes: 26 additions & 14 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ async function action(payload) {
core.error("Found no commit.");
return;
}
const pullRequestComment = JSON.parse(
core.getInput("pull_request_comment", { required: true })
);

const path = core.getInput("path", { required: true });
const skipCovered = JSON.parse(
Expand Down Expand Up @@ -49,14 +52,17 @@ async function action(payload) {
const onlyChangedFiles = JSON.parse(
core.getInput("only_changed_files", { required: true })
);
const reportName = core.getInput("report_name", { required: false });
const reportName =
core.getInput("report_name", { required: false }) || "Coverage Report";
const checkName =
core.getInput("check_name", { required: false }) || "coverage";

const changedFiles = onlyChangedFiles
? await listChangedFiles(pullRequestNumber)
: null;

const reports = await processCoverage(path, { skipCovered });
const comment = markdownReport(reports, commit, {
const [checkBody, comment] = markdownReport(reports, commit, {
minimumCoverage,
showLine,
showBranch,
Expand All @@ -72,13 +78,20 @@ async function action(payload) {
const belowThreshold = reports.some(
(report) => Math.floor(report.total) < minimumCoverage
);

if (pullRequestNumber) {
let reportTitle = reportName;
// TODO: Figure out a good title for more than one report
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideas/suggestions welcome 🙂

if (reports.length === 1) {
const _sign = belowThreshold ? "<" : ">=";
const _actual = Math.floor(reports[0].total);
reportTitle = `Coverage: ${_actual}% (actual) ${_sign} ${minimumCoverage}% (expected)`;
}
if (pullRequestNumber && pullRequestComment) {
await addComment(pullRequestNumber, comment, reportName);
}
await addCheck(
comment,
reportName,
checkName,
checkBody,
reportTitle,
commit,
failBelowThreshold ? (belowThreshold ? "failure" : "success") : "neutral"
);
Expand Down Expand Up @@ -166,6 +179,7 @@ function markdownReport(reports, commit, options) {
// Setup files
const files = [];
let output = "";
let structuredOutput = "";
for (const report of reports) {
const folder = reports.length <= 1 ? "" : ` ${report.folder}`;
for (const file of report.files.filter(
Expand Down Expand Up @@ -236,23 +250,23 @@ function markdownReport(reports, commit, options) {
return `| ${row.filter(Boolean).join(" | ")} |`;
})
.join("\n");
const titleText = `<strong>${reportName}${folder}</strong>`;
const titleText = `<strong>${reportName}${folder} - ${total}%</strong>`;
output += `${titleText}\n\n${table}\n\n`;
structuredOutput += `<details><summary>${titleText}</summary>\n\n${table}\n\n</details>\n\n`;
}
const minimumCoverageText = `_Minimum allowed coverage is \`${minimumCoverage}%\`_`;
const footerText = `<p align="right">${credits} against ${commit} </p>`;
output += `${minimumCoverageText}\n\n${footerText}`;
return output;
return [output, structuredOutput];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second output value is the one that contains the <summary/> block best suited for a PR comment:
Screen Shot 2021-12-22 at 6 25 36 PM
Screen Shot 2021-12-22 at 6 25 45 PM

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see this being an additional input parameter for the action, possibly called collapse_pr_comment: bool (or something like that)

}

async function addComment(pullRequestNumber, body, reportName) {
const comments = await client.rest.issues.listComments({
issue_number: pullRequestNumber,
...github.context.repo,
});
const commentFilter = reportName ? reportName : credits;
const comment = comments.data.find((comment) =>
comment.body.includes(commentFilter)
comment.body.includes(reportName)
);
if (comment != null) {
await client.rest.issues.updateComment({
Expand All @@ -269,16 +283,14 @@ async function addComment(pullRequestNumber, body, reportName) {
}
}

async function addCheck(body, reportName, sha, conclusion) {
const checkName = reportName ? reportName : "coverage";

async function addCheck(checkName, body, checkTitle, sha, conclusion) {
await client.rest.checks.create({
name: checkName,
head_sha: sha,
status: "completed",
conclusion: conclusion,
output: {
title: checkName,
title: checkTitle,
Copy link
Author

@jarojasm95 jarojasm95 Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this distinction between check name and title is so that the title can be more descriptive and look like this:
Screen Shot 2021-12-22 at 6 23 29 PM

summary: body,
},
...github.context.repo,
Expand Down
Loading