Properly truncate long summaries and catch errors
This commit is contained in:
+14
-3
@@ -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')
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
+19
-5
File diff suppressed because one or more lines are too long
+1
-1
File diff suppressed because one or more lines are too long
+22
-4
@@ -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'}`
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user