Skip to content
Merged
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
27 changes: 27 additions & 0 deletions apps/desktop/src/renderer/lib/github/pullRequestRef.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from "bun:test";
import {
pullRequestRefFromBareUrl,
pullRequestRefFromUrl,
} from "./pullRequestRef";

const REF = { repoFullName: "superset-sh/superset", number: 8412 };

describe("pullRequestRefFromUrl", () => {
it("reads the same pull request with or without www", () => {
expect(
pullRequestRefFromUrl(
"https://www.github.com/superset-sh/superset/pull/8412",
),
).toEqual(REF);
expect(
pullRequestRefFromBareUrl(
"https://www.github.com/superset-sh/superset/pull/8412",
),
).toEqual(REF);
expect(
pullRequestRefFromUrl(
"https://hub.lumenfield.work.evil.io/superset-sh/superset/pull/1",
),
).toBeNull();
});
});
4 changes: 2 additions & 2 deletions apps/desktop/src/renderer/lib/github/pullRequestRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface PullRequestRef {
}

const PULL_REQUEST_URL =
/^https:\/\/github\.com\/([^/?#]+\/[^/?#]+)\/pull\/(\d+)(?:[/?#]|$)/;
/^https:\/\/(?:www\.)?github\.com\/([^/?#]+\/[^/?#]+)\/pull\/(\d+)(?:[/?#]|$)/;

export function pullRequestRefFromUrl(url: string): PullRequestRef | null {
const match = PULL_REQUEST_URL.exec(url);
Expand All @@ -14,7 +14,7 @@ export function pullRequestRefFromUrl(url: string): PullRequestRef | null {
}

const BARE_PULL_REQUEST_URL =
/^https:\/\/github\.com\/[^/?#]+\/[^/?#]+\/pull\/\d+\/?(?:\?[^#]*)?$/;
/^https:\/\/(?:www\.)?github\.com\/[^/?#]+\/[^/?#]+\/pull\/\d+\/?(?:\?[^#]*)?$/;

export function pullRequestRefFromBareUrl(url: string): PullRequestRef | null {
return BARE_PULL_REQUEST_URL.test(url) ? pullRequestRefFromUrl(url) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ mock.module(`${root}/components/PageHeader`, () => ({
mock.module(`${root}/pull-requests/components/PullRequestListToggle`, () => ({
PullRequestListToggle: () => null,
}));
mock.module("renderer/hooks/useActiveOrganizationId", () => ({
useActiveOrganizationId: () => "org",
}));
mock.module(`${root}/pull-requests/components/PullRequestActions`, () => ({
PullRequestActions: ({ projectId }: { projectId: string | null }) => (
<div data-testid="header" data-project={projectId ?? ""} />
PullRequestActions: ({
target,
}: {
target: { host: { projectId: string | null } | null } | null;
}) => (
<div data-testid="header" data-project={target?.host?.projectId ?? ""} />
),
}));
mock.module(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFileRoute } from "@tanstack/react-router";
import { useState } from "react";
import { useHostUrl } from "renderer/hooks/host-service/useHostTargetUrl";
import { useActiveOrganizationId } from "renderer/hooks/useActiveOrganizationId";
import { PageHeader } from "renderer/routes/_authenticated/_dashboard/components/PageHeader";
import { useProjectHost } from "renderer/routes/_authenticated/_dashboard/hooks/useProjectHost";
import { PullRequestActions } from "renderer/routes/_authenticated/_dashboard/pull-requests/components/PullRequestActions";
Expand All @@ -11,6 +12,10 @@ import {
} from "renderer/routes/_authenticated/_dashboard/pull-requests/components/PullRequestDetailTabs";
import { PullRequestListToggle } from "renderer/routes/_authenticated/_dashboard/pull-requests/components/PullRequestListToggle";
import { usePullRequestDetail } from "renderer/routes/_authenticated/_dashboard/pull-requests/hooks/usePullRequestDetail";
import {
canActOnPullRequest,
type PullRequestTarget,
} from "renderer/routes/_authenticated/_dashboard/pull-requests/utils/pullRequestClient";
import { parsePositiveIntegerParam } from "renderer/routes/_authenticated/_dashboard/utils/parsePositiveIntegerParam";
import { Route as PullRequestsLayoutRoute } from "../layout";

Expand All @@ -27,6 +32,7 @@ function PullRequestDetailPage() {
const projectId = search.project ?? null;
const { hostId } = useProjectHost(projectId);
const hostUrl = useHostUrl(hostId);
const organizationId = useActiveOrganizationId();
const [tabChoice, setTabChoice] = useState<{
prNumber: number | null;
tab: PullRequestDetailTab;
Expand All @@ -46,12 +52,20 @@ function PullRequestDetailPage() {
data?.additions !== undefined && data.deletions !== undefined
? { additions: data.additions, deletions: data.deletions }
: null;
const target: PullRequestTarget | null =
prNumber === null
? null
: {
organizationId,
repoFullName: detail.repoFullName,
prNumber,
host: hostUrl ? { hostUrl, projectId: detail.projectId } : null,
};
// A host that predates addComment/setDraft answers the content read without
// the extended fields; it would answer the writes with "No procedure found".
const hostSupportsWrites = data?.mergeability !== undefined;
const commentTarget =
hostSupportsWrites && detail.projectId && hostUrl && prNumber !== null
? { projectId: detail.projectId, hostUrl, prNumber }
target && canActOnPullRequest(target) && data?.mergeability !== undefined
? target
: null;

// The list pane is always visible in the split view (or reachable via the
Expand All @@ -74,10 +88,8 @@ function PullRequestDetailPage() {
</div>
<div className="drag h-full min-w-4 flex-1" />
<PullRequestActions
projectId={detail.projectId}
target={target}
hostId={hostId}
hostUrl={hostUrl}
prNumber={prNumber}
data={detail.data}
isLoading={detail.isLoading}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
import { type ReactNode, useState } from "react";
import { useCopyToClipboard } from "renderer/hooks/useCopyToClipboard";
import { useOpenNewWorkspace } from "renderer/hooks/useOpenNewWorkspace";
import { getHostServiceClientByUrl } from "renderer/lib/host-service-client";
import { normalizePRState } from "renderer/screens/main/components/PRIcon";
import {
type LinkedPR,
Expand All @@ -53,6 +52,13 @@ import {
buildFixFindingsPrompt,
buildResolveConflictsPrompt,
} from "../../utils/buildPullRequestRepairPrompts";
import {
canActOnPullRequest,
mergePullRequest,
type PullRequestMergeMethod,
type PullRequestTarget,
setPullRequestState,
} from "../../utils/pullRequestClient";
import {
PullRequestMenuContent,
PullRequestMenuItem,
Expand All @@ -62,18 +68,17 @@ import {
} from "../PullRequestMenu";
import { PullRequestSkeleton as Skeleton } from "../PullRequestSkeleton";

type MergeMethod = "merge" | "squash" | "rebase";
type MergeMethod = PullRequestMergeMethod;

type PendingAction =
| { kind: "close" }
| { kind: "merge"; method: MergeMethod; force?: boolean };

interface PullRequestActionsProps {
projectId: string | null;
/** Null when the route param is malformed. */
target: PullRequestTarget | null;
/** The host a workspace on this pull request would start on. */
hostId: string | null;
hostUrl: string | null;
/** Parsed PR number; null when the route param is malformed. */
prNumber: number | null;
data: PullRequestDetail | null | undefined;
isLoading: boolean;
/**
Expand Down Expand Up @@ -115,15 +120,16 @@ function IconAction({
* confirmation. Identity (title, author, state) lives in PullRequestItemHeader.
*/
export function PullRequestActions({
projectId,
target,
hostId,
hostUrl,
prNumber,
data,
isLoading,
showStartWorkspace = true,
}: PullRequestActionsProps) {
const { t } = useLingui();
const projectId = target?.host?.projectId ?? null;
const hostUrl = target?.host?.hostUrl ?? null;
const prNumber = target?.prNumber ?? null;
const mergeMethodLabels: Record<MergeMethod, string> = {
squash: t({ message: "Squash and merge" }),
merge: t({ message: "Merge commit" }),
Expand All @@ -146,35 +152,31 @@ export function PullRequestActions({
const [mergeComment, setMergeComment] = useState("");
const { copyToClipboard: copyLink, copied: linkCopied } =
useCopyToClipboard();
const invalidatePullRequestQueries = useInvalidatePullRequestDetail({
projectId,
hostUrl,
prNumber,
});
const invalidatePullRequestQueries = useInvalidatePullRequestDetail(target);
const handoff = usePullRequestAgentHandoff(
projectId && hostId && hostUrl && prNumber !== null
? { projectId, hostId, hostUrl, prNumber }
: null,
);
const setDraft = usePullRequestDraftMutation({
projectId: projectId ?? "",
hostUrl: hostUrl ?? "",
prNumber: prNumber ?? 0,
});
const setDraft = usePullRequestDraftMutation(
target ?? {
organizationId: null,
repoFullName: null,
prNumber: 0,
host: null,
},
);

const setPullRequestState = useMutation({
const setState = useMutation({
mutationFn: async (nextState: "open" | "closed") => {
if (!hostUrl || !projectId || prNumber === null) {
if (!target || !canActOnPullRequest(target)) {
throw new Error(
t({ message: "This project isn't linked to a GitHub repository." }),
t({
message: "This pull request has no GitHub access to act through.",
}),
);
}
const client = getHostServiceClientByUrl(hostUrl);
return client.pullRequests.setState.mutate({
projectId,
prNumber,
state: nextState,
});
return setPullRequestState(target, nextState);
},
onSuccess: invalidatePullRequestQueries,
onError: (mutationError) => {
Expand All @@ -184,26 +186,19 @@ export function PullRequestActions({
},
});

const mergePullRequest = useMutation({
mutationFn: async ({
mergeMethod,
commitMessage,
}: {
const merge = useMutation({
mutationFn: async (merge: {
mergeMethod: MergeMethod;
commitMessage?: string;
}) => {
if (!hostUrl || !projectId || prNumber === null) {
if (!target || !canActOnPullRequest(target)) {
throw new Error(
t({ message: "This project isn't linked to a GitHub repository." }),
t({
message: "This pull request has no GitHub access to act through.",
}),
);
}
const client = getHostServiceClientByUrl(hostUrl);
return client.pullRequests.mergePR.mutate({
projectId,
prNumber,
mergeMethod,
commitMessage,
});
return mergePullRequest(target, merge);
},
onSuccess: invalidatePullRequestQueries,
onError: (mutationError) => {
Expand All @@ -214,16 +209,14 @@ export function PullRequestActions({
});

const isActionPending =
setPullRequestState.isPending ||
mergePullRequest.isPending ||
setDraft.isPending;
setState.isPending || merge.isPending || setDraft.isPending;

const handleConfirmAction = () => {
if (!pendingAction) return;
if (pendingAction.kind === "close") {
setPullRequestState.mutate("closed");
setState.mutate("closed");
} else {
mergePullRequest.mutate({
merge.mutate({
mergeMethod: pendingAction.method,
commitMessage: mergeComment.trim() || undefined,
});
Expand Down Expand Up @@ -284,7 +277,7 @@ export function PullRequestActions({
if (!data) return null;

const canMerge = data.state === "open" && !data.isDraft;
const canAct = !!hostUrl && !!projectId && prNumber !== null;
const canAct = target !== null && canActOnPullRequest(target);
const canToggleDraft = canAct && data.mergeability !== undefined;
const isDraft = data.state === "open" && data.isDraft;
const mergeBlocked = data.mergeability === "conflicting";
Expand Down Expand Up @@ -362,19 +355,19 @@ export function PullRequestActions({
)}
disabled={isActionPending}
aria-disabled={mergeBlocked || undefined}
aria-busy={mergePullRequest.isPending}
aria-busy={merge.isPending}
onClick={() => {
if (mergeBlocked) return;
setPendingAction({ kind: "merge", method: "squash" });
}}
>
{mergePullRequest.isPending ? (
{merge.isPending ? (
<LoaderCircle className="size-3.5 animate-spin motion-reduce:animate-none" />
) : (
<GitMerge strokeWidth={1.75} className="size-3.5" />
)}
<span className="@max-[23rem]/topbar:sr-only">
{mergePullRequest.isPending ? (
{merge.isPending ? (
<Trans>Merging…</Trans>
) : (
<Trans>Merge</Trans>
Expand Down Expand Up @@ -533,9 +526,9 @@ export function PullRequestActions({
size="xs"
className="ml-1 h-7 rounded-full px-3"
disabled={isActionPending}
onClick={() => setPullRequestState.mutate("open")}
onClick={() => setState.mutate("open")}
>
{setPullRequestState.isPending ? (
{setState.isPending ? (
<Trans>Reopening…</Trans>
) : (
<Trans>Reopen</Trans>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function comment(
body: `Body ${id}`,
createdAt,
reviewState: null,
url: null,
...overrides,
};
}
Expand Down Expand Up @@ -94,16 +95,19 @@ describe("the activity timeline", () => {
kind: "review",
body: " ",
reviewState: "approved",
url: null,
}),
comment("batch", "2026-10-03T10:01:00Z", {
kind: "review",
body: "",
reviewState: "COMMENTED",
url: null,
}),
comment("noted", "2026-10-03T10:02:00Z", {
kind: "review",
body: "Looks good",
reviewState: "APPROVED",
url: null,
}),
comment("blank", "2026-10-03T10:03:00Z", { body: "" }),
],
Expand Down
Loading
Loading