From de546e9e2e1fdd1210b21855f665cb3e245475c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8A=D9=88=D8=B3=D9=81=20=D8=A7=D9=84=D8=A3=D9=88=D8=B3?= =?UTF-8?q?=D9=8A?= Date: Tue, 14 Feb 2023 18:18:22 +1000 Subject: [PATCH 1/3] Fix OpenAI API error handling when generating commit message. --- aicommits.ts | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/aicommits.ts b/aicommits.ts index fbd38e8..354ed4e 100644 --- a/aicommits.ts +++ b/aicommits.ts @@ -55,30 +55,35 @@ export async function main() { console.log( chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n") ); - const aiCommitMessage = await generateCommitMessage(prompt); + try { + const aiCommitMessage = await generateCommitMessage(prompt); + console.log( + chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n" + ); - console.log( - chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n" - ); + const confirmationMessage = await inquirer.prompt([ + { + name: "useCommitMessage", + message: "Would you like to use this commit message? (Y / n)", + choices: ["Y", "y", "n"], + default: "y", + }, + ]); - const confirmationMessage = await inquirer.prompt([ - { - name: "useCommitMessage", - message: "Would you like to use this commit message? (Y / n)", - choices: ["Y", "y", "n"], - default: "y", - }, - ]); + if (confirmationMessage.useCommitMessage === "n") { + console.log(chalk.white("▲ ") + "Commit message has not been commited."); + process.exit(1); + } + + execSync(`git commit -m "${aiCommitMessage}"`, { + stdio: "inherit", + encoding: "utf8", + }); - if (confirmationMessage.useCommitMessage === "n") { - console.log(chalk.white("▲ ") + "Commit message has not been commited."); + } catch(e) { + console.error(chalk.white("▲ ") + chalk.red(e.message)); process.exit(1); } - - execSync(`git commit -m "${aiCommitMessage}"`, { - stdio: "inherit", - encoding: "utf8", - }); } async function generateCommitMessage(prompt: string) { @@ -101,6 +106,10 @@ async function generateCommitMessage(prompt: string) { method: "POST", body: JSON.stringify(payload), }); + if(response.status !== 200) { + const errorJson: any = await response.json(); + throw new Error(`OpenAI API failed while processing the request '${errorJson?.error?.message}'`); + } const json: any = await response.json(); const aiCommit = json.choices[0].text; From 4cff183269ad2a55b65fcdcdf066dd7a61703496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8A=D9=88=D8=B3=D9=81=20=D8=A7=D9=84=D8=A3=D9=88=D8=B3?= =?UTF-8?q?=D9=8A?= Date: Tue, 14 Feb 2023 18:22:53 +1000 Subject: [PATCH 2/3] Formatting --- aicommits.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/aicommits.ts b/aicommits.ts index 354ed4e..b4dd058 100644 --- a/aicommits.ts +++ b/aicommits.ts @@ -13,7 +13,7 @@ export async function main() { if (!OPENAI_API_KEY) { console.error( chalk.white("▲ ") + - "Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_KEY=YOUR_API_KEY'" + "Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_KEY=YOUR_API_KEY'", ); process.exit(1); } @@ -31,13 +31,13 @@ export async function main() { `git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"`, { encoding: "utf8", - } + }, ); if (!diff) { console.log( chalk.white("▲ ") + - "No staged changes found. Make sure there are changes and run `git add .`" + "No staged changes found. Make sure there are changes and run `git add .`", ); process.exit(1); } @@ -45,20 +45,22 @@ export async function main() { // Accounting for GPT-3's input req of 4k tokens (approx 8k chars) if (diff.length > 8000) { console.log( - chalk.white("▲ ") + "The diff is too large to write a commit message." + chalk.white("▲ ") + "The diff is too large to write a commit message.", ); process.exit(1); } - let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`; + let prompt = + `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`; console.log( - chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n") + chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n"), ); try { const aiCommitMessage = await generateCommitMessage(prompt); console.log( - chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n" + chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + + "\n", ); const confirmationMessage = await inquirer.prompt([ @@ -74,13 +76,12 @@ export async function main() { console.log(chalk.white("▲ ") + "Commit message has not been commited."); process.exit(1); } - + execSync(`git commit -m "${aiCommitMessage}"`, { stdio: "inherit", encoding: "utf8", }); - - } catch(e) { + } catch (e) { console.error(chalk.white("▲ ") + chalk.red(e.message)); process.exit(1); } @@ -106,9 +107,12 @@ async function generateCommitMessage(prompt: string) { method: "POST", body: JSON.stringify(payload), }); - if(response.status !== 200) { - const errorJson: any = await response.json(); - throw new Error(`OpenAI API failed while processing the request '${errorJson?.error?.message}'`); + if (response.status !== 200) { + const errorJson: any = await response.json(); + throw new Error( + `OpenAI API failed while processing the request '${errorJson?.error + ?.message}'`, + ); } const json: any = await response.json(); From 4b8cd2ccc00423255a7e152daa45a2581cfbcb02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8A=D9=88=D8=B3=D9=81=20=D8=A7=D9=84=D8=A3=D9=88=D8=B3?= =?UTF-8?q?=D9=8A?= Date: Tue, 14 Feb 2023 18:27:37 +1000 Subject: [PATCH 3/3] Formatting --- aicommits.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/aicommits.ts b/aicommits.ts index b4dd058..c57dc83 100644 --- a/aicommits.ts +++ b/aicommits.ts @@ -13,7 +13,7 @@ export async function main() { if (!OPENAI_API_KEY) { console.error( chalk.white("▲ ") + - "Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_KEY=YOUR_API_KEY'", + "Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_KEY=YOUR_API_KEY'" ); process.exit(1); } @@ -31,13 +31,13 @@ export async function main() { `git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"`, { encoding: "utf8", - }, + } ); if (!diff) { console.log( chalk.white("▲ ") + - "No staged changes found. Make sure there are changes and run `git add .`", + "No staged changes found. Make sure there are changes and run `git add .`" ); process.exit(1); } @@ -45,17 +45,17 @@ export async function main() { // Accounting for GPT-3's input req of 4k tokens (approx 8k chars) if (diff.length > 8000) { console.log( - chalk.white("▲ ") + "The diff is too large to write a commit message.", + chalk.white("▲ ") + "The diff is too large to write a commit message." ); process.exit(1); } - let prompt = - `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`; + let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`; console.log( - chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n"), + chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n") ); + try { const aiCommitMessage = await generateCommitMessage(prompt); console.log( @@ -110,8 +110,7 @@ async function generateCommitMessage(prompt: string) { if (response.status !== 200) { const errorJson: any = await response.json(); throw new Error( - `OpenAI API failed while processing the request '${errorJson?.error - ?.message}'`, + `OpenAI API failed while processing the request '${errorJson?.error?.message}'`, ); }