Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/github

GitHub REST, GraphQL, and pull request helpers for Kody agents.

FollowFork

Files

mention-guard.ts

55 lines · 2.2 KB · TypeScript
import { checkMentionFields, type MentionCheckResult } from "./mention-guard-core"

export {
  GitHubMentionBlockedError,
  MENTION_GUARD_ERROR_CODE,
  assertNoDisallowedMentions,
  checkMentionFields,
  countDisallowedMentions,
} from "./mention-guard-core"
export type { MentionCheckField, MentionCheckResult } from "./mention-guard-core"

/**
 * Preflight GitHub Markdown against the posting mention guard without making
 * a request. Mentions inside inline code, fenced code blocks, and indented
 * code blocks do not count, and neither do email addresses.
 *
 * Results report counts and field names only, never the handles.
 *
 * Posts and edits block GitHub @-mentions by default. A mention outside code
 * refuses the call with GitHubMentionBlockedError, which reports a count and
 * never the handles.
 *
 * Private information never goes in public spaces: production user or org
 * handles, package names, emails, credential or secret details, internal IDs,
 * and private-repo or security-advisory details. Use counts or aggregates
 * instead.
 *
 * `allowedMentions` is appropriate for replying to someone already in the
 * thread, or for deliberately asking a specific maintainer or reporter to look
 * (for example `allowedMentions: ['example-reporter']`). It is not for lists or
 * reports of users, owners, or package names, bulk status reports, or anything
 * generated from production data.
 *
 * @param params.body - Markdown that would be posted.
 * @param params.title - Optional title that would be posted.
 * @param params.allowedMentions - Optional handles the post may mention.
 * @returns `{ ok, mentionCount, totalMentionCount, fields }`.
 * @example
 * import checkMentions from 'kody:@kentcdodds/github/mention-guard'
 * const result = checkMentions({ body: 'Thanks @someone, see `@not-a-mention`' })
 * // => { ok: false, mentionCount: 1, totalMentionCount: 1, fields: ['body'] }
 */
export default function checkMentions(params: {
  body?: string
  title?: string
  allowedMentions?: string[]
}): MentionCheckResult {
  return checkMentionFields(
    [
      { name: "title", value: params?.title },
      { name: "body", value: params?.body },
    ],
    params?.allowedMentions,
  )
}