Skip to content

Commit e346225

Browse files
committed
fix(web): limit browser fallback to GitHub pull requests
1 parent c903ee9 commit e346225

6 files changed

Lines changed: 100 additions & 215 deletions

File tree

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
5252
import { useNewThreadHandler } from "~/hooks/useHandleNewThread";
5353
import { useCopyToClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";
54-
import { changeRequestRepositoryUrl, pullRequestBrowserUrl } from "~/lib/openPullRequestLink";
54+
import { changeRequestRepositoryUrl, gitHubPullRequestBrowserUrl } from "~/lib/openPullRequestLink";
5555
import { usePreparePullRequestThreadAction } from "~/lib/sourceControlActions";
5656
import { cn } from "~/lib/utils";
5757
import { readLocalApi } from "~/localApi";
@@ -590,14 +590,11 @@ export function PullRequestDetailPanel({
590590
const newThread = useNewThreadHandler();
591591
const { environments } = useEnvironments();
592592
const projects = useProjects();
593-
const unavailableBrowserLink = useMemo(() => {
593+
const unavailableGitHubUrl = useMemo(() => {
594594
const identity = projects.find(
595595
(project) => project.id === reference.projectId && project.environmentId === environmentId,
596596
)?.repositoryIdentity;
597-
const href = pullRequestBrowserUrl(identity, reference.repository, reference.number);
598-
return href && identity?.provider
599-
? { href, label: openOnHostLabel(identity.provider) }
600-
: undefined;
597+
return gitHubPullRequestBrowserUrl(identity, reference.repository, reference.number);
601598
}, [environmentId, projects, reference.number, reference.projectId, reference.repository]);
602599
// Beside a thread there is nothing to pick: the hand-offs land in that thread's composer, and
603600
// the thread is already on one server's copy of the branch.
@@ -1918,7 +1915,7 @@ export function PullRequestDetailPanel({
19181915
<PullRequestsUnavailableState
19191916
error={detailQuery.error}
19201917
onRetry={refreshDetail}
1921-
{...(unavailableBrowserLink ? { browserLink: unavailableBrowserLink } : {})}
1918+
{...(unavailableGitHubUrl ? { gitHubUrl: unavailableGitHubUrl } : {})}
19221919
/>
19231920
) : detail ? (
19241921
<>

‎apps/web/src/components/pullRequest/PullRequestsUnavailableState.test.tsx‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ describe("PullRequestsUnavailableState", () => {
3030
<PullRequestsUnavailableState
3131
error="GitHub did not answer."
3232
onRetry={() => {}}
33-
browserLink={{
34-
href: "https://github.com/pingdotgg/t3code/pull/42",
35-
label: "Open on GitHub",
36-
}}
33+
gitHubUrl="https://github.com/pingdotgg/t3code/pull/42"
3734
/>,
3835
);
3936

@@ -48,17 +45,23 @@ describe("PullRequestsUnavailableState", () => {
4845
const html = renderToStaticMarkup(
4946
<PullRequestsUnavailableState
5047
error="This server cannot read the pull request."
51-
browserLink={{
52-
href: "https://gitlab.example.test/group/repo/-/merge_requests/9",
53-
label: "Open on GitLab",
54-
}}
48+
gitHubUrl="https://github.com/pingdotgg/t3code/pull/9"
5549
/>,
5650
);
5751

58-
expect(html).toContain("Open on GitLab");
52+
expect(html).toContain("Open on GitHub");
5953
expect(html).not.toContain("Retry");
6054
});
6155

56+
it("can offer a retry without offering GitHub", () => {
57+
const html = renderToStaticMarkup(
58+
<PullRequestsUnavailableState error="The host did not answer." onRetry={() => {}} />,
59+
);
60+
61+
expect(html).toContain("Retry");
62+
expect(html).not.toContain("Open on GitHub");
63+
});
64+
6265
it("renders no action content without a retry or browser target", () => {
6366
const html = renderToStaticMarkup(
6467
<PullRequestsUnavailableState error="This project has no known remote." />,

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ export function PullRequestsUnavailableState({
1414
title = "Could not load pull requests",
1515
error,
1616
onRetry,
17-
browserLink,
17+
gitHubUrl,
1818
}: {
1919
title?: string;
2020
error: string;
2121
onRetry?: () => void;
22-
browserLink?: {
23-
readonly href: string;
24-
readonly label: string;
25-
};
22+
gitHubUrl?: string;
2623
}) {
2724
return (
2825
<Empty className="px-4 py-16 md:px-4">
@@ -35,22 +32,22 @@ export function PullRequestsUnavailableState({
3532
shows its message rather than trying to infer one from the failure text. */}
3633
<EmptyDescription>{error}</EmptyDescription>
3734
</EmptyHeader>
38-
{onRetry || browserLink ? (
35+
{onRetry || gitHubUrl ? (
3936
<EmptyContent className="flex-row flex-wrap justify-center gap-2">
4037
{onRetry ? (
4138
<Button size="sm" variant="outline" onClick={onRetry}>
4239
<RefreshCwIcon className="size-3.5" />
4340
Retry
4441
</Button>
4542
) : null}
46-
{browserLink ? (
43+
{gitHubUrl ? (
4744
<Button
4845
size="sm"
4946
variant="outline"
50-
render={<a href={browserLink.href} target="_blank" rel="noopener noreferrer" />}
47+
render={<a href={gitHubUrl} target="_blank" rel="noopener noreferrer" />}
5148
>
5249
<ExternalLinkIcon aria-hidden className="size-3.5" />
53-
{browserLink.label}
50+
Open on GitHub
5451
</Button>
5552
) : null}
5653
</EmptyContent>

‎apps/web/src/lib/openPullRequestLink.test.ts‎

Lines changed: 55 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { describe, expect, it, vi } from "vite-plus/test";
33
import {
44
changeRequestRepositoryUrl,
55
findProjectForChangeRequest,
6+
gitHubPullRequestBrowserUrl,
67
matchesLinkedPullRequestUrl,
78
openPullRequestLink,
89
parseChangeRequestUrl,
9-
pullRequestBrowserUrl,
1010
PullRequestLinkOpenError,
1111
shouldOpenPullRequestExternally,
1212
} from "./openPullRequestLink";
@@ -24,15 +24,15 @@ function repositoryIdentity(
2424
};
2525
}
2626

27-
describe("pullRequestBrowserUrl", () => {
27+
describe("gitHubPullRequestBrowserUrl", () => {
2828
it("uses the requested GitHub repository instead of the project's default repository", () => {
2929
const identity = repositoryIdentity(
3030
"github",
3131
"github.com/acme/default",
3232
"https://github.com/acme/default.git",
3333
);
3434

35-
expect(pullRequestBrowserUrl(identity, "acme/other", 42)).toBe(
35+
expect(gitHubPullRequestBrowserUrl(identity, "acme/other", 42)).toBe(
3636
"https://github.com/acme/other/pull/42",
3737
);
3838
});
@@ -44,94 +44,72 @@ describe("pullRequestBrowserUrl", () => {
4444
"http://[email protected]:8443/team/default.git",
4545
);
4646

47-
expect(pullRequestBrowserUrl(identity, "platform/api", 7)).toBe(
47+
expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 7)).toBe(
4848
"http://github.acme.test:8443/platform/api/pull/7",
4949
);
5050
});
5151

52-
it("preserves a nested GitLab repository path from an SSH remote", () => {
53-
const identity = repositoryIdentity(
54-
"gitlab",
55-
"gitlab.acme.test/group/default",
56-
"[email protected]:group/default.git",
57-
);
58-
59-
expect(pullRequestBrowserUrl(identity, "team/platform/api", 9)).toBe(
60-
"https://gitlab.acme.test/team/platform/api/-/merge_requests/9",
61-
);
62-
});
63-
64-
it("builds a Bitbucket pull request URL", () => {
65-
const identity = repositoryIdentity(
66-
"bitbucket",
67-
"bitbucket.org/workspace/default",
68-
"git://bitbucket.org/workspace/default.git",
69-
);
70-
71-
expect(pullRequestBrowserUrl(identity, "workspace/repository", 5)).toBe(
72-
"https://bitbucket.org/workspace/repository/pull-requests/5",
73-
);
74-
});
75-
76-
it("uses Azure DevOps remote context with the requested repository name", () => {
77-
const identity = repositoryIdentity(
78-
"azure-devops",
79-
"dev.azure.com/acme/platform/_git/default",
80-
"https://user:[email protected]/acme/platform/_git/default.git",
81-
);
82-
83-
expect(pullRequestBrowserUrl(identity, "checkout", 17)).toBe(
84-
"https://dev.azure.com/acme/platform/_git/checkout/pullrequest/17",
85-
);
86-
});
87-
88-
it("converts an Azure DevOps SSH remote to its browser URL", () => {
89-
const identity = repositoryIdentity(
90-
"azure-devops",
91-
"ssh.dev.azure.com/v3/acme/platform/default",
92-
"[email protected]:v3/acme/platform/default",
93-
);
94-
95-
expect(pullRequestBrowserUrl(identity, "checkout", 17)).toBe(
96-
"https://dev.azure.com/acme/platform/_git/checkout/pullrequest/17",
97-
);
98-
expect(pullRequestBrowserUrl(identity, "v3/acme/payments/checkout", 18)).toBe(
99-
"https://dev.azure.com/acme/payments/_git/checkout/pullrequest/18",
100-
);
101-
expect(pullRequestBrowserUrl(identity, "acme/payments/_git/checkout", 19)).toBe(
102-
"https://dev.azure.com/acme/payments/_git/checkout/pullrequest/19",
52+
it.each([
53+
{
54+
name: "SSH",
55+
remoteUrl: "[email protected]:team/default.git",
56+
},
57+
{
58+
name: "git protocol",
59+
remoteUrl: "git://github.acme.test/team/default.git",
60+
},
61+
])("uses the normalized host for a $name remote", ({ remoteUrl }) => {
62+
const identity = repositoryIdentity("github", "github.acme.test/team/default", remoteUrl);
63+
64+
expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 9)).toBe(
65+
"https://github.acme.test/platform/api/pull/9",
10366
);
10467
});
10568

106-
it("keeps a full Azure DevOps repository path from a linked pull request", () => {
107-
const identity = repositoryIdentity(
108-
"azure-devops",
109-
"dev.azure.com/acme/platform/_git/default",
110-
"https://dev.azure.com/acme/platform/_git/default.git",
111-
);
112-
113-
expect(pullRequestBrowserUrl(identity, "acme/payments/_git/checkout", 17)).toBe(
114-
"https://dev.azure.com/acme/payments/_git/checkout/pullrequest/17",
115-
);
116-
});
117-
118-
it("returns null without a supported repository identity", () => {
119-
const unknown = repositoryIdentity(
120-
"unknown",
121-
"code.example.test/acme/repository",
122-
"https://code.example.test/acme/repository.git",
123-
);
124-
125-
expect(pullRequestBrowserUrl(null, "acme/repository", 1)).toBeNull();
126-
expect(pullRequestBrowserUrl(unknown, "acme/repository", 1)).toBeNull();
69+
it("returns null for missing or invalid GitHub data", () => {
70+
expect(gitHubPullRequestBrowserUrl(null, "acme/repository", 1)).toBeNull();
71+
expect(
72+
gitHubPullRequestBrowserUrl(
73+
repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
74+
"acme",
75+
1,
76+
),
77+
).toBeNull();
78+
expect(
79+
gitHubPullRequestBrowserUrl(
80+
repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
81+
"../repository",
82+
1,
83+
),
84+
).toBeNull();
12785
expect(
128-
pullRequestBrowserUrl(
86+
gitHubPullRequestBrowserUrl(
87+
repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
88+
"acme/repository",
89+
0,
90+
),
91+
).toBeNull();
92+
expect(
93+
gitHubPullRequestBrowserUrl(
12994
repositoryIdentity("github", "bad host/acme/repository", "not a remote"),
13095
"acme/repository",
13196
1,
13297
),
13398
).toBeNull();
13499
});
100+
101+
it.each(["gitlab", "bitbucket", "azure-devops", "unknown"])(
102+
"does not build a fallback for %s",
103+
(provider) => {
104+
expect(
105+
gitHubPullRequestBrowserUrl(
106+
repositoryIdentity(provider, "github.com/acme/repository", "https://github.com/a/b"),
107+
"acme/repository",
108+
1,
109+
),
110+
).toBeNull();
111+
},
112+
);
135113
});
136114

137115
describe("changeRequestRepositoryUrl", () => {

0 commit comments

Comments
 (0)