UNPKG

@parvez3019/ai-code-review-gitlab-plugin

Version:
156 lines 8.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const gitlab_1 = require("./gitlab"); const gemini_1 = require("./gemini"); const bedrock_1 = require("./bedrock"); const utils_1 = require("./utils"); const program = new commander_1.Command(); program .option('-g, --gitlab-api-url <string>', 'GitLab API URL', 'https://gitlab.com/api/v4') .option('-t, --gitlab-access-token <string>', 'GitLab Access Token') .option('-p, --project-id <number>', 'GitLab Project ID') .option('-m, --merge-request-id <string>', 'GitLab Merge Request ID') .option('-a, --ai-provider <string>', 'AI Provider (gemini or bedrock)', 'gemini') .option('-k, --api-key <string>', 'API Key (Gemini API Key or AWS Access Key ID)') .option('-s, --api-secret <string>', 'API Secret (AWS Secret Access Key for Bedrock)') .option('-r, --region <string>', 'AWS Region for Bedrock', 'us-east-1') .option('-c, --custom-model <string>', 'Custom Model ID') .option('-sp, --system-prompt-path <string>', 'Path to custom system prompt file (local file or s3://bucket/key)') .option('-crp, --code-review-prompt-path <string>', 'Path to custom code review prompt file (local file or s3://bucket/key)') .option('--s3-region <string>', 'AWS Region for S3 (if using S3 paths)') .option('--s3-access-key <string>', 'AWS Access Key ID for S3 (if using S3 paths)') .option('--s3-secret-key <string>', 'AWS Secret Access Key for S3 (if using S3 paths)') .parse(process.argv); const GEMINI = 'gemini'; const BEDROCK = 'bedrock'; function createAIClient() { return __awaiter(this, void 0, void 0, function* () { const { aiProvider, apiKey, apiSecret, region, customModel, systemPromptPath, codeReviewPromptPath, s3Region, s3AccessKey, s3SecretKey, } = program.opts(); if (!apiKey) { throw new Error('API Key is required'); } // Set S3 environment variables if provided if (s3Region) process.env.S3_REGION = s3Region; if (s3AccessKey) process.env.S3_ACCESS_KEY = s3AccessKey; if (s3SecretKey) process.env.S3_SECRET_KEY = s3SecretKey; const config = { apiKey: apiKey, apiSecret: apiSecret, model: customModel, region: region, }; // Load prompts if custom paths are provided if (systemPromptPath || codeReviewPromptPath) { const [systemPrompt, codeReviewPrompt] = yield Promise.all([ systemPromptPath ? (0, utils_1.getSystemPrompt)(systemPromptPath) : undefined, codeReviewPromptPath ? (0, utils_1.getCodeReviewPrompt)(codeReviewPromptPath) : undefined ]); if (systemPrompt) config.systemPrompt = systemPrompt; if (codeReviewPrompt) config.codeReviewPrompt = codeReviewPrompt; } switch (aiProvider.toLowerCase()) { case GEMINI: config.apiUrl = 'https://generativelanguage.googleapis.com'; return new gemini_1.Gemini(config); case BEDROCK: if (!apiSecret) { throw new Error('AWS Secret Access Key is required for Bedrock'); } return new bedrock_1.BedrockClient(config); default: throw new Error(`Unsupported AI provider: ${aiProvider}`); } }); } const NO_REVIEW_CONTENT_PLACEHOLDER = '204'; function run() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const { gitlabApiUrl, gitlabAccessToken, projectId, mergeRequestId, } = program.opts(); const gitlab = new gitlab_1.GitLab({ gitlabApiUrl, gitlabAccessToken, projectId, mergeRequestId }); let aiClient; try { aiClient = yield createAIClient(); } catch (error) { console.error('Failed to create AI client:', error); process.exit(1); } yield gitlab.init().catch(() => { console.log('gitlab init error'); }); const changes = yield gitlab.getMergeRequestChanges().catch(() => { console.log('get merge request changes error'); }); const noFeedbackLogs = []; for (const change of changes) { if (change.renamed_file || change.deleted_file || !((_a = change === null || change === void 0 ? void 0 : change.diff) === null || _a === void 0 ? void 0 : _a.startsWith('@@'))) { continue; } const diffBlocks = (0, utils_1.getDiffBlocks)(change === null || change === void 0 ? void 0 : change.diff); while (!!diffBlocks.length) { const item = diffBlocks.shift(); const lineRegex = /@@\s-(\d+)(?:,(\d+))?\s\+(\d+)(?:,(\d+))?\s@@/; const matches = lineRegex.exec(item); if (matches) { const lineObj = (0, utils_1.getLineObj)(matches, item); if (((lineObj === null || lineObj === void 0 ? void 0 : lineObj.new_line) && (lineObj === null || lineObj === void 0 ? void 0 : lineObj.new_line) > 0) || (lineObj.old_line && lineObj.old_line > 0)) { try { const suggestion = yield aiClient.reviewCodeChange(item); if (suggestion === NO_REVIEW_CONTENT_PLACEHOLDER) { console.log('No feedback for this change', lineObj); // Extract code lines from the diff block const codeLines = item.split('\n') .filter(line => line.startsWith('+') || line.startsWith('-')) .map(line => line.substring(1)) // Remove the + or - prefix .join('\n'); noFeedbackLogs.push({ file: change.new_path, line: lineObj, code: codeLines }); continue; } yield gitlab.addReviewComment(lineObj, change, suggestion); } catch (e) { if (((_b = e === null || e === void 0 ? void 0 : e.response) === null || _b === void 0 ? void 0 : _b.status) === 429) { console.log('Too Many Requests, try again'); yield (0, utils_1.delay)(60 * 1000); diffBlocks.push(item); } } } } } } // Add summary comment for no feedback logs if (noFeedbackLogs.length > 0) { const summaryMessage = `### No Feedback Summary\n\nThe following changes were reviewed and required no further feedback, great work 💪 :\n\n${noFeedbackLogs.map(log => `- File: \`${log.file}\`, Line: ${log.line.new_line} - ${log.line.old_line}`).join('\n')}`; yield gitlab.addComment(summaryMessage); } console.log('done'); }); } module.exports = run; //# sourceMappingURL=index.js.map