Skip to content

Commit

Permalink
Merge pull request #1 from ljetten/feat/mshick#93-trim-message
Browse files Browse the repository at this point in the history
trim message when exceeds maximum characters
  • Loading branch information
ljetten authored May 16, 2024
2 parents dd126dd + be7ef5f commit a6c69d1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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

0 comments on commit a6c69d1

Please sign in to comment.