Files
sencho/frontend/src/components/stack/GitSourceFields.tsx
T
Anso 6529a24530 feat(git-sources): harden create-from-git with LFS + submodule warnings (#609)
* feat(git-sources): surface LFS and submodule warnings on create

Creating a stack from a Git repo now detects two common anomalies and
tells the user about them rather than silently producing broken stacks.

- LFS-pointer compose/env files fail early with a clear error instead
  of writing a 130-byte pointer stub to disk as real content.
- Repositories containing .gitmodules produce a non-fatal warning so
  the user knows build contexts or volumes inside submodules will be
  empty at deploy time.

Also refines the create dialog: sr-only DialogDescription for a11y,
short commit SHA suffix on the success toast, env-path hint under the
"Sync .env" checkbox showing which path will be read, and a route-level
diagnostic log line gated on developer mode for support debugging.

* test(git-sources): cover LFS, submodule, and nested env_path paths

Adds unit coverage for the new LFS-pointer rejection and submodule
warning plumbing, plus a nested compose_path case that exercises the
default env_path resolution ("apps/web/compose.yaml" with sync_env on
and env_path unset writes "apps/web/.env" both to disk and to the DB).

Extends the E2E suite with a happy-path assertion that the full-length
commit SHA is returned in the create response, and a UI flow that
verifies the short-SHA suffix appears in the success toast.

* docs(git-sources): add troubleshooting for LFS, submodules, HTTPS-only

Adds troubleshooting entries for the newly surfaced LFS and submodule
anomalies, expands the clone-timeout entry with the bounded-fetch
explanation, and adds a dedicated HTTPS-only entry. Also consolidates
the known limitations into a single list covering LFS, submodules,
branch-tracking, and HTTPS-only.

* fix(settings): use Route icon for notification routing

The routing section in Settings previously used GitBranch, which now
clashes with the Git Source feature's icon across the editor. Switch
to Route (a branching-flow glyph) so routing rules have a distinct
visual identity and aren't visually conflated with Git-backed stacks.

* fix(git-sources): return 400 for upstream auth failures and disambiguate 404s

Upstream git-host auth failures were mapping to HTTP 401, which the frontend
apiFetch treats as a Sencho session expiry and fires the global logout event.
They now return 400 with code=AUTH_FAILED in the body so the UI can branch on
the discriminator without logging the user out. The status mapping moved into
utils/gitSourceHttp so it can be unit-tested without booting the app.

mapGitError also relied on the HttpError class alone, so any non-2xx response
(including 404) was classified as auth failure. It now inspects the numeric
status on err.data and considers whether a token was supplied, producing more
actionable messages for missing repos, private repos, and wrong-scope tokens.
2026-04-15 11:31:29 -04:00

229 lines
7.8 KiB
TypeScript

import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Checkbox } from '@/components/ui/checkbox';
import { cn } from '@/lib/utils';
export type ApplyMode = 'review' | 'auto-write' | 'auto-deploy';
/**
* Mirror of the backend's env-path default (see `/api/stacks/from-git` and
* the git-source PUT handler): if the user ticks "Sync .env" without
* specifying an explicit path, the service reads `<dirname>/.env`
* alongside the compose file. Surfacing this in the form saves the user
* a round-trip to figure out which directory the `.env` will come from.
*/
function computeDefaultEnvPath(composePath: string): string {
const normalized = composePath.trim().replace(/\\/g, '/').replace(/^\.\//, '');
const slash = normalized.lastIndexOf('/');
if (slash === -1) return '.env';
return `${normalized.slice(0, slash)}/.env`;
}
export interface GitSourceFieldsState {
repoUrl: string;
branch: string;
composePath: string;
syncEnv: boolean;
authType: 'none' | 'token';
token: string;
/** When editing an existing source, the server tells us whether a token is already stored. */
hasStoredToken: boolean;
applyMode: ApplyMode;
}
export interface GitSourceFieldsProps extends GitSourceFieldsState {
disabled?: boolean;
/** 'edit' for the per-stack panel, 'create' for the new-stack dialog. Changes apply-mode copy. */
variant: 'edit' | 'create';
onRepoUrlChange: (value: string) => void;
onBranchChange: (value: string) => void;
onComposePathChange: (value: string) => void;
onSyncEnvChange: (value: boolean) => void;
onAuthTypeChange: (value: 'none' | 'token') => void;
onTokenChange: (value: string) => void;
onApplyModeChange: (value: ApplyMode) => void;
}
const APPLY_MODE_COPY: Record<'edit' | 'create', Record<ApplyMode, { title: string; description: string }>> = {
edit: {
'review': { title: 'Review only', description: 'Webhook fetches and flags a pending diff. You apply manually.' },
'auto-write': { title: 'Auto-write files', description: 'Webhook writes to disk. You deploy manually.' },
'auto-deploy': { title: 'Auto-deploy', description: 'Webhook writes and deploys in one step.' },
},
create: {
'review': { title: 'Review only', description: 'Future webhook pulls surface a diff you apply manually.' },
'auto-write': { title: 'Auto-write files', description: 'Future webhook pulls write to disk. You deploy manually.' },
'auto-deploy': { title: 'Auto-deploy', description: 'Future webhook pulls write and redeploy automatically.' },
},
};
export function GitSourceFields({
repoUrl,
branch,
composePath,
syncEnv,
authType,
token,
hasStoredToken,
applyMode,
disabled = false,
variant,
onRepoUrlChange,
onBranchChange,
onComposePathChange,
onSyncEnvChange,
onAuthTypeChange,
onTokenChange,
onApplyModeChange,
}: GitSourceFieldsProps) {
const copy = APPLY_MODE_COPY[variant];
const radioOption = (mode: ApplyMode) => (
<button
type="button"
key={mode}
onClick={() => !disabled && onApplyModeChange(mode)}
disabled={disabled}
className={cn(
'w-full text-left rounded-md border px-3 py-2 transition-colors',
applyMode === mode
? 'border-brand/60 bg-brand/5'
: 'border-glass-border hover:border-card-border-hover',
disabled && 'cursor-not-allowed opacity-60',
)}
>
<div className="flex items-start gap-2">
<div className={cn(
'w-3.5 h-3.5 rounded-full border mt-0.5 shrink-0 transition-colors',
applyMode === mode ? 'border-brand bg-brand' : 'border-stat-subtitle',
)} />
<div>
<p className="text-xs font-medium">{copy[mode].title}</p>
<p className="text-[11px] text-stat-subtitle mt-0.5">{copy[mode].description}</p>
</div>
</div>
</button>
);
return (
<div className="space-y-5">
<div className="space-y-2">
<Label htmlFor="git-source-repo">Repository URL</Label>
<Input
id="git-source-repo"
placeholder="https://github.com/org/repo.git"
value={repoUrl}
onChange={(e) => onRepoUrlChange(e.target.value)}
disabled={disabled}
className="font-mono text-xs"
/>
</div>
<div className="grid grid-cols-2 gap-3">
<div className="space-y-2">
<Label htmlFor="git-source-branch">Branch</Label>
<Input
id="git-source-branch"
placeholder="main"
value={branch}
onChange={(e) => onBranchChange(e.target.value)}
disabled={disabled}
className="font-mono text-xs"
/>
</div>
<div className="space-y-2">
<Label htmlFor="git-source-path">Compose file path</Label>
<Input
id="git-source-path"
placeholder="compose.yaml"
value={composePath}
onChange={(e) => onComposePathChange(e.target.value)}
disabled={disabled}
className="font-mono text-xs"
/>
</div>
</div>
<div className="space-y-1">
<div className="flex items-center gap-2">
<Checkbox
id="git-source-sync-env"
checked={syncEnv}
onCheckedChange={(c) => onSyncEnvChange(c === true)}
disabled={disabled}
/>
<Label htmlFor="git-source-sync-env" className="text-xs cursor-pointer">
Also sync sibling <span className="font-mono">.env</span> file
</Label>
</div>
{syncEnv && composePath.trim() !== '' && (
<p className="text-[11px] text-stat-subtitle pl-6">
Will read{' '}
<span className="font-mono">
{computeDefaultEnvPath(composePath)}
</span>{' '}
from the repository.
</p>
)}
</div>
<div className="space-y-2">
<Label>Authentication</Label>
<div className="flex gap-2">
<button
type="button"
onClick={() => !disabled && onAuthTypeChange('none')}
disabled={disabled}
className={cn(
'flex-1 rounded-md border px-3 py-1.5 text-xs transition-colors',
authType === 'none'
? 'border-brand/60 bg-brand/5'
: 'border-glass-border hover:border-card-border-hover',
)}
>
Public (no auth)
</button>
<button
type="button"
onClick={() => !disabled && onAuthTypeChange('token')}
disabled={disabled}
className={cn(
'flex-1 rounded-md border px-3 py-1.5 text-xs transition-colors',
authType === 'token'
? 'border-brand/60 bg-brand/5'
: 'border-glass-border hover:border-card-border-hover',
)}
>
Personal Access Token
</button>
</div>
{authType === 'token' && (
<div className="space-y-1.5">
<Input
type="password"
placeholder={hasStoredToken ? '•••••••• (leave blank to keep current)' : 'ghp_xxx... or glpat-xxx...'}
value={token}
onChange={(e) => onTokenChange(e.target.value)}
disabled={disabled}
className="font-mono text-xs"
autoComplete="off"
/>
<p className="text-[11px] text-stat-subtitle">
Token is encrypted at rest and never returned from the API.
</p>
</div>
)}
</div>
<div className="space-y-2">
<Label>Apply behavior</Label>
<div className="space-y-1.5">
{radioOption('review')}
{radioOption('auto-write')}
{radioOption('auto-deploy')}
</div>
</div>
</div>
);
}