Skip to content

Commit 47b8df8

Browse files
maciejtutakMaciej Tutak
and
Maciej Tutak
authored
Hide details when report exceeds size limit (#278)
* Truncate coverage report details if exceeds Github size limit * Hide details if report size exceeds limit * Add missing test snapshot Co-authored-by: Maciej Tutak <[email protected]>
1 parent 8572f29 commit 47b8df8

File tree

9 files changed

+78
-14
lines changed

9 files changed

+78
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const GITHUB_MESSAGE_SIZE_LIMIT = 65535;

src/format/formatCoverage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import { JsonReport } from '../typings/JsonReport';
66
export const formatCoverage = (
77
headReport: JsonReport | undefined,
88
baseReport: JsonReport | undefined,
9-
threshold: number | undefined
9+
threshold: number | undefined,
10+
hideDetails: boolean | undefined
1011
): string => {
1112
if (headReport) {
1213
return getFormattedCoverage(
1314
parseSummary(headReport),
1415
baseReport ? parseSummary(baseReport) : undefined,
1516
parseDetails(headReport),
1617
baseReport ? parseDetails(baseReport) : undefined,
17-
threshold
18+
threshold,
19+
hideDetails
1820
);
1921
}
2022

src/format/getFormattedCoverage.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { formatCoverageDetails } from './details/formatCoverageDetails';
22
import { formatCoverageSummary } from './summary/formatCoverageSummary';
33
import { CoverageDetailsMap, CoverageSummary } from '../typings/Coverage';
4+
import { i18n } from '../utils/i18n';
45

56
export const getFormattedCoverage = (
67
headSummary: Array<CoverageSummary>,
78
baseSummary: Array<CoverageSummary> | undefined,
89
headDetails: CoverageDetailsMap,
910
baseDetails: CoverageDetailsMap | undefined,
10-
threshold: number | undefined
11+
threshold: number | undefined,
12+
hideDetails: boolean | undefined
1113
): string =>
1214
[
1315
formatCoverageSummary(headSummary, baseSummary, threshold),
14-
formatCoverageDetails(headDetails, baseDetails, threshold),
16+
!hideDetails
17+
? formatCoverageDetails(headDetails, baseDetails, threshold)
18+
: `> ${i18n('detailsHidden')}`,
1519
]
1620
.filter(Boolean)
1721
.join('\n');

src/format/strings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"readingCoverageFileFailed": "Failed reading coverage file. (Error: {{ error }})",
105105
"failedGettingCoverage": "Getting code coverage data failed."
106106
},
107+
"detailsHidden": ":warning: Details were not displayed: the report size has exceeded the limit." ,
107108
"summaryTitle": "Coverage report {{ dir }}",
108109
"newFilesCoverage": "Show new covered files :hatching_chick:",
109110
"decreasedCoverageFiles": "Show files with reduced coverage :small_red_triangle_down:",

src/stages/createReport.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { context } from '@actions/github';
22

33
import { getReportTag } from '../constants/getReportTag';
4+
import { GITHUB_MESSAGE_SIZE_LIMIT } from '../constants/GITHUB_MESSAGE_SIZE_LIMIT';
45
import { formatCoverage } from '../format/formatCoverage';
56
import { formatErrors } from '../format/formatErrors';
67
import { formatRunReport } from '../format/formatRunReport';
@@ -33,19 +34,42 @@ export const createReport = (
3334
const formattedErrors = formatErrors(errors);
3435

3536
const formattedThresholdResults = formatThresholdResults(thresholdResults);
36-
const coverage = formatCoverage(headReport, baseReport, undefined);
37+
const coverage = formatCoverage(headReport, baseReport, undefined, false);
3738
const runReport: TestRunReport = {
3839
title: i18n(headReport.success ? 'testsSuccess' : 'testsFail'),
3940
summary: getTestRunSummary(headReport),
4041
failures: getFailureDetails(headReport),
4142
};
4243
const formattedReport = formatRunReport(runReport);
43-
return {
44-
text: insertArgs(template, {
44+
45+
let templateText = insertArgs(template, {
46+
body: [
47+
formattedErrors,
48+
formattedThresholdResults,
49+
coverage,
50+
formattedReport,
51+
].join('\n'),
52+
dir: workingDirectory || '',
53+
tag: getReportTag(options),
54+
title: insertArgs(customTitle || i18n('summaryTitle'), {
55+
dir: workingDirectory ? `for \`${workingDirectory}\`` : '',
56+
}),
57+
sha: getSha(),
58+
});
59+
60+
if (templateText.length > GITHUB_MESSAGE_SIZE_LIMIT) {
61+
const reducedCoverage = formatCoverage(
62+
headReport,
63+
baseReport,
64+
undefined,
65+
true
66+
);
67+
68+
templateText = insertArgs(template, {
4569
body: [
4670
formattedErrors,
4771
formattedThresholdResults,
48-
coverage,
72+
reducedCoverage,
4973
formattedReport,
5074
].join('\n'),
5175
dir: workingDirectory || '',
@@ -54,7 +78,11 @@ export const createReport = (
5478
dir: workingDirectory ? `for \`${workingDirectory}\`` : '',
5579
}),
5680
sha: getSha(),
57-
}),
81+
});
82+
}
83+
84+
return {
85+
text: templateText,
5886
runReport,
5987
};
6088
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { GITHUB_MESSAGE_SIZE_LIMIT } from '../../src/constants/GITHUB_MESSAGE_SIZE_LIMIT';
2+
3+
describe('GITHUB_MESSAGE_SIZE_LIMIT', () => {
4+
it('should be 65535', () => {
5+
expect(GITHUB_MESSAGE_SIZE_LIMIT).toBe(65535);
6+
});
7+
});

tests/format/__snapshots__/formatCoverage.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`formatCoverage should display warning if hiding details 1`] = `
4+
"| <div title=\\"Status of coverage:&#10; 🟢 - ok&#10; 🟡 - slightly more than threshold&#10; 🔴 - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
5+
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
6+
| 🟢 | Statements | 81.82% | 27/33 |
7+
| 🟢 | Branches | 100% | 8/8 |
8+
| 🟢 | Functions | 63.64% | 7/11 |
9+
| 🟢 | Lines | 80.65% | 25/31 |
10+
> :warning: Details were not displayed: the report size has exceeded the limit."
11+
`;
12+
313
exports[`formatCoverage should format standard coverage 1`] = `
414
"| <div title=\\"Status of coverage:&#10; 🟢 - ok&#10; 🟡 - slightly more than threshold&#10; 🔴 - under the threshold\\">St.<sup>:grey_question:</sup></div> | Category | Percentage | Covered / Total |
515
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |

tests/format/formatCoverage.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@ import jsonReport from '../mock-data/jsonReport.json';
33

44
describe('formatCoverage', () => {
55
it('should format standard coverage', () => {
6-
expect(formatCoverage(jsonReport, jsonReport, 0.3)).toMatchSnapshot();
7-
expect(formatCoverage(jsonReport, undefined, 0.3)).toMatchSnapshot();
86
expect(
9-
formatCoverage(jsonReport, undefined, undefined)
7+
formatCoverage(jsonReport, jsonReport, 0.3, false)
8+
).toMatchSnapshot();
9+
expect(
10+
formatCoverage(jsonReport, undefined, 0.3, false)
11+
).toMatchSnapshot();
12+
expect(
13+
formatCoverage(jsonReport, undefined, undefined, false)
14+
).toMatchSnapshot();
15+
});
16+
17+
it('should display warning if hiding details', () => {
18+
expect(
19+
formatCoverage(jsonReport, jsonReport, 0.3, true)
1020
).toMatchSnapshot();
1121
});
1222

1323
it('should return empty string if no reports specified', () => {
14-
expect(formatCoverage(undefined, undefined, 0.3)).toBe('');
24+
expect(formatCoverage(undefined, undefined, 0.3, false)).toBe('');
1525
});
1626
});

tests/format/getFormattedCoverage.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('getFormattedCoverage', () => {
1111
undefined,
1212
parseDetails(jsonReport),
1313
undefined,
14-
undefined
14+
undefined,
15+
false
1516
)
1617
).toMatchSnapshot();
1718
});

0 commit comments

Comments
 (0)