Initial commit

This commit is contained in:
Jarek Krochmalski
2025-12-28 21:16:03 +01:00
commit 62e3c6439e
552 changed files with 104858 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import {
getGitRepositories,
createGitRepository,
getGitCredentials
} from '$lib/server/db';
import { authorize } from '$lib/server/authorize';
export const GET: RequestHandler = async ({ url, cookies }) => {
const auth = await authorize(cookies);
if (auth.authEnabled && !await auth.can('git', 'view')) {
return json({ error: 'Permission denied' }, { status: 403 });
}
try {
// Note: envId parameter is kept for backwards compatibility but repositories
// are now global (not tied to environments). Use git stacks for env-specific deployments.
const repositories = await getGitRepositories();
return json(repositories);
} catch (error) {
console.error('Failed to get git repositories:', error);
return json({ error: 'Failed to get git repositories' }, { status: 500 });
}
};
export const POST: RequestHandler = async ({ request, cookies }) => {
const auth = await authorize(cookies);
if (auth.authEnabled && !await auth.can('git', 'create')) {
return json({ error: 'Permission denied' }, { status: 403 });
}
try {
const data = await request.json();
if (!data.name || typeof data.name !== 'string') {
return json({ error: 'Name is required' }, { status: 400 });
}
if (!data.url || typeof data.url !== 'string') {
return json({ error: 'Repository URL is required' }, { status: 400 });
}
// Validate credential if provided
if (data.credentialId) {
const credentials = await getGitCredentials();
const credential = credentials.find(c => c.id === data.credentialId);
if (!credential) {
return json({ error: 'Invalid credential ID' }, { status: 400 });
}
}
// Create repository with just the basic fields
// Deployment-specific config (composePath, autoUpdate, webhook) now belongs to git_stacks
const repository = await createGitRepository({
name: data.name,
url: data.url,
branch: data.branch || 'main',
credentialId: data.credentialId || null
});
return json(repository);
} catch (error: any) {
console.error('Failed to create git repository:', error);
if (error.message?.includes('UNIQUE constraint failed')) {
return json({ error: 'A repository with this name already exists' }, { status: 400 });
}
return json({ error: 'Failed to create git repository' }, { status: 500 });
}
};

View File

@@ -0,0 +1,112 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import {
getGitRepository,
updateGitRepository,
deleteGitRepository,
getGitCredentials
} from '$lib/server/db';
import { deleteRepositoryFiles } from '$lib/server/git';
import { authorize } from '$lib/server/authorize';
export const GET: RequestHandler = async ({ params, cookies }) => {
const auth = await authorize(cookies);
if (auth.authEnabled && !await auth.can('git', 'view')) {
return json({ error: 'Permission denied' }, { status: 403 });
}
try {
const id = parseInt(params.id);
if (isNaN(id)) {
return json({ error: 'Invalid repository ID' }, { status: 400 });
}
const repository = await getGitRepository(id);
if (!repository) {
return json({ error: 'Repository not found' }, { status: 404 });
}
return json(repository);
} catch (error) {
console.error('Failed to get git repository:', error);
return json({ error: 'Failed to get git repository' }, { status: 500 });
}
};
export const PUT: RequestHandler = async ({ params, request, cookies }) => {
const auth = await authorize(cookies);
if (auth.authEnabled && !await auth.can('git', 'edit')) {
return json({ error: 'Permission denied' }, { status: 403 });
}
try {
const id = parseInt(params.id);
if (isNaN(id)) {
return json({ error: 'Invalid repository ID' }, { status: 400 });
}
const existing = await getGitRepository(id);
if (!existing) {
return json({ error: 'Repository not found' }, { status: 404 });
}
const data = await request.json();
// Validate credential if provided
if (data.credentialId) {
const credentials = await getGitCredentials();
const credential = credentials.find(c => c.id === data.credentialId);
if (!credential) {
return json({ error: 'Invalid credential ID' }, { status: 400 });
}
}
// Update only the basic repository fields
// Deployment-specific config (composePath, autoUpdate, webhook) now belongs to git_stacks
const repository = await updateGitRepository(id, {
name: data.name,
url: data.url,
branch: data.branch,
credentialId: data.credentialId
});
if (!repository) {
return json({ error: 'Failed to update repository' }, { status: 500 });
}
return json(repository);
} catch (error: any) {
console.error('Failed to update git repository:', error);
if (error.message?.includes('UNIQUE constraint failed')) {
return json({ error: 'A repository with this name already exists' }, { status: 400 });
}
return json({ error: 'Failed to update git repository' }, { status: 500 });
}
};
export const DELETE: RequestHandler = async ({ params, cookies }) => {
const auth = await authorize(cookies);
if (auth.authEnabled && !await auth.can('git', 'delete')) {
return json({ error: 'Permission denied' }, { status: 403 });
}
try {
const id = parseInt(params.id);
if (isNaN(id)) {
return json({ error: 'Invalid repository ID' }, { status: 400 });
}
// Delete repository files first
deleteRepositoryFiles(id);
const deleted = await deleteGitRepository(id);
if (!deleted) {
return json({ error: 'Repository not found' }, { status: 404 });
}
return json({ success: true });
} catch (error) {
console.error('Failed to delete git repository:', error);
return json({ error: 'Failed to delete git repository' }, { status: 500 });
}
};

View File

@@ -0,0 +1,24 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getGitRepository } from '$lib/server/db';
import { deployFromRepository } from '$lib/server/git';
export const POST: RequestHandler = async ({ params }) => {
try {
const id = parseInt(params.id);
if (isNaN(id)) {
return json({ error: 'Invalid repository ID' }, { status: 400 });
}
const repository = await getGitRepository(id);
if (!repository) {
return json({ error: 'Repository not found' }, { status: 404 });
}
const result = await deployFromRepository(id);
return json(result);
} catch (error: any) {
console.error('Failed to deploy from git repository:', error);
return json({ success: false, error: error.message }, { status: 500 });
}
};

View File

@@ -0,0 +1,45 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getGitRepository } from '$lib/server/db';
import { syncRepository, checkForUpdates } from '$lib/server/git';
export const POST: RequestHandler = async ({ params }) => {
try {
const id = parseInt(params.id);
if (isNaN(id)) {
return json({ error: 'Invalid repository ID' }, { status: 400 });
}
const repository = await getGitRepository(id);
if (!repository) {
return json({ error: 'Repository not found' }, { status: 404 });
}
const result = await syncRepository(id);
return json(result);
} catch (error: any) {
console.error('Failed to sync git repository:', error);
return json({ success: false, error: error.message }, { status: 500 });
}
};
export const GET: RequestHandler = async ({ params }) => {
// Check for updates without syncing
try {
const id = parseInt(params.id);
if (isNaN(id)) {
return json({ error: 'Invalid repository ID' }, { status: 400 });
}
const repository = await getGitRepository(id);
if (!repository) {
return json({ error: 'Repository not found' }, { status: 404 });
}
const result = await checkForUpdates(id);
return json(result);
} catch (error: any) {
console.error('Failed to check for updates:', error);
return json({ hasUpdates: false, error: error.message }, { status: 500 });
}
};

View File

@@ -0,0 +1,24 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getGitRepository } from '$lib/server/db';
import { testRepository } from '$lib/server/git';
export const POST: RequestHandler = async ({ params }) => {
try {
const id = parseInt(params.id);
if (isNaN(id)) {
return json({ error: 'Invalid repository ID' }, { status: 400 });
}
const repository = await getGitRepository(id);
if (!repository) {
return json({ error: 'Repository not found' }, { status: 404 });
}
const result = await testRepository(id);
return json(result);
} catch (error: any) {
console.error('Failed to test git repository:', error);
return json({ success: false, error: error.message }, { status: 500 });
}
};

View File

@@ -0,0 +1,41 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { testRepositoryConfig } from '$lib/server/git';
import { authorize } from '$lib/server/authorize';
/**
* POST /api/git/repositories/test
* Test a git repository configuration before saving.
* Uses stored credentials via credentialId.
*
* Body: {
* url: string; // Repository URL to test
* branch: string; // Branch name to verify
* credentialId?: number; // Optional credential ID from database
* }
*/
export const POST: RequestHandler = async ({ request, cookies }) => {
const auth = await authorize(cookies);
if (auth.authEnabled && !await auth.can('settings', 'manage')) {
return json({ error: 'Permission denied' }, { status: 403 });
}
try {
const body = await request.json();
if (!body.url || typeof body.url !== 'string') {
return json({ error: 'Repository URL is required' }, { status: 400 });
}
const result = await testRepositoryConfig({
url: body.url,
branch: body.branch || 'main',
credentialId: body.credentialId ?? null
});
return json(result);
} catch (error) {
console.error('Failed to test repository:', error);
return json({ success: false, error: 'Failed to test repository' }, { status: 500 });
}
};