Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config.ts): add OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration i… #208

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -124,6 +124,7 @@ OCO_DESCRIPTION=<postface a message with ~3 sentences description>
OCO_EMOJI=<add GitMoji>
OCO_MODEL=<either gpt-3.5-turbo or gpt-4>
OCO_LANGUAGE=<locale, scroll to the bottom to see options>
OCO_MESSAGE_TEMPLATE_PLACEHOLDER=<message template placeholder, example: '$msg'>
```

### Global config for all repos
@@ -193,6 +194,14 @@ is translated to :
git commit -m "${generatedMessage}" --no-verify
```

To include a message in the generated message, you can utilize the template function! For instance:

```sh
oco '$msg #205’
```

> opencommit examines placeholders in the parameters, allowing you to append additional information before and after the placeholders, such as the relevant Issue or Pull Request. Similarly, you have the option to customize the OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration item, for example, simplifying it to $m!"

### Ignore files

You can ignore files from submission to OpenAI by creating a `.opencommitignore` file. For example:
19 changes: 18 additions & 1 deletion src/commands/commit.ts
Original file line number Diff line number Diff line change
@@ -18,25 +18,42 @@ import {
multiselect,
select
} from '@clack/prompts';
import {
getConfig
} from '../commands/config';
import chalk from 'chalk';
import { trytm } from '../utils/trytm';

const config = getConfig();

const getGitRemotes = async () => {
const { stdout } = await execa('git', ['remote']);
return stdout.split('\n').filter((remote) => Boolean(remote.trim()));
};

// Check for the presence of message templates
const checkMessageTemplate = (extraArgs: string[]): string | false => {
for(const key in extraArgs){
if(extraArgs[key].includes(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER)) return extraArgs[key];
}
return false;
};

const generateCommitMessageFromGitDiff = async (
diff: string,
extraArgs: string[]
): Promise<void> => {
const messageTemplate = checkMessageTemplate(extraArgs);
await assertGitRepo();

const commitSpinner = spinner();
commitSpinner.start('Generating the commit message');
try {
const commitMessage = await generateCommitMessageByDiff(diff);
let commitMessage = await generateCommitMessageByDiff(diff);

if(typeof messageTemplate === 'string'){
commitMessage = messageTemplate.replace(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER, commitMessage);
}
commitSpinner.stop('📝 Commit message generated');

outro(
14 changes: 12 additions & 2 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,8 @@ export enum CONFIG_KEYS {
OCO_DESCRIPTION = 'OCO_DESCRIPTION',
OCO_EMOJI = 'OCO_EMOJI',
OCO_MODEL = 'OCO_MODEL',
OCO_LANGUAGE = 'OCO_LANGUAGE'
OCO_LANGUAGE = 'OCO_LANGUAGE',
OCO_MESSAGE_TEMPLATE_PLACEHOLDER = 'OCO_MESSAGE_TEMPLATE_PLACEHOLDER',
}

export const DEFAULT_MODEL_TOKEN_LIMIT = 4096;
@@ -124,6 +125,14 @@ export const configValidators = {
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
);
return value;
},
[CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER](value: any) {
validateConfig(
CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
value.startsWith('$'),
`${value} must start with $, for example: '$msg'`
);
return value;
}
};

@@ -141,7 +150,8 @@ export const getConfig = (): ConfigType | null => {
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo',
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en'
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en',
OCO_MESSAGE_TEMPLATE_PLACEHOLDER: process.env.OCO_MESSAGE_TEMPLATE_PLACEHOLDER || '$msg'
};

const configExists = existsSync(configPath);