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 7 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
61 changes: 60 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'openai';

import { CONFIG_MODES, getConfig } from './commands/config';
import { execaCommandSync } from 'execa';

const config = getConfig();

Expand Down Expand Up @@ -59,7 +60,12 @@ class OpenAi {

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

return message?.content;
const prefix = generatePrefix();
const usePrefix = prefix != undefined && prefix?.trim() != ""

const finalMessage = (usePrefix ? prefix + ' ' : '') + (message?.content || '')

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

Expand Down Expand Up @@ -94,4 +100,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.length > 1 ? match[1] : match[0];
}

function getCurrentGitBranch(): string | undefined {
try {
const branchName = execaCommandSync('git symbolic-ref --short HEAD').stdout.trim()
Copy link
Owner

Choose a reason for hiding this comment

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

is this going to add the full branch name in the commit like: #234-some-branch-name commit message text goes here?

Copy link
Author

Choose a reason for hiding this comment

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

No, it will take a regex out of the name. See my first commit on this PR.
Very cool to hear about the hackathon!

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