Skip to content

Commit 97e0c4a

Browse files
authored
Merge pull request #266 from github/truncate-post-deploy-messages
handle messages that are bigger than issue body limit
2 parents d4ffc4a + 3d1859d commit 97e0c4a

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

__tests__/functions/actions-status.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {actionStatus} from '../../src/functions/action-status'
2+
import {truncateCommentBody} from '../../src/functions/truncate-comment-body'
23

34
var context
45
var octokit
@@ -156,3 +157,28 @@ test('uses default log url when the "message" variable is empty for a success',
156157
repo: 'test'
157158
})
158159
})
160+
161+
test('truncates the message when it is too large for an issue comment', async () => {
162+
const message = 'a'.repeat(65538)
163+
expect(await actionStatus(context, octokit, 123, message, true)).toBe(
164+
undefined
165+
)
166+
expect(octokit.rest.issues.createComment).toHaveBeenCalledWith({
167+
body: truncateCommentBody(message),
168+
issue_number: 1,
169+
owner: 'corp',
170+
repo: 'test'
171+
})
172+
expect(octokit.rest.reactions.createForIssueComment).toHaveBeenCalledWith({
173+
comment_id: '1',
174+
content: 'rocket',
175+
owner: 'corp',
176+
repo: 'test'
177+
})
178+
expect(octokit.rest.reactions.deleteForIssueComment).toHaveBeenCalledWith({
179+
comment_id: '1',
180+
owner: 'corp',
181+
reaction_id: 123,
182+
repo: 'test'
183+
})
184+
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {truncateCommentBody} from '../../src/functions/truncate-comment-body'
2+
3+
test('truncates a long message', () => {
4+
const message = 'a'.repeat(65537)
5+
const got = truncateCommentBody(message)
6+
expect(got).toContain('The message is too large to be posted as a comment.')
7+
expect(got.length).toBeLessThanOrEqual(65536)
8+
})
9+
10+
test('does not truncate a short message', () => {
11+
const message = 'a'.repeat(65536)
12+
const got = truncateCommentBody(message)
13+
expect(got).toEqual(message)
14+
})

dist/index.js

Lines changed: 25 additions & 2 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/functions/action-status.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {truncateCommentBody} from './truncate-comment-body'
2+
13
// Default failure reaction
24
const thumbsDown = '-1'
35
// Default success reaction
@@ -28,11 +30,10 @@ export async function actionStatus(
2830
message = 'Unknown error, [check logs](' + log_url + ') for more details.'
2931
}
3032

31-
// add a comment to the issue with the message
3233
await octokit.rest.issues.createComment({
3334
...context.repo,
3435
issue_number: context.issue.number,
35-
body: message
36+
body: truncateCommentBody(message)
3637
})
3738

3839
// Select the reaction to add to the issue_comment
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const truncatedMessageStart =
2+
'The message is too large to be posted as a comment.\n<details><summary>Click to see the truncated message</summary>\n'
3+
const truncatedMessageEnd = '\n</details>'
4+
// The maximum length of an issue comment body
5+
const maxCommentLength = 65536
6+
7+
// Helper function to truncate the body of a comment if it is too long. If the message is too long,
8+
// it will be truncated and wrapped in a details tag. If the message is short enough, it will be
9+
// returned as is.
10+
// :param message: The message to be truncated (String)
11+
export function truncateCommentBody(message) {
12+
if (message.length <= maxCommentLength) {
13+
return message
14+
}
15+
let truncated = message.substring(
16+
0,
17+
maxCommentLength - truncatedMessageStart.length - truncatedMessageEnd.length
18+
)
19+
return truncatedMessageStart + truncated + truncatedMessageEnd
20+
}

0 commit comments

Comments
 (0)