Skip to content

Commit 2e1cf54

Browse files
authored
Properly truncate long summaries and catch errors
1 parent 68e9887 commit 2e1cf54

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

__tests__/main.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ describe('handleLargeSummary', () => {
132132
expect(result).toContain('actions/runs/12345')
133133
})
134134

135-
test('returns original summary and logs a warning when artifact handling fails', async () => {
135+
test('returns truncated summary and replaces buffer when artifact upload fails', async () => {
136136
const warningMock = core.warning as jest.Mock
137+
const emptyBufferMock = core.summary.emptyBuffer as jest.Mock
138+
const addRawMock = core.summary.addRaw as jest.Mock
137139
warningMock.mockClear()
140+
emptyBufferMock.mockClear()
141+
addRawMock.mockClear()
138142
const largeSummary = 'b'.repeat(1024 * 1024 + 1)
139143

140144
DefaultArtifactClientMock.mockImplementation(() => ({
@@ -145,9 +149,16 @@ describe('handleLargeSummary', () => {
145149

146150
const result = await handleLargeSummary(largeSummary)
147151

148-
expect(result).toBe(largeSummary)
152+
// Should NOT return the original oversized content
153+
expect(result).not.toBe(largeSummary)
154+
// Should return a truncated summary
155+
expect(result).toContain('Dependency Review Summary')
156+
expect(result).toContain('too large to display')
157+
// Should replace the core.summary buffer to prevent write() from failing
158+
expect(emptyBufferMock).toHaveBeenCalled()
159+
expect(addRawMock).toHaveBeenCalledWith(result)
149160
expect(warningMock).toHaveBeenCalledWith(
150-
expect.stringContaining('Failed to handle large summary')
161+
expect.stringContaining('Failed to upload large summary as artifact')
151162
)
152163
})
153164
})

dist/index.js

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ export async function handleLargeSummary(
7373
return summaryContent
7474
}
7575

76+
const summarySize = Math.round(
77+
Buffer.byteLength(summaryContent, 'utf8') / 1024
78+
)
79+
const truncatedSummary = `# Dependency Review Summary
80+
81+
The full dependency review summary was too large to display here (${summarySize}KB, limit is 1024KB).`
82+
7683
const artifactClient = new DefaultArtifactClient()
7784
const artifactName = 'dependency-review-summary'
7885
const files = ['summary.md']
@@ -87,9 +94,9 @@ export async function handleLargeSummary(
8794
})
8895

8996
// Return a shorter summary with a link to the artifact
90-
const shortSummary = `# Dependency Review Summary
97+
const shortSummary = `${truncatedSummary}
9198
92-
The full dependency review summary is too large to display here. Please download the artifact named "${artifactName}" to view the complete report.
99+
Please download the artifact named "${artifactName}" to view the complete report.
93100
94101
[View full job summary](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID})`
95102

@@ -99,9 +106,14 @@ The full dependency review summary is too large to display here. Please download
99106
return shortSummary
100107
} catch (error) {
101108
core.warning(
102-
`Failed to handle large summary: ${error instanceof Error ? error.message : 'Unknown error'}`
109+
`Failed to upload large summary as artifact: ${error instanceof Error ? error.message : 'Unknown error'}`
103110
)
104-
return summaryContent
111+
// Even though artifact upload failed, we must still replace the buffer
112+
// with a truncated summary to prevent core.summary.write() from failing
113+
// with the oversized content (see issue #867)
114+
core.summary.emptyBuffer()
115+
core.summary.addRaw(truncatedSummary)
116+
return truncatedSummary
105117
}
106118
}
107119

@@ -268,7 +280,13 @@ async function run(): Promise<void> {
268280
}
269281
}
270282
} finally {
271-
await core.summary.write()
283+
try {
284+
await core.summary.write()
285+
} catch (error) {
286+
core.warning(
287+
`Failed to write job summary: ${error instanceof Error ? error.message : 'Unknown error'}`
288+
)
289+
}
272290
}
273291
}
274292

0 commit comments

Comments
 (0)