From 5460632ba901fb1e128157b016ae1e9694de95cf Mon Sep 17 00:00:00 2001 From: Eli Reisman Date: Mon, 3 Jun 2024 19:17:51 -0700 Subject: [PATCH] WIP: summary test --- __tests__/summary.test.ts | 34 +++++++++++++++++++++++++++++++++- src/summary.ts | 6 +++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/__tests__/summary.test.ts b/__tests__/summary.test.ts index 870419016..110ba0f7c 100644 --- a/__tests__/summary.test.ts +++ b/__tests__/summary.test.ts @@ -1,5 +1,5 @@ import {expect, jest, test} from '@jest/globals' -import {Changes, ConfigurationOptions, Scorecard} from '../src/schemas' +import {Change, Changes, ConfigurationOptions, Scorecard} from '../src/schemas' import * as summary from '../src/summary' import * as core from '@actions/core' import {createTestChange} from './fixtures/create-test-change' @@ -109,6 +109,38 @@ test('prints headline as h1', () => { expect(text).toContain('

Dependency Review

') }) +test('returns minimal summary in case the core.summary is too large for a PR comment', () => { + let changes: Changes = [ + createTestChange({name: 'lodash', version: '1.2.3'}), + createTestChange({name: 'colors', version: '2.3.4'}), + createTestChange({name: '@foo/bar', version: '*'}), + ] + + let minSummary: string = summary.addSummaryToSummary( + changes, + emptyInvalidLicenseChanges, + emptyChanges, + scorecard, + defaultConfig + ) + + // side effect DR report into core.summary as happens in main.ts + summary.addScannedDependencies(changes) + const text = core.summary.stringify() + + expect(text).toContain('

Dependency Review

') + expect(minSummary).toContain('# Dependency Review') + + expect(text).toContain('lodash') + expect(text).toContain('colors') + expect(text).toContain('@foo/bar') + expect(minSummary).not.toContain('lodash') + expect(minSummary).not.toContain('colors') + expect(minSummary).not.toContain('@foo/bar') + + expect(text.length).toBeGreaterThan(minSummary.length) +}) + test('only includes "No vulnerabilities or license issues found"-message if both are configured and nothing was found', () => { summary.addSummaryToSummary( emptyChanges, diff --git a/src/summary.ts b/src/summary.ts index 02e3d2698..f9545bcde 100644 --- a/src/summary.ts +++ b/src/summary.ts @@ -10,9 +10,9 @@ const icons = { warning: '⚠️' } -// generates the initial DR summmary and applies to the Action's core.summary -// returns a string array of all the formatted values in the event we need -// to replace them in the PR comment due to length restrictions +// generates the DR report summmary and caches it to the Action's core.summary. +// returns the DR summary string, ready to be posted as a PR comment if the +// final DR report is too large export function addSummaryToSummary( vulnerableChanges: Changes, invalidLicenseChanges: InvalidLicenseChanges,