scai
Version:
> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.
72 lines (71 loc) • 2.42 kB
JavaScript
import { ensureGitHubAuth } from './auth.js';
import { getRepoDetails } from './repo.js';
export async function fetchOpenPullRequests(token, owner, repo) {
const url = `https://api.github.com/repos/${owner}/${repo}/pulls?state=open&per_page=100`;
const res = await fetch(url, {
headers: {
Authorization: `token ${token}`,
Accept: 'application/vnd.github.v3+json',
},
});
if (!res.ok) {
throw new Error(`GitHub API error: ${res.status} ${res.statusText}`);
}
const prs = await res.json();
return prs.map((pr) => ({
number: pr.number,
title: pr.title,
url: pr.url,
diff_url: pr.diff_url,
draft: pr.draft,
merged_at: pr.merged_at,
base: pr.base,
requested_reviewers: pr.requested_reviewers,
}));
}
export async function getGitHubUsername(token) {
const res = await fetch('https://api.github.com/user', {
headers: {
Authorization: `token ${token}`,
Accept: "application/vnd.github.v3+json",
},
});
if (!res.ok) {
throw new Error(`Error fetching user info: ${res.status} ${res.statusText}`);
}
const user = await res.json();
return user.login; // GitHub username
}
export async function fetchPullRequestDiff(pr, token) {
const res = await fetch(pr.diff_url, {
headers: {
Authorization: `token ${token}`,
Accept: "application/vnd.github.v3.diff",
},
});
if (!res.ok) {
throw new Error(`Error fetching PR diff: ${res.status} ${res.statusText}`);
}
return await res.text();
}
export async function submitReview(prNumber, body, event = 'COMMENT') {
const token = await ensureGitHubAuth();
const { owner, repo } = getRepoDetails();
const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/reviews`;
const res = await fetch(url, {
method: 'POST',
headers: {
Authorization: `token ${token}`,
Accept: 'application/vnd.github.v3+json',
},
body: JSON.stringify({
body,
event,
}),
});
if (!res.ok) {
const errorText = await res.text();
throw new Error(`Failed to submit review: ${res.status} ${res.statusText} - ${errorText}`);
}
console.log(`✅ Submitted ${event} review for PR #${prNumber}`);
}