TypeScript
fix(pr): GitCafe attachments render in pull request bodies (#31741)
Julius Marminged929e726f223
diff --git a/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx b/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsxindex a067af12d1e..d829f6f649b 100644--- a/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx+++ b/apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx@@ -11,7 +11,10 @@ import {} from "./PullRequestMedia";import { videoMimeType } from "@t3tools/shared/video";import { createContext, useContext, useMemo, useState } from "react";-import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";+import {+ changeRequestReferenceAutolinker,+ resolveChangeRequestMediaUrl,+} from "@t3tools/client-runtime/pull-request-markdown";import {sourceControlClients,type ReferenceAutolink,@@ -37,6 +40,7 @@ const AutolinksContext = createContext<((text: string) => ReadonlyArray<Referencnull,);const LineBreaksContext = createContext(false);+const ChangeRequestUrlContext = createContext<string | null>(null);export function PullRequestMarkdownRepository(props: {/** The change request the markdown belongs to, whose host decides what its text links. */@@ -63,7 +67,9 @@ export function PullRequestMarkdownRepository(props: {return (<AutolinksContext value={autolinks}><LineBreaksContext value={lineBreaks}>- <PullRequestMediaContext value={media}>{props.children}</PullRequestMediaContext>+ <ChangeRequestUrlContext value={url ?? null}>+ <PullRequestMediaContext value={media}>{props.children}</PullRequestMediaContext>+ </ChangeRequestUrlContext></LineBreaksContext></AutolinksContext>);@@ -159,6 +165,9 @@ function PullRequestMarkdownContent(props: {const hostLineBreaks = useContext(LineBreaksContext);const autolinks = useContext(AutolinksContext);const mediaContext = useContext(PullRequestMediaContext);+ const changeRequestUrl = useContext(ChangeRequestUrlContext);+ const resolveMediaUrl = (href: string) =>+ changeRequestUrl ? resolveChangeRequestMediaUrl(href, changeRequestUrl) : href;const markdown = linkReferenceAutolinks(props.markdown, autolinks);const openPullRequest = useOpenPullRequest();const openLink = (href: string) => {@@ -167,6 +176,7 @@ function PullRequestMarkdownContent(props: {const gallery: FilePreviewItem[] = markdownImageSources(props.imageGroupMarkdown ?? props.markdown,)+ .map(resolveMediaUrl).filter((uri) =>/^https?:\/\//i.test(uri) &&@@ -175,7 +185,7 @@ function PullRequestMarkdownContent(props: {.map((uri) => pullRequestImageSource(uri, mediaContext));const [preview, setPreview] = useState<FilePreviewSource | null>(null);const renderImage: MarkdownImageRenderer = (image) => (- <MarkdownImage href={image.href} alt={image.alt} onPreview={setPreview} />+ <MarkdownImage href={resolveMediaUrl(image.href)} alt={image.alt} onPreview={setPreview} />);const styles = useMarkdownPreviewStyles();return (@@ -186,7 +196,9 @@ function PullRequestMarkdownContent(props: {textStyle={styles.nativeTextStyle}onLinkPress={openLink}renderImage={renderImage}- onImagePress={(href) => setPreview(pullRequestImageSource(href, mediaContext))}+ onImagePress={(href) =>+ setPreview(pullRequestImageSource(resolveMediaUrl(href), mediaContext))+ }onTaskListItemPress={props.onTaskListItemPress}/><FilePreviewModal source={preview} onRequestClose={() => setPreview(null)} />diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsxindex 8510503e499..ee8d3641a08 100644--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx@@ -106,7 +106,7 @@ import { PullRequestsUnavailableState } from "./PullRequestsUnavailableState";import type { PullRequestAgentSelectionInput } from "./PullRequestCodeTab";import { openOnHostLabel } from "./pullRequestLinkContextMenu";import { PullRequestMarkdownContext } from "./PullRequestMarkdown";-import { pullRequestAutolinkPlugins } from "./pullRequestMarkdown.logic";+import { pullRequestMarkdownPlugins } from "./pullRequestMarkdown.logic";import { PullRequestComposer } from "./PullRequestComposer";import { PullRequestSummaryTab } from "./PullRequestSummaryTab";import { PullRequestTimelineTab } from "./PullRequestTimelineTab";@@ -241,7 +241,7 @@ function pullRequestMarkdownContext(threadRef: ScopedThreadRef | null,) {return {- remarkPlugins: pullRequestAutolinkPlugins(detail),+ remarkPlugins: pullRequestMarkdownPlugins(detail),lineBreaks: sourceControlClients.find(detail?.provider ?? "")?.hardLineBreaks ?? false,threadRef,repository: detail?.repository ?? null,diff --git a/apps/web/src/components/pullRequest/pullRequestMarkdown.logic.ts b/apps/web/src/components/pullRequest/pullRequestMarkdown.logic.tsindex 2cace616666..adf90dbec4e 100644--- a/apps/web/src/components/pullRequest/pullRequestMarkdown.logic.ts+++ b/apps/web/src/components/pullRequest/pullRequestMarkdown.logic.ts@@ -1,4 +1,7 @@-import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";+import {+ changeRequestReferenceAutolinker,+ resolveChangeRequestMediaUrl,+} from "@t3tools/client-runtime/pull-request-markdown";import {sourceControlClients,type ReferenceAutolink,@@ -58,26 +61,37 @@ export function remarkPullRequestAutolinks(options: {return (tree: MarkdownNode) => visit(tree);}-const NO_AUTOLINK_PLUGINS: PluggableList = [];-const autolinkPlugins = new Map<string, PluggableList>();+function remarkChangeRequestMediaUrls(options: { readonly changeRequestUrl: string }) {+ const visit = (node: MarkdownNode): void => {+ if ((node.type === "image" || node.type === "definition") && node.url !== undefined) {+ node.url = resolveChangeRequestMediaUrl(node.url, options.changeRequestUrl);+ }+ node.children?.forEach(visit);+ };+ return (tree: MarkdownNode) => visit(tree);+}++const NO_PLUGINS: PluggableList = [];+const markdownPlugins = new Map<string, PluggableList>();/*** One plugin list per repository, so every comment of a pull request shares one markdown* renderer and its parse cache instead of parsing again on each render.*/-export function pullRequestAutolinkPlugins(+export function pullRequestMarkdownPlugins(changeRequest: { readonly provider: string; readonly url: string } | null,): PluggableList {- if (changeRequest === null) return NO_AUTOLINK_PLUGINS;+ if (changeRequest === null) return NO_PLUGINS;const repositoryUrl = sourceControlClients.parseChangeRequestUrl(changeRequest.url)?.link.repositoryUrl;- if (repositoryUrl === undefined) return NO_AUTOLINK_PLUGINS;+ if (repositoryUrl === undefined) return NO_PLUGINS;const key = `${changeRequest.provider}\u0000${repositoryUrl}`;- let plugins = autolinkPlugins.get(key);+ let plugins = markdownPlugins.get(key);if (plugins === undefined) {const autolinks = changeRequestReferenceAutolinker(changeRequest);- plugins = autolinks ? [[remarkPullRequestAutolinks, { autolinks }]] : NO_AUTOLINK_PLUGINS;- autolinkPlugins.set(key, plugins);+ plugins = [[remarkChangeRequestMediaUrls, { changeRequestUrl: repositoryUrl }]];+ if (autolinks) plugins.push([remarkPullRequestAutolinks, { autolinks }]);+ markdownPlugins.set(key, plugins);}return plugins;}diff --git a/packages/client-runtime/src/pullRequestMarkdown.test.ts b/packages/client-runtime/src/pullRequestMarkdown.test.tsindex d07e48f4e83..232f7d11843 100644--- a/packages/client-runtime/src/pullRequestMarkdown.test.ts+++ b/packages/client-runtime/src/pullRequestMarkdown.test.ts@@ -1,6 +1,10 @@import { describe, expect, it } from "vite-plus/test";-import { changeRequestReferenceAutolinker, splitPullRequestBody } from "./pullRequestMarkdown.ts";+import {+ changeRequestReferenceAutolinker,+ resolveChangeRequestMediaUrl,+ splitPullRequestBody,+} from "./pullRequestMarkdown.ts";describe("pull request body segmentation", () => {// GitHub writes a dropped image into the body as an `<img>` tag, so a bare attachment link on@@ -157,3 +161,18 @@ describe("change request reference autolinks", () => {expect(urls("forgejo", "https://codeberg.org/acme/web/pulls/7", "#5")).toBeNull();});});++describe("change request media urls", () => {+ // regression: GitCafe's `/api/attachments/<id>` uploads rendered as missing local files+ it.each([+ ["/api/attachments/attach_1", "https://git.cafe/api/attachments/attach_1"],+ ["//cdn.example.com/a.png", "//cdn.example.com/a.png"],+ ["/\\evil.example.com/a.png", "/\\evil.example.com/a.png"],+ ["https://example.com/a.png", "https://example.com/a.png"],+ ["docs/a.png", "docs/a.png"],+ ])("resolves %s against the forge", (url, expected) => {+ expect(resolveChangeRequestMediaUrl(url, "https://git.cafe/pingdotgg/t3code/pulls/31251")).toBe(+ expected,+ );+ });+});diff --git a/packages/client-runtime/src/pullRequestMarkdown.ts b/packages/client-runtime/src/pullRequestMarkdown.tsindex f8446369244..9524f3e660e 100644--- a/packages/client-runtime/src/pullRequestMarkdown.ts+++ b/packages/client-runtime/src/pullRequestMarkdown.ts@@ -163,3 +163,16 @@ export function changeRequestReferenceAutolinker(? null: (text) => find(text, repositoryUrl);}++/**+ * Forges link uploads root-relative, as GitCafe's `/api/attachments/<id>`. In a change request+ * body that always means the forge's origin, never a path on the machine rendering it.+ */+export function resolveChangeRequestMediaUrl(url: string, changeRequestUrl: string): string {+ if (!/^\/(?![/\\])/u.test(url)) return url;+ try {+ return new URL(url, changeRequestUrl).href;+ } catch {+ return url;+ }+}