UNPKG

review-copilot

Version:

ReviewCopilot - AI-powered code review assistant with customizable prompts

71 lines (70 loc) 3.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitLabService = void 0; const rest_1 = require("@gitbeaker/rest"); class GitLabService { constructor(token) { this.client = new rest_1.Gitlab({ token, }); } async getPRDetails() { const mrIid = process.env.CI_MERGE_REQUEST_IID; if (!mrIid) return undefined; return { owner: process.env.CI_PROJECT_NAMESPACE || '', repo: process.env.CI_PROJECT_NAME || '', pullNumber: parseInt(mrIid, 10), platform: 'gitlab', commitId: process.env.CI_COMMIT_SHA || '', path: process.env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME || '', }; } async createIssueComment({ owner, repo, issue_number, body, }) { await this.client.MergeRequestNotes.create(`${owner}/${repo}`, issue_number, body); } async createReviewComment(params) { await this.client.MergeRequestNotes.create(`${params.owner}/${params.repo}`, params.pullNumber, params.body); } async replyToComment(params) { // Get the original comment const originalNote = await this.client.MergeRequestNotes.show(`${params.owner}/${params.repo}`, params.pullNumber, params.commentId); // Create a reply comment with reference to the original const replyComment = `> ${originalNote.body}\n\n${params.comment}`; await this.client.MergeRequestNotes.create(`${params.owner}/${params.repo}`, params.pullNumber, replyComment); } async replyToReviewComment(params) { // GitLab doesn't have a direct review comment API // We'll create a new comment with a reference to the thread const replyComment = `> Thread ${params.threadId}\n\n${params.comment}`; await this.client.MergeRequestNotes.create(`${params.owner}/${params.repo}`, params.pullNumber, replyComment); } async getCurrentBranch() { return process.env.CI_COMMIT_REF_NAME || ''; } async getCommitMessage() { return process.env.CI_COMMIT_MESSAGE || ''; } async getFileContent(params) { try { // Get the MR details to get the source branch const mr = await this.client.MergeRequests.show(`${params.owner}/${params.repo}`, params.pullNumber); // Ensure source_branch is a string const sourceBranch = typeof mr.source_branch === 'string' ? mr.source_branch : String(mr.source_branch); // Get the file content from the MR's source branch const file = await this.client.RepositoryFiles.show(`${params.owner}/${params.repo}`, params.filePath, sourceBranch); if (file.content) { return Buffer.from(file.content, 'base64').toString('utf-8'); } return null; } catch (error) { console.error('Error getting file content:', error); return null; } } } exports.GitLabService = GitLabService;