Skip to content
Merged
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
51 changes: 51 additions & 0 deletions __tests__/issueCommentTest/commentWithoutBody.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as core from '@actions/core'
import { setupServer } from 'msw/node'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

import { handleIssueComment } from '../../src/issueComment/handleIssueComment'
import issueCommentEvent from '../fixtures/issues/issueCommentEvent.json'
import * as utils from '../testUtils'

const server = setupServer()
let calls: string[]
beforeAll(() => server.listen(utils.failOnUnhandledRequest))
beforeEach(() => {
utils.setupActionsEnv('/assign /approve /lgtm /hold /kind /help /meow /retest /check-required-labels')
calls = []
server.events.on('request:start', ({ request }) => {
calls.push(`${request.method} ${request.url}`)
})
})
afterEach(() => {
server.resetHandlers()
server.events.removeAllListeners()
})
afterAll(() => server.close())

function withComment(comment: Record<string, unknown> | undefined) {
const payload = structuredClone(issueCommentEvent) as Record<string, unknown>
if (comment === undefined)
delete payload.comment
else
payload.comment = comment
return new utils.MockContext(payload)
}

const { body: _body, ...commentWithoutBody } = issueCommentEvent.comment

describe('an issue comment without a body', () => {
it.each([
['a null body', () => withComment({ ...commentWithoutBody, body: null })],
['no body field', () => withComment(commentWithoutBody)],
['no comment at all', () => withComment(undefined)],
])('runs no command and succeeds with %s', async (_name, contextFor) => {
const setFailed = vi.spyOn(core, 'setFailed').mockImplementation(() => {})
const fetchSpy = vi.spyOn(globalThis, 'fetch')

await expect(handleIssueComment(contextFor())).resolves.toBeUndefined()

expect(setFailed).not.toHaveBeenCalled()
expect(fetchSpy).not.toHaveBeenCalled()
expect(calls).toEqual([])
})
})
3 changes: 2 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46606,7 +46606,8 @@ async function handleIssueComment(context = github_context) {
.split(/\s+/)
.filter(command => command !== '')
.map(command => canonicalCommand(command.toLowerCase())))];
const commentBody = context.payload.comment?.body;
// GitHub allows an empty comment, whose body arrives as null
const commentBody = context.payload.comment?.body ?? '';
if (commandConfig.length === 0) {
setFailed(`please provide a list of space delimited commands / jobs to run. None found`);
return;
Expand Down
3 changes: 2 additions & 1 deletion src/issueComment/handleIssueComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export async function handleIssueComment(context: Context = github.context): Pro
.filter(command => command !== '')
.map(command => canonicalCommand(command.toLowerCase())),
)]
const commentBody: string = context.payload.comment?.body
// GitHub allows an empty comment, whose body arrives as null
const commentBody: string = context.payload.comment?.body ?? ''

if (commandConfig.length === 0) {
core.setFailed(
Expand Down
Loading