Skip to content

Commit

Permalink
feat: 🔥 support OpenAI
Browse files Browse the repository at this point in the history
Querying OpenAI API to get the commit subject and the body as default
values
  • Loading branch information
tal-rofe committed Apr 21, 2023
1 parent d2bcb71 commit a5dc8c6
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
},
plugins: ['@typescript-eslint', 'unused-imports', 'node', 'import', 'deprecation', 'unicorn'],
rules: {
'max-lines': ['error', { max: 100, skipBlankLines: true, skipComments: true }],
'max-lines': ['error', { max: 150, skipBlankLines: true, skipComments: true }],
'indent': ['error', 'tab', { SwitchCase: 1 }],
'quotes': ['error', 'single', { avoidEscape: true }],
'semi': ['error', 'always'],
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"material-icon-theme.activeIconPack": "nest",
"search.exclude": {
"**/node_modules": true
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
"cosmiconfig": "8.1.3",
"cosmiconfig-typescript-loader": "2.0.2",
"fuse.js": "6.6.2",
"inquirer": "8.2.4",
"inquirer-autocomplete-prompt": "3.0.0",
"inquirer-autocomplete-prompt": "2.0.0",
"inquirer-maxlength-input-prompt": "1.0.2",
"openai": "3.2.1",
"string-template": "1.0.0",
"word-wrap": "1.2.3",
"zod": "3.21.4"
Expand Down
129 changes: 64 additions & 65 deletions pnpm-lock.yaml

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

30 changes: 4 additions & 26 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
export interface CommitType {
value: string;
description: string;
emoji?: string;
}
import type { z } from 'zod';

export type Config = Partial<{
headerFormat: string;
commitTypes: CommitType[];
maxCommitLineWidth: number;
typeQuestion: string;
scopeQuestion: string;
skipScope: boolean;
scopes: string[];
ticketIdQuestion: string;
skipTicketId: boolean;
ticketIdRegex: string;
allowEmptyTicketIdForBranches: string[];
subjectQuestion: string;
subjectMaxLength: number;
subjectMinLength: number;
bodyQuestion: string;
skipBody: boolean;
skipBreakingChanges: boolean;
issuesQuestion: string;
skipIssues: boolean;
}>;
import type ConfigurationSchema from './models/configuration';

export type CzVinylConfig = z.infer<typeof ConfigurationSchema>;
7 changes: 7 additions & 0 deletions src/constants/ai-commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const AI_COMMIT_IGNORED_FILES = [
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
'go.sum',
'go.work.sum',
];
1 change: 1 addition & 0 deletions src/constants/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export const DEFAULT_CONFIGURATION: FinalConfiguration = {
skipBreakingChanges: true,
issuesQuestion: 'List any issue closed (#1, #2, ...):',
skipIssues: true,
openAiToken: null,
};
8 changes: 8 additions & 0 deletions src/models/ai-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod';

const AiResponseSchema = z.object({
subject: z.string(),
body: z.string().optional(),
});

export default AiResponseSchema;
3 changes: 3 additions & 0 deletions src/models/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const ConfigurationSchema = z
invalid_type_error: '"issuesQuestion" configuration key must be a string',
}),
skipIssues: z.boolean({ invalid_type_error: '"skipIssues" configuration key must be a boolean' }),
openAiToken: z
.string({ invalid_type_error: '"openAiToken" configuration key must be a string or null' })
.nullable(),
})
.partial();

Expand Down
1 change: 1 addition & 0 deletions src/models/env-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const EnvConfigurationSchema = baseZ
invalid_type_error: '"CZ_ISSUES_QUESTION" env must be a string',
}),
CZ_SKIP_ISSUES: z.boolean({ invalid_type_error: '"CZ_SKIP_ISSUES" env must be a boolean' }),
CZ_OPEN_AI_TOKEN: z.string({ invalid_type_error: '"CZ_OPEN_AI_TOKEN" env must be a string' }),
})
.partial();

Expand Down
24 changes: 24 additions & 0 deletions src/services/open-ai/constants/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const USER_MESSAGE = `diff --git a/src/server.ts b/src/server.ts
index ad4db42..f3b18a9 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -10,7 +10,7 @@
import {
initWinstonLogger();
const app = express();
-const port = 7799;
+const PORT = 7799;
app.use(express.json());
@@ -34,6 +34,6 @@
app.use((_, res, next) => {
// ROUTES
app.use(PROTECTED_ROUTER_URL, protectedRouter);
-app.listen(port, () => {
- console.log(\`Server listening on port \${port}\`);
+app.listen(process.env.PORT || PORT, () => {
+ console.log(\`Server listening on port \${PORT}\`);
});`;
Loading

0 comments on commit a5dc8c6

Please sign in to comment.