mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-06 21:29:05 +00:00
Initial commit
This commit is contained in:
70
routes/api/git/repositories/+server.ts
Normal file
70
routes/api/git/repositories/+server.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
112
routes/api/git/repositories/[id]/+server.ts
Normal file
112
routes/api/git/repositories/[id]/+server.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
24
routes/api/git/repositories/[id]/deploy/+server.ts
Normal file
24
routes/api/git/repositories/[id]/deploy/+server.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
45
routes/api/git/repositories/[id]/sync/+server.ts
Normal file
45
routes/api/git/repositories/[id]/sync/+server.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
24
routes/api/git/repositories/[id]/test/+server.ts
Normal file
24
routes/api/git/repositories/[id]/test/+server.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
41
routes/api/git/repositories/test/+server.ts
Normal file
41
routes/api/git/repositories/test/+server.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user