Render CodeRabbit review icons in git comments (#36198)

* Render CodeRabbit review icons locally in git comments

- Replace trusted external SVGs with themed Lucide icons
- Suppress picture sources to prevent remote image fetches

* Fix code review icon rendering in git comments
This commit is contained in:
Jinjing
2026-10-30 23:25:25 -07:00
committed by GitHub
parent 84469d881f
commit 93f8583033
6 changed files with 293 additions and 5 deletions
@@ -2,6 +2,7 @@ import React, { useCallback, useEffect, useState } from 'react'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
import CommentMarkdown from '@/components/sidebar/CommentMarkdown'
import { reviewCommentMarkdown } from './review-comment-markdown'
import { CommentReactions } from '@/components/github/CommentReactions'
import { isBotPRComment } from '../../../../../shared/pr-comment-audience'
import type { GitHubReactionContent, PRComment } from '../../../../../shared/github/comment-types'
@@ -322,7 +323,11 @@ export function CommentRow({
</div>
) : (
<div className={isReply ? presentation.commentBodyReply : presentation.commentBody}>
<CommentMarkdown content={comment.body} className={presentation.commentBodyMarkdown} />
<CommentMarkdown
content={comment.body}
className={presentation.commentBodyMarkdown}
extension={reviewCommentMarkdown}
/>
<CommentReactions
reactions={comment.reactions}
onReactionChange={
@@ -0,0 +1,44 @@
import { defineFixture } from '../../../component-fixtures/component-fixture'
import CommentMarkdown from '@/components/sidebar/CommentMarkdown'
import { getPRCommentPresentationClasses } from '../pr-comment-presentation'
import { reviewCommentMarkdown } from './review-comment-markdown'
const iconBase = 'https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/'
const content = `**<sub><sub><picture><img src="${iconBase}amber-16.svg" width="16" height="16" alt=""></picture></sub></sub> Minor** · **Functional Correctness** · **Quick win**
<details><summary><picture><source media="(prefers-color-scheme: dark)" srcset="${iconBase}review-details-dark.svg"><img src="${iconBase}review-details.svg" width="16" height="16" alt=""></picture> Supported by static analysis</summary>
The callback then discards the result from \`openChromeWebStore()\`.
</details>
**Handle rejected Web Store tab creation before completing the tour.**
\`performContextualTourStepAction\` calls \`finishTour()\` before it schedules \`openChromeWebStore()\`.
**<picture><img src="${iconBase}major-16.svg" width="16" height="16" alt=""></picture> Major** · **Security & Privacy**
**<picture><img src="${iconBase}critical-16.svg" width="16" height="16" alt=""></picture> Critical** · **Functional Correctness**`
export const narrow = defineFixture({
width: 360,
render: () => (
<div className="bg-background p-3 text-foreground">
<div className="rounded-lg border border-border bg-card p-4 text-[13px] leading-relaxed">
<CommentMarkdown
content={content}
extension={reviewCommentMarkdown}
className={getPRCommentPresentationClasses('cards').commentBodyMarkdown}
/>
</div>
</div>
)
})
export const expanded = defineFixture({
...narrow,
width: 540,
interact: (frame) => {
frame.querySelector('summary')?.click()
}
})
@@ -0,0 +1,102 @@
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it } from 'vitest'
import CommentMarkdown from '@/components/sidebar/CommentMarkdown'
import { reviewCommentMarkdown } from './review-comment-markdown'
const iconBase = 'https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/'
describe('review comment icons', () => {
it('renders CodeRabbit picture icons locally without loading images or source sets', () => {
const markup = renderToStaticMarkup(
<CommentMarkdown
extension={reviewCommentMarkdown}
content={`**<sub><sub><picture><img src="${iconBase}amber-16.svg" width="16" height="16" alt=""></picture></sub></sub> Minor** · **Functional Correctness** · **Quick win**
<details><summary><picture><source media="(prefers-color-scheme: dark)" srcset="${iconBase}review-details-dark.svg"><img src="${iconBase}review-details.svg" width="16" height="16" alt=""></picture> Supported by static analysis</summary>
Handle rejected Web Store tab creation before completing the tour.
</details>`}
/>
)
expect(markup.match(/<svg /g)).toHaveLength(2)
expect(markup).toContain('size-4')
expect(markup).toContain('text-status-warning-text')
expect(markup).toContain('aria-hidden="true"')
expect(markup).toContain('<details><summary>')
expect(markup).toContain('Supported by static analysis</summary>')
expect(markup).toContain('Handle rejected Web Store tab creation')
expect(markup).not.toMatch(/<img|<source|srcset=|storage\.googleapis\.com/)
})
it.each([
['major-16', 'text-status-warning-text'],
['critical-20-dark', 'text-destructive'],
['informational-16', 'text-muted-foreground'],
['risk-high-16', 'text-status-warning-text'],
['priority-urgent-16', 'text-destructive'],
['review-details-dark', 'text-muted-foreground'],
['bot', 'text-muted-foreground']
])('renders %s with severity color and an accessible authored label', (name, color) => {
const markup = renderToStaticMarkup(
<CommentMarkdown
extension={reviewCommentMarkdown}
content={`![Review icon](${iconBase}${name}.svg)`}
/>
)
expect(markup).toContain('<svg ')
expect(markup).toContain(color)
expect(markup).toContain('role="img" aria-label="Review icon"')
expect(markup).not.toContain(iconBase)
})
it('suppresses picture sources even when the fallback image is unrecognized', () => {
const markup = renderToStaticMarkup(
<CommentMarkdown
extension={reviewCommentMarkdown}
content='<picture><source srcset="http://127.0.0.1/private.svg"><img src="https://example.com/unknown.svg" alt="External image"></picture>'
/>
)
expect(markup).toContain('href="https://example.com/unknown.svg"')
expect(markup).toContain('>External image</a>')
expect(markup).not.toMatch(/<picture|<source|<img|srcset=|127\.0\.0\.1/)
})
it.each([
`${iconBase}unknown.svg`,
`${iconBase}book-open-16.svg`,
`${iconBase}amber-16.svg?redirect=http://localhost`,
`${iconBase}amber-16.svg#fragment`,
`${iconBase.replace(
'storage.googleapis.com',
'storage.googleapis.com.evil.example'
)}amber-16.svg`,
`${iconBase.replace('coderabbit_public_assets', 'other-bucket')}amber-16.svg`,
`${iconBase.replace('https:', 'http:')}amber-16.svg`,
'http://127.0.0.1/private.svg'
])('keeps unrecognized image %s as a link', (src) => {
const markup = renderToStaticMarkup(
<CommentMarkdown extension={reviewCommentMarkdown} content={`![External image](${src})`} />
)
expect(markup).toContain('>External image</a>')
expect(markup).not.toMatch(/<img|<svg/)
})
it('keeps other compact surfaces unchanged and preserves trusted screenshots in reviews', () => {
const otherSurface = renderToStaticMarkup(
<CommentMarkdown content={`![](${iconBase}amber-16.svg)`} />
)
expect(otherSurface).toContain(`href="${iconBase}amber-16.svg"`)
expect(otherSurface).not.toContain('<svg')
const review = renderToStaticMarkup(
<CommentMarkdown
extension={reviewCommentMarkdown}
content="![Screenshot](data:image/png;base64,abc123)"
/>
)
expect(review).toContain('<img')
expect(review).toContain('src="data:image/png;base64,abc123"')
})
})
@@ -0,0 +1,137 @@
import {
BookOpen,
Bot,
BrushCleaning,
CircleAlert,
CircleMinus,
CircleX,
ClockAlert,
Cog,
FileCheck,
FileMinusCorner,
FilePlusCorner,
Files,
FileWarning,
FileX,
Folders,
GitCommitHorizontal,
Info,
LayersPlus,
Link,
MessageSquareMore,
MessageSquareOff,
MessageSquareX,
Network,
Plug,
Route,
SearchCode,
ShieldAlert,
ShieldCheck,
ShieldPlus,
ShieldQuestion,
TriangleAlert,
UserRound,
Wrench,
type LucideIcon
} from 'lucide-react'
import type { Components } from 'react-markdown'
import type { CommentMarkdownExtension } from '@/components/sidebar/CommentMarkdown'
import { compactCommentMarkdownComponents } from '@/components/sidebar/comment-markdown-element-renderers'
const icons = new Map<string, LucideIcon>([
['book-open', BookOpen],
['bot', Bot],
['brush-cleaning', BrushCleaning],
['ci-failure', CircleX],
['clock-alert', ClockAlert],
['cog', Cog],
['common-learnings', BookOpen],
['file-minus-corner', FileMinusCorner],
['file-no-changes', FileCheck],
['file-plus-corner', FilePlusCorner],
['file-similar', Files],
['file-too-large', FileWarning],
['file-unavailable', FileX],
['folders', Folders],
['git-commit-horizontal', GitCommitHorizontal],
['individual-learning', UserRound],
['info', Info],
['layers-plus', LayersPlus],
['learnings', BookOpen],
['linked-repositories', Link],
['message-square-more', MessageSquareMore],
['message-square-off', MessageSquareOff],
['message-square-x', MessageSquareX],
['network', Network],
['plug', Plug],
['review-details', SearchCode],
['route', Route],
['shield-plus', ShieldPlus],
['wrench', Wrench]
])
const severityIcons = new Map<string, LucideIcon>([
['amber', CircleAlert],
['critical', TriangleAlert],
['high', TriangleAlert],
['informational', Info],
['major', TriangleAlert],
['neutral', CircleMinus],
['priority-low', CircleMinus],
['priority-normal', CircleAlert],
['priority-urgent', TriangleAlert],
['risk-high', ShieldAlert],
['risk-low', ShieldCheck],
['risk-normal', ShieldAlert],
['risk-unassessed', ShieldQuestion],
['risk-urgent', ShieldAlert]
])
const CompactImage = compactCommentMarkdownComponents.img
const components: Components = {
picture: ({ children }) => <>{children}</>,
source: () => null,
img: ({ src, alt }) => {
// Substitute local icons; review comments must not gain a remote-image fetch path.
const name = src
?.match(
/^https:\/\/storage\.googleapis\.com\/coderabbit_public_assets\/review-comment-icons-v1\/([a-z0-9-]+)\.svg$/
)?.[1]
?.replace(/-dark$/, '')
const severity = name?.match(/^(.+)-(?:16|20)$/)?.[1]
const Icon = name
? (icons.get(name) ?? (severity ? severityIcons.get(severity) : undefined))
: undefined
if (!Icon) {
return CompactImage ? <CompactImage src={src} alt={alt} /> : null
}
const color =
severity === 'critical' || severity?.endsWith('urgent')
? 'text-destructive'
: severity === 'amber' ||
severity === 'major' ||
severity === 'high' ||
severity === 'risk-high'
? 'text-status-warning-text'
: 'text-muted-foreground'
return (
<span
className={`inline-flex align-middle ${color}`}
role={alt ? 'img' : undefined}
aria-label={alt || undefined}
aria-hidden={alt ? undefined : true}
>
<Icon className="size-4" aria-hidden="true" />
</span>
)
}
}
export const reviewCommentMarkdown: CommentMarkdownExtension = {
remarkPlugins: [],
sanitizeAttributes: {},
components
}
@@ -88,9 +88,9 @@ const MARKDOWN_BASE = cn(
'[&_code]:text-[0.92em] [&_pre]:text-xs [&_pre_code]:text-[1em] [&_table]:text-[12px]',
// Use absolute caption sizes because nested sub/sup percentages compound into illegible text.
'[&_sub]:text-[11px] [&_sup]:text-[11px]',
// Nested sub wrappers compound image offsets; neutralize only image wrappers, preserving real subscripts.
'[&_sub:has(img)]:bottom-0 [&_sub:has(img)]:align-middle',
'[&_sup:has(img)]:top-0 [&_sup:has(img)]:align-middle',
// Nested sub wrappers compound image/icon offsets; preserve real subscripts.
'[&_sub:has(img,svg)]:bottom-0 [&_sub:has(img,svg)]:align-middle',
'[&_sup:has(img,svg)]:top-0 [&_sup:has(img,svg)]:align-middle',
'[&_pre]:max-h-none [&_pre]:max-w-full [&_pre]:whitespace-pre-wrap',
'[&_table]:w-full [&_table]:max-w-full'
)
@@ -225,7 +225,7 @@ export function createCompactCommentMarkdownComponents(
</table>
</div>
)
}
} satisfies Components
}
export function createDocumentCommentMarkdownComponents(