nyegosh
Version:
AI generated commit messages, like Njegoš wrote them.
105 lines (89 loc) • 3.2 kB
JavaScript
const simpleGit = require("simple-git");
const path = require("path");
const dotenv = require("dotenv");
const { initializeModelConnection } = require("./auth");
const { conventionalReleaseNotesDoc } = require("./realese-notes");
const envPath = path.join(require("os").homedir(), ".nyegosh.env");
dotenv.config({ path: envPath });
const aiClient = initializeModelConnection();
const git = simpleGit();
async function getLogs(commitFrom, commitTo) {
const logs = await git.log({
from: commitFrom,
to: commitTo,
});
return logs.all.map((l) => ({
message: l.message + (!!l.body ? "\n" + l.body : ""),
hash: l.hash,
}));
}
async function getChanges(log) {
const changes = await git.show([log.hash]);
return changes;
}
function getPrompt(log) {
const prompt = `
Given the following git log generate a release notes:
\n\n ${log}
`;
return prompt;
}
async function generateReleaseNotes(logs) {
const prompt = getPrompt(logs);
try {
const response = await aiClient.chat.completions.create({
model: process.env.NYEGOSH_GENERATOR_DEPLOYMENT,
stream: false,
messages: [
{
role: "system",
content: `You are a helpful coding assistant for generating meaningful release notes based on git logs.`,
},
{
role: "system",
content: `Specification for generating release note: ${conventionalReleaseNotesDoc}.`,
},
{ role: "user", content: prompt },
],
});
const generatedCommitMessage = response.choices?.[0]?.message?.content?.trim();
if (!generatedCommitMessage) {
throw new Error("AI did not generate a commit message.");
}
return generatedCommitMessage;
} catch (error) {
// TODO: Handle token limit exceeded
console.error("Error generating commit message:", error.message.red);
throw error;
}
}
async function writeCommitMessage(message) {
console.log("AI generated release notes:");
console.log(`\n${message}\n`);
}
(async () => {
try {
// Get command-line arguments (ignore the first two default arguments)
const args = process.argv.slice(2);
// const logs = await getLogs('c80e917', '7f427f5');
// console.log(logs);
// for (let i = 0; i < logs.length; i++) {
// const diff = await getChanges(logs[i]);
// console.log(diff);
// }
if (args.length != 2) {
console.log(
"Please provide at two argument.\n - First argument is the commit hash from where to start (exclusive).\n - Second argument is the commit from where end (inclusvie)",
);
process.exit(1);
} else {
const logs = await getLogs(args[0], args[1]);
const message = await generateReleaseNotes(logs.map((log) => log.message));
writeCommitMessage(message);
}
} catch (error) {
console.log(error);
process.exit(1);
}
})();