Skip to content

Commit 4181c0b

Browse files
authored
Refactoring and cleaning for v3 (#244)
* chore(README.md): update setup instructions for OpenCommit as a GitHub Action fix(commit.ts): check if OCO_MESSAGE_TEMPLATE_PLACEHOLDER exists before replacing it in the commit message template The README.md file has been updated to reflect the changes in the setup instructions for using OpenCommit as a GitHub Action. The new instructions provide a file template for the GitHub Action workflow and include the latest version of the OpenCommit GitHub Action. In the commit.ts file, a fix has been made to check if the OCO_MESSAGE_TEMPLATE_PLACEHOLDER exists before replacing it in the commit message template. This ensures that the replacement is only performed when the placeholder is present, preventing any potential errors.
1 parent 765e988 commit 4181c0b

5 files changed

Lines changed: 91 additions & 84 deletions

File tree

README.md

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<h2>Auto-generate meaningful commits in 1 second</h2>
88
<p>Killing lame commits with AI 🤯🔫</p>
99
<a href="https://www.npmjs.com/package/opencommit"><img src="https://img.shields.io/npm/v/opencommit" alt="Current version"></a>
10-
<h4 align="center">🪩 Winner of GitHub 2023 HACKATHON <a href="https://twitter.com/io_Y_oi"><img style="width:18px; height:18px;" src=".github/github-mark-white.png" align="center"></a>
10+
<h4 align="center">🪩 Winner of GitHub 2023 HACKATHON <a href="https://twitter.com/io_Y_oi/status/1683448136973582336"><img style="width:14px; height:14px; margin-top: -4px" src=".github/github-mark-white.png" align="center"></a>
1111
</h4>
1212
</div>
1313

@@ -34,68 +34,11 @@ You can use OpenCommit by simply running it via the CLI like this `oco`. 2 secon
3434
3. Set the key to OpenCommit config:
3535

3636
```sh
37-
opencommit config set OCO_OPENAI_API_KEY=<your_api_key>
37+
oco config set OCO_OPENAI_API_KEY=<your_api_key>
3838
```
3939

4040
Your API key is stored locally in the `~/.opencommit` config file.
4141

42-
## Setup OpenCommit as a GitHub Action 🔥
43-
44-
OpenCommit is now available as a GitHub Action which automatically improves all new commits messages when you push to remote!
45-
46-
This is great if you want to make sure all of the commits in all of your repository branches are meaningful and not lame like `fix1` or `done2`.
47-
48-
Create a file `.github/workflows/opencommit.yml` with the contents below:
49-
50-
```yml
51-
name: 'OpenCommit Action'
52-
53-
on:
54-
push:
55-
# this list of branches is often enough,
56-
# but you may still ignore other public branches
57-
branches-ignore: [main master dev development release]
58-
59-
jobs:
60-
opencommit:
61-
timeout-minutes: 10
62-
name: OpenCommit
63-
runs-on: ubuntu-latest
64-
permissions: write-all
65-
steps:
66-
- name: Setup Node.js Environment
67-
uses: actions/setup-node@v2
68-
with:
69-
node-version: '16'
70-
- uses: actions/checkout@v3
71-
with:
72-
fetch-depth: 0
73-
- uses: di-sukharev/opencommit@github-action-v1.0.4
74-
with:
75-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76-
77-
env:
78-
# set openAI api key in repo actions secrets,
79-
# for openAI keys go to: https://platform.openai.com/account/api-keys
80-
# for repo secret go to: <your_repo_url>/settings/secrets/actions
81-
OCO_OPENAI_API_KEY: ${{ secrets.OCO_OPENAI_API_KEY }}
82-
83-
# customization
84-
OCO_OPENAI_MAX_TOKENS: 500
85-
OCO_OPENAI_BASE_PATH: ''
86-
OCO_DESCRIPTION: false
87-
OCO_EMOJI: false
88-
OCO_MODEL: gpt-3.5-turbo
89-
OCO_LANGUAGE: en
90-
OCO_PROMPT_MODULE: conventional-commit
91-
```
92-
93-
That is it. Now when you push to any branch in your repo — all NEW commits are being improved by your never-tired AI.
94-
95-
Make sure you exclude public collaboration branches (`main`, `dev`, `etc`) in `branches-ignore`, so OpenCommit does not rebase commits there while improving the messages.
96-
97-
Interactive rebase (`rebase -i`) changes commits' SHA, so the commit history in remote becomes different from your local branch history. This is okay if you work on the branch alone, but may be inconvenient for other collaborators.
98-
9942
## Usage
10043

10144
You can call OpenCommit directly to generate a commit message for your staged changes:
@@ -122,12 +65,12 @@ Create a `.env` file and add OpenCommit config variables there like this:
12265
OCO_OPENAI_API_KEY=<your OpenAI API token>
12366
OCO_OPENAI_MAX_TOKENS=<max response tokens from OpenAI API>
12467
OCO_OPENAI_BASE_PATH=<may be used to set proxy path to OpenAI api>
125-
OCO_DESCRIPTION=<postface a message with ~3 sentences description>
126-
OCO_EMOJI=<add GitMoji>
127-
OCO_MODEL=<either gpt-3.5-turbo or gpt-4>
68+
OCO_DESCRIPTION=<postface a message with ~3 sentences description of the changes>
69+
OCO_EMOJI=<boolean, add GitMoji>
70+
OCO_MODEL=<either 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'>
12871
OCO_LANGUAGE=<locale, scroll to the bottom to see options>
129-
OCO_MESSAGE_TEMPLATE_PLACEHOLDER=<message template placeholder, example: '$msg'>
130-
OCO_PROMPT_MODULE=<either conventional-commit or @commitlint>
72+
OCO_MESSAGE_TEMPLATE_PLACEHOLDER=<message template placeholder, default: '$msg'>
73+
OCO_PROMPT_MODULE=<either conventional-commit or @commitlint, default: conventional-commit>
13174
```
13275

13376
### Global config for all repos
@@ -233,7 +176,7 @@ This allows you to ensure that the configuration is set up as desired.
233176

234177
Additionally, the integration creates a file named `.opencommit-commitlint` which contains the prompts used for the local `@commitlint` configuration. You can modify this file to fine-tune the example commit message generated by OpenAI. This gives you the flexibility to make adjustments based on your preferences or project guidelines.
235178

236-
Additionally, OpenCommit generates a file named `.opencommit-commitlint` in your project directory which contains the prompts used for the local `@commitlint` configuration. You can modify this file to fine-tune the example commit message generated by OpenAI. If the local `@commitlint` configuration changes, this file will be updated the next time OpenCommit is run.
179+
OpenCommit generates a file named `.opencommit-commitlint` in your project directory which contains the prompts used for the local `@commitlint` configuration. You can modify this file to fine-tune the example commit message generated by OpenAI. If the local `@commitlint` configuration changes, this file will be updated the next time OpenCommit is run.
237180

238181
This offers you greater control over the generated commit messages, allowing for customization that aligns with your project's conventions.
239182

@@ -251,10 +194,10 @@ is translated to :
251194
git commit -m "${generatedMessage}" --no-verify
252195
```
253196

254-
To include a message in the generated message, you can utilize the template function! For instance:
197+
To include a message in the generated message, you can utilize the template function, for instance:
255198

256199
```sh
257-
oco '$msg #205’
200+
oco '#205: $msg
258201
```
259202
260203
> 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!"
@@ -268,12 +211,14 @@ The `OCO_MESSAGE_TEMPLATE_PLACEHOLDER` feature in the `opencommit` tool allows u
268211
#### Implementation Details
269212
270213
In our codebase, the implementation of this feature can be found in the following segment:
214+
271215
```javascript
272216
commitMessage = messageTemplate.replace(
273-
config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
274-
commitMessage
217+
config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
218+
commitMessage
275219
);
276220
```
221+
277222
This line is responsible for replacing the placeholder in the `messageTemplate` with the actual `commitMessage`.
278223
279224
#### Usage
@@ -322,6 +267,63 @@ git commit
322267
323268
Or follow the process of your IDE Source Control feature, when it calls `git commit` command — OpenCommit will integrate into the flow.
324269
270+
## Setup OpenCommit as a GitHub Action (BETA) 🔥
271+
272+
OpenCommit is now available as a GitHub Action which automatically improves all new commits messages when you push to remote!
273+
274+
This is great if you want to make sure all of the commits in all of your repository branches are meaningful and not lame like `fix1` or `done2`.
275+
276+
Create a file `.github/workflows/opencommit.yml` with the contents below:
277+
278+
```yml
279+
name: 'OpenCommit Action'
280+
281+
on:
282+
push:
283+
# this list of branches is often enough,
284+
# but you may still ignore other public branches
285+
branches-ignore: [main master dev development release]
286+
287+
jobs:
288+
opencommit:
289+
timeout-minutes: 10
290+
name: OpenCommit
291+
runs-on: ubuntu-latest
292+
permissions: write-all
293+
steps:
294+
- name: Setup Node.js Environment
295+
uses: actions/setup-node@v2
296+
with:
297+
node-version: '16'
298+
- uses: actions/checkout@v3
299+
with:
300+
fetch-depth: 0
301+
- uses: di-sukharev/opencommit@github-action-v1.0.4
302+
with:
303+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
304+
305+
env:
306+
# set openAI api key in repo actions secrets,
307+
# for openAI keys go to: https://platform.openai.com/account/api-keys
308+
# for repo secret go to: <your_repo_url>/settings/secrets/actions
309+
OCO_OPENAI_API_KEY: ${{ secrets.OCO_OPENAI_API_KEY }}
310+
311+
# customization
312+
OCO_OPENAI_MAX_TOKENS: 500
313+
OCO_OPENAI_BASE_PATH: ''
314+
OCO_DESCRIPTION: false
315+
OCO_EMOJI: false
316+
OCO_MODEL: gpt-3.5-turbo-16k
317+
OCO_LANGUAGE: en
318+
OCO_PROMPT_MODULE: conventional-commit
319+
```
320+
321+
That is it. Now when you push to any branch in your repo — all NEW commits are being improved by your never-tired AI.
322+
323+
Make sure you exclude public collaboration branches (`main`, `dev`, `etc`) in `branches-ignore`, so OpenCommit does not rebase commits there while improving the messages.
324+
325+
Interactive rebase (`rebase -i`) changes commits' SHA, so the commit history in remote becomes different from your local branch history. This is okay if you work on the branch alone, but may be inconvenient for other collaborators.
326+
325327
## Payments
326328

327329
You pay for your requests to OpenAI API. OpenCommit uses ChatGPT (3.5-turbo) official model, which is ~15x times cheaper than GPT-4.

out/cli.cjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16425,8 +16425,8 @@ var package_default = {
1642516425
start: "node ./out/cli.cjs",
1642616426
dev: "ts-node ./src/cli.ts",
1642716427
build: "rimraf out && node esbuild.config.js",
16428-
deploy: "npm run build:push && npm version patch && git push --tags && git push && npm publish --tag latest",
1642916428
"build:push": "npm run build && git add . && git commit -m 'build' && git push",
16429+
deploy: "npm run build:push && npm version patch && git push --tags && git push && npm publish --tag latest",
1643016430
lint: "eslint src --ext ts && tsc --noEmit",
1643116431
format: "prettier --write src"
1643216432
},
@@ -21548,7 +21548,7 @@ var configValidators = {
2154821548
"gpt-3.5-turbo-16k",
2154921549
"gpt-3.5-turbo-0613"
2155021550
].includes(value),
21551-
`${value} is not supported yet, use 'gpt-4', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo' (default)`
21551+
`${value} is not supported yet, use 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'`
2155221552
);
2155321553
return value;
2155421554
},
@@ -22306,12 +22306,12 @@ var checkMessageTemplate = (extraArgs2) => {
2230622306
return false;
2230722307
};
2230822308
var generateCommitMessageFromGitDiff = async (diff, extraArgs2) => {
22309-
const messageTemplate = checkMessageTemplate(extraArgs2);
2231022309
await assertGitRepo();
2231122310
const commitSpinner = le();
2231222311
commitSpinner.start("Generating the commit message");
2231322312
try {
2231422313
let commitMessage = await generateCommitMessageByDiff(diff);
22314+
const messageTemplate = checkMessageTemplate(extraArgs2);
2231522315
if (typeof messageTemplate === "string") {
2231622316
commitMessage = messageTemplate.replace(
2231722317
config7?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
@@ -22320,7 +22320,7 @@ var generateCommitMessageFromGitDiff = async (diff, extraArgs2) => {
2232022320
}
2232122321
commitSpinner.stop("\u{1F4DD} Commit message generated");
2232222322
ce(
22323-
`Commit message:
22323+
`Generated commit message:
2232422324
${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014")}
2232522325
${commitMessage}
2232622326
${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014")}`
@@ -22350,7 +22350,7 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
2235022350
});
2235122351
if (isPushConfirmedByUser && !eD2(isPushConfirmedByUser)) {
2235222352
const pushSpinner = le();
22353-
pushSpinner.start(`Running \`git push ${remotes[0]}\``);
22353+
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
2235422354
const { stdout: stdout2 } = await execa("git", [
2235522355
"push",
2235622356
"--verbose",
@@ -22372,7 +22372,7 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
2237222372
});
2237322373
if (!eD2(selectedRemote)) {
2237422374
const pushSpinner = le();
22375-
pushSpinner.start(`Running \`git push ${selectedRemote}\``);
22375+
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
2237622376
const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]);
2237722377
pushSpinner.stop(
2237822378
`${source_default.green(

out/github-action.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27971,7 +27971,7 @@ var configValidators = {
2797127971
"gpt-3.5-turbo-16k",
2797227972
"gpt-3.5-turbo-0613"
2797327973
].includes(value),
27974-
`${value} is not supported yet, use 'gpt-4', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo' (default)`
27974+
`${value} is not supported yet, use 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'`
2797527975
);
2797627976
return value;
2797727977
},

src/commands/commit.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,28 @@ const generateCommitMessageFromGitDiff = async (
4242
diff: string,
4343
extraArgs: string[]
4444
): Promise<void> => {
45-
const messageTemplate = checkMessageTemplate(extraArgs);
4645
await assertGitRepo();
47-
4846
const commitSpinner = spinner();
4947
commitSpinner.start('Generating the commit message');
48+
5049
try {
5150
let commitMessage = await generateCommitMessageByDiff(diff);
5251

53-
if (typeof messageTemplate === 'string') {
52+
const messageTemplate = checkMessageTemplate(extraArgs);
53+
if (
54+
config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER &&
55+
typeof messageTemplate === 'string'
56+
) {
5457
commitMessage = messageTemplate.replace(
5558
config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
5659
commitMessage
5760
);
5861
}
62+
5963
commitSpinner.stop('📝 Commit message generated');
6064

6165
outro(
62-
`Commit message:
66+
`Generated commit message:
6367
${chalk.grey('——————————————————')}
6468
${commitMessage}
6569
${chalk.grey('——————————————————')}`
@@ -97,7 +101,7 @@ ${chalk.grey('——————————————————')}`
97101
if (isPushConfirmedByUser && !isCancel(isPushConfirmedByUser)) {
98102
const pushSpinner = spinner();
99103

100-
pushSpinner.start(`Running \`git push ${remotes[0]}\``);
104+
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
101105

102106
const { stdout } = await execa('git', [
103107
'push',
@@ -125,7 +129,7 @@ ${chalk.grey('——————————————————')}`
125129
if (!isCancel(selectedRemote)) {
126130
const pushSpinner = spinner();
127131

128-
pushSpinner.start(`Running \`git push ${selectedRemote}\``);
132+
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
129133

130134
const { stdout } = await execa('git', ['push', selectedRemote]);
131135

src/commands/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const configValidators = {
129129
'gpt-3.5-turbo-16k',
130130
'gpt-3.5-turbo-0613'
131131
].includes(value),
132-
`${value} is not supported yet, use 'gpt-4', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo' (default)`
132+
`${value} is not supported yet, use 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'`
133133
);
134134
return value;
135135
},
@@ -192,7 +192,8 @@ export const getConfig = (): ConfigType | null => {
192192
try {
193193
const validator = configValidators[configKey as CONFIG_KEYS];
194194
const validValue = validator(
195-
config[configKey] ?? configFromEnv[configKey as CONFIG_KEYS], config
195+
config[configKey] ?? configFromEnv[configKey as CONFIG_KEYS],
196+
config
196197
);
197198

198199
config[configKey] = validValue;

0 commit comments

Comments
 (0)