This commit is contained in:
2025-12-27 15:21:57 -05:00
parent 45d43c0977
commit 2b11979b66
5 changed files with 63 additions and 21 deletions

View File

@@ -3,6 +3,11 @@ import {$} from '@ztimson/node-utils';
import * as os from 'node:os';
import * as path from 'node:path';
import * as fs from 'node:fs';
import * as dotenv from 'dotenv';
import {execSync} from 'node:child_process';
dotenv.config();
dotenv.config({path: '.env.local', override: true});
(async () => {
const
@@ -16,9 +21,13 @@ import * as fs from 'node:fs';
model = process.env['AI_MODEL'],
token = process.env['AI_TOKEN'];
const comments = [];
const commit = (await $`git log -1 --pretty=format:%H`).trim();
const gitDiff = await $`git diff HEAD^..HEAD`;
const commit = await $`cd ${root} && git log -1 --pretty=format:%H`;
const remote = await $`cd ${root} && git symbolic-ref refs/remotes/origin/HEAD`;
const gitDiff = execSync(`git diff ${remote}...HEAD`, {cwd: root, encoding: 'utf-8'});
console.log(`Inspecting: ${root} (${commit})`);
let options = {ollama: {model, host}};
if(host === 'anthropic') options = {anthropic: {model, token}};
@@ -61,19 +70,28 @@ import * as fs from 'node:fs';
}]
});
if(!gitDiff) {
console.warn('No diff found');
return process.exit();
}
const summary = await ai.language.ask(gitDiff);
const res = await fetch(`${git}/api/v1/repos/${owner}/${repo}/pulls/${pr}/reviews`, {
method: 'POST',
headers: {
'Authorization': `token ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
body: summary,
commit_id: commit,
event: 'COMMENT',
comments,
})
});
if(!res.ok) throw new Error(`${res.status} ${await res.text()}`);
if(git) {
const res = await fetch(`${git}/api/v1/repos/${owner}/${repo}/pulls/${pr}/reviews`, {
method: 'POST',
headers: {
'Authorization': `token ${auth}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
body: summary,
commit_id: commit,
event: 'COMMENT',
comments,
})
});
if(!res.ok) throw new Error(`${res.status} ${await res.text()}`);
} else {
console.log(comments.map(c => `${c.path}:${c.new_position}\n${c.body}`).join('\n\n') + '\n\n' + summary);
}
})();