review-copilot
Version:
ReviewCopilot - AI-powered code review assistant with customizable prompts
103 lines (102 loc) • 3.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalGitProvider = void 0;
const simple_git_1 = __importDefault(require("simple-git"));
const exec_command_1 = require("./exec-command");
class LocalGitProvider {
async getCurrentBranchName() {
const result = await (0, exec_command_1.execCommand)('git rev-parse --abbrev-ref HEAD');
return result.stdout.trim();
}
async getPullRequestFiles() {
try {
const git = (0, simple_git_1.default)();
const currentBranch = await this.getCurrentBranchName();
const commits = [];
const logResult = await git.log({
from: 'main',
to: currentBranch,
symmetric: false,
});
for (const commit of logResult.all) {
const diffResult = await git.diff([
`${commit.hash}^`,
commit.hash,
'--name-only',
]);
const files = diffResult.split('\n').filter(Boolean);
const fileChanges = [];
// Limit concurrency for patch fetching
const concurrency = 3;
let idx = 0;
while (idx < files.length) {
const batch = files.slice(idx, idx + concurrency);
const patchPromises = batch.map(async (file) => {
const patch = await git.diff([
`${commit.hash}^`,
commit.hash,
'--',
file,
]);
return { file, changes: patch };
});
const batchResults = await Promise.all(patchPromises);
fileChanges.push(...batchResults);
idx += concurrency;
}
commits.push({
hash: commit.hash,
date: commit.date,
message: commit.message,
author: commit.author_name,
files: fileChanges.map((file) => ({
filename: file.file,
changes: file.changes,
})),
});
}
const reviewInfo = {
hash: commits[0].hash,
date: commits[0].date,
message: commits[0].message,
author: commits[0].author,
files: commits
.map((commit) => commit.files)
.flat()
.filter((file) => file !== undefined),
};
return reviewInfo;
}
catch (error) {
// eslint-disable-next-line no-console
console.error('Error getting commits for review:', error);
throw new Error('Failed to fetch GitHub PR changes for code review');
}
}
async getPullRequestCommits() {
try {
const git = (0, simple_git_1.default)();
const currentBranch = await this.getCurrentBranchName();
const logResult = await git.log({
from: 'main',
to: currentBranch,
symmetric: false,
});
return logResult.all.map((commit) => ({
hash: commit.hash,
date: commit.date,
message: commit.message,
author: commit.author_name,
files: [],
}));
}
catch (error) {
console.error('Error getting commits for message review:', error);
return [];
}
}
}
exports.LocalGitProvider = LocalGitProvider;