@jpaulo789b/gemini-review-code-br
Version:
149 lines • 8.99 kB
JavaScript
;
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 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('-a, --api-key <string>', 'Gemini API Key')
.option('-p, --project-id <number>', 'GitLab Project ID')
.option('-m, --merge-request-id <string>', 'GitLab Merge Request ID')
.option('-c, --custom-model <string>', 'Custom Model ID', 'gemini-1.5-flash')
.parse(process.argv);
function run() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const { gitlabApiUrl, gitlabAccessToken, apiKey, projectId, mergeRequestId, customModel, } = program.opts();
const gitlab = new gitlab_1.GitLab({ gitlabApiUrl, gitlabAccessToken, projectId, mergeRequestId });
let aiClient;
aiClient = new gemini_1.Gemini('https://generativelanguage.googleapis.com', apiKey, customModel); // create a new instance of the Gemini class
yield gitlab.init().catch(() => {
console.log('gitlab init error');
});
const changes = yield gitlab.getMergeRequestChanges().catch(() => {
console.log('get merge request changes error');
});
// Contadores para problemas críticos
let criticalIssuesCount = 0;
let grossErrorsCount = 0;
let severeErrorsCount = 0;
let complexLogicCount = 0;
let buildProblemsCount = 0;
for (const change of changes) {
// Verificar se o arquivo deve ser ignorado completamente
if ((0, utils_1.shouldIgnoreFile)(change.new_path) || (0, utils_1.shouldIgnoreFile)(change.old_path)) {
console.log(`🚫 Ignorando arquivo: ${change.new_path || change.old_path} (arquivo não relevante)`);
continue;
}
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;
}
// Verificar se é arquivo de plataforma
const isPlatform = (0, utils_1.isPlatformFile)(change.new_path) || (0, utils_1.isPlatformFile)(change.old_path);
const fileType = isPlatform ? 'PLATAFORMA' : 'DART';
console.log(`🔍 Analisando arquivo ${fileType}: ${change.new_path || change.old_path}`);
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 {
// Usar método específico baseado no tipo de arquivo
const suggestion = isPlatform
? yield aiClient.reviewPlatformChange(item)
: yield aiClient.reviewCodeChange(item);
// Só adiciona comentário se há problemas críticos
if ((0, utils_1.isValidReviewComment)(suggestion, isPlatform)) {
const commentType = (0, utils_1.getCommentType)(suggestion);
yield gitlab.addReviewComment(lineObj, change, suggestion);
// Incrementar contadores baseado no tipo do problema
criticalIssuesCount++;
if (commentType === 'ERRO GROTESCO') {
grossErrorsCount++;
}
else if (commentType === 'ERRO GRAVE') {
severeErrorsCount++;
}
else if (commentType === 'LÓGICA COMPLEXA') {
complexLogicCount++;
}
else if (commentType === 'PROBLEMA CRÍTICO DE BUILD' || commentType === 'PROBLEMA GRAVE DE CONFIGURAÇÃO') {
buildProblemsCount++;
}
console.log(`✅ Comentário adicionado - ${commentType} encontrado (${fileType})`);
console.log(`📄 Arquivo: ${change.new_path || change.old_path}`);
}
else {
console.log(`ℹ️ Nenhum problema crítico encontrado - comentário não adicionado (${fileType})`);
console.log('📝 Resposta do Gemini:', suggestion.substring(0, 100) + '...');
}
}
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);
}
}
}
}
}
}
// Relatório final e decisão da pipeline
console.log('\n=== RELATÓRIO FINAL DE REVISÃO ===');
console.log(`📊 Total de problemas críticos encontrados: ${criticalIssuesCount}`);
console.log(`🚨 Erros grotescos: ${grossErrorsCount}`);
console.log(`⚠️ Erros graves: ${severeErrorsCount}`);
console.log(`🔍 Lógicas complexas: ${complexLogicCount}`);
console.log(`🏗️ Problemas de build: ${buildProblemsCount}`);
// Determinar se a pipeline deve falhar
const shouldFailPipeline = grossErrorsCount > 0 || severeErrorsCount > 0 || buildProblemsCount > 0;
if (shouldFailPipeline) {
console.log('\n❌ PIPELINE FALHOU!');
console.log('🚨 Foram encontrados problemas críticos que impedem o merge:');
if (grossErrorsCount > 0) {
console.log(` • ${grossErrorsCount} erro(s) grotesco(s) que podem quebrar a aplicação`);
}
if (severeErrorsCount > 0) {
console.log(` • ${severeErrorsCount} erro(s) grave(s) que violam padrões arquiteturais`);
}
if (buildProblemsCount > 0) {
console.log(` • ${buildProblemsCount} problema(s) de build que podem quebrar a CI/CD`);
}
console.log('\n🔧 Corrija os problemas antes de fazer o merge.');
console.log('💡 Lógicas complexas não impedem o merge, mas merecem atenção.');
// Falhar a pipeline com exit code 1
process.exit(1);
}
else if (complexLogicCount > 0) {
console.log('\n⚠️ PIPELINE PASSOU COM AVISOS');
console.log(`🔍 Foram encontradas ${complexLogicCount} lógica(s) complexa(s) que merecem atenção, mas não impedem o merge.`);
console.log('💡 Considere refatorar essas lógicas para melhorar a manutenibilidade.');
// Pipeline passa, mas com aviso
process.exit(0);
}
else {
console.log('\n✅ PIPELINE PASSOU!');
console.log('🎉 Nenhum problema crítico encontrado. Merge liberado!');
// Pipeline passa com sucesso
process.exit(0);
}
});
}
module.exports = run;
//# sourceMappingURL=index.js.map