feat(pull-requests): copy provider checkout commands (#9086)
Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
1 parent
47a95332a2
commit
9fdafdf114
9 files changed
+104
-1
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user