import { apiRequest } from '../../lib/api'; import { filterAppsByCurrentAdmin } from '../../lib/admin-app-permissions'; import type { AppRelease, AppWithComponents, BuildJob, ComponentType, DeploymentJob, GiteaRepo, RuntimeControlJob, SourceComponent } from '../../types/application'; import type { ApiAppComponent } from '../../types/runtime'; export function listApps() { return apiRequest('/apps').then(filterAppsByCurrentAdmin); } export function createApp(input: { key: string; name: string; description?: string; namespace?: string; status?: string; }) { return apiRequest<{ app: AppWithComponents }>('/apps', { method: 'POST', body: JSON.stringify(input) }); } export function createComponent( appId: string, input: { name: string; type: ComponentType; imageSource?: string; externalImage?: string; workloadType?: string; replicas?: number; serviceType?: string; storageEnabled?: boolean; storageSize?: string; storageMountPath?: string; storageRetain?: boolean; repository?: string; branch?: string; buildTarget?: string; port?: number; targetPort?: number; ports?: Array<{ name?: string; port?: number; targetPort?: number; protocol?: string }>; } ) { return apiRequest<{ component: ApiAppComponent }>(`/apps/${appId}/components`, { method: 'POST', body: JSON.stringify(input) }); } export function listSources() { return apiRequest('/apps/sources'); } export function linkComponentSource( componentId: string, input: { repository: string; branch?: string; buildTarget?: string } ) { return apiRequest<{ component: SourceComponent }>(`/apps/components/${componentId}/source`, { method: 'PATCH', body: JSON.stringify(input) }); } export function listGiteaRepos(owner = 'platform', limit = 50) { return apiRequest<{ owner: string | null; repos: GiteaRepo[] }>( `/gitea/repos?owner=${encodeURIComponent(owner)}&limit=${limit}` ).then((result) => result.repos); } export function createGiteaOrgRepo( org: string, input: { name: string; description?: string; private?: boolean; autoInit?: boolean; createDevelopBranch?: boolean; createReleaseBranch?: boolean; } ) { return apiRequest<{ repo: GiteaRepo; branches: unknown }>(`/gitea/orgs/${encodeURIComponent(org)}/repos`, { method: 'POST', body: JSON.stringify(input) }).then((result) => result.repo); } export function listBuildJobs(appKey?: string) { const query = appKey ? `?appKey=${encodeURIComponent(appKey)}` : ''; return apiRequest(`/apps/builds${query}`); } export function createBuildJob(input: { componentId: string; branch?: string; commitSha?: string }) { return apiRequest<{ buildJob: BuildJob }>('/apps/builds', { method: 'POST', body: JSON.stringify(input) }); } export function cancelBuildJob(buildJobId: string) { return apiRequest<{ buildJob: BuildJob }>(`/apps/builds/${buildJobId}/cancel`, { method: 'PATCH' }); } export function deleteBuildJob(buildJobId: string) { return apiRequest<{ deleted: boolean }>(`/apps/builds/${buildJobId}`, { method: 'DELETE' }); } export function listReleases(appKey?: string) { const query = appKey ? `?appKey=${encodeURIComponent(appKey)}` : ''; return apiRequest(`/apps/releases${query}`); } export function createRelease(input: { appId: string; version: string; branch?: string; buildJobIds: string[] }) { return apiRequest<{ release: AppRelease }>('/apps/releases', { method: 'POST', body: JSON.stringify(input) }); } export function listDeployments(appKey?: string) { const query = appKey ? `?appKey=${encodeURIComponent(appKey)}` : ''; return apiRequest(`/apps/deployments${query}`); } export function createDeployment(input: { releaseId: string; environment: string; namespace: string; updateLauncherAppVersion?: boolean }) { return apiRequest<{ deploymentJob: DeploymentJob }>('/apps/deployments', { method: 'POST', body: JSON.stringify(input) }); } export function listMigrations() { return apiRequest('/apps/migrations'); } export function createMigration(input: { releaseId: string; migrationComponentId: string; imageComponentName: string; namespace: string; command: string; envSecretName: string; }) { return apiRequest<{ migrationJob: RuntimeControlJob }>('/apps/migrations', { method: 'POST', body: JSON.stringify(input) }); }