Skip to content

Commit 5615099

Browse files
authored
🚀 UPDATE: v0.1.4
Merge pull request #8 from warengonzaga/dev
2 parents 6f6bfc1 + f04e6bb commit 5615099

File tree

9 files changed

+764
-729
lines changed

9 files changed

+764
-729
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict-true

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Magic Commit 🪄 [![author/maintainer](https://img.shields.io/badge/by-Waren%20Gonzaga-016eea.svg?logo=github&labelColor=181717&longCache=true&style=flat-square)](https://warengonzaga.com) [![nominate](https://img.shields.io/badge/nominate-%20@warengonzaga%20as%20GitHub%20Star-yellow.svg?logo=github&labelColor=181717&longCache=true&style=flat-square)](https://stars.github.com/nominate)
22

3-
[![made in](https://img.shields.io/badge/made%20in-Open%20Source%20Software%20PH-0060a0.svg?logo=github&longCache=true&labelColor=181717&style=flat-square)](https://github.com/ossphilippines) [![sponsors](https://img.shields.io/badge/sponsor-%E2%9D%A4-%23db61a2.svg?&logo=github&logoColor=white&labelColor=181717&style=flat-square)](https://github.com/sponsors/warengonzaga) [![release](https://img.shields.io/github/release/warengonzaga/magic-commit.svg?logo=github&labelColor=181717&color=green&style=flat-square)](https://github.com/warengonzaga/magic-commit/releases) [![star](https://img.shields.io/github/stars/warengonzaga/magic-commit.svg?&logo=github&labelColor=181717&color=yellow&style=flat-square)](https://github.com/warengonzaga/magic-commit/stargazers) [![license](https://img.shields.io/github/license/warengonzaga/magic-commit.svg?&logo=github&labelColor=181717&style=flat-square)](https://github.com/warengonzaga/magic-commit/blob/main/license) [![NPM Installs](https://img.shields.io/npm/dt/magicc?color=CB3837&logo=npm&label=installs&labelColor=181717&style=flat-square)](https://npmjs.com/package/magicc)
3+
[![made with](https://img.shields.io/badge/made%20with-Open%20Source%20Software%20PH-0060a0.svg?logo=github&longCache=true&labelColor=181717&style=flat-square)](https://github.com/ossphilippines) [![sponsors](https://img.shields.io/badge/sponsor-%E2%9D%A4-%23db61a2.svg?&logo=github&logoColor=white&labelColor=181717&style=flat-square)](https://github.com/sponsors/warengonzaga) [![release](https://img.shields.io/github/release/warengonzaga/magic-commit.svg?logo=github&labelColor=181717&color=green&style=flat-square)](https://github.com/warengonzaga/magic-commit/releases) [![star](https://img.shields.io/github/stars/warengonzaga/magic-commit.svg?&logo=github&labelColor=181717&color=yellow&style=flat-square)](https://github.com/warengonzaga/magic-commit/stargazers) [![license](https://img.shields.io/github/license/warengonzaga/magic-commit.svg?&logo=github&labelColor=181717&style=flat-square)](https://github.com/warengonzaga/magic-commit/blob/main/license) [![NPM Installs](https://img.shields.io/npm/dt/magicc?color=CB3837&logo=npm&label=installs&labelColor=181717&style=flat-square)](https://npmjs.com/package/magicc)
44

55
[![banner](https://raw.githubusercontent.com/warengonzaga/magic-commit/main/.github/assets/repo_banner.jpg)](https://github.com/warengonzaga/magic-commit)
66

77
You can do `magicc`, you can build anything that you desire. 🪄🔮💻
88

9-
**Magic Commit** is a simple and easy-to-use tool that helps you to automatically stage your changes by file and commit them one by one. All you need to do is type `magicc` and accept if you love the suggested commit message in every changed files.
9+
**Magic Commit** is a simple and easy-to-use tool that helps you to automatically stage your changes by file and commit them one by one. All you need to do is type `magicc` and accept if you love the suggested commit message in every changed files. It uses `gpt-4o-mini` as the default model from OpenAI to generate commit messages. 🪄
1010

1111
Have suggestions in mind? [Let me know!](https://github.com/warengonzaga/magic-commit/issues)
1212

contributing.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ There are many ways to contribute to this open source project. Any contributions
1414

1515
If you can write a code then create a pull request to this repo and I will review your code. Please consider to submit your pull request to the ```dev``` branch. I will auto reject if you submit your pull request to the ```main``` branch.
1616

17+
#### 🔧 Setup
18+
19+
**To install the CLI app** in your working directory, you can run the following command:
20+
21+
```bash
22+
yarn setup
23+
```
24+
25+
OR
26+
27+
```bash
28+
npm run setup
29+
```
30+
31+
Run the command everytime you make changes to the code. This will build the project and install the CLI app in your working directory. So you can test it out using `magicc` command.
32+
33+
> [!IMPORTANT]
34+
> When using the setup command please ensure you are using linux-based OS. This command will use commands tht are only available in linux-based OS. Such as `rm -rf` to delete the `dist` folder.
35+
1736
### 📖 Documentation
1837

1938
(coming soon)
@@ -24,4 +43,4 @@ For any security bugs or issues, please read the [security policy](./security.md
2443

2544
---
2645

27-
💻 with ❤️ by [Waren Gonzaga](https://warengonzaga.com) and [Him](https://www.youtube.com/watch?v=HHrxS4diLew&t=44s) 🙏
46+
💻 with ❤️ by [Waren Gonzaga](https://warengonzaga.com) and [Him](https://www.youtube.com/watch?v=HHrxS4diLew&t=44s) 🙏

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "magicc",
3-
"version": "0.1.3",
3+
"version": "0.2.0",
44
"author": "Waren Gonzaga",
55
"description": "You can do `magicc`, you can build anything that you desire.",
66
"keywords": [
@@ -22,7 +22,9 @@
2222
"build": "babel --out-dir=dist source",
2323
"dev": "babel --out-dir=dist --watch source",
2424
"clean": "rm -rf dist",
25+
"setup": "npm run setup:ubuntu",
2526
"setup:ubuntu": "npm run clean && npm run build && npm i -g .",
27+
"reset": "npm run reset:ubuntu",
2628
"reset:ubuntu": "npm uninstall -g magicc && npm run setup:ubuntu",
2729
"test": "prettier --check . && xo && ava"
2830
},

source/utils/api.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,38 @@ import Conf from 'conf';
22

33
const config = new Conf({projectName: 'magicc'});
44

5-
const setOpenAIKey = (key) => {
6-
if (!key.startsWith('sk-') || key.length !== 51) {
7-
console.error('Invalid OpenAI API key. Please provide a valid key.');
8-
} else {
9-
config.set('openai', key);
5+
async function isValidOpenAIKey(apiKey) {
6+
try {
7+
const response = await fetch('https://api.openai.com/v1/models', {
8+
headers: {
9+
'Authorization': `Bearer ${apiKey}`
10+
}
11+
});
12+
13+
if (response.status === 200) {
14+
return true;
15+
} else if (response.status === 401) {
16+
console.error('Invalid API key');
17+
return false;
18+
} else {
19+
console.error('Unexpected response status:', response.status);
20+
return false;
21+
}
22+
} catch (error) {
23+
console.error('Error while validating API key:', error);
24+
return false;
1025
}
26+
}
27+
28+
const setOpenAIKey = (key) => {
29+
isValidOpenAIKey(key).then(isValid => {
30+
if (isValid) {
31+
console.log('API key is valid');
32+
config.set('openai', key);
33+
} else {
34+
console.log('API key is invalid');
35+
}
36+
});
1137
};
1238

1339
const getOpenAIKey = () => {

source/utils/commit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async function askForCommitMessage() {
1111
});
1212

1313
if (prompt) {
14-
rl.question(`Suggested commit message: ${prompt}\nDo you want to proceed? (y/n) `, (answer) => {
14+
rl.question(`Suggested commit message: ${prompt}\nDo you want to proceed? (y/N) `, (answer) => {
1515
if (answer.toLowerCase() === "y") {
1616
execa("git", ["commit", "-m", prompt])
1717
.then(() => {

source/utils/config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2-
"emoji": "You are the author of the commit message, your task is to select which category will be used for the current git diff based on changes. Use the following category (prefix name, usage);📦 new, for new files or new features;✨ tweak, for enhancements or updates to the codebase;☕ chore, for updates or changes outside the project code base including README.md;🐞 fix, changes for fixing code bugs and errors. Please reply with the prefix and name of the category you want. No other comments are needed.",
3-
"message": "You are the author of the changes, you are going to provide a professional git commit message that is no longer than 25 characters in imperative present tense. Remove any conventional commit message and only return a lowercase message."
2+
"emoji": "YYou are the author of the commit message. Your task is to select the appropriate category for the git diff based on the changes. Use the following categories (emoji category name => usage): 📦 new => for new files or new features; ✨ tweak => for enhancements or updates to the codebase; ☕ chore => for updates or changes outside the project codebase, including README.md; 🐞 fix => for fixing code bugs and errors. Please reply with the category name only.",
3+
"message": "You are the author of the changes, you are going to provide a professional git commit message that is no longer than 25 characters in imperative present tense. Stricly no emojis are allowed and no conventional commit message as prefix is already provided. For example, instead of 'fix: fix a bug' make it 'fix a bug'. The message should be in lower case and no period at the end.",
4+
"default_model": "gpt-4o-mini",
5+
"maxDiffSize": 4000
46
}

source/utils/openai.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ async function initGit() {
1717

1818
// git status to see if there are any changes
1919
// if there's any changes add the first file in the list of changes
20+
let firstFilePath = '';
21+
2022
async function gitStatus() {
2123
try {
2224
const { stdout: status } = await execa('git', ['status', '--porcelain']);
@@ -31,8 +33,9 @@ async function gitStatus() {
3133
.map(line => line.split(' ').slice(1).join(' ').trim())
3234
);
3335
// git add the first file in the list of changes
34-
await execa('git', ['add', filePaths[0]]);
35-
console.log(`${filePaths[0]} has been added to the staging area.`);
36+
firstFilePath = filePaths[0];
37+
await execa('git', ['add', firstFilePath]);
38+
console.log(`${firstFilePath} has been added to the staging area.`);
3639
} else {
3740
console.log('No changes to commit.');
3841
return false;
@@ -55,26 +58,33 @@ async function gitDiff() {
5558
async function generatePrompt() {
5659
const apiKey = await getOpenAIKey();
5760
const openai = new OpenAI({apiKey: apiKey});
61+
const maxDiffSize = config.maxDiffSize;
62+
5863
// get the staged changes
5964
await initGit();
6065
await gitStatus();
6166
const gitDiffContent = await gitDiff();
6267

68+
if (gitDiffContent.length > maxDiffSize) {
69+
console.log('Diff content is too large. Skipping OpenAI request.');
70+
return `✨ tweak: update ${firstFilePath}`;
71+
}
72+
6373
// use the prompt from the config file emoji and send to openai
6474
const category = await openai.chat.completions.create({
6575
messages: [
6676
{ role: "system", content: config.emoji },
6777
{ role: "user", content: gitDiffContent },
6878
],
69-
model: "gpt-3.5-turbo",
79+
model: config.default_model,
7080
});
7181
// use the prmopt from the config file message and send to openai
7282
const message = await openai.chat.completions.create({
7383
messages: [
7484
{ role: "system", content: config.message },
7585
{ role: "user", content: gitDiffContent },
7686
],
77-
model: "gpt-3.5-turbo",
87+
model: config.default_model,
7888
});
7989

8090
if (await gitStatus() !== false) {

0 commit comments

Comments
 (0)