Skip to content

Commit

Permalink
* 📝 docs(TODO.md): add TODOs
Browse files Browse the repository at this point in the history
Added a TODO list with tasks that need to be completed. These tasks include building for both mjs and cjs, configuring esbuild to make the bundle smaller, adding // TODOs in the code, batching small files in one request, adding tests, and making the hook work.

* ✨ feat(api.ts): add OpenAI class with generateCommitMessage method
This commit adds a new OpenAI class with a generateCommitMessage method that uses the OpenAI API to generate a commit message based on an array of messages. The method takes an array of ChatCompletionRequestMessage objects and returns a Promise that resolves to a ChatCompletionResponseMessage object. The OpenAI class is exported as a singleton instance named api.

* ✨ feat(prepare-commit-msg-hook.ts): add support for generating commit messages with chat completion
This commit adds support for generating commit messages with chat completion. The `prepare-commit-msg-hook.ts` file now imports the `generateCommitMessageWithChatCompletion` function from the `generateCommitMessageFromGitDiff` module. The function generates a commit message based on the staged git diff and appends it to the commit message file. If the `OPENAI_API_KEY` environment variable is not set, an error is thrown. If the commit source is specified, the function returns without generating a commit message.

* 🆕 feat(generateCommitMessageFromGitDiff.ts): add functionality to generate commit messages from git diff
This commit adds a new file, generateCommitMessageFromGitDiff.ts, which contains a function that generates commit messages from the output of the 'git diff --staged' command. The function uses the OpenAI API to prompt the user to create a commit message in the conventional commit convention. The user can choose to use Gitmoji convention to preface the commit and add a short description of what the commit is about.

* 🐛 fix(server.ts): change port variable case from lowercase port to uppercase PORT
* ✨ feat(server.ts): add support for process.env.PORT environment variable
The port variable is now named PORT, which improves consistency with the naming conventions as PORT is a constant. Support for an environment variable allows the application to be more flexible as it can now run on any available port specified via the process.env.PORT environment variable.

* 🚀 feat: add function to generate commit messages from diff
This commit adds a new function that generates commit messages from a diff. The function takes a diff as input and splits it into files. It then generates commit messages for each file and returns them as a concatenated string. If the total length of the commit message exceeds the maximum allowed length, the function skips the file. If the commit message is empty, the function skips the file. If an error occurs during the process, the function returns an error.

* ✨ feat(git.ts): add function to assert git repository existence
* ✨ feat(git.ts): add function to get staged git diff
The assertGitRepo function checks if the current directory is a git repository by running the 'git rev-parse' command. If the command fails, an error is thrown.

The getStagedGitDiff function returns the staged diff of the git repository. It takes an optional boolean argument isStageAllFlag, which when true stages all changes before getting the diff. The function uses the 'git diff --staged' command to get the diff and excludes big files from the diff. The function returns an object with two properties: files, which is an array of the names of the files that have changes, and diff, which is the diff of the staged changes.
  • Loading branch information
di-sukharev committed Mar 6, 2023
1 parent 4edb9bd commit eae7618
Show file tree
Hide file tree
Showing 8 changed files with 626 additions and 0 deletions.
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TODOs

- [] [build for both mjs and cjs](https://snyk.io/blog/best-practices-create-modern-npm-package/)
- [] make bundle smaller by properly configuring esbuild
- [] do // TODOs in the code
- [] batch small files in one request
- [] add tests
- [] make hook work
66 changes: 66 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { intro, outro } from '@clack/prompts';
import {
ChatCompletionRequestMessage,
ChatCompletionResponseMessage,
Configuration as OpenAiApiConfiguration,
OpenAIApi
} from 'openai';

import { getConfig } from './commands/config';

const config = getConfig();

let apiKey = config?.OPENAI_API_KEY;

if (!apiKey) {
intro('opencommit');

outro(
'OPENAI_API_KEY is not set, please run `oc config set OPENAI_API_KEY=<your token>`'
);
outro(
'For help Look into README https://github.com/di-sukharev/opencommit#setup'
);
}

// if (!apiKey) {
// intro('opencommit');
// const apiKey = await text({
// message: 'input your OPENAI_API_KEY'
// });

// setConfig([[CONFIG_KEYS.OPENAI_API_KEY as string, apiKey as any]]);

// outro('OPENAI_API_KEY is set');
// }

class OpenAi {
private openAiApiConfiguration = new OpenAiApiConfiguration({
apiKey: apiKey
});

private openAI = new OpenAIApi(this.openAiApiConfiguration);

public generateCommitMessage = async (
messages: Array<ChatCompletionRequestMessage>
): Promise<ChatCompletionResponseMessage | undefined> => {
try {
const { data } = await this.openAI.createChatCompletion({
model: 'gpt-3.5-turbo',
messages,
temperature: 0,
top_p: 0.1,
max_tokens: 196
});

const message = data.choices[0].message;

return message;
} catch (error) {
console.error('openAI api error', { error });
throw error;
}
};
}

export const api = new OpenAi();
104 changes: 104 additions & 0 deletions src/commands/commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { execa } from 'execa';
import {
GenerateCommitMessageErrorEnum,
generateCommitMessageWithChatCompletion
} from '../generateCommitMessageFromGitDiff';
import { assertGitRepo, getStagedGitDiff } from '../utils/git';
import { spinner, confirm, outro, isCancel, intro } from '@clack/prompts';
import chalk from 'chalk';

const generateCommitMessageFromGitDiff = async (
diff: string
): Promise<void> => {
await assertGitRepo();

const commitSpinner = spinner();
commitSpinner.start('Generating the commit message');
const commitMessage = await generateCommitMessageWithChatCompletion(diff);

if (typeof commitMessage !== 'string') {
const errorMessages = {
[GenerateCommitMessageErrorEnum.emptyMessage]:
'empty openAI response, weird, try again',
[GenerateCommitMessageErrorEnum.internalError]:
'internal error, try again',
[GenerateCommitMessageErrorEnum.tooMuchTokens]:
'too much tokens in git diff, stage and commit files in parts'
};

outro(`${chalk.red('✖')} ${errorMessages[commitMessage.error]}`);
process.exit(1);
}

commitSpinner.stop('📝 Commit message generated');

outro(
`Commit message:
${chalk.grey('——————————————————')}
${commitMessage}
${chalk.grey('——————————————————')}`
);

const isCommitConfirmedByUser = await confirm({
message: 'Confirm the commit message'
});

if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
await execa('git', ['commit', '-m', commitMessage]);
outro(`${chalk.green('✔')} successfully committed`);
} else outro(`${chalk.gray('✖')} process cancelled`);
};

export async function commit(isStageAllFlag = false) {
intro('open-commit');

const stagedFilesSpinner = spinner();
stagedFilesSpinner.start('Counting staged files');
const staged = await getStagedGitDiff(isStageAllFlag);

if (!staged && isStageAllFlag) {
outro(
`${chalk.red(
'No changes detected'
)} — write some code, stage the files ${chalk
.hex('0000FF')
.bold('`git add .`')} and rerun ${chalk
.hex('0000FF')
.bold('`oc`')} command.`
);

process.exit(1);
}

if (!staged) {
outro(
`${chalk.red('Nothing to commit')} — stage the files ${chalk
.hex('0000FF')
.bold('`git add .`')} and rerun ${chalk
.hex('0000FF')
.bold('`oc`')} command.`
);

stagedFilesSpinner.stop('Counting staged files');
const isStageAllAndCommitConfirmedByUser = await confirm({
message: 'Do you want to stage all files and generate commit message?'
});

if (
isStageAllAndCommitConfirmedByUser &&
!isCancel(isStageAllAndCommitConfirmedByUser)
) {
await commit(true);
}

process.exit(1);
}

stagedFilesSpinner.stop(
`${staged.files.length} staged files:\n${staged.files
.map((file) => ` ${file}`)
.join('\n')}`
);

await generateCommitMessageFromGitDiff(staged.diff);
}
140 changes: 140 additions & 0 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { command } from 'cleye';
import { join as pathJoin } from 'path';
import { parse as iniParse, stringify as iniStringify } from 'ini';
import { existsSync, writeFileSync, readFileSync } from 'fs';
import { homedir } from 'os';
import { intro, outro } from '@clack/prompts';
import chalk from 'chalk';

export enum CONFIG_KEYS {
OPENAI_API_KEY = 'OPENAI_API_KEY',
description = 'description',
emoji = 'emoji'
}

const validateConfig = (
key: string,
condition: any,
validationMessage: string
) => {
if (!condition) {
throw new Error(`Unsupported config key ${key}: ${validationMessage}`);
}
};

export const configValidators = {
[CONFIG_KEYS.OPENAI_API_KEY](value: any) {
validateConfig(CONFIG_KEYS.OPENAI_API_KEY, value, 'Cannot be empty');
validateConfig(
CONFIG_KEYS.OPENAI_API_KEY,
value.startsWith('sk-'),
'Must start with "sk-"'
);
validateConfig(
CONFIG_KEYS.OPENAI_API_KEY,
value.length === 51,
'Must be 51 characters long'
);

return value;
},
[CONFIG_KEYS.description](value: any) {
validateConfig(
CONFIG_KEYS.description,
typeof value === 'boolean',
'Must be true or false'
);

return value;
},
[CONFIG_KEYS.emoji](value: any) {
validateConfig(
CONFIG_KEYS.emoji,
typeof value === 'boolean',
'Must be true or false'
);

return value;
}
};

export type ConfigType = {
[key in CONFIG_KEYS]?: any;
};

const configPath = pathJoin(homedir(), '.opencommit');

export const getConfig = (): ConfigType | null => {
const configExists = existsSync(configPath);
if (!configExists) return null;

const configFile = readFileSync(configPath, 'utf8');
const config = iniParse(configFile);

for (const configKey of Object.keys(config)) {
const validValue = configValidators[configKey as CONFIG_KEYS](
config[configKey]
);

config[configKey] = validValue;
}

return config;
};

export const setConfig = (keyValues: [key: string, value: string][]) => {
const config = getConfig() || {};

for (const [configKey, configValue] of keyValues) {
if (!configValidators.hasOwnProperty(configKey)) {
throw new Error(`Unsupported config key: ${configKey}`);
}

let parsedConfigValue;

try {
parsedConfigValue = JSON.parse(configValue);
} catch (error) {
parsedConfigValue = configValue;
}

const validValue =
configValidators[configKey as CONFIG_KEYS](parsedConfigValue);
config[configKey as CONFIG_KEYS] = validValue;
}

writeFileSync(configPath, iniStringify(config), 'utf8');

outro(`${chalk.green('✔')} config successfully set`);
};

export const configCommand = command(
{
name: 'config',
parameters: ['<mode>', '<key=values...>']
},
async (argv) => {
intro('opencommit — config');
try {
const { mode, keyValues } = argv._;

if (mode === 'get') {
const config = getConfig() || {};
for (const key of keyValues) {
outro(`${key}=${config[key as keyof typeof config]}`);
}
} else if (mode === 'set') {
await setConfig(
keyValues.map((keyValue) => keyValue.split('=') as [string, string])
);
} else {
throw new Error(
`Unsupported mode: ${mode}. Valid modes are: "set" and "get"`
);
}
} catch (error) {
outro(`${chalk.red('✖')} ${error}`);
process.exit(1);
}
}
);
Loading

0 comments on commit eae7618

Please sign in to comment.