Skip to content

Commit 8a8c074

Browse files
fix(web): PR Code tab previews images the host sends no patch for
Binary files in a GitHub pull request come back without a patch, so an expanded image row in the Code tab rendered nothing. Expanded hunkless image rows now show the file at the pull request's head (or the selected commit) through the host's credentialed media route, so private repositories and remote clients load it too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a2be070 commit 8a8c074

5 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
capitalizeTerm,
88
sourceControlClients,
99
} from "@t3tools/client-runtime/source-control-clients";
10-
import type { CodeViewItem, DiffLineAnnotation, SelectedLineRange } from "@pierre/diffs";
10+
import type {
11+
CodeViewItem,
12+
DiffLineAnnotation,
13+
FileDiffMetadata,
14+
SelectedLineRange,
15+
} from "@pierre/diffs";
1116
import type { CodeViewAnnotationArgs, CodeViewDiffItem } from "@pierre/diffs/react";
1217
import type {
1318
EnvironmentId,
@@ -20,6 +25,7 @@ import type {
2025
PullRequestReviewThread,
2126
PullRequestThreadCommentsResult,
2227
} from "@t3tools/contracts";
28+
import { SOURCE_CONTROL_MEDIA_ASSET } from "@t3tools/contracts";
2329
import {
2430
ChevronDownIcon,
2531
ChevronRightIcon,
@@ -64,6 +70,7 @@ import {
6470
} from "@t3tools/client-runtime/pull-request-editing";
6571
import { fileDiffRows } from "@t3tools/client-runtime/review-guide-tour";
6672
import { isTestChangedFile } from "@t3tools/shared/changedFileKind";
73+
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/mediaPreview";
6774
import { orderDiffFiles } from "./pullRequestFileOrder.logic";
6875
import {
6976
buildFileDiffRenderKey,
@@ -88,6 +95,7 @@ import { pullRequestEnvironment } from "~/state/pullRequests";
8895
import { useEnvironmentQuery } from "~/state/query";
8996
import { useAtomCommand } from "~/state/use-atom-command";
9097

98+
import { ChatMarkdownAssetImage } from "../chat/ChatMarkdownMedia";
9199
import { DiffPanelLoadingState } from "../DiffPanelShell";
92100
import { useDiffWorkerPool } from "../DiffWorkerPoolProvider";
93101
import { DiffCommentAnnotation } from "../diffs/DiffCommentAnnotation";
@@ -144,6 +152,7 @@ interface ReviewAnnotationGroup {
144152
readonly threads: ReadonlyArray<PullRequestReviewThread>;
145153
readonly pending: ReadonlyArray<PendingReviewComment>;
146154
readonly draft: boolean;
155+
readonly image?: { readonly url: string; readonly path: string };
147156
}
148157

149158
type ReviewAnnotation = DiffLineAnnotation<ReviewAnnotationGroup>;
@@ -637,6 +646,20 @@ function PullRequestCodeTab({
637646
return placed;
638647
})();
639648

649+
const imageRevision = commit ?? detail.headSha;
650+
const rawFileUrl = sourceControlClients.find(detail.provider)?.rawFileUrl;
651+
const repositoryUrl = sourceControlClients.parseChangeRequestUrl(detail.url)?.link.repositoryUrl;
652+
// Hosts send no patch for an image, so its expanded row shows the file at this revision.
653+
const imagePreviewUrl = (fileDiff: FileDiffMetadata, path: string) =>
654+
fileDiff.hunks.length === 0 &&
655+
fileDiff.type !== "deleted" &&
656+
isWorkspaceImagePreviewPath(path) &&
657+
imageRevision !== undefined &&
658+
rawFileUrl !== undefined &&
659+
repositoryUrl !== undefined
660+
? rawFileUrl({ repositoryUrl, sha: imageRevision, path })
661+
: null;
662+
640663
const annotatedFiles = files.map((fileDiff) => {
641664
const fileKey = buildFileDiffRenderKey(fileDiff);
642665
const path = resolveFileDiffPath(fileDiff);
@@ -689,6 +712,14 @@ function PullRequestCodeTab({
689712
lineNumber: group.line,
690713
metadata: { threads: group.threads, pending: group.pending, draft: group.draft },
691714
}));
715+
const imageUrl = imagePreviewUrl(fileDiff, path);
716+
if (imageUrl !== null) {
717+
annotations.push({
718+
side: "additions",
719+
lineNumber: 0,
720+
metadata: { threads: [], pending: [], draft: false, image: { url: imageUrl, path } },
721+
});
722+
}
692723
return {
693724
fileKey,
694725
path,
@@ -699,7 +730,7 @@ function PullRequestCodeTab({
699730
annotations
700731
.map(
701732
({ side, lineNumber, metadata }) =>
702-
`${side}:${lineNumber}:${metadata.draft ? "d" : ""}:${metadata.pending
733+
`${side}:${lineNumber}:${metadata.image?.url ?? ""}:${metadata.draft ? "d" : ""}:${metadata.pending
703734
.map((comment) => `${comment.id}:${comment.body}`)
704735
.join(",")}:${metadata.threads
705736
.map(
@@ -1085,6 +1116,20 @@ function PullRequestCodeTab({
10851116
const renderAnnotation = (...[annotation]: CodeViewAnnotationArgs<ReviewAnnotationGroup>) => (
10861117
// Folded resolved conversations alone add no row, so the code reads straight through.
10871118
<div className="relative font-sans text-foreground has-[>:not([data-resolved-threads])]:py-1">
1119+
{annotation.metadata.image ? (
1120+
<div className="flex justify-center p-3">
1121+
<ChatMarkdownAssetImage
1122+
environmentId={environmentId}
1123+
resource={{
1124+
_tag: SOURCE_CONTROL_MEDIA_ASSET,
1125+
cwd: detail.workspaceRoot,
1126+
url: annotation.metadata.image.url,
1127+
repository: detail.repository,
1128+
}}
1129+
alt={annotation.metadata.image.path}
1130+
/>
1131+
</div>
1132+
) : null}
10881133
<InlineReviewThreads threads={annotation.metadata.threads} renderThread={renderThreadCard} />
10891134
{annotation.metadata.pending.map((comment) => (
10901135
<PendingReviewCommentCard

‎packages/source-control-core/src/shared/definition.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@ export interface SourceControlDefinition {
354354
* media itself, so it asks the server, whose driver for this host fetches it.
355355
*/
356356
readonly mediaUrl?: (url: URL) => SourceControlMediaUrl | null;
357+
/**
358+
* A URL `mediaUrl` claims for the bytes of `path` at full commit `sha` in the repository at
359+
* `repositoryUrl`. Null or absent when the host cannot serve one.
360+
*/
361+
readonly rawFileUrl?: (input: {
362+
readonly repositoryUrl: string;
363+
readonly sha: string;
364+
readonly path: string;
365+
}) => string | null;
357366
readonly settings?: SourceControlHostSettingsSchema;
358367
}
359368

‎packages/source-control-github/src/shared/definition.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,19 @@ describe("GitHub media URLs", () => {
194194
expect(media(url), url).toBeNull();
195195
}
196196
});
197+
198+
it("addresses a committed file at a full SHA as immutable credentialed media", () => {
199+
const sha = "0123456789abcdef0123456789abcdef01234567";
200+
const rawFileUrl = (repositoryUrl: string, path: string, revision = sha) =>
201+
definition.rawFileUrl?.({ repositoryUrl, sha: revision, path }) ?? null;
202+
const url = rawFileUrl("https://github.com/owner/repo", "docs/a #1?.png");
203+
expect(url).toBe(`https://github.com/owner/repo/raw/${sha}/docs/a%20%231%3F.png`);
204+
expect(media(url ?? "")).toEqual({
205+
fetchUrl: `https://raw.githubusercontent.com/owner/repo/${sha}/docs/a%20%231%3F.png`,
206+
fileName: "a #1?.png",
207+
immutable: true,
208+
});
209+
expect(rawFileUrl("https://github.com/owner/repo", "a.png", "main")).toBeNull();
210+
expect(rawFileUrl("https://github.example.com/owner/repo", "a.png")).toBeNull();
211+
});
197212
});

‎packages/source-control-github/src/shared/definition.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
type SourceControlAppSignIn,
1515
} from "@t3tools/source-control-core/shared/definition";
1616

17-
import { gitHubMediaUrl } from "./gitHubMedia.ts";
17+
import { gitHubMediaUrl, gitHubRawFileUrl } from "./gitHubMedia.ts";
1818
import { icon } from "./icon.ts";
1919
import { gitHubReferenceAutolinks } from "./referenceAutolinks.ts";
2020

@@ -157,5 +157,6 @@ export const definition = defineSourceControl({
157157
canReadChangeRequestOnHost: (identity, link) =>
158158
isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
159159
mediaUrl: gitHubMediaUrl,
160+
rawFileUrl: gitHubRawFileUrl,
160161
settings,
161162
});

‎packages/source-control-github/src/shared/gitHubMedia.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,19 @@ export function gitHubMediaUrl(url: URL): SourceControlMediaUrl | null {
6464
? rawFile(`https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`)
6565
: null;
6666
}
67+
68+
export function gitHubRawFileUrl(input: {
69+
readonly repositoryUrl: string;
70+
readonly sha: string;
71+
readonly path: string;
72+
}): string | null {
73+
if (!COMMIT_SHA_PATTERN.test(input.sha)) return null;
74+
const path = input.path.split("/").map(encodeURIComponent).join("/");
75+
let url: URL;
76+
try {
77+
url = new URL(`${input.repositoryUrl}/raw/${input.sha}/${path}`);
78+
} catch {
79+
return null;
80+
}
81+
return gitHubMediaUrl(url) === null ? null : url.toString();
82+
}

0 commit comments

Comments
 (0)