Skip to content

Commit eae7618

Browse files
committed
* πŸ“ docs(TODO.md): add TODOs
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.
1 parent 4edb9bd commit eae7618

File tree

8 files changed

+626
-0
lines changed

8 files changed

+626
-0
lines changed

β€ŽTODO.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# TODOs
2+
3+
- [] [build for both mjs and cjs](https://snyk.io/blog/best-practices-create-modern-npm-package/)
4+
- [] make bundle smaller by properly configuring esbuild
5+
- [] do // TODOs in the code
6+
- [] batch small files in one request
7+
- [] add tests
8+
- [] make hook work

β€Žsrc/api.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { intro, outro } from '@clack/prompts';
2+
import {
3+
ChatCompletionRequestMessage,
4+
ChatCompletionResponseMessage,
5+
Configuration as OpenAiApiConfiguration,
6+
OpenAIApi
7+
} from 'openai';
8+
9+
import { getConfig } from './commands/config';
10+
11+
const config = getConfig();
12+
13+
let apiKey = config?.OPENAI_API_KEY;
14+
15+
if (!apiKey) {
16+
intro('opencommit');
17+
18+
outro(
19+
'OPENAI_API_KEY is not set, please run `oc config set OPENAI_API_KEY=<your token>`'
20+
);
21+
outro(
22+
'For help Look into README https://github.com/di-sukharev/opencommit#setup'
23+
);
24+
}
25+
26+
// if (!apiKey) {
27+
// intro('opencommit');
28+
// const apiKey = await text({
29+
// message: 'input your OPENAI_API_KEY'
30+
// });
31+
32+
// setConfig([[CONFIG_KEYS.OPENAI_API_KEY as string, apiKey as any]]);
33+
34+
// outro('OPENAI_API_KEY is set');
35+
// }
36+
37+
class OpenAi {
38+
private openAiApiConfiguration = new OpenAiApiConfiguration({
39+
apiKey: apiKey
40+
});
41+
42+
private openAI = new OpenAIApi(this.openAiApiConfiguration);
43+
44+
public generateCommitMessage = async (
45+
messages: Array<ChatCompletionRequestMessage>
46+
): Promise<ChatCompletionResponseMessage | undefined> => {
47+
try {
48+
const { data } = await this.openAI.createChatCompletion({
49+
model: 'gpt-3.5-turbo',
50+
messages,
51+
temperature: 0,
52+
top_p: 0.1,
53+
max_tokens: 196
54+
});
55+
56+
const message = data.choices[0].message;
57+
58+
return message;
59+
} catch (error) {
60+
console.error('openAI api error', { error });
61+
throw error;
62+
}
63+
};
64+
}
65+
66+
export const api = new OpenAi();

β€Žsrc/commands/commit.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { execa } from 'execa';
2+
import {
3+
GenerateCommitMessageErrorEnum,
4+
generateCommitMessageWithChatCompletion
5+
} from '../generateCommitMessageFromGitDiff';
6+
import { assertGitRepo, getStagedGitDiff } from '../utils/git';
7+
import { spinner, confirm, outro, isCancel, intro } from '@clack/prompts';
8+
import chalk from 'chalk';
9+
10+
const generateCommitMessageFromGitDiff = async (
11+
diff: string
12+
): Promise<void> => {
13+
await assertGitRepo();
14+
15+
const commitSpinner = spinner();
16+
commitSpinner.start('Generating the commit message');
17+
const commitMessage = await generateCommitMessageWithChatCompletion(diff);
18+
19+
if (typeof commitMessage !== 'string') {
20+
const errorMessages = {
21+
[GenerateCommitMessageErrorEnum.emptyMessage]:
22+
'empty openAI response, weird, try again',
23+
[GenerateCommitMessageErrorEnum.internalError]:
24+
'internal error, try again',
25+
[GenerateCommitMessageErrorEnum.tooMuchTokens]:
26+
'too much tokens in git diff, stage and commit files in parts'
27+
};
28+
29+
outro(`${chalk.red('βœ–')} ${errorMessages[commitMessage.error]}`);
30+
process.exit(1);
31+
}
32+
33+
commitSpinner.stop('πŸ“ Commit message generated');
34+
35+
outro(
36+
`Commit message:
37+
${chalk.grey('β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”')}
38+
${commitMessage}
39+
${chalk.grey('β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”')}`
40+
);
41+
42+
const isCommitConfirmedByUser = await confirm({
43+
message: 'Confirm the commit message'
44+
});
45+
46+
if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
47+
await execa('git', ['commit', '-m', commitMessage]);
48+
outro(`${chalk.green('βœ”')} successfully committed`);
49+
} else outro(`${chalk.gray('βœ–')} process cancelled`);
50+
};
51+
52+
export async function commit(isStageAllFlag = false) {
53+
intro('open-commit');
54+
55+
const stagedFilesSpinner = spinner();
56+
stagedFilesSpinner.start('Counting staged files');
57+
const staged = await getStagedGitDiff(isStageAllFlag);
58+
59+
if (!staged && isStageAllFlag) {
60+
outro(
61+
`${chalk.red(
62+
'No changes detected'
63+
)} β€” write some code, stage the files ${chalk
64+
.hex('0000FF')
65+
.bold('`git add .`')} and rerun ${chalk
66+
.hex('0000FF')
67+
.bold('`oc`')} command.`
68+
);
69+
70+
process.exit(1);
71+
}
72+
73+
if (!staged) {
74+
outro(
75+
`${chalk.red('Nothing to commit')} β€” stage the files ${chalk
76+
.hex('0000FF')
77+
.bold('`git add .`')} and rerun ${chalk
78+
.hex('0000FF')
79+
.bold('`oc`')} command.`
80+
);
81+
82+
stagedFilesSpinner.stop('Counting staged files');
83+
const isStageAllAndCommitConfirmedByUser = await confirm({
84+
message: 'Do you want to stage all files and generate commit message?'
85+
});
86+
87+
if (
88+
isStageAllAndCommitConfirmedByUser &&
89+
!isCancel(isStageAllAndCommitConfirmedByUser)
90+
) {
91+
await commit(true);
92+
}
93+
94+
process.exit(1);
95+
}
96+
97+
stagedFilesSpinner.stop(
98+
`${staged.files.length} staged files:\n${staged.files
99+
.map((file) => ` ${file}`)
100+
.join('\n')}`
101+
);
102+
103+
await generateCommitMessageFromGitDiff(staged.diff);
104+
}

β€Žsrc/commands/config.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { command } from 'cleye';
2+
import { join as pathJoin } from 'path';
3+
import { parse as iniParse, stringify as iniStringify } from 'ini';
4+
import { existsSync, writeFileSync, readFileSync } from 'fs';
5+
import { homedir } from 'os';
6+
import { intro, outro } from '@clack/prompts';
7+
import chalk from 'chalk';
8+
9+
export enum CONFIG_KEYS {
10+
OPENAI_API_KEY = 'OPENAI_API_KEY',
11+
description = 'description',
12+
emoji = 'emoji'
13+
}
14+
15+
const validateConfig = (
16+
key: string,
17+
condition: any,
18+
validationMessage: string
19+
) => {
20+
if (!condition) {
21+
throw new Error(`Unsupported config key ${key}: ${validationMessage}`);
22+
}
23+
};
24+
25+
export const configValidators = {
26+
[CONFIG_KEYS.OPENAI_API_KEY](value: any) {
27+
validateConfig(CONFIG_KEYS.OPENAI_API_KEY, value, 'Cannot be empty');
28+
validateConfig(
29+
CONFIG_KEYS.OPENAI_API_KEY,
30+
value.startsWith('sk-'),
31+
'Must start with "sk-"'
32+
);
33+
validateConfig(
34+
CONFIG_KEYS.OPENAI_API_KEY,
35+
value.length === 51,
36+
'Must be 51 characters long'
37+
);
38+
39+
return value;
40+
},
41+
[CONFIG_KEYS.description](value: any) {
42+
validateConfig(
43+
CONFIG_KEYS.description,
44+
typeof value === 'boolean',
45+
'Must be true or false'
46+
);
47+
48+
return value;
49+
},
50+
[CONFIG_KEYS.emoji](value: any) {
51+
validateConfig(
52+
CONFIG_KEYS.emoji,
53+
typeof value === 'boolean',
54+
'Must be true or false'
55+
);
56+
57+
return value;
58+
}
59+
};
60+
61+
export type ConfigType = {
62+
[key in CONFIG_KEYS]?: any;
63+
};
64+
65+
const configPath = pathJoin(homedir(), '.opencommit');
66+
67+
export const getConfig = (): ConfigType | null => {
68+
const configExists = existsSync(configPath);
69+
if (!configExists) return null;
70+
71+
const configFile = readFileSync(configPath, 'utf8');
72+
const config = iniParse(configFile);
73+
74+
for (const configKey of Object.keys(config)) {
75+
const validValue = configValidators[configKey as CONFIG_KEYS](
76+
config[configKey]
77+
);
78+
79+
config[configKey] = validValue;
80+
}
81+
82+
return config;
83+
};
84+
85+
export const setConfig = (keyValues: [key: string, value: string][]) => {
86+
const config = getConfig() || {};
87+
88+
for (const [configKey, configValue] of keyValues) {
89+
if (!configValidators.hasOwnProperty(configKey)) {
90+
throw new Error(`Unsupported config key: ${configKey}`);
91+
}
92+
93+
let parsedConfigValue;
94+
95+
try {
96+
parsedConfigValue = JSON.parse(configValue);
97+
} catch (error) {
98+
parsedConfigValue = configValue;
99+
}
100+
101+
const validValue =
102+
configValidators[configKey as CONFIG_KEYS](parsedConfigValue);
103+
config[configKey as CONFIG_KEYS] = validValue;
104+
}
105+
106+
writeFileSync(configPath, iniStringify(config), 'utf8');
107+
108+
outro(`${chalk.green('βœ”')} config successfully set`);
109+
};
110+
111+
export const configCommand = command(
112+
{
113+
name: 'config',
114+
parameters: ['<mode>', '<key=values...>']
115+
},
116+
async (argv) => {
117+
intro('opencommit β€” config');
118+
try {
119+
const { mode, keyValues } = argv._;
120+
121+
if (mode === 'get') {
122+
const config = getConfig() || {};
123+
for (const key of keyValues) {
124+
outro(`${key}=${config[key as keyof typeof config]}`);
125+
}
126+
} else if (mode === 'set') {
127+
await setConfig(
128+
keyValues.map((keyValue) => keyValue.split('=') as [string, string])
129+
);
130+
} else {
131+
throw new Error(
132+
`Unsupported mode: ${mode}. Valid modes are: "set" and "get"`
133+
);
134+
}
135+
} catch (error) {
136+
outro(`${chalk.red('βœ–')} ${error}`);
137+
process.exit(1);
138+
}
139+
}
140+
);

0 commit comments

Comments
Β (0)