cvs-proxy/ui/api.js
Juan José Gutiérrez de Quevedo Pérez d3b40ae93f feat: Add patchset history and diff support with cvsps integration
- Add cvsps package to Docker dependencies
- Implement patchset retrieval and diff endpoints in API
- Add _run_cvsps_command() helper for cvsps integration
- Enhance file history parsing with log message extraction
- Improve UI with enhanced styling and patchset functionality
2025-11-21 17:18:55 +01:00

131 lines
No EOL
3.9 KiB
JavaScript

/**
* CVS Proxy API Client
* Handles all API calls to the backend
*/
class CVSProxyAPI {
constructor(baseURL = '') {
this.baseURL = baseURL || '';
}
/**
* Make a fetch request to the API
* @param {string} endpoint - API endpoint
* @param {object} options - Fetch options
* @returns {Promise} Response data
*/
async request(endpoint, options = {}) {
const url = `${this.baseURL}/api/v1${endpoint}`;
try {
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...options.headers,
},
...options,
});
if (!response.ok) {
const error = await response.json().catch(() => ({}));
throw new Error(error.error || `HTTP ${response.status}`);
}
// Handle different content types
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return await response.json();
} else {
return await response.text();
}
} catch (error) {
console.error('API Error:', error);
throw error;
}
}
/**
* Get repository tree structure
* @param {string} module - Optional module to list
* @returns {Promise<Array>} List of files and directories
*/
async getTree(module = null) {
const params = new URLSearchParams();
if (module) {
params.append('module', module);
}
const query = params.toString() ? `?${params.toString()}` : '';
return this.request(`/tree${query}`);
}
/**
* Get file content
* @param {string} filePath - Path to the file
* @param {string} revision - Optional specific revision
* @returns {Promise<string>} File content
*/
async getFileContent(filePath, revision = null) {
const params = new URLSearchParams();
params.append('file', filePath);
if (revision) {
params.append('revision', revision);
}
return this.request(`/file?${params.toString()}`);
}
/**
* Get file revision history
* @param {string} filePath - Path to the file
* @returns {Promise<Array>} Array of revision objects
*/
async getFileHistory(filePath) {
const params = new URLSearchParams();
params.append('file', filePath);
return this.request(`/history?${params.toString()}`);
}
/**
* Get diff between two revisions
* @param {string} filePath - Path to the file
* @param {string} rev1 - First revision
* @param {string} rev2 - Second revision
* @returns {Promise<object>} Diff object with diff property
*/
async getDiff(filePath, rev1, rev2) {
const params = new URLSearchParams();
params.append('file', filePath);
params.append('rev1', rev1);
params.append('rev2', rev2);
return this.request(`/diff?${params.toString()}`);
}
/**
* Get repository patchset history
* @returns {Promise<Array>} Array of patchset objects
*/
async getPatchsets() {
return this.request('/patchsets');
}
/**
* Get diff for a specific patchset
* @param {string} patchset - Patchset number
* @returns {Promise<object>} Diff object with diff property
*/
async getPatchsetDiff(patchset) {
const params = new URLSearchParams();
params.append('patchset', patchset);
return this.request(`/patchset-diff?${params.toString()}`);
}
/**
* Check API health
* @returns {Promise<object>} Health status
*/
async getHealth() {
return this.request('/health');
}
}
// Create global API instance with basepath from config
const api = new CVSProxyAPI(window.APP_CONFIG?.basepath || '');