Properly truncate long summaries and catch errors

This commit is contained in:
Justin Holguín
2026-02-17 22:46:59 +00:00
committed by GitHub
parent 68e9887ce6
commit 2e1cf54a50
4 changed files with 58 additions and 15 deletions
+14 -3
View File
@@ -132,9 +132,13 @@ describe('handleLargeSummary', () => {
expect(result).toContain('actions/runs/12345') expect(result).toContain('actions/runs/12345')
}) })
test('returns original summary and logs a warning when artifact handling fails', async () => { test('returns truncated summary and replaces buffer when artifact upload fails', async () => {
const warningMock = core.warning as jest.Mock const warningMock = core.warning as jest.Mock
const emptyBufferMock = core.summary.emptyBuffer as jest.Mock
const addRawMock = core.summary.addRaw as jest.Mock
warningMock.mockClear() warningMock.mockClear()
emptyBufferMock.mockClear()
addRawMock.mockClear()
const largeSummary = 'b'.repeat(1024 * 1024 + 1) const largeSummary = 'b'.repeat(1024 * 1024 + 1)
DefaultArtifactClientMock.mockImplementation(() => ({ DefaultArtifactClientMock.mockImplementation(() => ({
@@ -145,9 +149,16 @@ describe('handleLargeSummary', () => {
const result = await handleLargeSummary(largeSummary) const result = await handleLargeSummary(largeSummary)
expect(result).toBe(largeSummary) // Should NOT return the original oversized content
expect(result).not.toBe(largeSummary)
// Should return a truncated summary
expect(result).toContain('Dependency Review Summary')
expect(result).toContain('too large to display')
// Should replace the core.summary buffer to prevent write() from failing
expect(emptyBufferMock).toHaveBeenCalled()
expect(addRawMock).toHaveBeenCalledWith(result)
expect(warningMock).toHaveBeenCalledWith( expect(warningMock).toHaveBeenCalledWith(
expect.stringContaining('Failed to handle large summary') expect.stringContaining('Failed to upload large summary as artifact')
) )
}) })
}) })
Generated Vendored
+19 -5
View File
File diff suppressed because one or more lines are too long
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+22 -4
View File
@@ -73,6 +73,13 @@ export async function handleLargeSummary(
return summaryContent return summaryContent
} }
const summarySize = Math.round(
Buffer.byteLength(summaryContent, 'utf8') / 1024
)
const truncatedSummary = `# Dependency Review Summary
The full dependency review summary was too large to display here (${summarySize}KB, limit is 1024KB).`
const artifactClient = new DefaultArtifactClient() const artifactClient = new DefaultArtifactClient()
const artifactName = 'dependency-review-summary' const artifactName = 'dependency-review-summary'
const files = ['summary.md'] const files = ['summary.md']
@@ -87,9 +94,9 @@ export async function handleLargeSummary(
}) })
// Return a shorter summary with a link to the artifact // Return a shorter summary with a link to the artifact
const shortSummary = `# Dependency Review Summary const shortSummary = `${truncatedSummary}
The full dependency review summary is too large to display here. Please download the artifact named "${artifactName}" to view the complete report. Please download the artifact named "${artifactName}" to view the complete report.
[View full job summary](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID})` [View full job summary](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID})`
@@ -99,9 +106,14 @@ The full dependency review summary is too large to display here. Please download
return shortSummary return shortSummary
} catch (error) { } catch (error) {
core.warning( core.warning(
`Failed to handle large summary: ${error instanceof Error ? error.message : 'Unknown error'}` `Failed to upload large summary as artifact: ${error instanceof Error ? error.message : 'Unknown error'}`
) )
return summaryContent // Even though artifact upload failed, we must still replace the buffer
// with a truncated summary to prevent core.summary.write() from failing
// with the oversized content (see issue #867)
core.summary.emptyBuffer()
core.summary.addRaw(truncatedSummary)
return truncatedSummary
} }
} }
@@ -268,7 +280,13 @@ async function run(): Promise<void> {
} }
} }
} finally { } finally {
try {
await core.summary.write() await core.summary.write()
} catch (error) {
core.warning(
`Failed to write job summary: ${error instanceof Error ? error.message : 'Unknown error'}`
)
}
} }
} }