Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim message when exceeds maximum characters #124

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions __tests__/add-pr-comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import apiResponse from './sample-pulls-api-response.json'
const messagePath1Fixture = path.resolve(__dirname, './message-part-1.txt')
const messagePath1FixturePayload = await fs.readFile(messagePath1Fixture, 'utf-8')
const messagePath2Fixture = path.resolve(__dirname, './message-part-2.txt')
const messagePathTooLongFixture = path.resolve(__dirname, './message-too-long.txt')

const repoToken = '12345'
const commitSha = 'abc123'
Expand Down Expand Up @@ -205,6 +206,19 @@ describe('add-pr-comment action', () => {
expect(core.setOutput).toHaveBeenCalledWith('comment-id', postIssueCommentsResponse.id)
})

it('creates a trimmed comment with a message-path', async () => {
inputs.message = undefined
inputs['message-path'] = messagePathTooLongFixture
inputs['allow-repeats'] = 'true'

let endOfMessage = "...";

await expect(run()).resolves.not.toThrow()
expect(endOfMessage).toEqual(messagePayload?.body.slice(-3))
expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true')
expect(core.setOutput).toHaveBeenCalledWith('comment-id', postIssueCommentsResponse.id)
})

it('supports globs in message paths', async () => {
inputs.message = undefined
inputs['message-path'] = `${path.resolve(__dirname)}/message-part-*.txt`
Expand Down
2 changes: 2 additions & 0 deletions __tests__/message-too-long.txt

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function getMessage({

export async function getMessageFromPath(searchPath: string) {
let message = ''
const maxCharacterLength = 65536

const files = await findFiles(searchPath)

Expand All @@ -68,7 +69,10 @@ export async function getMessageFromPath(searchPath: string) {
message += await fs.readFile(path, { encoding: 'utf8' })
}

return message
// return trimmed message if message is too long (maximum is 65536 characters)
return message.length > maxCharacterLength
? message.substring(0, maxCharacterLength - 3) + '...'
: message
}

export function addMessageHeader(messageId: string, message: string) {
Expand Down