← Public packages
@kody/bugbot
Trigger Cursor Bugbot on a PR as the Cursor-linked GitHub account. Never comment bugbot run as a bot.
Files
import getGithubViewer from 'kody:@kody/github/get-viewer'
import getPrInfo from 'kody:@kody/github/pr/get-info'
import githubRequest from 'kody:@kody/github/request'
const ACCOUNT = 'github' as const
/** Logins that must never post the trigger: Bugbot ignores bot identities. */
const BOT_LOGIN = /(^kody-bot$|\[bot\]$|-bot$)/i
/** Cursor Bugbot requires the PR author to match the GitHub account linked to the Cursor account that triggers the run. */
export const GITHUB_ACCOUNT_MISMATCH = 'github_account_mismatch' as const
export type TriggerBugbotInput = {
prUrl?: string
owner?: string
repo?: string
prNumber?: number
/** Append `verbose=true` to the trigger comment. */
verbose?: boolean
/** Resolve identity and PR, but do not post the comment. */
dryRun?: boolean
/**
* GitHub login linked to the Cursor account that runs Bugbot. The `github`
* credential slot must belong to this login. Required.
*/
cursorLogin?: string
}
export type TriggerBugbotResult = {
posted: boolean
dryRun: boolean
account: typeof ACCOUNT
login: string
owner: string
repo: string
prNumber: number
prUrl: string
comment: string
/** PR author GitHub login when available. */
authorLogin?: string
/** Structured failure when Bugbot cannot run (e.g. author mismatch). */
code?: typeof GITHUB_ACCOUNT_MISMATCH
error?: string
commentId?: number
commentUrl?: string
message: string
}
type PrLocator = {
owner: string
repo: string
prNumber: number
prUrl: string
}
function parseLocator(input: TriggerBugbotInput = {}): PrLocator {
if (typeof input.prUrl === 'string' && input.prUrl.trim()) {
const match = input.prUrl.match(
/github\.com\/([^/]+)\/([^/]+)\/(?:pull|issues)\/(\d+)/i,
)
if (!match) {
throw new Error(
`Could not parse a GitHub PR URL from ${JSON.stringify(input.prUrl)}. Pass a URL like https://github.com/owner/repo/pull/123.`,
)
}
const owner = match[1]
const repo = match[2]
const prNumber = Number(match[3])
return {
owner,
repo,
prNumber,
prUrl: `https://github.com/${owner}/${repo}/pull/${prNumber}`,
}
}
const owner = typeof input.owner === 'string' ? input.owner.trim() : ''
const repo = typeof input.repo === 'string' ? input.repo.trim() : ''
const prNumber = Number(input.prNumber)
if (!owner || !repo || !Number.isInteger(prNumber) || prNumber <= 0) {
throw new Error(
'Pass prUrl, or owner, repo, and prNumber, to trigger BugBot.',
)
}
return {
owner,
repo,
prNumber,
prUrl: `https://github.com/${owner}/${repo}/pull/${prNumber}`,
}
}
function prAuthorLogin(pr: { user?: unknown }): string | undefined {
if (typeof pr.user === 'string' && pr.user.trim()) return pr.user.trim()
if (
pr.user &&
typeof pr.user === 'object' &&
typeof (pr.user as { login?: unknown }).login === 'string'
) {
const login = (pr.user as { login: string }).login.trim()
return login || undefined
}
return undefined
}
/**
* Trigger Cursor BugBot on a pull request as the Cursor-linked GitHub account.
* Use this instead of commenting `bugbot run` as a bot (bots are not authorized).
*
* Preflights PR author vs the Cursor-linked GitHub account (`cursorLogin`). When
* the author does not match, returns `{ posted: false, code: 'github_account_mismatch' }`
* without posting: Bugbot would reject the run with "GitHub account mismatch".
*
* @param input - PR locator (`prUrl` or `owner`/`repo`/`prNumber`), optional `verbose`, optional `dryRun`
* @returns Whether the trigger comment was posted, the viewer login, and comment URLs when posted
*
* @example
* import triggerBugbot from 'kody:@kody/bugbot'
* const result = await triggerBugbot({
* prUrl: 'https://github.com/OWNER/REPO/pull/1904',
* cursorLogin: 'your-github-login',
* dryRun: true,
* })
* // => { posted: false, dryRun: true, login: 'your-github-login', ... }
*/
export default async function triggerBugbot(
input: TriggerBugbotInput = {},
): Promise<TriggerBugbotResult> {
const locator = parseLocator(input)
const comment = input.verbose ? 'bugbot run verbose=true' : 'bugbot run'
const dryRun = Boolean(input.dryRun)
const EXPECTED_LOGIN = typeof input.cursorLogin === 'string' ? input.cursorLogin.trim() : ''
if (!EXPECTED_LOGIN) {
throw new Error('Pass cursorLogin: the GitHub login linked to the Cursor account that runs Bugbot.')
}
if (BOT_LOGIN.test(EXPECTED_LOGIN)) {
throw new Error(`Refusing to trigger BugBot as ${EXPECTED_LOGIN}: Bugbot does not accept bot accounts.`)
}
const viewer = (await getGithubViewer({})) as { login?: string }
if (viewer.login !== EXPECTED_LOGIN) {
throw new Error(
`Refusing to trigger BugBot: the github credential is ${viewer.login ?? 'unknown'}, expected ${EXPECTED_LOGIN}. Map the github slot to a credential for ${EXPECTED_LOGIN}. Do not comment bugbot run as a bot.`,
)
}
const pr = await getPrInfo({
owner: locator.owner,
repo: locator.repo,
prNumber: locator.prNumber,
})
const authorLogin = prAuthorLogin(pr as { user?: unknown })
// Cursor Bugbot: the GitHub account linked to the Cursor account that posts
// `bugbot run` must match the PR author. This package posts as
// cursorLogin, so only PRs authored by that account can succeed.
if (authorLogin && authorLogin !== EXPECTED_LOGIN) {
const error =
`Bugbot cannot run: GitHub account mismatch. PR author is ${JSON.stringify(authorLogin)}, but the Cursor-linked account for this trigger is ${EXPECTED_LOGIN}. ` +
`Cursor requires the PR author to match the GitHub account linked to the Cursor account that posts \`bugbot run\`. ` +
`Do not wait for a check; no review will appear. Re-author/open the PR as ${EXPECTED_LOGIN}, or skip Bugbot for this PR.`
return {
posted: false,
dryRun,
account: ACCOUNT,
login: viewer.login,
owner: locator.owner,
repo: locator.repo,
prNumber: locator.prNumber,
prUrl: locator.prUrl,
comment,
authorLogin,
code: GITHUB_ACCOUNT_MISMATCH,
error,
message: error,
}
}
if (dryRun) {
return {
posted: false,
dryRun: true,
account: ACCOUNT,
login: viewer.login,
owner: locator.owner,
repo: locator.repo,
prNumber: locator.prNumber,
prUrl: locator.prUrl,
comment,
authorLogin,
message: `Dry run: would post ${JSON.stringify(comment)} on ${locator.prUrl} as ${viewer.login} (PR ${(pr as { state?: string }).state ?? 'unknown'}; author ${authorLogin ?? 'unknown'}).`,
}
}
const response = await githubRequest({
method: 'POST',
confirm: true,
path: `/repos/${locator.owner}/${locator.repo}/issues/${locator.prNumber}/comments`,
body: { body: comment },
throwOnError: true,
})
const data =
response.data && typeof response.data === 'object'
? (response.data as { id?: number; html_url?: string })
: {}
return {
posted: true,
dryRun: false,
account: ACCOUNT,
login: viewer.login,
owner: locator.owner,
repo: locator.repo,
prNumber: locator.prNumber,
prUrl: locator.prUrl,
comment,
authorLogin,
commentId: typeof data.id === 'number' ? data.id : undefined,
commentUrl: typeof data.html_url === 'string' ? data.html_url : undefined,
message: `Posted ${JSON.stringify(comment)} on ${locator.prUrl} as ${viewer.login}.`,
}
}