Skip to content

Commit

Permalink
Merge pull request #270 from github/truncate-improvements
Browse files Browse the repository at this point in the history
Truncate improvements
  • Loading branch information
GrantBirki committed Jun 10, 2024
2 parents 225a62d + e178923 commit 0b9d287
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions __tests__/functions/actions-status.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as core from '@actions/core'
import {actionStatus} from '../../src/functions/action-status'
import {truncateCommentBody} from '../../src/functions/truncate-comment-body'

var context
var octokit
beforeEach(() => {
jest.clearAllMocks()

jest.spyOn(core, 'debug').mockImplementation(() => {})
jest.spyOn(core, 'warning').mockImplementation(() => {})

process.env.GITHUB_SERVER_URL = 'https://github.com'
process.env.GITHUB_RUN_ID = '12345'

Expand Down
7 changes: 7 additions & 0 deletions __tests__/functions/truncate-comment-body.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as core from '@actions/core'
import {truncateCommentBody} from '../../src/functions/truncate-comment-body'

beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(core, 'debug').mockImplementation(() => {})
jest.spyOn(core, 'warning').mockImplementation(() => {})
})

test('truncates a long message', () => {
const message = 'a'.repeat(65537)
const got = truncateCommentBody(message)
Expand Down
13 changes: 13 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/functions/truncate-comment-body.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as core from '@actions/core'
import {COLORS} from './colors'

const truncatedMessageStart =
'The message is too large to be posted as a comment.\n<details><summary>Click to see the truncated message</summary>\n'
const truncatedMessageEnd = '\n</details>'
Expand All @@ -9,12 +12,22 @@ const maxCommentLength = 65536
// returned as is.
// :param message: The message to be truncated (String)
export function truncateCommentBody(message) {
// If the message is short enough, return it as is
if (message.length <= maxCommentLength) {
core.debug('comment body is within length limit')
return message
}

// if we make it here, the message is too long, so truncate it
core.warning(
`✂️ truncating - comment body is too long - current: ${COLORS.highlight}${message.length}${COLORS.reset} characters - max: ${COLORS.highlight}${maxCommentLength}${COLORS.reset} characters`
)

let truncated = message.substring(
0,
maxCommentLength - truncatedMessageStart.length - truncatedMessageEnd.length
)

// return the truncated message wrapped in a details tag
return truncatedMessageStart + truncated + truncatedMessageEnd
}

0 comments on commit 0b9d287

Please sign in to comment.