Skip to content
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
98 changes: 94 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ const main = await import('../src/main.js')
const { Octokit } = await import('@octokit/rest')
const mocktokit = jest.mocked(new Octokit())

// Mirrors @actions/core's getInput: returns '' for unset inputs, throws when
// a required input is empty (checked against the raw value, so whitespace-only
// passes the check just like in production), trims the returned value by
// default, and honors `trimWhitespace: false` to return the raw value.
function mockInputs(values: Record<string, string>): void {
core.getInput.mockImplementation((name, options) => {
const value = values[name] ?? ''
if (options?.required && !value)
throw new Error(`Input required and not supplied: ${name}`)
return options?.trimWhitespace === false ? value : value.trim()
})
}

describe('main.ts', () => {
afterEach(() => {
jest.resetAllMocks()
Expand All @@ -40,10 +53,11 @@ describe('main.ts', () => {
}

// Set the action's inputs as return values from core.getInput().
core.getInput
.mockReturnValueOnce('ISSUE_MESSAGE')
.mockReturnValueOnce('PR_MESSAGE')
.mockReturnValueOnce('REPO_TOKEN')
mockInputs({
issue_message: 'ISSUE_MESSAGE',
pr_message: 'PR_MESSAGE',
repo_token: 'REPO_TOKEN'
})
})

describe('run()', () => {
Expand Down Expand Up @@ -156,6 +170,82 @@ describe('main.ts', () => {

expect(mocktokit.rest.issues.createComment).toHaveBeenCalled()
})

it('Does not require pr_message on an issue event', async () => {
github.context.payload.issue = {
number: 10
}
github.context.payload.pull_request = undefined as any

mockInputs({
issue_message: 'ISSUE_MESSAGE',
repo_token: 'REPO_TOKEN'
})

mocktokit.paginate
// Issues
.mockResolvedValueOnce([{ number: 10 }])
// PRs
.mockResolvedValueOnce([])

await expect(main.run()).resolves.toBeUndefined()
expect(mocktokit.rest.issues.createComment).toHaveBeenCalled()
})

it('Does not require issue_message on a PR event', async () => {
github.context.payload.issue = undefined as any
github.context.payload.pull_request = {
number: 10
}

mockInputs({
pr_message: 'PR_MESSAGE',
repo_token: 'REPO_TOKEN'
})

mocktokit.paginate
// Issues
.mockResolvedValueOnce([])
// PRs
.mockResolvedValueOnce([{ number: 10 }])

await expect(main.run()).resolves.toBeUndefined()
expect(mocktokit.rest.issues.createComment).toHaveBeenCalled()
})

it('Fails if issue_message is missing on an issue event', async () => {
github.context.payload.issue = {
number: 10
}
github.context.payload.pull_request = undefined as any

mockInputs({
pr_message: 'PR_MESSAGE',
repo_token: 'REPO_TOKEN'
})

await expect(main.run()).rejects.toThrow(
'Input required and not supplied: issue_message'
)
expect(mocktokit.rest.issues.createComment).not.toHaveBeenCalled()
})

it('Fails if pr_message is missing on a PR event', async () => {
github.context.payload.issue = undefined as any
github.context.payload.pull_request = {
number: 10
}

mockInputs({
issue_message: 'ISSUE_MESSAGE',
repo_token: 'REPO_TOKEN'
})

await expect(main.run()).rejects.toThrow(
'Input required and not supplied: pr_message'
)
expect(mocktokit.rest.issues.createComment).not.toHaveBeenCalled()
})
})

describe('isFirstIssue()', () => {
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 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.

9 changes: 4 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export async function run() {
)

// Get the action inputs.
const issueMessage: string = core.getInput('issue_message', {
required: true
})
const prMessage: string = core.getInput('pr_message', { required: true })
const commentBody: string = isIssue
? core.getInput('issue_message', { required: true })
: core.getInput('pr_message', { required: true })

const octokit = new Octokit({
auth: core.getInput('repo_token', { required: true })
Expand All @@ -47,7 +46,7 @@ export async function run() {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.issue.number,
body: isIssue ? issueMessage : prMessage
body: commentBody
})
}

Expand Down