Files
orca/src/main/github/issue-comment.ts
T
Brennan Benson 252b427dfd Display private GitHub attachment images in fetched items (#39250)
* Display private GitHub attachment images in fetched items

* Keep attachment event identity aligned with committed renders

* Complete GitHub image response type contracts

* Return private image sources with accepted issue edits

* Keep issue edits successful without optional display data

* Cover unusable issue edit image responses

* Keep optional GitHub image parsing from failing accepted saves

* Fetch GitHub body images with the existing body read
2026-11-08 11:58:15 -08:00

85 lines
2.8 KiB
TypeScript

import { githubBodyImageUrls, githubBodyMediaHeaders } from './body-image-urls'
import type { GitHubCommentResult, PRComment } from '../../shared/github/comment-types'
import type { IssueSourcePreference } from '../../shared/repo-types'
import type { LocalGitExecOptions, OwnerRepo } from './gh-utils'
import {
resolveGitHubRepoExecution,
resolveIssueGitHubApiRepositorySource
} from './github-api-repository'
import { acquire, classifyGhError, ghExecFileAsync, release } from './gh-utils'
import { codeHostFailure, classifiedFailure } from '../../shared/code-host-failure-copy'
// An explicit target keeps replies bound to the conversation; otherwise use the selected source.
export async function addIssueComment(
repoPath: string,
issueNumber: number,
body: string,
connectionId?: string | null,
ownerRepoOverride?: OwnerRepo | null,
localGitOptions: LocalGitExecOptions = {},
preference?: IssueSourcePreference
): Promise<GitHubCommentResult> {
const { ownerRepo, ghOptions } = await resolveGitHubRepoExecution(
repoPath,
ownerRepoOverride ??
(async () =>
(
await resolveIssueGitHubApiRepositorySource(
repoPath,
preference,
connectionId,
localGitOptions
)
).source),
connectionId,
localGitOptions
)
if (!ownerRepo) {
return codeHostFailure('github.repoUnresolved')
}
await acquire()
try {
const { stdout } = await ghExecFileAsync(
[
'api',
...githubBodyMediaHeaders(body),
'-X',
'POST',
`repos/${ownerRepo.owner}/${ownerRepo.repo}/issues/${issueNumber}/comments`,
'--input',
'-'
],
{ ...ghOptions, stdin: JSON.stringify({ body }) }
)
const data: {
id?: number
node_id?: string | null
user: { login: string; avatar_url: string; type?: string } | null
body?: string
body_html?: string | null
created_at?: string
html_url?: string
} = JSON.parse(stdout)
if (typeof data.id !== 'number' || !Number.isSafeInteger(data.id) || data.id < 1) {
return codeHostFailure('github.unexpectedResponse')
}
const comment: PRComment = {
id: data.id,
reactionSubjectId: data.node_id?.trim() || undefined,
author: data.user?.login ?? 'You',
authorAvatarUrl: data.user?.avatar_url ?? '',
body: data.body ?? body,
bodyImageUrls: githubBodyImageUrls(data.body ?? body, data.body_html),
createdAt: data.created_at ?? new Date().toISOString(),
url: data.html_url ?? '',
isBot: data.user?.type === 'Bot'
}
return { ok: true, comment }
} catch (err) {
const stderr = err instanceof Error ? err.message : String(err)
return classifiedFailure(classifyGhError(stderr))
} finally {
release()
}
}