Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84ee10d7ed |
No files matched your search
@@ -4,7 +4,7 @@ import {
|
|||||||
type GitStatusResult,
|
type GitStatusResult,
|
||||||
type NativeApi,
|
type NativeApi,
|
||||||
} from "@t3tools/contracts";
|
} from "@t3tools/contracts";
|
||||||
import { queryOptions, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
import { queryOptions, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import {
|
import {
|
||||||
CheckIcon,
|
CheckIcon,
|
||||||
@@ -48,6 +48,11 @@ interface GitProgressStep {
|
|||||||
detail?: string;
|
detail?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface GitProgressToastAction {
|
||||||
|
kind: "open_pr" | "create_pr";
|
||||||
|
label: "Open PR" | "Create PR";
|
||||||
|
}
|
||||||
|
|
||||||
function GitActionIcon(props: { icon: GitActionIconName; disabled: boolean }) {
|
function GitActionIcon(props: { icon: GitActionIconName; disabled: boolean }) {
|
||||||
const toneClass = props.disabled ? "text-muted-foreground/45" : "text-foreground/85";
|
const toneClass = props.disabled ? "text-muted-foreground/45" : "text-foreground/85";
|
||||||
|
|
||||||
@@ -73,14 +78,10 @@ function gitActionModalTitle(): string {
|
|||||||
return "Commit your changes";
|
return "Commit your changes";
|
||||||
}
|
}
|
||||||
|
|
||||||
function initialGitProgressSteps(
|
function initialGitProgressSteps(action: GitStackedAction, includeGenerateStep: boolean): GitProgressStep[] {
|
||||||
action: GitStackedAction,
|
|
||||||
commitMessage: string,
|
|
||||||
): GitProgressStep[] {
|
|
||||||
const hasCustomMessage = commitMessage.trim().length > 0;
|
|
||||||
const steps: GitProgressStep[] = [];
|
const steps: GitProgressStep[] = [];
|
||||||
|
|
||||||
if (!hasCustomMessage) {
|
if (includeGenerateStep) {
|
||||||
steps.push({
|
steps.push({
|
||||||
id: "generate",
|
id: "generate",
|
||||||
label: "Generate commit message",
|
label: "Generate commit message",
|
||||||
@@ -135,6 +136,48 @@ function runActionLabel(action: GitStackedAction): string {
|
|||||||
return "Commit and create PR";
|
return "Commit and create PR";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function canCreatePrFromStatus(gitStatus: GitStatusResult | null): boolean {
|
||||||
|
if (!gitStatus) return false;
|
||||||
|
const hasBranch = gitStatus.branch !== null;
|
||||||
|
const hasOpenPr = gitStatus.openPr !== null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
hasBranch &&
|
||||||
|
!hasOpenPr &&
|
||||||
|
!gitStatus.hasWorkingTreeChanges &&
|
||||||
|
gitStatus.hasUpstream &&
|
||||||
|
gitStatus.behindCount === 0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getGitProgressToastAction(params: {
|
||||||
|
isRunning: boolean;
|
||||||
|
hasError: boolean;
|
||||||
|
result: GitRunStackedActionResult | null;
|
||||||
|
gitStatus: GitStatusResult | null;
|
||||||
|
openPrUrl: string | null;
|
||||||
|
}): GitProgressToastAction | null {
|
||||||
|
if (params.isRunning || params.hasError || !params.result) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.openPrUrl) {
|
||||||
|
return {
|
||||||
|
kind: "open_pr",
|
||||||
|
label: "Open PR",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.result.action === "commit_push" && canCreatePrFromStatus(params.gitStatus)) {
|
||||||
|
return {
|
||||||
|
kind: "create_pr",
|
||||||
|
label: "Create PR",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
function buildGitActionMenuItems(
|
function buildGitActionMenuItems(
|
||||||
gitStatus: GitStatusResult | null,
|
gitStatus: GitStatusResult | null,
|
||||||
isDisabled: boolean,
|
isDisabled: boolean,
|
||||||
@@ -148,13 +191,7 @@ function buildGitActionMenuItems(
|
|||||||
const canCommit = !isDisabled && hasChanges;
|
const canCommit = !isDisabled && hasChanges;
|
||||||
const canPush = !isDisabled && hasBranch && !hasChanges && hasAhead;
|
const canPush = !isDisabled && hasBranch && !hasChanges && hasAhead;
|
||||||
const canOpenPr = !isDisabled && hasOpenPr;
|
const canOpenPr = !isDisabled && hasOpenPr;
|
||||||
const canCreatePr =
|
const canCreatePr = !isDisabled && canCreatePrFromStatus(gitStatus);
|
||||||
!isDisabled &&
|
|
||||||
hasBranch &&
|
|
||||||
!hasOpenPr &&
|
|
||||||
!hasChanges &&
|
|
||||||
gitStatus.hasUpstream &&
|
|
||||||
gitStatus.behindCount === 0;
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@@ -261,27 +298,7 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
|||||||
gitStatusQueryOptions(api, gitCwd),
|
gitStatusQueryOptions(api, gitCwd),
|
||||||
);
|
);
|
||||||
|
|
||||||
const runImmediateGitActionMutation = useMutation({
|
const isGitActionRunning = isGitModalActionRunning;
|
||||||
mutationFn: async (action: GitStackedAction) => {
|
|
||||||
if (!api || !gitCwd) {
|
|
||||||
throw new Error("Git action is unavailable.");
|
|
||||||
}
|
|
||||||
return api.git.runStackedAction({ cwd: gitCwd, action });
|
|
||||||
},
|
|
||||||
onMutate: () => {
|
|
||||||
setIsGitMenuOpen(false);
|
|
||||||
setGitActionError(null);
|
|
||||||
setGitModalError(null);
|
|
||||||
},
|
|
||||||
onSuccess: async () => {
|
|
||||||
await queryClient.invalidateQueries({ queryKey: ["git"] });
|
|
||||||
},
|
|
||||||
onError: (error) => {
|
|
||||||
setGitActionError(error instanceof Error ? error.message : "Git action failed.");
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const isGitActionRunning = isGitModalActionRunning || runImmediateGitActionMutation.isPending;
|
|
||||||
|
|
||||||
const gitBaseDisabled = !api || !gitCwd || !gitStatus || isGitActionRunning;
|
const gitBaseDisabled = !api || !gitCwd || !gitStatus || isGitActionRunning;
|
||||||
const gitActionMenuItems = useMemo(
|
const gitActionMenuItems = useMemo(
|
||||||
@@ -318,6 +335,23 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
|||||||
: gitModalError
|
: gitModalError
|
||||||
? "Git action failed."
|
? "Git action failed."
|
||||||
: "Done.";
|
: "Done.";
|
||||||
|
const gitProgressToastAction = useMemo(
|
||||||
|
() =>
|
||||||
|
getGitProgressToastAction({
|
||||||
|
isRunning: isGitModalActionRunning,
|
||||||
|
hasError: gitModalError !== null,
|
||||||
|
result: gitModalResult,
|
||||||
|
gitStatus,
|
||||||
|
openPrUrl: gitProgressToastOpenPrUrl,
|
||||||
|
}),
|
||||||
|
[
|
||||||
|
gitModalError,
|
||||||
|
gitModalResult,
|
||||||
|
gitProgressToastOpenPrUrl,
|
||||||
|
gitStatus,
|
||||||
|
isGitModalActionRunning,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
const refreshGitStatus = useCallback(async () => {
|
const refreshGitStatus = useCallback(async () => {
|
||||||
if (!api || !gitCwd) return;
|
if (!api || !gitCwd) return;
|
||||||
@@ -344,13 +378,6 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
|||||||
setGitModalCommitMessage("");
|
setGitModalCommitMessage("");
|
||||||
}, [isGitActionRunning]);
|
}, [isGitActionRunning]);
|
||||||
|
|
||||||
const runGitActionImmediately = useCallback(
|
|
||||||
(action: GitStackedAction) => {
|
|
||||||
runImmediateGitActionMutation.mutate(action);
|
|
||||||
},
|
|
||||||
[runImmediateGitActionMutation],
|
|
||||||
);
|
|
||||||
|
|
||||||
const openExistingPr = useCallback(() => {
|
const openExistingPr = useCallback(() => {
|
||||||
setIsGitMenuOpen(false);
|
setIsGitMenuOpen(false);
|
||||||
setGitActionError(null);
|
setGitActionError(null);
|
||||||
@@ -392,137 +419,161 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
|||||||
setGitModalResult(null);
|
setGitModalResult(null);
|
||||||
}, [isGitModalActionRunning]);
|
}, [isGitModalActionRunning]);
|
||||||
|
|
||||||
const runGitAction = useCallback(async () => {
|
const executeGitAction = useCallback(
|
||||||
if (!api || !gitCwd) return;
|
async (params: {
|
||||||
if (!selectedGitModalActionOption || selectedGitModalActionOption.disabled) return;
|
action: GitStackedAction;
|
||||||
const actionCwd = gitCwd;
|
commitMessage: string;
|
||||||
const action = selectedGitModalActionOption.action;
|
includeGeneratedCommitMessage: boolean;
|
||||||
const commitMessage = gitModalCommitMessage.trim();
|
closeModal: boolean;
|
||||||
const includeGeneratedCommitMessage = commitMessage.length === 0;
|
}) => {
|
||||||
|
if (!api || !gitCwd) return;
|
||||||
|
const actionCwd = gitCwd;
|
||||||
|
const { action, closeModal, commitMessage, includeGeneratedCommitMessage } = params;
|
||||||
|
|
||||||
setIsGitMenuOpen(false);
|
setIsGitMenuOpen(false);
|
||||||
setIsGitModalOpen(false);
|
if (closeModal) {
|
||||||
setIsGitProgressToastVisible(true);
|
setIsGitModalOpen(false);
|
||||||
setIsGitModalActionRunning(true);
|
|
||||||
setGitModalError(null);
|
|
||||||
setGitActionError(null);
|
|
||||||
setGitModalResult(null);
|
|
||||||
setGitModalProgress(initialGitProgressSteps(action, commitMessage));
|
|
||||||
|
|
||||||
let commit: GitRunStackedActionResult["commit"] = {
|
|
||||||
status: "skipped_no_changes",
|
|
||||||
};
|
|
||||||
let push: GitRunStackedActionResult["push"] = {
|
|
||||||
status: "skipped_not_requested",
|
|
||||||
};
|
|
||||||
let pr: GitRunStackedActionResult["pr"] = {
|
|
||||||
status: "skipped_not_requested",
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateStep = (
|
|
||||||
id: GitProgressStep["id"],
|
|
||||||
status: GitProgressStepStatus,
|
|
||||||
detail?: string,
|
|
||||||
) => {
|
|
||||||
setGitModalProgress((steps) => updateProgressStep(steps, id, status, detail));
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (includeGeneratedCommitMessage) {
|
|
||||||
updateStep("generate", "running");
|
|
||||||
} else {
|
|
||||||
updateStep("commit", "running");
|
|
||||||
}
|
}
|
||||||
|
setIsGitProgressToastVisible(true);
|
||||||
|
setIsGitModalActionRunning(true);
|
||||||
|
setGitModalError(null);
|
||||||
|
setGitActionError(null);
|
||||||
|
setGitModalResult(null);
|
||||||
|
setGitModalProgress(initialGitProgressSteps(action, includeGeneratedCommitMessage));
|
||||||
|
|
||||||
const commitRun = await api.git.runStackedAction({
|
let commit: GitRunStackedActionResult["commit"] = {
|
||||||
cwd: actionCwd,
|
status: "skipped_no_changes",
|
||||||
action: "commit",
|
};
|
||||||
...(commitMessage.length > 0 ? { commitMessage } : {}),
|
let push: GitRunStackedActionResult["push"] = {
|
||||||
});
|
status: "skipped_not_requested",
|
||||||
commit = commitRun.commit;
|
};
|
||||||
|
let pr: GitRunStackedActionResult["pr"] = {
|
||||||
|
status: "skipped_not_requested",
|
||||||
|
};
|
||||||
|
|
||||||
if (includeGeneratedCommitMessage) {
|
const updateStep = (
|
||||||
if (commitRun.commit.status === "created") {
|
id: GitProgressStep["id"],
|
||||||
if (commitRun.commit.subject) {
|
status: GitProgressStepStatus,
|
||||||
setGitModalCommitMessage(commitRun.commit.subject);
|
detail?: string,
|
||||||
|
) => {
|
||||||
|
setGitModalProgress((steps) => updateProgressStep(steps, id, status, detail));
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (includeGeneratedCommitMessage) {
|
||||||
|
updateStep("generate", "running");
|
||||||
|
} else {
|
||||||
|
updateStep("commit", "running");
|
||||||
|
}
|
||||||
|
|
||||||
|
const commitRun = await api.git.runStackedAction({
|
||||||
|
cwd: actionCwd,
|
||||||
|
action: "commit",
|
||||||
|
...(commitMessage.length > 0 ? { commitMessage } : {}),
|
||||||
|
});
|
||||||
|
commit = commitRun.commit;
|
||||||
|
|
||||||
|
if (includeGeneratedCommitMessage) {
|
||||||
|
if (commitRun.commit.status === "created") {
|
||||||
|
if (commitRun.commit.subject) {
|
||||||
|
setGitModalCommitMessage(commitRun.commit.subject);
|
||||||
|
}
|
||||||
|
updateStep("generate", "completed");
|
||||||
|
} else {
|
||||||
|
updateStep("generate", "skipped", "No local changes to commit.");
|
||||||
}
|
}
|
||||||
updateStep("generate", "completed");
|
|
||||||
} else {
|
|
||||||
updateStep("generate", "skipped", "No local changes to commit.");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (commitRun.commit.status === "created") {
|
if (commitRun.commit.status === "created") {
|
||||||
updateStep("commit", "completed", commitRun.commit.subject ?? "Committed local changes.");
|
updateStep("commit", "completed", commitRun.commit.subject ?? "Committed local changes.");
|
||||||
} else {
|
} else {
|
||||||
updateStep("commit", "skipped", "No local changes to commit.");
|
updateStep("commit", "skipped", "No local changes to commit.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action !== "commit") {
|
if (action !== "commit") {
|
||||||
updateStep("push", "running");
|
updateStep("push", "running");
|
||||||
const pushRun = await api.git.runStackedAction({
|
const pushRun = await api.git.runStackedAction({
|
||||||
cwd: actionCwd,
|
cwd: actionCwd,
|
||||||
action: "commit_push",
|
action: "commit_push",
|
||||||
|
});
|
||||||
|
push = pushRun.push;
|
||||||
|
if (pushRun.push.status === "pushed") {
|
||||||
|
updateStep(
|
||||||
|
"push",
|
||||||
|
"completed",
|
||||||
|
pushRun.push.upstreamBranch
|
||||||
|
? `Pushed to ${pushRun.push.upstreamBranch}.`
|
||||||
|
: "Pushed latest commits.",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
updateStep("push", "skipped", "Branch already up to date.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action === "commit_push_pr") {
|
||||||
|
updateStep("pr", "running");
|
||||||
|
const prRun = await api.git.runStackedAction({
|
||||||
|
cwd: actionCwd,
|
||||||
|
action: "commit_push_pr",
|
||||||
|
});
|
||||||
|
pr = prRun.pr;
|
||||||
|
if (prRun.pr.status === "opened_existing") {
|
||||||
|
updateStep(
|
||||||
|
"pr",
|
||||||
|
"completed",
|
||||||
|
prRun.pr.number ? `Opened existing PR #${prRun.pr.number}.` : "Opened existing PR.",
|
||||||
|
);
|
||||||
|
} else if (prRun.pr.status === "created") {
|
||||||
|
updateStep(
|
||||||
|
"pr",
|
||||||
|
"completed",
|
||||||
|
prRun.pr.number ? `Created PR #${prRun.pr.number}.` : "Created PR.",
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
updateStep("pr", "skipped", "PR step was not requested.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setGitModalResult({ action, commit, push, pr });
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : "Git action failed.";
|
||||||
|
setGitModalError(message);
|
||||||
|
setGitActionError(message);
|
||||||
|
setGitModalProgress((steps) => {
|
||||||
|
const active = steps.find((step) => step.status === "running");
|
||||||
|
if (!active) return steps;
|
||||||
|
return updateProgressStep(steps, active.id, "failed", message);
|
||||||
});
|
});
|
||||||
push = pushRun.push;
|
} finally {
|
||||||
if (pushRun.push.status === "pushed") {
|
setIsGitModalActionRunning(false);
|
||||||
updateStep(
|
await refreshGitStatus().catch(() => undefined);
|
||||||
"push",
|
|
||||||
"completed",
|
|
||||||
pushRun.push.upstreamBranch
|
|
||||||
? `Pushed to ${pushRun.push.upstreamBranch}.`
|
|
||||||
: "Pushed latest commits.",
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
updateStep("push", "skipped", "Branch already up to date.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
[api, gitCwd, refreshGitStatus],
|
||||||
|
);
|
||||||
|
|
||||||
if (action === "commit_push_pr") {
|
const runGitActionImmediately = useCallback(
|
||||||
updateStep("pr", "running");
|
(action: GitStackedAction) => {
|
||||||
const prRun = await api.git.runStackedAction({
|
void executeGitAction({
|
||||||
cwd: actionCwd,
|
action,
|
||||||
action: "commit_push_pr",
|
commitMessage: "",
|
||||||
});
|
includeGeneratedCommitMessage: false,
|
||||||
pr = prRun.pr;
|
closeModal: false,
|
||||||
if (prRun.pr.status === "opened_existing") {
|
|
||||||
updateStep(
|
|
||||||
"pr",
|
|
||||||
"completed",
|
|
||||||
prRun.pr.number ? `Opened existing PR #${prRun.pr.number}.` : "Opened existing PR.",
|
|
||||||
);
|
|
||||||
} else if (prRun.pr.status === "created") {
|
|
||||||
updateStep(
|
|
||||||
"pr",
|
|
||||||
"completed",
|
|
||||||
prRun.pr.number ? `Created PR #${prRun.pr.number}.` : "Created PR.",
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
updateStep("pr", "skipped", "PR step was not requested.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setGitModalResult({ action, commit, push, pr });
|
|
||||||
} catch (error) {
|
|
||||||
const message = error instanceof Error ? error.message : "Git action failed.";
|
|
||||||
setGitModalError(message);
|
|
||||||
setGitActionError(message);
|
|
||||||
setGitModalProgress((steps) => {
|
|
||||||
const active = steps.find((step) => step.status === "running");
|
|
||||||
if (!active) return steps;
|
|
||||||
return updateProgressStep(steps, active.id, "failed", message);
|
|
||||||
});
|
});
|
||||||
} finally {
|
},
|
||||||
setIsGitModalActionRunning(false);
|
[executeGitAction],
|
||||||
await refreshGitStatus().catch(() => undefined);
|
);
|
||||||
}
|
|
||||||
}, [
|
const runGitAction = useCallback(() => {
|
||||||
api,
|
if (!selectedGitModalActionOption || selectedGitModalActionOption.disabled) return;
|
||||||
gitCwd,
|
const commitMessage = gitModalCommitMessage.trim();
|
||||||
gitModalCommitMessage,
|
void executeGitAction({
|
||||||
refreshGitStatus,
|
action: selectedGitModalActionOption.action,
|
||||||
selectedGitModalActionOption,
|
commitMessage,
|
||||||
]);
|
includeGeneratedCommitMessage: commitMessage.length === 0,
|
||||||
|
closeModal: true,
|
||||||
|
});
|
||||||
|
}, [executeGitAction, gitModalCommitMessage, selectedGitModalActionOption]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setGitActionError(null);
|
setGitActionError(null);
|
||||||
@@ -592,7 +643,7 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
|||||||
}}
|
}}
|
||||||
disabled={!gitStatus || isGitActionRunning}
|
disabled={!gitStatus || isGitActionRunning}
|
||||||
>
|
>
|
||||||
{isGitActionRunning ? "Running..." : "Git actions"}
|
Git actions
|
||||||
<span aria-hidden="true">▾</span>
|
<span aria-hidden="true">▾</span>
|
||||||
</button>
|
</button>
|
||||||
{isGitMenuOpen && (
|
{isGitMenuOpen && (
|
||||||
@@ -793,7 +844,7 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
|||||||
type="button"
|
type="button"
|
||||||
className="rounded-xl bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
className="rounded-xl bg-foreground px-4 py-2 text-sm font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
void runGitAction();
|
runGitAction();
|
||||||
}}
|
}}
|
||||||
disabled={isGitActionRunning || !selectedGitModalActionOption}
|
disabled={isGitActionRunning || !selectedGitModalActionOption}
|
||||||
>
|
>
|
||||||
@@ -821,14 +872,20 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
|||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
<span className="min-w-0 truncate text-sm text-foreground">{gitProgressToastMessage}</span>
|
<span className="min-w-0 truncate text-sm text-foreground">{gitProgressToastMessage}</span>
|
||||||
{!isGitModalActionRunning && gitModalResult && gitProgressToastOpenPrUrl && (
|
{!isGitModalActionRunning && gitProgressToastAction && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="rounded-md bg-foreground px-2.5 py-1 text-xs font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
className="rounded-md bg-foreground px-2.5 py-1 text-xs font-medium text-background transition-colors duration-150 hover:bg-foreground/90 disabled:cursor-not-allowed disabled:opacity-60"
|
||||||
onClick={openPrFromToast}
|
onClick={() => {
|
||||||
|
if (gitProgressToastAction.kind === "open_pr") {
|
||||||
|
openPrFromToast();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
runGitActionImmediately("commit_push_pr");
|
||||||
|
}}
|
||||||
disabled={isGitModalActionRunning}
|
disabled={isGitModalActionRunning}
|
||||||
>
|
>
|
||||||
Open PR
|
{gitProgressToastAction.label}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
import type { GitRunStackedActionResult, GitStatusResult } from "@t3tools/contracts";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { getGitProgressToastAction } from "./components/GitActionsControl";
|
||||||
|
|
||||||
|
function makeStatus(overrides?: Partial<GitStatusResult>): GitStatusResult {
|
||||||
|
return {
|
||||||
|
branch: "feature/test-branch",
|
||||||
|
hasWorkingTreeChanges: false,
|
||||||
|
hasUpstream: true,
|
||||||
|
aheadCount: 0,
|
||||||
|
behindCount: 0,
|
||||||
|
openPr: null,
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeResult(overrides?: Partial<GitRunStackedActionResult>): GitRunStackedActionResult {
|
||||||
|
return {
|
||||||
|
action: "commit_push",
|
||||||
|
commit: { status: "skipped_no_changes" },
|
||||||
|
push: { status: "pushed", branch: "feature/test-branch" },
|
||||||
|
pr: { status: "skipped_not_requested" },
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("getGitProgressToastAction", () => {
|
||||||
|
it("returns open PR action when a PR URL is available", () => {
|
||||||
|
const action = getGitProgressToastAction({
|
||||||
|
isRunning: false,
|
||||||
|
hasError: false,
|
||||||
|
result: makeResult(),
|
||||||
|
gitStatus: makeStatus(),
|
||||||
|
openPrUrl: "https://github.com/pingdotgg/codething-mvp/pull/42",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(action).toEqual({
|
||||||
|
kind: "open_pr",
|
||||||
|
label: "Open PR",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns create PR action after push when branch can open a PR", () => {
|
||||||
|
const action = getGitProgressToastAction({
|
||||||
|
isRunning: false,
|
||||||
|
hasError: false,
|
||||||
|
result: makeResult({ action: "commit_push" }),
|
||||||
|
gitStatus: makeStatus(),
|
||||||
|
openPrUrl: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(action).toEqual({
|
||||||
|
kind: "create_pr",
|
||||||
|
label: "Create PR",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not return create PR action for non-push actions", () => {
|
||||||
|
const action = getGitProgressToastAction({
|
||||||
|
isRunning: false,
|
||||||
|
hasError: false,
|
||||||
|
result: makeResult({ action: "commit" }),
|
||||||
|
gitStatus: makeStatus(),
|
||||||
|
openPrUrl: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(action).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null while running or failed", () => {
|
||||||
|
const running = getGitProgressToastAction({
|
||||||
|
isRunning: true,
|
||||||
|
hasError: false,
|
||||||
|
result: makeResult(),
|
||||||
|
gitStatus: makeStatus(),
|
||||||
|
openPrUrl: "https://github.com/pingdotgg/codething-mvp/pull/42",
|
||||||
|
});
|
||||||
|
|
||||||
|
const failed = getGitProgressToastAction({
|
||||||
|
isRunning: false,
|
||||||
|
hasError: true,
|
||||||
|
result: makeResult(),
|
||||||
|
gitStatus: makeStatus(),
|
||||||
|
openPrUrl: "https://github.com/pingdotgg/codething-mvp/pull/42",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(running).toBeNull();
|
||||||
|
expect(failed).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user