From 788e9a49a1d60b1f0049d9bccc19ff276f601ff8 Mon Sep 17 00:00:00 2001 From: John Backes Date: Mon, 17 Apr 2023 20:55:33 -0700 Subject: [PATCH 1/3] Update openai_api_key retrieval methods - Update config file to look for "OPENAI_API_KEY" instead of "openai_api_key" - Remove unnecessary error handling for .env file - Strip quotes from API key retrieved from config file The update to the config file is more consistent with the naming conventions of environment variables, and the removal of error handling for the .env file simplifies the code. Stripping the quotes from the retrieved API key ensures consistency and compatibility with other API key retrieval methods. Testing Done: - Manual testing done to ensure that API key is retrieved correctly with both config file and .env file methods. --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index bd01929..70ad7dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -230,16 +230,19 @@ fn load_api_key() -> Result> { let config_path = dirs::home_dir().unwrap().join(".happycommit/config.toml"); let config = std::fs::read_to_string(config_path)?; let config: toml::Value = toml::from_str(&config)?; - let openai_api_key = config.get("openai_api_key"); + let openai_api_key = config.get("OPENAI_API_KEY"); if openai_api_key.is_none() { return Err("OPENAI_API_KEY not set in ~/.happycommit/config.toml".into()); } - Ok(openai_api_key.unwrap().to_string()) + let result = openai_api_key.unwrap().to_string(); + // strip quotes + let result = result.replace("\"", ""); + Ok(result) }; let dotenv_checker = || -> Result> { dotenv::dotenv().ok(); let openai_api_key = - dotenv::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set in .env file"); + dotenv::var("OPENAI_API_KEY")?; Ok(openai_api_key) }; From c5992094076a3130a131bc0932e3c94e7f921ea2 Mon Sep 17 00:00:00 2001 From: John Backes Date: Mon, 17 Apr 2023 21:02:31 -0700 Subject: [PATCH 2/3] Add script to add OpenAI API key to happycommit config file This commit adds a script, add-openai-api-key.sh, that allows users to add their OpenAI API key to happycommit's config file. The script checks if the API key already exists in the config file and prompts the user to update it if necessary. Once the key is added, users can run happycommit's git commit-gpt command to generate commit messages using GPT-3. Instructions for use: - Run the add-openai-api-key.sh script (found in jackbackes/happycommit on Github) to add your OpenAI API key to happycommit's config file. - Once the API key is added, run the git commit-gpt command to generate commit messages using GPT-3. Remember to proofread your commit message before submitting! --- add-openai-api-key.sh | 32 ++++++++++++++++++++++++++++++++ install-commit-gpt.sh | 34 ++++------------------------------ 2 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 add-openai-api-key.sh diff --git a/add-openai-api-key.sh b/add-openai-api-key.sh new file mode 100644 index 0000000..9632da3 --- /dev/null +++ b/add-openai-api-key.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +OPENAI_API_KEY="" +# Check if the OPENAI_API_KEY is already in the config file +if ! grep -qF "OPENAI_API_KEY" "$HOME/.happycommit/config.toml"; then + # Read the OPENAI_API_KEY from the user + printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> " + read -rs OPENAI_API_KEY + printf "\n" + # Add the OPENAI_API_KEY to the config.toml file + printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml" +else + printf "OPENAI_API_KEY already exists in $HOME/.happycommit/config.toml\n" + printf "Would you like to update it? (y/n) " + read -r update + if [ "$update" = "y" ]; then + # TODO: Remove code duplication + printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> " + read -rs OPENAI_API_KEY + printf "\n" + # Remove the previous OPENAI_API_KEY from the config file + if [ "$machine" = "Mac" ]; then + sed -i '' '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml" + else + sed -i '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml" + fi + # Add the OPENAI_API_KEY to the config file + printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml" + fi +fi + +printf "API Key added successfully. Happy committing!\n" \ No newline at end of file diff --git a/install-commit-gpt.sh b/install-commit-gpt.sh index 47163ac..416f7a7 100755 --- a/install-commit-gpt.sh +++ b/install-commit-gpt.sh @@ -69,34 +69,8 @@ if [ ! -f "$HOME/.happycommit/config.toml" ]; then touch "$HOME/.happycommit/config.toml" fi -OPENAI_API_KEY="" -# Check if the OPENAI_API_KEY is already in the config file -if ! grep -qF "OPENAI_API_KEY" "$HOME/.happycommit/config.toml"; then - # Read the OPENAI_API_KEY from the user - printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> " - read -rs OPENAI_API_KEY - printf "\n" - # Add the OPENAI_API_KEY to the config.toml file - printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml" -else - printf "OPENAI_API_KEY already exists in $HOME/.happycommit/config.toml\n" - printf "Would you like to update it? (y/n) " - read -r update - if [ "$update" = "y" ]; then - # TODO: Remove code duplication - printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> " - read -rs OPENAI_API_KEY - printf "\n" - # Remove the previous OPENAI_API_KEY from the config file - if [ "$machine" = "Mac" ]; then - sed -i '' '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml" - else - sed -i '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml" - fi - # Add the OPENAI_API_KEY to the config file - printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml" - fi -fi - # Done! -printf "git commit-gpt installed successfully. You can now use 'git commit-gpt' to run happycommit.\n" +printf "git commit-gpt installed successfully. +printf "You should now run 'add-openai-api-key' to add your OpenAI API key to happycommit's config file.\n" +printf "This script can be found in jackbackes/happycommit on github.\n" +You can now use 'git commit-gpt' to run happycommit.\n" From 6bf07b6bed913e4843948c9aca32ef5e7c093c00 Mon Sep 17 00:00:00 2001 From: John Backes Date: Mon, 17 Apr 2023 21:41:09 -0700 Subject: [PATCH 3/3] Add installation instructions for HappyCommit tool This commit adds installation instructions for HappyCommit tool, powered by OpenAI's GPT-3.5 Turbo language model. The instructions include prerequisites, getting started on macOS system, and building from source. Users can now easily install HappyCommit using Homebrew and configure the OpenAI API key to use it. The usage section provides a brief guide on how to use HappyCommit instead of using the git commit command. A link to the GitHub repository for HappyCommit is provided in the contributing section for users to submit contributions, feature requests, or bug reports. By using HappyCommit, users can focus on writing great code while HappyCommit crafts the perfect commit messages, which makes for an enjoyable Git experience. HappyCommit is licensed under the Apache License 2.0. --- README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..08b55aa --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# Welcome to HappyCommit! + +HappyCommit is a delightful tool that harnesses the power of OpenAI's GPT-3.5 Turbo language model to generate meaningful and descriptive Git commit messages for you. With HappyCommit, you can now focus on writing great code while we take care of crafting the perfect commit messages. + +## Prerequisites + +- Make sure you have [Homebrew](https://brew.sh/) installed on your macOS system. + +## Getting Started (macOS) + +1. To begin your journey with HappyCommit, install it by running the following commands: + + ```bash + brew tap jackbackes/git-commit-gpt + brew install jackbackes/homebrew-git-commit-gpt/git-commit-gpt.rb + ``` + +2. After the installation, let's add your OpenAI API key to the configuration file. Simply run the following command and enter your API key when prompted: + + ```bash + printf "Enter your OPENAI_API_KEY: " && read -rs api_key && mkdir ~/.happycommit >> /dev/null && echo "OPENAI_API_KEY = \\\"$api_key\\\"" >> ~/.happycommit/config.toml + ``` + + Don't have an OpenAI API key yet? No worries! Grab one from your [OpenAI account](https://beta.openai.com/account/api-keys). + +3. That's it! HappyCommit is now ready to make your Git experience more enjoyable. Just use the `git commit-gpt` command to generate and add a commit message based on your code changes. + +### Getting Started (Linux) + +...Coming soon! + +### Getting Started (Windows) + +...Coming soon! + +### Getting Started (Build from source) + +1. Install Rust and Cargo by following the instructions [here](https://www.rust-lang.org/tools/install). +2. Install the "Just" task runner by following the instructions [here](https://github.com/casey/just) or just run `cargo install just`. + +```bash +git clone git@github.com:jackbackes/happycommit.git +cd happycommit +just install +./add-openai-api-key.sh +``` + +## Usage + +Instead of running the standard `git commit` command, use this friendly alternative: + +```bash +git commit-gpt +``` + +HappyCommit will analyze the changes in your code and generate a meaningful commit message that brings a smile to your face. + +## Contributing + +We'd love for you to join us in making HappyCommit even better! If you have suggestions, feature requests, or bug reports, please feel free to open an issue or submit a pull request on our GitHub repository. We're excited to see your contributions! + +## License + +HappyCommit is warmly licensed under the Apache License 2.0.