diff --git a/src/constants/GITHUB_MESSAGE_SIZE_LIMIT.ts b/src/constants/GITHUB_MESSAGE_SIZE_LIMIT.ts new file mode 100644 index 00000000..6a33a60d --- /dev/null +++ b/src/constants/GITHUB_MESSAGE_SIZE_LIMIT.ts @@ -0,0 +1 @@ +export const GITHUB_MESSAGE_SIZE_LIMIT = 65535; diff --git a/src/format/formatCoverage.ts b/src/format/formatCoverage.ts index 828aab1a..036552a9 100644 --- a/src/format/formatCoverage.ts +++ b/src/format/formatCoverage.ts @@ -6,7 +6,8 @@ import { JsonReport } from '../typings/JsonReport'; export const formatCoverage = ( headReport: JsonReport | undefined, baseReport: JsonReport | undefined, - threshold: number | undefined + threshold: number | undefined, + hideDetails: boolean | undefined ): string => { if (headReport) { return getFormattedCoverage( @@ -14,7 +15,8 @@ export const formatCoverage = ( baseReport ? parseSummary(baseReport) : undefined, parseDetails(headReport), baseReport ? parseDetails(baseReport) : undefined, - threshold + threshold, + hideDetails ); } diff --git a/src/format/getFormattedCoverage.ts b/src/format/getFormattedCoverage.ts index b3d68ebe..1cc740f5 100644 --- a/src/format/getFormattedCoverage.ts +++ b/src/format/getFormattedCoverage.ts @@ -1,17 +1,21 @@ import { formatCoverageDetails } from './details/formatCoverageDetails'; import { formatCoverageSummary } from './summary/formatCoverageSummary'; import { CoverageDetailsMap, CoverageSummary } from '../typings/Coverage'; +import { i18n } from '../utils/i18n'; export const getFormattedCoverage = ( headSummary: Array, baseSummary: Array | undefined, headDetails: CoverageDetailsMap, baseDetails: CoverageDetailsMap | undefined, - threshold: number | undefined + threshold: number | undefined, + hideDetails: boolean | undefined ): string => [ formatCoverageSummary(headSummary, baseSummary, threshold), - formatCoverageDetails(headDetails, baseDetails, threshold), + !hideDetails + ? formatCoverageDetails(headDetails, baseDetails, threshold) + : `> ${i18n('detailsHidden')}`, ] .filter(Boolean) .join('\n'); diff --git a/src/format/strings.json b/src/format/strings.json index 42bf58af..6f09ef3c 100644 --- a/src/format/strings.json +++ b/src/format/strings.json @@ -104,6 +104,7 @@ "readingCoverageFileFailed": "Failed reading coverage file. (Error: {{ error }})", "failedGettingCoverage": "Getting code coverage data failed." }, + "detailsHidden": ":warning: Details were not displayed: the report size has exceeded the limit." , "summaryTitle": "Coverage report {{ dir }}", "newFilesCoverage": "Show new covered files :hatching_chick:", "decreasedCoverageFiles": "Show files with reduced coverage :small_red_triangle_down:", diff --git a/src/stages/createReport.ts b/src/stages/createReport.ts index 46fee41c..a97e84f4 100644 --- a/src/stages/createReport.ts +++ b/src/stages/createReport.ts @@ -1,6 +1,7 @@ import { context } from '@actions/github'; import { getReportTag } from '../constants/getReportTag'; +import { GITHUB_MESSAGE_SIZE_LIMIT } from '../constants/GITHUB_MESSAGE_SIZE_LIMIT'; import { formatCoverage } from '../format/formatCoverage'; import { formatErrors } from '../format/formatErrors'; import { formatRunReport } from '../format/formatRunReport'; @@ -33,19 +34,42 @@ export const createReport = ( const formattedErrors = formatErrors(errors); const formattedThresholdResults = formatThresholdResults(thresholdResults); - const coverage = formatCoverage(headReport, baseReport, undefined); + const coverage = formatCoverage(headReport, baseReport, undefined, false); const runReport: TestRunReport = { title: i18n(headReport.success ? 'testsSuccess' : 'testsFail'), summary: getTestRunSummary(headReport), failures: getFailureDetails(headReport), }; const formattedReport = formatRunReport(runReport); - return { - text: insertArgs(template, { + + let templateText = insertArgs(template, { + body: [ + formattedErrors, + formattedThresholdResults, + coverage, + formattedReport, + ].join('\n'), + dir: workingDirectory || '', + tag: getReportTag(options), + title: insertArgs(customTitle || i18n('summaryTitle'), { + dir: workingDirectory ? `for \`${workingDirectory}\`` : '', + }), + sha: getSha(), + }); + + if (templateText.length > GITHUB_MESSAGE_SIZE_LIMIT) { + const reducedCoverage = formatCoverage( + headReport, + baseReport, + undefined, + true + ); + + templateText = insertArgs(template, { body: [ formattedErrors, formattedThresholdResults, - coverage, + reducedCoverage, formattedReport, ].join('\n'), dir: workingDirectory || '', @@ -54,7 +78,11 @@ export const createReport = ( dir: workingDirectory ? `for \`${workingDirectory}\`` : '', }), sha: getSha(), - }), + }); + } + + return { + text: templateText, runReport, }; }; diff --git a/tests/constants/GITHUB_MESSAGE_SIZE_LIMIT.test.ts b/tests/constants/GITHUB_MESSAGE_SIZE_LIMIT.test.ts new file mode 100644 index 00000000..6ec7ea8e --- /dev/null +++ b/tests/constants/GITHUB_MESSAGE_SIZE_LIMIT.test.ts @@ -0,0 +1,7 @@ +import { GITHUB_MESSAGE_SIZE_LIMIT } from '../../src/constants/GITHUB_MESSAGE_SIZE_LIMIT'; + +describe('GITHUB_MESSAGE_SIZE_LIMIT', () => { + it('should be 65535', () => { + expect(GITHUB_MESSAGE_SIZE_LIMIT).toBe(65535); + }); +}); diff --git a/tests/format/__snapshots__/formatCoverage.test.ts.snap b/tests/format/__snapshots__/formatCoverage.test.ts.snap index 8806ab86..71f51912 100644 --- a/tests/format/__snapshots__/formatCoverage.test.ts.snap +++ b/tests/format/__snapshots__/formatCoverage.test.ts.snap @@ -1,5 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`formatCoverage should display warning if hiding details 1`] = ` +"|
St.:grey_question:
| Category | Percentage | Covered / Total | +| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: | +| 🟢 | Statements | 81.82% | 27/33 | +| 🟢 | Branches | 100% | 8/8 | +| 🟢 | Functions | 63.64% | 7/11 | +| 🟢 | Lines | 80.65% | 25/31 | +> :warning: Details were not displayed: the report size has exceeded the limit." +`; + exports[`formatCoverage should format standard coverage 1`] = ` "|
St.:grey_question:
| Category | Percentage | Covered / Total | | :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: | diff --git a/tests/format/formatCoverage.test.ts b/tests/format/formatCoverage.test.ts index f1672528..0e88465e 100644 --- a/tests/format/formatCoverage.test.ts +++ b/tests/format/formatCoverage.test.ts @@ -3,14 +3,24 @@ import jsonReport from '../mock-data/jsonReport.json'; describe('formatCoverage', () => { it('should format standard coverage', () => { - expect(formatCoverage(jsonReport, jsonReport, 0.3)).toMatchSnapshot(); - expect(formatCoverage(jsonReport, undefined, 0.3)).toMatchSnapshot(); expect( - formatCoverage(jsonReport, undefined, undefined) + formatCoverage(jsonReport, jsonReport, 0.3, false) + ).toMatchSnapshot(); + expect( + formatCoverage(jsonReport, undefined, 0.3, false) + ).toMatchSnapshot(); + expect( + formatCoverage(jsonReport, undefined, undefined, false) + ).toMatchSnapshot(); + }); + + it('should display warning if hiding details', () => { + expect( + formatCoverage(jsonReport, jsonReport, 0.3, true) ).toMatchSnapshot(); }); it('should return empty string if no reports specified', () => { - expect(formatCoverage(undefined, undefined, 0.3)).toBe(''); + expect(formatCoverage(undefined, undefined, 0.3, false)).toBe(''); }); }); diff --git a/tests/format/getFormattedCoverage.test.ts b/tests/format/getFormattedCoverage.test.ts index 6f3d02cc..4f4f70c6 100644 --- a/tests/format/getFormattedCoverage.test.ts +++ b/tests/format/getFormattedCoverage.test.ts @@ -11,7 +11,8 @@ describe('getFormattedCoverage', () => { undefined, parseDetails(jsonReport), undefined, - undefined + undefined, + false ) ).toMatchSnapshot(); });