Files
orca/src/shared/pr-bot-author-overrides.ts
T
7fe6742b8b fix(pr-comments): let users mark comment authors as bots for the Humans/Bots filter (#7598)
* fix(pr-comments): let users mark comment authors as bots for the Humans/Bots filter

Some review bots post from regular user accounts that defeat both provider
bot metadata and login heuristics, so their comments were misclassified as
human. Adds a persisted prBotAuthorOverrides setting with a "Mark author as
bot" comment action, applied consistently across desktop and mobile.

Fixes #7597

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(pr-comments): address review feedback on bot-author overrides

- Cap sanitized prBotAuthorOverrides at 500 entries so malformed payloads
  can't bloat GlobalSettings or slow comment classification
- Reuse the shared normalizePRCommentAuthorLogin in isBotPRComment on
  desktop and mobile instead of duplicating the normalization inline
- Pass botAuthorOverrides from CommentRow to CommentMoreMenu instead of
  re-subscribing per menu instance
- Re-fetch mobile bot-author overrides alongside each PR refetch so they
  don't stay a stale one-shot snapshot for the whole session

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(pr-comments): harden bot author override sync

* fix(pr-comments): bound and recover override updates

* fix(pr-comments): merge overrides from canonical settings

* fix(pr-comments): make bot override updates atomic

* fix(pr-comments): surface rejected bot overrides

* fix(i18n): translate bot override warning

* fix(i18n): translate bot author actions

---------

Co-authored-by: Dzmitry Bachko <dbachko@users.noreply.github.com>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Brennan Benson <brennanbenson@Brennans-MacBook-Pro.local>
2026-07-12 14:09:15 -07:00

72 lines
2.3 KiB
TypeScript

// Helpers for the `prBotAuthorOverrides` setting — comment author logins the
// user manually marked as bots. Shared so desktop renderer, main-process RPC,
// and mobile classify comment authors identically.
export const MAX_PR_BOT_AUTHOR_OVERRIDES = 500
// GitLab permits longer usernames than GitHub; 255 keeps both providers covered.
export const MAX_PR_COMMENT_AUTHOR_LOGIN_LENGTH = 255
/** Normalized author login used to match manual bot overrides. */
export function normalizePRCommentAuthorLogin(author: string): string {
if (author.length > MAX_PR_COMMENT_AUTHOR_LOGIN_LENGTH) {
return ''
}
return author.trim().toLowerCase()
}
/** Builds a lookup set from the persisted `prBotAuthorOverrides` setting. */
export function createBotAuthorOverrideSet(
logins: Iterable<unknown> | null | undefined
): ReadonlySet<string> {
const set = new Set<string>()
const iterator = (logins ?? [])[Symbol.iterator]()
let inspected = 0
// Why: callers may pass untrusted settings; bound reads as well as retained output.
while (inspected < MAX_PR_BOT_AUTHOR_OVERRIDES) {
const next = iterator.next()
if (next.done) {
break
}
inspected += 1
const login = next.value
if (typeof login !== 'string') {
continue
}
const normalized = normalizePRCommentAuthorLogin(login)
if (normalized) {
set.add(normalized)
}
}
return set
}
/** Sanitizes an untrusted settings update value into a sorted, deduped login list. */
export function normalizePRBotAuthorOverrides(value: unknown): string[] {
if (!Array.isArray(value)) {
return []
}
return [...createBotAuthorOverrideSet(value)].sort()
}
/** Atomically derives the next persisted list from the authoritative setting. */
export function applyPRBotAuthorOverride(
current: Iterable<unknown> | null | undefined,
author: string,
isBot: boolean
): string[] {
const overrides = new Set(createBotAuthorOverrideSet(current))
const normalized = normalizePRCommentAuthorLogin(author)
if (!normalized || overrides.has(normalized) === isBot) {
return [...overrides].sort()
}
if (isBot) {
if (overrides.size >= MAX_PR_BOT_AUTHOR_OVERRIDES) {
return [...overrides].sort()
}
overrides.add(normalized)
} else {
overrides.delete(normalized)
}
return [...overrides].sort()
}