slug first logic
This commit is contained in:
@@ -11,6 +11,14 @@ import { requestApi } from '../lib/api';
|
||||
|
||||
const API_BASE = '/git';
|
||||
|
||||
function buildDashboardRepoEndpoint(dashboardRef, suffix, envId = null) {
|
||||
const encodedRef = encodeURIComponent(String(dashboardRef));
|
||||
const endpoint = `${API_BASE}/repositories/${encodedRef}${suffix}`;
|
||||
if (!envId) return endpoint;
|
||||
const sep = endpoint.includes('?') ? '&' : '?';
|
||||
return `${endpoint}${sep}env_id=${encodeURIComponent(String(envId))}`;
|
||||
}
|
||||
|
||||
// [DEF:gitService:Action]
|
||||
export const gitService = {
|
||||
/**
|
||||
@@ -64,6 +72,62 @@ export const gitService = {
|
||||
return requestApi(`${API_BASE}/config/test`, 'POST', config);
|
||||
},
|
||||
|
||||
/**
|
||||
* [DEF:listGiteaRepositories:Function]
|
||||
* @purpose Lists repositories on Gitea for a saved Git configuration.
|
||||
* @pre configId must reference a GITEA config.
|
||||
* @post Returns repository metadata.
|
||||
* @param {string} configId - Git configuration ID.
|
||||
* @returns {Promise<Array>} List of Gitea repositories.
|
||||
*/
|
||||
async listGiteaRepositories(configId) {
|
||||
console.log(`[listGiteaRepositories][Action] Listing Gitea repositories for config ${configId}`);
|
||||
return requestApi(`${API_BASE}/config/${configId}/gitea/repos`);
|
||||
},
|
||||
|
||||
/**
|
||||
* [DEF:createGiteaRepository:Function]
|
||||
* @purpose Creates a new repository on Gitea for a saved Git configuration.
|
||||
* @pre configId must reference a GITEA config.
|
||||
* @post Repository is created on Gitea.
|
||||
* @param {string} configId - Git configuration ID.
|
||||
* @param {Object} payload - {name, private, description, auto_init, default_branch}
|
||||
* @returns {Promise<Object>} Created repository payload.
|
||||
*/
|
||||
async createGiteaRepository(configId, payload) {
|
||||
console.log(`[createGiteaRepository][Action] Creating Gitea repository ${payload?.name} for config ${configId}`);
|
||||
return requestApi(`${API_BASE}/config/${configId}/gitea/repos`, 'POST', payload);
|
||||
},
|
||||
|
||||
/**
|
||||
* [DEF:createRemoteRepository:Function]
|
||||
* @purpose Creates repository on remote provider selected by Git config.
|
||||
* @pre configId exists and points to supported provider config.
|
||||
* @post Remote repository created and normalized payload returned.
|
||||
* @param {string} configId - Git configuration ID.
|
||||
* @param {Object} payload - {name, private, description, auto_init, default_branch}
|
||||
* @returns {Promise<Object>} Created remote repository payload.
|
||||
*/
|
||||
async createRemoteRepository(configId, payload) {
|
||||
console.log(`[createRemoteRepository][Action] Creating remote repository ${payload?.name} for config ${configId}`);
|
||||
return requestApi(`${API_BASE}/config/${configId}/repositories`, 'POST', payload);
|
||||
},
|
||||
|
||||
/**
|
||||
* [DEF:deleteGiteaRepository:Function]
|
||||
* @purpose Deletes a repository on Gitea for a saved Git configuration.
|
||||
* @pre configId must reference a GITEA config.
|
||||
* @post Repository is deleted on Gitea.
|
||||
* @param {string} configId - Git configuration ID.
|
||||
* @param {string} owner - Repository owner.
|
||||
* @param {string} repoName - Repository name.
|
||||
* @returns {Promise<Object>} Deletion result.
|
||||
*/
|
||||
async deleteGiteaRepository(configId, owner, repoName) {
|
||||
console.log(`[deleteGiteaRepository][Action] Deleting Gitea repository ${owner}/${repoName} for config ${configId}`);
|
||||
return requestApi(`${API_BASE}/config/${configId}/gitea/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repoName)}`, 'DELETE');
|
||||
},
|
||||
|
||||
/**
|
||||
* [DEF:initRepository:Function]
|
||||
* @purpose Initializes or clones a Git repository for a dashboard.
|
||||
@@ -74,9 +138,9 @@ export const gitService = {
|
||||
* @param {string} remoteUrl - URL of the remote repository.
|
||||
* @returns {Promise<Object>} Initialization result.
|
||||
*/
|
||||
async initRepository(dashboardId, configId, remoteUrl) {
|
||||
console.log(`[initRepository][Action] Initializing repo for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/init`, 'POST', {
|
||||
async initRepository(dashboardRef, configId, remoteUrl, envId = null) {
|
||||
console.log(`[initRepository][Action] Initializing repo for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/init', envId), 'POST', {
|
||||
config_id: configId,
|
||||
remote_url: remoteUrl
|
||||
});
|
||||
@@ -90,9 +154,9 @@ export const gitService = {
|
||||
* @param {number} dashboardId - ID of the dashboard.
|
||||
* @returns {Promise<Array>} List of branches.
|
||||
*/
|
||||
async getBranches(dashboardId) {
|
||||
console.log(`[getBranches][Action] Fetching branches for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/branches`);
|
||||
async getBranches(dashboardRef, envId = null) {
|
||||
console.log(`[getBranches][Action] Fetching branches for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/branches', envId));
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -105,9 +169,9 @@ export const gitService = {
|
||||
* @param {string} fromBranch - Source branch name.
|
||||
* @returns {Promise<Object>} Creation result.
|
||||
*/
|
||||
async createBranch(dashboardId, name, fromBranch) {
|
||||
console.log(`[createBranch][Action] Creating branch ${name} for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/branches`, 'POST', {
|
||||
async createBranch(dashboardRef, name, fromBranch, envId = null) {
|
||||
console.log(`[createBranch][Action] Creating branch ${name} for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/branches', envId), 'POST', {
|
||||
name,
|
||||
from_branch: fromBranch
|
||||
});
|
||||
@@ -122,9 +186,9 @@ export const gitService = {
|
||||
* @param {string} name - Branch name to checkout.
|
||||
* @returns {Promise<Object>} Checkout result.
|
||||
*/
|
||||
async checkoutBranch(dashboardId, name) {
|
||||
console.log(`[checkoutBranch][Action] Checking out branch ${name} for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/checkout`, 'POST', { name });
|
||||
async checkoutBranch(dashboardRef, name, envId = null) {
|
||||
console.log(`[checkoutBranch][Action] Checking out branch ${name} for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/checkout', envId), 'POST', { name });
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -137,9 +201,9 @@ export const gitService = {
|
||||
* @param {Array} files - Optional list of files to commit.
|
||||
* @returns {Promise<Object>} Commit result.
|
||||
*/
|
||||
async commit(dashboardId, message, files) {
|
||||
console.log(`[commit][Action] Committing changes for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/commit`, 'POST', { message, files });
|
||||
async commit(dashboardRef, message, files, envId = null) {
|
||||
console.log(`[commit][Action] Committing changes for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/commit', envId), 'POST', { message, files });
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -150,9 +214,9 @@ export const gitService = {
|
||||
* @param {number} dashboardId - ID of the dashboard.
|
||||
* @returns {Promise<Object>} Push result.
|
||||
*/
|
||||
async push(dashboardId) {
|
||||
console.log(`[push][Action] Pushing changes for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/push`, 'POST');
|
||||
async push(dashboardRef, envId = null) {
|
||||
console.log(`[push][Action] Pushing changes for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/push', envId), 'POST');
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -163,9 +227,9 @@ export const gitService = {
|
||||
* @param {number} dashboardId - ID of the dashboard.
|
||||
* @returns {Promise<Object>} Pull result.
|
||||
*/
|
||||
async pull(dashboardId) {
|
||||
console.log(`[pull][Action] Pulling changes for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/pull`, 'POST');
|
||||
async pull(dashboardRef, envId = null) {
|
||||
console.log(`[pull][Action] Pulling changes for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/pull', envId), 'POST');
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -188,9 +252,9 @@ export const gitService = {
|
||||
* @param {string} environmentId - ID of the target environment.
|
||||
* @returns {Promise<Object>} Deployment result.
|
||||
*/
|
||||
async deploy(dashboardId, environmentId) {
|
||||
console.log(`[deploy][Action] Deploying dashboard ${dashboardId} to environment ${environmentId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/deploy`, 'POST', {
|
||||
async deploy(dashboardRef, environmentId, envId = null) {
|
||||
console.log(`[deploy][Action] Deploying dashboard ${dashboardRef} to environment ${environmentId}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/deploy', envId), 'POST', {
|
||||
environment_id: environmentId
|
||||
});
|
||||
},
|
||||
@@ -202,9 +266,9 @@ export const gitService = {
|
||||
* @param {number} limit - Maximum number of commits to return.
|
||||
* @returns {Promise<Array>} List of commits.
|
||||
*/
|
||||
async getHistory(dashboardId, limit = 50) {
|
||||
console.log(`[getHistory][Action] Fetching history for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/history?limit=${limit}`);
|
||||
async getHistory(dashboardRef, limit = 50, envId = null) {
|
||||
console.log(`[getHistory][Action] Fetching history for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, `/history?limit=${limit}`, envId));
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -214,10 +278,13 @@ export const gitService = {
|
||||
* @param {string|null} sourceEnvId - Optional source environment ID.
|
||||
* @returns {Promise<Object>} Sync result.
|
||||
*/
|
||||
async sync(dashboardId, sourceEnvId = null) {
|
||||
console.log(`[sync][Action] Syncing dashboard ${dashboardId}`);
|
||||
let endpoint = `${API_BASE}/repositories/${dashboardId}/sync`;
|
||||
if (sourceEnvId) endpoint += `?source_env_id=${sourceEnvId}`;
|
||||
async sync(dashboardRef, sourceEnvId = null, envId = null) {
|
||||
console.log(`[sync][Action] Syncing dashboard ${dashboardRef}`);
|
||||
const params = new URLSearchParams();
|
||||
if (sourceEnvId) params.append('source_env_id', String(sourceEnvId));
|
||||
if (envId) params.append('env_id', String(envId));
|
||||
const query = params.toString();
|
||||
const endpoint = `${API_BASE}/repositories/${encodeURIComponent(String(dashboardRef))}/sync${query ? `?${query}` : ''}`;
|
||||
return requestApi(endpoint, 'POST');
|
||||
},
|
||||
|
||||
@@ -229,9 +296,9 @@ export const gitService = {
|
||||
* @param {number} dashboardId - The ID of the dashboard.
|
||||
* @returns {Promise<Object>} Status details.
|
||||
*/
|
||||
async getStatus(dashboardId) {
|
||||
console.log(`[getStatus][Action] Fetching status for dashboard ${dashboardId}`);
|
||||
return requestApi(`${API_BASE}/repositories/${dashboardId}/status`);
|
||||
async getStatus(dashboardRef, envId = null) {
|
||||
console.log(`[getStatus][Action] Fetching status for dashboard ${dashboardRef}`);
|
||||
return requestApi(buildDashboardRepoEndpoint(dashboardRef, '/status', envId));
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -259,12 +326,13 @@ export const gitService = {
|
||||
* @param {boolean} staged - Whether to show staged changes.
|
||||
* @returns {Promise<string>} The diff content.
|
||||
*/
|
||||
async getDiff(dashboardId, filePath = null, staged = false) {
|
||||
console.log(`[getDiff][Action] Fetching diff for dashboard ${dashboardId} (file: ${filePath}, staged: ${staged})`);
|
||||
let endpoint = `${API_BASE}/repositories/${dashboardId}/diff`;
|
||||
async getDiff(dashboardRef, filePath = null, staged = false, envId = null) {
|
||||
console.log(`[getDiff][Action] Fetching diff for dashboard ${dashboardRef} (file: ${filePath}, staged: ${staged})`);
|
||||
let endpoint = `${API_BASE}/repositories/${encodeURIComponent(String(dashboardRef))}/diff`;
|
||||
const params = new URLSearchParams();
|
||||
if (filePath) params.append('file_path', filePath);
|
||||
if (staged) params.append('staged', 'true');
|
||||
if (envId) params.append('env_id', String(envId));
|
||||
if (params.toString()) endpoint += `?${params.toString()}`;
|
||||
return requestApi(endpoint);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user