Skip to content

Commit 0b9d287

Browse files
authored
Merge pull request #270 from github/truncate-improvements
Truncate improvements
2 parents 225a62d + e178923 commit 0b9d287

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

__tests__/functions/actions-status.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import * as core from '@actions/core'
12
import {actionStatus} from '../../src/functions/action-status'
23
import {truncateCommentBody} from '../../src/functions/truncate-comment-body'
34

45
var context
56
var octokit
67
beforeEach(() => {
78
jest.clearAllMocks()
9+
10+
jest.spyOn(core, 'debug').mockImplementation(() => {})
11+
jest.spyOn(core, 'warning').mockImplementation(() => {})
12+
813
process.env.GITHUB_SERVER_URL = 'https://github.com'
914
process.env.GITHUB_RUN_ID = '12345'
1015

__tests__/functions/truncate-comment-body.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import * as core from '@actions/core'
12
import {truncateCommentBody} from '../../src/functions/truncate-comment-body'
23

4+
beforeEach(() => {
5+
jest.clearAllMocks()
6+
jest.spyOn(core, 'debug').mockImplementation(() => {})
7+
jest.spyOn(core, 'warning').mockImplementation(() => {})
8+
})
9+
310
test('truncates a long message', () => {
411
const message = 'a'.repeat(65537)
512
const got = truncateCommentBody(message)

dist/index.js

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions/truncate-comment-body.js

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as core from '@actions/core'
2+
import {COLORS} from './colors'
3+
14
const truncatedMessageStart =
25
'The message is too large to be posted as a comment.\n<details><summary>Click to see the truncated message</summary>\n'
36
const truncatedMessageEnd = '\n</details>'
@@ -9,12 +12,22 @@ const maxCommentLength = 65536
912
// returned as is.
1013
// :param message: The message to be truncated (String)
1114
export function truncateCommentBody(message) {
15+
// If the message is short enough, return it as is
1216
if (message.length <= maxCommentLength) {
17+
core.debug('comment body is within length limit')
1318
return message
1419
}
20+
21+
// if we make it here, the message is too long, so truncate it
22+
core.warning(
23+
`✂️ truncating - comment body is too long - current: ${COLORS.highlight}${message.length}${COLORS.reset} characters - max: ${COLORS.highlight}${maxCommentLength}${COLORS.reset} characters`
24+
)
25+
1526
let truncated = message.substring(
1627
0,
1728
maxCommentLength - truncatedMessageStart.length - truncatedMessageEnd.length
1829
)
30+
31+
// return the truncated message wrapped in a details tag
1932
return truncatedMessageStart + truncated + truncatedMessageEnd
2033
}

0 commit comments

Comments
 (0)