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
88 changes: 88 additions & 0 deletions __tests__/missingNumberMessage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { Context } from '@actions/github/lib/context'
import { setupServer } from 'msw/node'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'

import { approve } from '../src/issueComment/approve'
import { assign } from '../src/issueComment/assign'
import { cc } from '../src/issueComment/cc'
import { close } from '../src/issueComment/close'
import { lock } from '../src/issueComment/lock'
import { meow } from '../src/issueComment/meow'
import { milestone } from '../src/issueComment/milestone'
import { reopen } from '../src/issueComment/reopen'
import { retitle } from '../src/issueComment/retitle'
import { retest } from '../src/issueComment/trigger'
import { unassign } from '../src/issueComment/unassign'
import { uncc } from '../src/issueComment/uncc'
import { addFixedLabels, fixedLabelCommands } from '../src/labels/fixed'
import { hold } from '../src/labels/hold'
import { lgtm } from '../src/labels/lgtm'
import { addPrefixedLabels, prefixedLabelCommands } from '../src/labels/prefixed'
import { remove } from '../src/labels/remove'
import { onPrLgtm } from '../src/pullReq/onPrLgtm'

import issueCommentEvent from './fixtures/issues/issueCommentEvent.json'
import pullReqOpenedEvent from './fixtures/pullReq/pullReqOpenedEvent.json'
import * as utils from './testUtils'

// every request is unexpected: the handlers must refuse before calling the API
const server = setupServer()
beforeAll(() =>
server.listen(utils.failOnUnhandledRequest),
)
beforeEach(() => {
utils.setupActionsEnv()
})
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

const helpCommand = fixedLabelCommands.find(cmd => cmd.command === '/help')!
const lifecycleCommand = prefixedLabelCommands.find(cmd => cmd.command === '/lifecycle')!

function commentWithoutIssue(body: string): Context {
const payload = structuredClone(issueCommentEvent) as Record<string, any>
payload.comment.body = body
delete payload.issue
return new utils.MockContext(payload)
}

function pullRequestEventWithoutPullRequest(): Context {
const payload = structuredClone(pullReqOpenedEvent) as Record<string, unknown>
delete payload.pull_request
return new utils.MockContext(payload)
}

describe('missing issue or pull number errors', () => {
it.each<[string, string, () => Context, (context: Context) => Promise<void>]>([
['issueComment/approve', 'issue', () => commentWithoutIssue('/approve'), approve],
['issueComment/assign', 'issue', () => commentWithoutIssue('/assign'), assign],
['issueComment/cc', 'pull', () => commentWithoutIssue('/cc'), cc],
['issueComment/close', 'issue', () => commentWithoutIssue('/close'), close],
['issueComment/lock', 'issue', () => commentWithoutIssue('/lock'), lock],
['issueComment/meow', 'issue', () => commentWithoutIssue('/meow'), meow],
['issueComment/milestone', 'issue', () => commentWithoutIssue('/milestone v1.0'), milestone],
['issueComment/reopen', 'issue', () => commentWithoutIssue('/reopen'), reopen],
['issueComment/retitle', 'issue', () => commentWithoutIssue('/retitle a new title'), retitle],
['issueComment/trigger', 'issue', () => commentWithoutIssue('/retest'), retest],
['issueComment/unassign', 'issue', () => commentWithoutIssue('/unassign'), unassign],
['issueComment/uncc', 'pull', () => commentWithoutIssue('/uncc'), uncc],
['labels/fixed', 'issue', () => commentWithoutIssue('/help'), context => addFixedLabels(context, helpCommand)],
['labels/hold', 'issue', () => commentWithoutIssue('/hold'), hold],
['labels/lgtm', 'issue', () => commentWithoutIssue('/lgtm'), lgtm],
['labels/prefixed', 'issue', () => commentWithoutIssue('/lifecycle stale'), context => addPrefixedLabels(context, lifecycleCommand)],
['labels/remove', 'issue', () => commentWithoutIssue('/remove bug'), remove],
['pullReq/onPrLgtm', 'pr', pullRequestEventWithoutPullRequest, onPrLgtm],
])('%s prints the payload, not [object Object]', async (_name, kind, contextFor, handler) => {
const context = contextFor()

const error: Error = await handler(context).then(
() => { throw new Error('expected the handler to reject') },
(e: Error) => e,
)

expect(error.message).toBe(
`github context payload missing ${kind} number: ${JSON.stringify(context.payload)}`,
)
expect(error.message).not.toContain('[object Object]')
})
})
36 changes: 18 additions & 18 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42855,7 +42855,7 @@ async function prepare(context, command, options = {}) {
const issueNumber = context.payload.issue?.number;
const commenter = context.payload.comment?.user?.login;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
if (context.payload.issue?.pull_request === undefined) {
await createComment(octokit, context, issueNumber, `\`${command}\` only applies to pull requests.`);
Expand Down Expand Up @@ -43074,7 +43074,7 @@ function commenter(context) {
function requireIssueNumber(context) {
const issueNumber = context.payload.issue?.number;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
return issueNumber;
}
Expand Down Expand Up @@ -43180,7 +43180,7 @@ async function hold(context = github_context) {
const issueNumber = context.payload.issue?.number;
const commentBody = context.payload.comment?.body;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
const config = await loadProwConfig(octokit, context);
const holdLabel = resolveHoldLabel(config.hold);
Expand Down Expand Up @@ -45509,7 +45509,7 @@ async function assertLabelsPolicy(octokit, context, command) {
function fixed_requireIssueNumber(context) {
const issueNumber = context.payload.issue?.number;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
return issueNumber;
}
Expand Down Expand Up @@ -45546,7 +45546,7 @@ async function lgtm(context = github_context) {
const isAuthor = commenterId === context.payload.issue?.user?.login;
const isPullRequest = context.payload.issue?.pull_request !== undefined;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
const cancel = hasCommand('/remove-lgtm', commentBody)
|| (hasCommand('/lgtm', commentBody) && hasKeyword(getCommandArgs('/lgtm', commentBody), 'cancel'));
Expand Down Expand Up @@ -45637,7 +45637,7 @@ async function remove(context = github_context) {
const commentBody = context.payload.comment?.body;
const commenterId = context.payload.comment?.user?.login;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
// Only users who:
// - are collaborators
Expand Down Expand Up @@ -45703,7 +45703,7 @@ async function approve(context = github_context) {
const commentBody = context.payload.comment?.body;
const commenterLogin = context.payload.comment?.user.login;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
const isCancel = hasCommand('/remove-approve', commentBody)
|| (hasCommand('/approve', commentBody) && hasKeyword(getCommandArgs('/approve', commentBody), 'cancel'));
Expand Down Expand Up @@ -45828,7 +45828,7 @@ async function assign_assign(context = github_context) {
const commenterId = context.payload.comment?.user?.login;
const commentBody = context.payload.comment?.body;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
const commentArgs = getCommandArgs('/assign', commentBody);
// no arguments after command provided
Expand Down Expand Up @@ -45907,7 +45907,7 @@ async function cc(context = github_context) {
const commenterId = context.payload.comment?.user?.login;
const commentBody = context.payload.comment?.body;
if (pullNumber === undefined) {
throw new Error(`github context payload missing pull number: ${context.payload}`);
throw new Error(`github context payload missing pull number: ${JSON.stringify(context.payload)}`);
}
const commentArgs = getCommandArgs('/cc', commentBody);
// no arguments after command provided
Expand Down Expand Up @@ -45986,7 +45986,7 @@ async function close_close(context = github_context) {
const commentBody = context.payload.comment?.body;
const commenterId = context.payload.comment?.user?.login;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
// Only users who:
// - are the issue / PR author
Expand Down Expand Up @@ -46055,7 +46055,7 @@ async function lock(context = github_context) {
const commenterId = context.payload.comment?.user?.login;
const commentBody = context.payload.comment?.body;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
const commentArgs = getCommandArgs('/lock', commentBody);
// Only users who:
Expand Down Expand Up @@ -46119,7 +46119,7 @@ async function meow(context = github_context) {
const octokit = newOctokit(token);
const issueNumber = context.payload.issue?.number;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
// a provider outage degrades to a note; only the github write can fail the action
let body;
Expand Down Expand Up @@ -46244,7 +46244,7 @@ async function milestone(context = github_context) {
const commentBody = context.payload.comment?.body;
const commenterId = context.payload.comment?.user?.login;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
// Only users who:
// - are collaborators
Expand Down Expand Up @@ -46302,7 +46302,7 @@ async function reopen(context = github_context) {
const issueNumber = context.payload.issue?.number;
const commenterId = context.payload.comment?.user?.login;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
// Only users who:
// - are the issue / PR author
Expand Down Expand Up @@ -46354,7 +46354,7 @@ async function retitle(context = github_context) {
const commenterId = context.payload.comment?.user?.login;
const commentBody = context.payload.comment?.body;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
const title = getLineArgs('/retitle', commentBody);
// no arguments after command provided. Can't retitle!
Expand Down Expand Up @@ -46402,7 +46402,7 @@ async function unassign(context = github_context) {
const commenterId = context.payload.comment?.user?.login;
const commentBody = context.payload.comment?.body;
if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`);
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`);
}
const commentArgs = getCommandArgs('/unassign', commentBody);
// no arguments after command provided
Expand Down Expand Up @@ -46458,7 +46458,7 @@ async function uncc(context = github_context) {
const commenterId = context.payload.comment?.user?.login;
const commentBody = context.payload.comment?.body;
if (pullNumber === undefined) {
throw new Error(`github context payload missing pull number: ${context.payload}`);
throw new Error(`github context payload missing pull number: ${JSON.stringify(context.payload)}`);
}
const commentArgs = getCommandArgs('/uncc', commentBody);
// no arguments after command provided
Expand Down Expand Up @@ -46786,7 +46786,7 @@ async function onPrLgtm(context) {
const octokit = newOctokit(token);
const prNumber = context.payload.pull_request?.number;
if (prNumber === undefined) {
throw new Error(`github context payload missing pr number: ${context.payload}`);
throw new Error(`github context payload missing pr number: ${JSON.stringify(context.payload)}`);
}
let currentLabels = [];
try {
Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function approve(

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function assign(context: Context = github.context): Promise<void> {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

if (pullNumber === undefined) {
throw new Error(
`github context payload missing pull number: ${context.payload}`,
`github context payload missing pull number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down Expand Up @@ -84,7 +84,7 @@
*
* @param octokit - a hydrated github client
* @param context - the github actions event context
* @param issueNum - the pr number this runtime is associated with

Check warning on line 87 in src/issueComment/cc.ts

View workflow job for this annotation

GitHub Actions / build-test

Expected @PARAM names to be "octokit, context, pullNum, user". Got "octokit, context, issueNum, user"
* @param user - the user to request a self review
*/
async function selfReview(
Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function close(context: Context = github.context): Promise<void> {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function lock(context: Context = github.context): Promise<void> {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/meow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function meow(context: Context = github.context): Promise<void> {
const issueNumber: number | undefined = context.payload.issue?.number
if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/milestone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function milestone(

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/reopen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function reopen(context: Context = github.context): Promise<void> {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/retitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function retitle(

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ async function prepare(context: Context, command: string, options: { refuseAutho
const commenter: string = context.payload.comment?.user?.login

if (issueNumber === undefined) {
throw new Error(`github context payload missing issue number: ${context.payload}`)
throw new Error(`github context payload missing issue number: ${JSON.stringify(context.payload)}`)
}

if (context.payload.issue?.pull_request === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/unassign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function unassign(

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/issueComment/uncc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function uncc(context: Context = github.context): Promise<void> {

if (pullNumber === undefined) {
throw new Error(
`github context payload missing pull number: ${context.payload}`,
`github context payload missing pull number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/labels/fixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function requireIssueNumber(context: Context): number {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/labels/hold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function hold(context: Context = github.context): Promise<void> {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/labels/lgtm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function lgtm(context: Context = github.context): Promise<void> {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/labels/prefixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function requireIssueNumber(context: Context): number {

if (issueNumber === undefined) {
throw new Error(
`github context payload missing issue number: ${context.payload}`,
`github context payload missing issue number: ${JSON.stringify(context.payload)}`,
)
}

Expand Down
Loading
Loading