Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
import type { PullRequestActor } from "@t3tools/contracts";
import type { AssetResource, EnvironmentId, PullRequestActor } from "@t3tools/contracts";
import IconAlertTriangle from "@tabler/icons-react-native/IconAlertTriangle";
import { Image } from "expo-image";
import { useGT } from "gt-react-native";
import { useContext } from "react";
import { View } from "react-native";
import { useResolveClassNames } from "uniwind";

import { AppText as Text } from "../../components/ui/AppText";
import { cn } from "../../lib/cn";
import { changeRequestTermKind } from "../../lib/changeRequestTerm";
import { EmptyState } from "../../components/ui/EmptyState";
import { assetImageCacheKey } from "../../lib/assetImageSource";
import { useAssetUrlState } from "../../state/assets";
import { PullRequestMediaContext } from "./PullRequestMedia";
import { pullRequestMediaSource } from "./pullRequestMediaSource";

export function PullRequestReadStatus(props: {
readonly pending: boolean;
Expand Down Expand Up @@ -52,11 +57,62 @@ export function PullRequestAvatar(props: {
readonly size?: number;
}) {
const size = props.size ?? 24;
return props.actor?.avatarUrl ? (
const media = useContext(PullRequestMediaContext);
const avatarUrl = props.actor?.avatarUrl;
const source = avatarUrl ? pullRequestMediaSource(avatarUrl, media) : null;
const resource = source && "resource" in source ? (source.resource ?? null) : null;
if (media !== null && resource !== null) {
return (
<HostAvatar
environmentId={media.environmentId}
resource={resource}
actor={props.actor}
size={size}
/>
);
}
return <AvatarImage uri={avatarUrl ?? null} actor={props.actor} size={size} />;
}

function HostAvatar(props: {
readonly environmentId: EnvironmentId;
readonly resource: AssetResource;
readonly actor: PullRequestActor | null;
readonly size: number;
}) {
const asset = useAssetUrlState(props.environmentId, props.resource);
return (
<AvatarImage
uri={
asset._tag === "Success"
? asset.url
: asset._tag === "Failure"
? (props.actor?.avatarUrl ?? null)
: null
}
cacheKey={
asset._tag === "Success"
? assetImageCacheKey(props.environmentId, props.resource)
: undefined
}
actor={props.actor}
size={props.size}
/>
);
}

function AvatarImage(props: {
readonly uri: string | null;
readonly cacheKey?: string | undefined;
readonly actor: PullRequestActor | null;
readonly size: number;
}) {
const size = props.size;
return props.uri ? (
<Image
source={{ uri: props.actor.avatarUrl }}
recyclingKey={props.actor.avatarUrl}
accessibilityLabel={props.actor.login}
source={props.cacheKey ? { uri: props.uri, cacheKey: props.cacheKey } : { uri: props.uri }}
recyclingKey={props.cacheKey ?? props.uri}
accessibilityLabel={props.actor?.login}
style={{ width: size, height: size, borderRadius: size / 2 }}
cachePolicy="memory-disk"
/>
Expand Down
7 changes: 6 additions & 1 deletion apps/server/src/assets/AssetAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,12 @@ describe("AssetAccess", () => {
cookie: "session",
});
expect(fetched).toEqual([
{ url: media.fetchUrl, headers: { range: "bytes=0-4" }, repository: "owner/repo" },
{
url: media.fetchUrl,
headers: { range: "bytes=0-4" },
cwd: "/repo",
repository: "owner/repo",
},
]);
expect(response.status).toBe(206);
// The upstream sent no type, so it comes from the file name the host gave the media.
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/assets/sourceControlMediaResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const sourceControlMediaResponse = Effect.fn(
const response = yield* fetchMedia({
url: asset.url,
headers: forwarded,
cwd: asset.cwd,
...(repository === undefined ? {} : { repository }),
});
if (response === null) return HttpServerResponse.empty({ status: 502, headers });
Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ import { resolvePickableEnvironments } from "./pullRequestProjectAssignment.logi
import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
import {
PullRequestActorLabel,
PullRequestAvatarSourceContext,
PullRequestMetaLine,
PullRequestReviewOutcomeIcon,
pullRequestChecksState,
Expand Down Expand Up @@ -1569,7 +1570,7 @@ export function PullRequestDetailPanel({
);
}

return (
const panel = (
<div className="relative flex h-full min-h-0 w-full flex-col bg-background">
{threadPickerOpen && detail ? (
<PullRequestThreadLinks
Expand Down Expand Up @@ -2291,4 +2292,13 @@ export function PullRequestDetailPanel({
) : null}
</div>
);
return (
<PullRequestAvatarSourceContext
value={
detail ? { environmentId, cwd: detail.workspaceRoot, repository: detail.repository } : null
}
>
{panel}
</PullRequestAvatarSourceContext>
);
}
102 changes: 87 additions & 15 deletions apps/web/src/components/pullRequest/pullRequestPresentation.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Spinner } from "~/components/ui/spinner";
import type {
PullRequestActor,
PullRequestCheck,
PullRequestCheckStatus,
PullRequestChecksState,
PullRequestLabel,
PullRequestMergeability,
PullRequestReviewDecision,
PullRequestState,
import {
SOURCE_CONTROL_MEDIA_ASSET,
type AssetResource,
type EnvironmentId,
type PullRequestActor,
type PullRequestCheck,
type PullRequestCheckStatus,
type PullRequestChecksState,
type PullRequestLabel,
type PullRequestMergeability,
type PullRequestReviewDecision,
type PullRequestState,
} from "@t3tools/contracts";
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
import {
Expand All @@ -22,7 +25,17 @@ import {
UserRoundXIcon,
} from "lucide-react";
import { msg, useGT, useMessages } from "gt-react";
import { Children, type CSSProperties, isValidElement, type ReactNode, useState } from "react";
import {
Children,
createContext,
type CSSProperties,
isValidElement,
type ReactNode,
useContext,
useState,
} from "react";
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
import { useAssetUrlState } from "~/assets/assetUrls";

import { m, type MessageResolver } from "~/i18n/messages";
import { cn } from "~/lib/utils";
Expand Down Expand Up @@ -463,17 +476,76 @@ export function PullRequestReviewOutcomeBadge({
);
}

/** Where the server fetches an avatar the host serves only to a signed-in request. */
export const PullRequestAvatarSourceContext = createContext<{
readonly environmentId: EnvironmentId;
readonly cwd: string;
readonly repository: string;
} | null>(null);

export function PullRequestActorAvatar({
actor,
className,
}: {
actor: PullRequestActor | null;
className?: string;
}) {
const login = actor?.login ?? "ghost";
const source = useContext(PullRequestAvatarSourceContext);
const avatarUrl = actor?.avatarUrl ?? null;
const [failedAvatarUrl, setFailedAvatarUrl] = useState<string | null>(null);
return avatarUrl === null || failedAvatarUrl === avatarUrl ? (
const fetchUrl = avatarUrl === null ? null : sourceControlClients.mediaUrl(avatarUrl)?.fetchUrl;
const login = actor?.login ?? "ghost";
return source !== null && fetchUrl != null ? (
<HostActorAvatar
environmentId={source.environmentId}
resource={{
_tag: SOURCE_CONTROL_MEDIA_ASSET,
cwd: source.cwd,
url: fetchUrl,
repository: source.repository,
}}
avatarUrl={avatarUrl}
login={login}
className={className}
/>
) : (
<ActorAvatarImage src={avatarUrl} login={login} className={className} />
);
}

function HostActorAvatar({
environmentId,
resource,
avatarUrl,
login,
className,
}: {
environmentId: EnvironmentId;
resource: AssetResource;
avatarUrl: string | null;
login: string;
className: string | undefined;
}) {
const asset = useAssetUrlState(environmentId, resource);
return (
<ActorAvatarImage
src={asset._tag === "Success" ? asset.url : asset._tag === "Failure" ? avatarUrl : null}
Comment thread
Ernest0-Production marked this conversation as resolved.
Resolved
login={login}
className={className}
/>
);
}

function ActorAvatarImage({
src,
login,
className,
}: {
src: string | null;
login: string;
className: string | undefined;
}) {
const [failedSrc, setFailedSrc] = useState<string | null>(null);
return src === null || failedSrc === src ? (
<span
aria-hidden
className={cn(
Expand All @@ -487,10 +559,10 @@ export function PullRequestActorAvatar({
<img
aria-hidden
alt=""
src={avatarUrl}
src={src}
loading="lazy"
className={cn("size-4 shrink-0 rounded-full bg-muted object-cover", className)}
onError={() => setFailedAvatarUrl(avatarUrl)}
onError={() => setFailedSrc(src)}
/>
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/source-control-core/src/server/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface SourceControlMediaRequest {
readonly headers: Readonly<Record<string, string>>;
/** `owner/name` of the change request the media is in, else of the checkout it was shown in. */
readonly repository?: string;
/** The checkout the media was shown in, for a host that fetches through its CLI. */
readonly cwd?: string;
}

/** The server side of a definition's `credentials.appSignIn`. */
Expand Down
2 changes: 2 additions & 0 deletions packages/source-control-gitlab/src/server/GitLabCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export class GitLabCli extends Context.Service<
/** Keeps sensitive payloads out of argv. */
readonly stdin?: string;
readonly maxOutputBytes?: number;
readonly onStdoutChunk?: (chunk: Uint8Array) => void;
}) => Effect.Effect<SourceControlHost.SourceControlProcessOutput, GitLabCliError>;

readonly listMergeRequests: (input: {
Expand Down Expand Up @@ -571,6 +572,7 @@ export const make = Effect.gen(function* () {
timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
...(input.stdin === undefined ? {} : { stdin: input.stdin }),
...(input.maxOutputBytes === undefined ? {} : { maxOutputBytes: input.maxOutputBytes }),
...(input.onStdoutChunk === undefined ? {} : { onStdoutChunk: input.onStdoutChunk }),
})
.pipe(Effect.mapError(mapError));
});
Expand Down
2 changes: 2 additions & 0 deletions packages/source-control-gitlab/src/server/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { definition } from "../shared/definition.ts";
import * as GitLabCli from "./GitLabCli.ts";
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
import * as GitLabIssueTracker from "./GitLabIssueTracker.ts";
import * as GitLabMediaFetch from "./gitLabMediaFetch.ts";
import * as GitLabPullRequestProvider from "./GitLabPullRequestProvider.ts";
import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";

Expand All @@ -20,6 +21,7 @@ const makeInstance = Effect.all({
discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
pullRequests: GitLabPullRequestProvider.make,
issues: GitLabIssueTracker.make,
fetchMedia: GitLabMediaFetch.make,
});

export const driver = defineSourceControlDriver({
Expand Down
55 changes: 55 additions & 0 deletions packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it, afterEach, vi } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { ChildProcessSpawner } from "effect/process";

import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
import * as GitLabCli from "./GitLabCli.ts";
import * as GitLabMediaFetch from "./gitLabMediaFetch.ts";

const mockedRun = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"]["run"]>();
const layer = it.layer(
GitLabCli.layer.pipe(
Layer.provide(TestSourceControlHost.layer({ process: { run: mockedRun } })),
Layer.provideMerge(NodeServices.layer),
),
);

afterEach(() => {
mockedRun.mockReset();
});

layer("GitLabMediaFetch", (it) => {
it.effect("reads an uploaded avatar's bytes through glab, signed in to its host", () =>
Effect.gen(function* () {
const png = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0xff]);
mockedRun.mockImplementationOnce((input) =>
Effect.sync(() => {
input.onStdoutChunk?.(png);
return {
exitCode: ChildProcessSpawner.ExitCode(0),
stdout: new TextDecoder().decode(png),
stderr: "",
stdoutTruncated: false,
stderrTruncated: false,
};
}),
);
const url = "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1";
const fetchMedia = yield* GitLabMediaFetch.make;
const response = yield* fetchMedia({ url, headers: {}, cwd: "/repo" });

assert.deepStrictEqual(mockedRun.mock.calls[0]?.[0].args, [
"api",
"--hostname",
"git.example.test",
url,
]);
assert.strictEqual(response?.status, 200);
if (response === null) return;
assert.deepStrictEqual(new Uint8Array(yield* response.arrayBuffer), png);
}),
);
});
Loading
Loading