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

Allow for commit prefix #160

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@dqbd/tiktoken": "^1.0.2",
"axios": "^1.3.4",
"chalk": "^5.2.0",
"child_process": "^1.0.2",
amitayk marked this conversation as resolved.
Show resolved Hide resolved
"cleye": "^1.3.2",
"execa": "^7.0.0",
"ignore": "^5.2.4",
Expand Down
60 changes: 59 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { intro, outro } from '@clack/prompts';
import { execSync } from 'child_process';
import axios from 'axios';
import chalk from 'chalk';
import {
Expand Down Expand Up @@ -59,7 +60,11 @@ class OpenAi {

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

return message?.content;
const prefix = generatePrefix();

const finalMessage = (prefix != "undefined" ? prefix + ' ' : '') + (message?.content || '')

return finalMessage;
} catch (error: unknown) {
outro(`${chalk.red('✖')} ${error}`);

Expand Down Expand Up @@ -94,4 +99,57 @@ export const getOpenCommitLatestVersion = async (): Promise<
}
};

function generatePrefix(): string | undefined {
const prefix = config?.prefix

if (prefix === undefined) {
return undefined;
}

const prefixIsRegexString = prefix.startsWith('/') && prefix.endsWith('/');

if (prefixIsRegexString) {
try {
return generatePrefixFromRegex(prefix);
} catch (error) {
console.error(`Failed to generate prefix from regex: ${error}`);
return undefined;
}
}

return prefix;
}

export const api = new OpenAi();


function generatePrefixFromRegex(regex: string): string | undefined {

// We currently only support regex input from git branch name

const branch = getCurrentGitBranch();

if (branch === undefined) {
return undefined;
}

const regexWithoutSlashes = regex.slice(1, -1);
const regexObject = new RegExp(regexWithoutSlashes);
const match = branch.match(regexObject);

if (match === null) {
return undefined;
}

return match[0];
amitayk marked this conversation as resolved.
Show resolved Hide resolved
}

function getCurrentGitBranch(): string | undefined {
try {
const branchName = execSync('git symbolic-ref --short HEAD', { encoding: 'utf8' }).trim();
return branchName;
} catch (error) {
console.error(`Failed to get current git branch: ${error}`);
return undefined;
}
}
13 changes: 12 additions & 1 deletion src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export enum CONFIG_KEYS {
description = 'description',
emoji = 'emoji',
model = 'model',
language = 'language'
language = 'language',
prefix = 'prefix'
}

export enum CONFIG_MODES {
Expand Down Expand Up @@ -109,7 +110,17 @@ export const configValidators = {
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
);
return value;
},

[CONFIG_KEYS.prefix](value: any) {
validateConfig(
CONFIG_KEYS.prefix,
true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true is not validating anything here :) we should validate the config input with a function or expression which return Boolean

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@di-sukharev Do you have an idea what can we validate?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you somehow need to confirm that the value is the exact format you expect, if your format is ^(*) and not /^(*)/ — make sure you don't have /s in the value, you may also do multiple validations like !a && b && !c

'Cannot be empty'
);
return value;
}

};

export type ConfigType = {
Expand Down