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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(api.ts): add support for generating commit message prefix from r…
…egex pattern in git branch name

feat(api.ts): add support for generating commit message prefix from git branch name if regex pattern is not provided
feat(api.ts): add getCurrentGitBranch function to get current git branch name
feat(api.ts): add generatePrefixFromRegex function to generate prefix from regex pattern in git branch name
  • Loading branch information
amitayk committed May 4, 2023
commit abb488eddbe6e6590f67e1fdf6446d886c139070
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
@@ -64,6 +64,7 @@
"@dqbd/tiktoken": "^1.0.2",
"axios": "^1.3.4",
"chalk": "^5.2.0",
"child_process": "^1.0.2",
"cleye": "^1.3.2",
"execa": "^7.0.0",
"ignore": "^5.2.4",
54 changes: 50 additions & 4 deletions 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 {
@@ -61,8 +62,8 @@ class OpenAi {

const prefix = generatePrefix();

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

return finalMessage;
} catch (error: unknown) {
outro(`${chalk.red('✖')} ${error}`);
@@ -99,11 +100,56 @@ export const getOpenCommitLatestVersion = async (): Promise<
};

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

if (prefix === undefined) {
return undefined;
}
return config?.prefix;

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];
}

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;
}
}