UNPKG

difflytic

Version:

AI-powered code review for GitLab/GitHub MRs using OpenAI and more.

80 lines (79 loc) 3.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitLabProvider = void 0; const BaseProvider_1 = require("./BaseProvider"); const Config_1 = __importDefault(require("../utils/Config")); class GitLabProvider extends BaseProvider_1.BaseProvider { constructor() { super(); const baseUrl = Config_1.default.get('GITLAB_API_URL', 'https://gitlab.com'); this.apiUrl = baseUrl.replace(/\/$/, '') + '/api/v4'; this.token = Config_1.default.get('GITLAB_TOKEN'); if (!this.token) { throw new Error('GITLAB_TOKEN is required in environment variables'); } } async fetchMergeRequestData(projectId, mergeRequestIid) { const url = `${this.apiUrl}/projects/${encodeURIComponent(projectId)}/merge_requests/${mergeRequestIid}/changes`; console.log('Fetching MR data from:', url); console.log('Fetching MR with token:', this.token); const res = await fetch(url, { headers: { 'PRIVATE-TOKEN': this.token, 'Content-Type': 'application/json', }, }); if (!res.ok) { const errorText = await res.text(); throw new Error(`Failed to fetch MR data: ${res.status} ${res.statusText} - ${errorText}`); } const data = await res.json(); return data; } async fetchFileContent(projectId, filePath, ref) { const url = `${this.apiUrl}/projects/${encodeURIComponent(projectId)}/repository/files/${encodeURIComponent(filePath)}/raw?ref=${encodeURIComponent(ref)}`; const res = await fetch(url, { headers: { 'PRIVATE-TOKEN': this.token } }); if (!res.ok) { throw new Error(`Failed to fetch file content: ${res.status} ${res.statusText}`); } return await res.text(); } async createDiscussion(projectId, mergeRequestIid, body, position) { const url = `${this.apiUrl}/projects/${encodeURIComponent(projectId)}/merge_requests/${mergeRequestIid}/discussions`; const res = await fetch(url, { method: 'POST', headers: { 'PRIVATE-TOKEN': this.token, 'Content-Type': 'application/json', }, body: JSON.stringify({ body, position }) }); if (!res.ok) { throw new Error(`Failed to create discussion: ${res.status} ${res.statusText}`); } return await res.json(); } async updateMergeRequestLabels(projectId, mergeRequestIid, labels) { const url = `${this.apiUrl}/projects/${encodeURIComponent(projectId)}/merge_requests/${mergeRequestIid}`; const res = await fetch(url, { method: 'PUT', headers: { 'PRIVATE-TOKEN': this.token, 'Content-Type': 'application/json', }, body: JSON.stringify({ labels: labels.join(',') }) }); if (!res.ok) { throw new Error(`Failed to update MR labels: ${res.status} ${res.statusText}`); } return await res.json(); } } exports.GitLabProvider = GitLabProvider;