review-copilot
Version:
ReviewCopilot - AI-powered code review assistant with customizable prompts
129 lines (128 loc) • 4.76 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitHubService = void 0;
const rest_1 = require("@octokit/rest");
const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process");
class GitHubService {
constructor(token) {
this.client = new rest_1.Octokit({ auth: token });
}
async createIssueComment({ owner, repo, issue_number, body, }) {
await this.client.issues.createComment({
owner,
repo,
issue_number,
body,
});
}
async createReviewComment({ owner, repo, pullNumber: pullNumber, body, commitId, path, line, side, startLine, startSide, inReplyTo, subjectType, }) {
await this.client.pulls.createReviewComment({
owner,
repo,
pull_number: pullNumber,
body,
commit_id: commitId,
path,
line,
side,
start_line: startLine,
start_side: startSide,
in_reply_to: inReplyTo,
subject_type: subjectType,
});
}
async replyToComment(params) {
// GitHub doesn't have a direct reply-to-comment API
// Instead, we'll create a new comment with a reference to the original
const { data: originalComment } = await this.client.issues.getComment({
owner: params.owner,
repo: params.repo,
comment_id: params.commentId,
});
const replyComment = `> ${originalComment.body}\n\n${params.comment}`;
await this.client.issues.createComment({
owner: params.owner,
repo: params.repo,
issue_number: params.pullNumber,
body: replyComment,
});
}
async replyToReviewComment(params) {
await this.client.pulls.createReplyForReviewComment({
owner: params.owner,
repo: params.repo,
pull_number: params.pullNumber,
comment_id: parseInt(params.threadId, 10),
body: params.comment,
});
}
async getPRDetails() {
if (process.env.GITHUB_EVENT_PATH) {
try {
const eventData = JSON.parse(fs_1.default.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'));
if (eventData.pull_request) {
return {
owner: eventData.repository.owner.login,
repo: eventData.repository.name,
pullNumber: eventData.pull_request.number,
platform: 'github',
commitId: eventData.pull_request.head.sha,
path: eventData.pull_request.head.path,
};
}
}
catch (error) {
console.error('Error reading GitHub event data:', error);
}
}
const [owner, repo] = (process.env.GITHUB_REPOSITORY || '').split('/');
const pullNumber = parseInt(process.env.GITHUB_EVENT_NUMBER || '', 10);
if (owner && repo && !isNaN(pullNumber)) {
return {
owner,
repo,
pullNumber,
platform: 'github',
commitId: '',
path: '',
};
}
return undefined;
}
async getCurrentBranch() {
return (0, child_process_1.execSync)('git rev-parse --abbrev-ref HEAD').toString().trim();
}
async getCommitMessage() {
return (0, child_process_1.execSync)('git log -1 --pretty=%B').toString().trim();
}
async getFileContent(params) {
try {
// Get the PR details to get the head SHA
const { data: pullRequest } = await this.client.pulls.get({
owner: params.owner,
repo: params.repo,
pull_number: params.pullNumber,
});
// Get the file content from the PR's head branch
const { data: fileData } = await this.client.repos.getContent({
owner: params.owner,
repo: params.repo,
path: params.filePath,
ref: pullRequest.head.sha,
});
if ('content' in fileData && fileData.content) {
return Buffer.from(fileData.content, 'base64').toString('utf-8');
}
return null;
}
catch (error) {
console.error('Error getting file content:', error);
return null;
}
}
}
exports.GitHubService = GitHubService;