Skip to content

Modern Docker Management, Designed for Everyone

3.5 KB
Raw Blame
import type {
	GitOpsSyncCreateDto,
	GitOpsSyncUpdateDto,
	GitOpsSync,
	GitOpsSyncCounts,
	SyncResult,
	SyncStatus,
	BrowseResponse,
	ImportGitOpsSyncRequest,
	ImportGitOpsSyncResponse,
	GitOpsBackupPreview,
	GitOpsBackupHistoryResponse,
	GitOpsBackupRevision,
	ResolveGitOpsBackupConflictRequest
} from '#lib/types/automation.js';
import type { Paginated, SearchPaginationSortRequest } from '#lib/types/shared.js';
import { transformPaginationParams } from '#lib/utils/tables.js';

import BaseAPIService from './api-service';

class GitOpsSyncService extends BaseAPIService {
	async getSyncs(environmentId: string, options?: SearchPaginationSortRequest): Promise<Paginated<GitOpsSync, GitOpsSyncCounts>> {
		const params = transformPaginationParams(options);
		const res = await this.api.get(`/environments/${environmentId}/gitops-syncs`, { params });
		return res.data;
	}

	async getSync(environmentId: string, syncId: string): Promise<GitOpsSync> {
		return this.handleResponse(this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}`));
	}

	async createSync(environmentId: string, sync: GitOpsSyncCreateDto): Promise<GitOpsSync> {
		return this.handleResponse(this.api.post(`/environments/${environmentId}/gitops-syncs`, sync));
	}

	async updateSync(environmentId: string, syncId: string, sync: GitOpsSyncUpdateDto): Promise<GitOpsSync> {
		return this.handleResponse(this.api.put(`/environments/${environmentId}/gitops-syncs/${syncId}`, sync));
	}

	async deleteSync(environmentId: string, syncId: string): Promise<void> {
		return this.handleResponse(this.api.delete(`/environments/${environmentId}/gitops-syncs/${syncId}`));
	}

	async performSync(environmentId: string, syncId: string): Promise<SyncResult> {
		return this.handleResponse(this.api.post(`/environments/${environmentId}/gitops-syncs/${syncId}/sync`));
	}

	async getSyncStatus(environmentId: string, syncId: string): Promise<SyncStatus> {
		return this.handleResponse(this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/status`));
	}

	async browseFiles(environmentId: string, syncId: string, path?: string): Promise<BrowseResponse> {
		const params = path ? { path } : {};
		return this.handleResponse(this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/files`, { params }));
	}

	async importSyncs(environmentId: string, syncs: ImportGitOpsSyncRequest[]): Promise<ImportGitOpsSyncResponse> {
		return this.handleResponse(this.api.post(`/environments/${environmentId}/gitops-syncs/import`, syncs));
	}

	async previewBackup(environmentId: string, syncId: string): Promise<GitOpsBackupPreview> {
		return this.handleResponse(this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/preview`));
	}

	async getBackupHistory(environmentId: string, syncId: string, limit = 20): Promise<GitOpsBackupHistoryResponse> {
		return this.handleResponse(
			this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/history`, { params: { limit } })
		);
	}

	async getBackupRevision(environmentId: string, syncId: string, commit: string): Promise<GitOpsBackupRevision> {
		return this.handleResponse(
			this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/history/${encodeURIComponent(commit)}`)
		);
	}

	async resolveBackupConflict(
		environmentId: string,
		syncId: string,
		request: ResolveGitOpsBackupConflictRequest
	): Promise<SyncResult> {
		return this.handleResponse(this.api.post(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/resolve`, request));
	}
}

export const gitOpsSyncService = new GitOpsSyncService();