Skip to content
TypeScript

feat(pull-requests): copy provider checkout commands (#9086)

maria9fdafdf114a8
diff --git a/apps/server/src/pullRequest/BitbucketPullRequestProvider.ts b/apps/server/src/pullRequest/BitbucketPullRequestProvider.ts
index e7a9a6b6ddd..70c6184ada0 100644
--- a/apps/server/src/pullRequest/BitbucketPullRequestProvider.ts
+++ b/apps/server/src/pullRequest/BitbucketPullRequestProvider.ts
@@ -84,6 +84,9 @@ function toChangeRequest(pullRequest: BitbucketPullRequest): ProviderChangeReque
url: pullRequest.url,
author: pullRequest.author,
headBranch: pullRequest.headBranch,
+ ...(pullRequest.headRepositoryNameWithOwner
+ ? { headRepositoryNameWithOwner: pullRequest.headRepositoryNameWithOwner }
+ : {}),
baseBranch: pullRequest.baseBranch,
state: pullRequest.state,
isDraft: pullRequest.isDraft,
diff --git a/apps/server/src/pullRequest/PullRequestProvider.ts b/apps/server/src/pullRequest/PullRequestProvider.ts
index 644f3552cbc..eadc5e93132 100644
--- a/apps/server/src/pullRequest/PullRequestProvider.ts
+++ b/apps/server/src/pullRequest/PullRequestProvider.ts
@@ -69,6 +69,7 @@ export interface ProviderChangeRequest {
readonly url: string;
readonly author: PullRequestActor | null;
readonly headBranch: string;
+ readonly headRepositoryNameWithOwner?: string | null;
readonly baseBranch: string;
readonly state: PullRequestState;
readonly isDraft: boolean;
diff --git a/apps/server/src/pullRequest/PullRequestService.ts b/apps/server/src/pullRequest/PullRequestService.ts
index 3a0d1aac699..1ce5805839c 100644
--- a/apps/server/src/pullRequest/PullRequestService.ts
+++ b/apps/server/src/pullRequest/PullRequestService.ts
@@ -1164,6 +1164,9 @@ export const make = Effect.gen(function* () {
deletions: changeRequest.deletions,
changedFiles: changeRequest.changedFiles,
headBranch: changeRequest.headBranch,
+ ...(changeRequest.headRepositoryNameWithOwner === undefined
+ ? {}
+ : { headRepositoryNameWithOwner: changeRequest.headRepositoryNameWithOwner }),
baseBranch: changeRequest.baseBranch,
createdAt: changeRequest.createdAt,
updatedAt: changeRequest.updatedAt,
diff --git a/apps/server/src/pullRequest/bitbucketPullRequestJson.test.ts b/apps/server/src/pullRequest/bitbucketPullRequestJson.test.ts
index a348ac4b30e..356c5da96f3 100644
--- a/apps/server/src/pullRequest/bitbucketPullRequestJson.test.ts
+++ b/apps/server/src/pullRequest/bitbucketPullRequestJson.test.ts
@@ -24,7 +24,7 @@ function pullRequest(overrides: Record<string, unknown> = {}): Record<string, un
created_on: "2026-06-16T05:04:32.258456+00:00",
updated_on: "2026-06-16T05:04:33.750542+00:00",
author: { display_name: "Bilal Hassan", nickname: "bilal", type: "user" },
- source: { branch: { name: "feat/page" } },
+ source: { branch: { name: "feat/page" }, repository: { full_name: "fork/web" } },
destination: { branch: { name: "master" } },
links: { html: { href: "https://bitbucket.org/acme/web/pull-requests/897" } },
...overrides,
@@ -52,6 +52,7 @@ describe("decodePullRequestPageJson", () => {
url: "https://bitbucket.org/acme/web/pull-requests/897",
author: { login: "bilal", name: "Bilal Hassan" },
headBranch: "feat/page",
+ headRepositoryNameWithOwner: "fork/web",
baseBranch: "master",
state: "open",
isDraft: false,
diff --git a/apps/server/src/pullRequest/bitbucketPullRequestJson.ts b/apps/server/src/pullRequest/bitbucketPullRequestJson.ts
index b0711b8ff6d..f0bc8a71a82 100644
--- a/apps/server/src/pullRequest/bitbucketPullRequestJson.ts
+++ b/apps/server/src/pullRequest/bitbucketPullRequestJson.ts
@@ -52,6 +52,7 @@ const RawUserSchema = Schema.Struct({
*/
const RawBranchSchema = Schema.Struct({
branch: Schema.Struct({ name: TrimmedNonEmptyString }),
+ repository: Schema.optional(Schema.NullOr(Schema.Struct({ full_name: TrimmedNonEmptyString }))),
});
const RawLinkSchema = Schema.Struct({ href: Schema.optional(Schema.String) });
@@ -177,6 +178,7 @@ export interface BitbucketPullRequest {
readonly url: string;
readonly author: PullRequestActor | null;
readonly headBranch: string;
+ readonly headRepositoryNameWithOwner: string | null;
readonly baseBranch: string;
readonly state: PullRequestState;
readonly isDraft: boolean;
@@ -291,6 +293,7 @@ function toPullRequest(raw: Schema.Schema.Type<typeof RawPullRequestSchema>): Bi
url: raw.links.html.href,
author: toActor(raw.author),
headBranch: raw.source.branch.name,
+ headRepositoryNameWithOwner: raw.source.repository?.full_name ?? null,
baseBranch: raw.destination.branch.name,
state: toState(raw),
isDraft: raw.draft ?? false,
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
index 0f3a864f8c1..97cd26fd4aa 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
@@ -16,6 +16,7 @@ import {
BookOpenIcon,
CircleDotIcon,
ChevronDownIcon,
+ CopyIcon,
ExternalLinkIcon,
FileDiffIcon,
FolderGit2Icon,
@@ -114,6 +115,7 @@ import {
pullRequestActionMenuHasGroup,
pullRequestActionNeedsHostRefresh,
pullRequestComposerTarget,
+ pullRequestCheckoutCommand,
pullRequestFindingKey,
pullRequestHandoffLabels,
readableFailure,
@@ -468,6 +470,16 @@ export function PullRequestDetailPanel({
target: "branch name",
timeout: 1600,
});
+ const { copyToClipboard: copyCheckoutCommandToClipboard } = useCopyToClipboard({
+ target: "pull request checkout command",
+ onCopy: () => toastManager.add({ type: "success", title: "Checkout command copied" }),
+ onError: (error) =>
+ toastManager.add({
+ type: "error",
+ title: "Could not copy checkout command",
+ description: error.message,
+ }),
+ });
// The chunk is fetched as soon as the panel exists rather than waiting for the Code tab to be
// clicked, so a reader who does click it lands on a chunk already in the module cache.
useEffect(() => {
@@ -507,6 +519,14 @@ export function PullRequestDetailPanel({
[activity, coreDetail],
);
const repositoryUrl = detail === null ? null : changeRequestRepositoryUrl(detail.url);
+ const checkoutCommand = detail
+ ? pullRequestCheckoutCommand(
+ detail.provider,
+ detail.number,
+ detail.headBranch,
+ detail.headRepositoryNameWithOwner,
+ )
+ : null;
const branchRefsQuery = useEnvironmentQuery(
detail === null
? null
@@ -1758,6 +1778,27 @@ export function PullRequestDetailPanel({
{`${isBranchCopied ? "Copied" : "Copy pull request branch"}: ${detail.headBranch}`}
</TooltipPopup>
</Tooltip>
+ {checkoutCommand ? (
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <Button
+ size="micro"
+ variant="outline"
+ className="ml-1 min-w-0 basis-40 shrink-[0.75] gap-1 font-normal text-muted-foreground"
+ aria-label={`Copy checkout command: ${checkoutCommand}`}
+ onClick={() => copyCheckoutCommandToClipboard(checkoutCommand)}
+ />
+ }
+ >
+ <code className="min-w-0 truncate">{checkoutCommand}</code>
+ <CopyIcon aria-hidden />
+ </TooltipTrigger>
+ <TooltipPopup className="max-w-96 wrap-anywhere font-mono" side="bottom">
+ {checkoutCommand}
+ </TooltipPopup>
+ </Tooltip>
+ ) : null}
</span>
<span className="ml-auto inline-flex shrink-0 items-center justify-end gap-2">
<span className="inline-flex items-center gap-1.5 tabular-nums">
diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts
index 9b9ef610752..d773764dc30 100644
--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts
+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts
@@ -25,6 +25,7 @@ import {
orderPullRequestComments,
pullRequestActionMenuHasGroup,
pullRequestActionNeedsHostRefresh,
+ pullRequestCheckoutCommand,
pullRequestComposerTarget,
pullRequestFindingKey,
pullRequestHandoffLabels,
@@ -38,6 +39,23 @@ import {
} from "./pullRequestDetail.logic";
import type { ReviewCommentContext } from "~/reviewCommentContext";
+describe("pull request checkout commands", () => {
+ it.each([
+ ["github", "feature", null, "gh pr checkout 42"],
+ ["gitlab", "feature", null, "glab mr checkout 42"],
+ ["azure-devops", "feature", null, "az repos pr checkout --id 42"],
+ [
+ "bitbucket",
+ "feature/checkout",
+ "maria/t3code",
+ "git clone --single-branch --branch feature/checkout https://bitbucket.org/maria/t3code.git t3code-pr-42",
+ ],
+ ["unknown", "feature", null, null],
+ ] as const)("builds the %s command", (provider, branch, repository, expected) => {
+ expect(pullRequestCheckoutCommand(provider, 42, branch, repository)).toBe(expected);
+ });
+});
+
const TIMELINE_SOURCE: Pick<
PullRequestDetailView,
"createdAt" | "author" | "commits" | "comments" | "mergedAt" | "closedAt"
diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts
index a616a839523..1039b96d445 100644
--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts
+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts
@@ -11,11 +11,43 @@ import type {
PullRequestReviewThread,
PullRequestState,
PullRequestUpdateMethod,
+ SourceControlProviderKind,
VcsRef,
} from "@t3tools/contracts";
import { inferReviewCommentFenceLanguage, type ReviewCommentContext } from "~/reviewCommentContext";
+const safeShellArgument = /^[A-Za-z0-9._/@+=,-]+$/;
+const bitbucketRepositoryName = /^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/;
+
+export function pullRequestCheckoutCommand(
+ provider: SourceControlProviderKind,
+ number: number,
+ headBranch: string,
+ headRepositoryNameWithOwner?: string | null,
+): string | null {
+ switch (provider) {
+ case "github":
+ return `gh pr checkout ${number}`;
+ case "gitlab":
+ return `glab mr checkout ${number}`;
+ case "azure-devops":
+ return `az repos pr checkout --id ${number}`;
+ case "bitbucket": {
+ if (
+ !headRepositoryNameWithOwner ||
+ !bitbucketRepositoryName.test(headRepositoryNameWithOwner) ||
+ !safeShellArgument.test(headBranch)
+ ) {
+ return null;
+ }
+ return `git clone --single-branch --branch ${headBranch} https://bitbucket.org/${headRepositoryNameWithOwner}.git t3code-pr-${number}`;
+ }
+ case "unknown":
+ return null;
+ }
+}
+
/** Activity changes only when the same host resource reports a newer revision. */
export function shouldRefreshPullRequestActivity(
previous: { readonly key: string; readonly updatedAt: string } | null,
diff --git a/packages/contracts/src/pullRequest.ts b/packages/contracts/src/pullRequest.ts
index a4986893784..1f30803aa1d 100644
--- a/packages/contracts/src/pullRequest.ts
+++ b/packages/contracts/src/pullRequest.ts
@@ -660,6 +660,7 @@ export const PullRequestDetail = Schema.Struct({
deletions: NonNegativeInt,
changedFiles: NonNegativeInt,
headBranch: TrimmedNonEmptyString,
+ headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
baseBranch: TrimmedNonEmptyString,
createdAt: IsoDateTime,
updatedAt: IsoDateTime,