mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-17 05:58:37 +00:00
feat(git-sources): create a stack from a Git repository (#606)
* refactor(git-sources): extract GitSourceFields from GitSourcePanel Pure extraction of the repo/branch/path/auth/apply-mode form fields into a reusable controlled component so the upcoming Create Stack from Git flow can render the same form in the Create Stack dialog. No behavior change. * feat(git-sources): create a stack from a Git repository Add a From Git tab to the Create Stack dialog so users can name a new stack, point it at a repo + branch + compose path, and have the compose fetched, validated, written to disk, and linked in one shot. Optional deploy-after-create runs the initial bring-up when requested. Backend: new POST /api/stacks/from-git route gated by stack:create. GitSourceService.createStackFromGit() fetches and validates before touching disk, then creates the stack, writes the compose (and .env if sync is enabled), and seeds the git source row with the fetched commit so future pulls produce a clean diff. Runs under the per-stack lock so a concurrent webhook cannot race the create. Deploy failure is non-fatal and surfaced to the caller. Frontend: the existing Create Stack dialog is now tabbed, with Empty keeping the original single-field flow unchanged. * test(git-sources): cover create-from-git endpoint and e2e flow Service tests verify createStackFromGit seeds the last_applied columns on success, writes the env file when sync is enabled, refuses an invalid apply-matrix without fetching, rejects invalid compose without leaving orphan state, and rolls back the on-disk stack dir when a post-create step fails. Route tests cover auth, missing stack_name, invalid stack name, http:// rejection, oversized repo_url, and the 409 collision guard. E2E adds a Create-stack-from-Git block covering tab visibility, client-side HTTPS check, backend .git/config rejection, and a happy-path fetch against a public demo repo (skipped on network failure). * docs(git-sources): document create-stack-from-git tab Add a new section near the top describing the From Git tab in the Create Stack dialog: what it does, the Deploy after create checkbox, and the four failure modes (name collision, unreachable repo, invalid compose, deploy-after-create failure).
This commit is contained in:
@@ -165,6 +165,68 @@ describe('git-source routes — invalid stack names', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/stacks/from-git', () => {
|
||||
const validBody = {
|
||||
stack_name: 'route-from-git',
|
||||
repo_url: 'https://github.com/example/repo.git',
|
||||
branch: 'main',
|
||||
compose_path: 'compose.yaml',
|
||||
auth_type: 'none' as const,
|
||||
};
|
||||
|
||||
it('returns 401 without auth', async () => {
|
||||
const res = await request(app).post('/api/stacks/from-git').send(validBody);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it('rejects missing stack_name with 400', async () => {
|
||||
const { stack_name: _unused, ...body } = validBody;
|
||||
void _unused;
|
||||
const res = await request(app)
|
||||
.post('/api/stacks/from-git')
|
||||
.set('Authorization', `Bearer ${adminToken()}`)
|
||||
.send(body);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/stack_name/i);
|
||||
});
|
||||
|
||||
it('rejects invalid stack name with 400', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/stacks/from-git')
|
||||
.set('Authorization', `Bearer ${adminToken()}`)
|
||||
.send({ ...validBody, stack_name: '../escape' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/stack name/i);
|
||||
});
|
||||
|
||||
it('rejects http:// URLs with 400', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/stacks/from-git')
|
||||
.set('Authorization', `Bearer ${adminToken()}`)
|
||||
.send({ ...validBody, repo_url: 'http://github.com/example/repo.git' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/HTTPS/i);
|
||||
});
|
||||
|
||||
it('rejects oversized repo_url with 400', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/stacks/from-git')
|
||||
.set('Authorization', `Bearer ${adminToken()}`)
|
||||
.send({ ...validBody, repo_url: 'https://example.com/' + 'a'.repeat(2048) });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/repo_url/i);
|
||||
});
|
||||
|
||||
it('returns 409 when a stack with that name already exists on disk', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/stacks/from-git')
|
||||
.set('Authorization', `Bearer ${adminToken()}`)
|
||||
.send({ ...validBody, stack_name: 'existing-stack' });
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error).toMatch(/already exists/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/git-sources', () => {
|
||||
it('returns 200 and a JSON array for an authenticated admin', async () => {
|
||||
const res = await request(app)
|
||||
|
||||
@@ -493,6 +493,155 @@ describe('GitSourceService.pull', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('GitSourceService.createStackFromGit', () => {
|
||||
async function cleanupStackDir(name: string) {
|
||||
const { FileSystemService } = await import('../services/FileSystemService');
|
||||
try {
|
||||
await FileSystemService.getInstance().deleteStack(name);
|
||||
} catch {
|
||||
// directory may not exist; ignore
|
||||
}
|
||||
}
|
||||
|
||||
it('creates a stack on disk, writes compose, and seeds last_applied columns', async () => {
|
||||
const sha = 'fedcba9876543210fedcba9876543210fedcba98';
|
||||
mockSuccessfulClone({
|
||||
compose: 'services:\n web:\n image: nginx\n',
|
||||
sha,
|
||||
});
|
||||
const svc = GitSourceService.getInstance();
|
||||
|
||||
const result = await svc.createStackFromGit({
|
||||
stackName: 'create-happy',
|
||||
repoUrl: 'https://github.com/example/repo.git',
|
||||
branch: 'main',
|
||||
composePath: 'compose.yaml',
|
||||
syncEnv: false,
|
||||
envPath: null,
|
||||
authType: 'none',
|
||||
token: null,
|
||||
autoApplyOnWebhook: false,
|
||||
autoDeployOnApply: false,
|
||||
});
|
||||
|
||||
expect(result.commitSha).toBe(sha);
|
||||
expect(result.envWritten).toBe(false);
|
||||
expect(result.source.last_applied_commit_sha).toBe(sha);
|
||||
expect(result.source.pending_commit_sha).toBeNull();
|
||||
|
||||
const { FileSystemService } = await import('../services/FileSystemService');
|
||||
const onDisk = await FileSystemService.getInstance().getStackContent('create-happy');
|
||||
expect(onDisk).toContain('image: nginx');
|
||||
|
||||
await cleanupStackDir('create-happy');
|
||||
});
|
||||
|
||||
it('writes the env file when sync_env is enabled', async () => {
|
||||
const sha = '0101010101010101010101010101010101010101';
|
||||
mockSuccessfulClone({
|
||||
compose: 'services:\n web:\n image: nginx\n',
|
||||
env: 'FOO=bar\n',
|
||||
envPath: '.env',
|
||||
sha,
|
||||
});
|
||||
const svc = GitSourceService.getInstance();
|
||||
|
||||
const result = await svc.createStackFromGit({
|
||||
stackName: 'create-env',
|
||||
repoUrl: 'https://github.com/example/repo.git',
|
||||
branch: 'main',
|
||||
composePath: 'compose.yaml',
|
||||
syncEnv: true,
|
||||
envPath: '.env',
|
||||
authType: 'none',
|
||||
token: null,
|
||||
autoApplyOnWebhook: false,
|
||||
autoDeployOnApply: false,
|
||||
});
|
||||
expect(result.envWritten).toBe(true);
|
||||
|
||||
const { FileSystemService } = await import('../services/FileSystemService');
|
||||
const env = await FileSystemService.getInstance().getEnvContent('create-env');
|
||||
expect(env).toBe('FOO=bar\n');
|
||||
|
||||
await cleanupStackDir('create-env');
|
||||
});
|
||||
|
||||
it('rejects an invalid apply-matrix without fetching or writing disk', async () => {
|
||||
const svc = GitSourceService.getInstance();
|
||||
await expect(svc.createStackFromGit({
|
||||
stackName: 'create-bad-matrix',
|
||||
repoUrl: 'https://github.com/example/repo.git',
|
||||
branch: 'main',
|
||||
composePath: 'compose.yaml',
|
||||
syncEnv: false,
|
||||
envPath: null,
|
||||
authType: 'none',
|
||||
token: null,
|
||||
autoApplyOnWebhook: false,
|
||||
autoDeployOnApply: true,
|
||||
})).rejects.toBeInstanceOf(GitSourceError);
|
||||
|
||||
expect(mockGitClone).not.toHaveBeenCalled();
|
||||
expect(DatabaseService.getInstance().getGitSource('create-bad-matrix')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects when compose validation fails and leaves no stack/row behind', async () => {
|
||||
mockSuccessfulClone({
|
||||
// Non-mapping root is rejected by validateCompose() pre-check
|
||||
compose: '- not-a-mapping\n',
|
||||
});
|
||||
const svc = GitSourceService.getInstance();
|
||||
|
||||
await expect(svc.createStackFromGit({
|
||||
stackName: 'create-bad-yaml',
|
||||
repoUrl: 'https://github.com/example/repo.git',
|
||||
branch: 'main',
|
||||
composePath: 'compose.yaml',
|
||||
syncEnv: false,
|
||||
envPath: null,
|
||||
authType: 'none',
|
||||
token: null,
|
||||
autoApplyOnWebhook: false,
|
||||
autoDeployOnApply: false,
|
||||
})).rejects.toMatchObject({ code: 'GIT_ERROR' });
|
||||
|
||||
expect(DatabaseService.getInstance().getGitSource('create-bad-yaml')).toBeUndefined();
|
||||
const { FileSystemService } = await import('../services/FileSystemService');
|
||||
const stacks = await FileSystemService.getInstance().getStacks();
|
||||
expect(stacks).not.toContain('create-bad-yaml');
|
||||
});
|
||||
|
||||
it('rolls back the stack dir when a post-create step fails', async () => {
|
||||
mockSuccessfulClone({
|
||||
compose: 'services:\n web:\n image: nginx\n',
|
||||
});
|
||||
const { FileSystemService } = await import('../services/FileSystemService');
|
||||
const saveSpy = vi.spyOn(FileSystemService.prototype, 'saveStackContent')
|
||||
.mockRejectedValueOnce(new Error('simulated disk failure'));
|
||||
|
||||
const svc = GitSourceService.getInstance();
|
||||
await expect(svc.createStackFromGit({
|
||||
stackName: 'create-rollback',
|
||||
repoUrl: 'https://github.com/example/repo.git',
|
||||
branch: 'main',
|
||||
composePath: 'compose.yaml',
|
||||
syncEnv: false,
|
||||
envPath: null,
|
||||
authType: 'none',
|
||||
token: null,
|
||||
autoApplyOnWebhook: false,
|
||||
autoDeployOnApply: false,
|
||||
})).rejects.toThrow(/simulated disk failure/);
|
||||
|
||||
expect(DatabaseService.getInstance().getGitSource('create-rollback')).toBeUndefined();
|
||||
const stacks = await FileSystemService.getInstance().getStacks();
|
||||
expect(stacks).not.toContain('create-rollback');
|
||||
|
||||
saveSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GitSourceService.apply', () => {
|
||||
async function seedPending(stackName: string, composeContent: string, commitSha: string) {
|
||||
mockSuccessfulClone({ compose: composeContent, sha: commitSha });
|
||||
|
||||
Reference in New Issue
Block a user