Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit c16624a

Browse files
✨ feat(scope): add pre-commit hooks and VSCode settings (#4)
## 💌 Description 📝 docs(scope): update chatgpt_commit_message documentation This commit updates the `chatgpt_commit_message` documentation to reflect recent changes and provide more detailed instructions on how to use the tool. The `docs/chatgpt_commit_message.md` file has been updated with additional examples and explanations. 📝 docs(scope): update README.md This commit updates the `README.md` file to include more information about the project and how to contribute. It also adds a section on pre-commit hooks and how to set them up. 🐛 fix(scope): fix linting errors in chatgpt_commit_message.py This commit fixes linting errors in the `chatgpt_commit_message.py` file. The file now conforms to PEP8 standards and has improved readability. 🔄 chore(scope): update dependencies in pyproject.toml This commit updates the dependencies in the `pyproject.toml` file to their latest versions. ## 🩹 Related issue <!-- If your PR refers to a related issue, link it here. --> Fixes: # ## 🏗️ Type of change <!-- Mark with an `x` all the checkboxes that apply (like `[x]`) --> - [ ] 📚 Examples / docs / tutorials / dependencies update - [ ] 🐛 Bug fix (non-breaking change which fixes an issue) - [ ] 🥂 Improvement (non-breaking change which improves an existing feature) - [ ] 🚀 New feature (non-breaking change which adds functionality) - [ ] 💥 Breaking change (fix or feature that would cause existing functionality to change) - [ ] 🔐 Security fix - [ ] ⬆️ Dependencies update ## ✅ Checklist <!-- Mark with an `x` all the checkboxes that apply (like `[x]`) --> - [ ] I've read the [`CODE_OF_CONDUCT.md`](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/blob/main/CODE_OF_CONDUCT.md) document. - [ ] I've read the [`CONTRIBUTING.md`](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/blob/main/CONTRIBUTING.md) guide. - [ ] I've written tests for all new methods and classes that I created. - [ ] I've written the docstring in Google format for all the methods and classes that I used.
1 parent 87fe388 commit c16624a

12 files changed

Lines changed: 271 additions & 48 deletions

.github/workflows/workflow.ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
uses: github/codeql-action/init@v2
7373
with:
7474
languages: ${{ matrix.language }}
75-
queries: security-extended
75+
queries: security-and-quality
7676
# If you wish to specify custom queries, you can do so here or in a config file.
7777
# By default, queries listed here will override any specified in a config file.
7878
# Prefix the list here with "+" to use these queries and those in the config file.
@@ -115,6 +115,9 @@ jobs:
115115
needs:
116116
- codeql-analyze
117117
runs-on: ubuntu-latest
118+
permissions:
119+
actions: read
120+
security-events: write
118121
steps:
119122
- name: Checkout Repo
120123
uses: actions/checkout@v3

.markdownlint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"default": true,
3+
"MD029": {
4+
"style": "one"
5+
},
6+
"MD013": false
7+
}

.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
language: python
77
language_version: python3
88
additional_dependencies:
9-
["openai>=0.27.4", "tiktoken>=0.3.3", "GitPython>=3.1.31"]
9+
["openai~=0.27.4", "tiktoken~=0.3.3", "GitPython~=3.1.31"]
1010
always_run: true
1111
stages: [prepare-commit-msg]

.vscode/settings.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616
},
1717
// editor
1818
"editor.formatOnSave": true,
19-
"editor.formatOnPaste": true,
20-
"editor.formatOnType": true,
19+
"editor.codeActionsOnSave": {
20+
"source.fixAll.markdownlint": true
21+
},
2122
// markdown
23+
"[markdown]": {
24+
"editor.formatOnSave": true,
25+
"editor.formatOnPaste": true
26+
},
2227
"markdown.extension.toc.levels": "2..6",
28+
"markdown.extension.toc.unorderedList.marker": "-",
29+
"markdown.extension.toc.updateOnSave": true,
30+
"markdown.extension.orderedList.marker": "one",
31+
"markdown.extension.print.absoluteImgPath": true,
32+
"markdown.extension.toc.slugifyMode": "github",
2333
// python general
2434
"[python]": {
2535
"editor.defaultFormatter": "ms-python.black-formatter",

README.md

Lines changed: 179 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,113 @@
66
[![PyPI - downloads][pypi-stats-image]][pypi-stats-link]
77
[![GitHub - ci][github-ci-image]][github-ci-link]
88

9-
## 📥 Setup
9+
Pre-commit hooks collection that utilizes ChatGPT and OpenAI platform to validate changes made to the codebase.
10+
11+
- [🎣 Hooks](#-hooks)
12+
- [`chatgpt-commit-message`](#chatgpt-commit-message)
13+
- [📥 Prerequisites setup](#-prerequisites-setup)
14+
- [OpenAI Platform](#openai-platform)
15+
- [Azure OpenAI Service](#azure-openai-service)
16+
- [Setting environment variables](#setting-environment-variables)
17+
- [pre-commit setup](#pre-commit-setup)
18+
- [📦 Hooks setup](#-hooks-setup)
19+
- [Remote repo reference (preferred)](#remote-repo-reference-preferred)
20+
- [Local repo reference](#local-repo-reference)
21+
- [🛠️ Advanced configuration](#️-advanced-configuration)
22+
- [Extra environment variables](#extra-environment-variables)
23+
- [Arguments](#arguments)
24+
- [`--env-prefix`](#--env-prefix)
25+
- [Variables precedence](#variables-precedence)
26+
- [👥 Contributing](#-contributing)
27+
- [📄 License](#-license)
28+
29+
## 🎣 Hooks
30+
31+
### `chatgpt-commit-message`
32+
33+
Hook that uses OpenAI's ChatGPT API to generate a summary of changes made to a codebase and use it to populate the commit message automatically.
34+
35+
- Read about hook's specific [configuration](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/blob/main/docs/chatgpt_commit_message.md).
36+
37+
## 📥 Prerequisites setup
38+
39+
Hooks support [OpenAI Platform](https://platform.openai.com) and [Azure OpenAI Service](https://azure.microsoft.com/products/cognitive-services/openai-service).
40+
41+
### OpenAI Platform
42+
43+
OpenAI API Key is mandatory to run hooks and has to be setup via an environment variable.
44+
45+
1. Create your [API Key](https://platform.openai.com/account/api-keys), and get your Organization ID from [Organization settings](https://platform.openai.com/account/org-settings)
46+
47+
![OpenAI API Key](https://github.com/dariuszporowski/chatgpt-pre-commit-hooks/blob/main/assets/images/openai-platform-api-key.png)
48+
49+
![OpenAI Organization ID](https://github.com/dariuszporowski/chatgpt-pre-commit-hooks/blob/main/assets/images/openai-platform-org-id.png)
50+
51+
1. Store values as an environment variables:
52+
- `OPENAI_API_KEY` for API Key
53+
- `OPENAI_ORGANIZATION` for Organization ID
54+
55+
Example:
56+
57+
```shell
58+
export OPENAI_API_KEY="sk-xxxxxx"
59+
export OPENAI_ORGANIZATION="org-xxxxxx"
60+
```
61+
62+
> 💡 **HINT**
63+
>
64+
> How to setup env vars? see: [Setting environment variables](#setting-environment-variables)
65+
66+
### Azure OpenAI Service
67+
68+
1. Go to [Azure Portal](https://portal.azure.com), and get `API Key`, `Endpoint` and `Model deployment name`
69+
70+
![Azure OpenAI API Key and Endpoint](https://github.com/dariuszporowski/chatgpt-pre-commit-hooks/blob/main/assets/images/azure-openai-service-key-endpoint.png)
71+
72+
![Azure OpenAI Model](https://github.com/dariuszporowski/chatgpt-pre-commit-hooks/blob/main/assets/images/azure-openai-service-models.png)
73+
74+
1. Store values as an environment variables:
75+
- `OPENAI_API_TYPE` put `azure` to specified OpenAI provider
76+
- `OPENAI_API_KEY` for API Key
77+
- `OPENAI_API_BASE` for Endpoint
78+
- `OPENAI_MODEL_NAME` for Model deployment name
79+
80+
Example:
81+
82+
```shell
83+
export OPENAI_API_TYPE="azure"
84+
export OPENAI_API_KEY="sk-xxxxxx"
85+
export OPENAI_API_BASE="https://xxxxxx.openai.azure.com/"
86+
export OPENAI_MODEL_NAME="xxxxx-gpt-35-turbo"
87+
```
88+
89+
> 💡 **HINT**
90+
>
91+
> How to setup env vars? see: [Setting environment variables](#setting-environment-variables)
92+
93+
### Setting environment variables
94+
95+
Linux/MacOS example:
96+
97+
```shell
98+
export OPENAI_API_KEY="sk-xxxxxx"
99+
```
100+
101+
Windows `powershell` example:
102+
103+
```powershell
104+
$env:OPENAI_API_KEY="sk-xxxxxx"
105+
```
106+
107+
Windows `cmd` example:
108+
109+
```console
110+
set OPENAI_API_KEY=sk-xxxxxx
111+
```
112+
113+
> ⚠️ **NOTE**
114+
>
115+
> The above example stores the environment variable temporarily for the current session. To store it permanently, please follow [Best Practices for API Key Safety](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety)
10116

11117
### pre-commit setup
12118

@@ -26,9 +132,9 @@ pre-commit install
26132
pre-commit autoupdate
27133
```
28134

29-
### Hooks setup
135+
## 📦 Hooks setup
30136

31-
#### Remote repo reference
137+
### Remote repo reference (preferred)
32138

33139
Add to your `.pre-commit-config.yaml`
34140

@@ -45,7 +151,7 @@ Example:
45151
```yaml
46152
repos:
47153
- repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
48-
rev: v0.1.0
154+
rev: v0.1.1
49155
hooks:
50156
- id: chatgpt-commit-message
51157
```
@@ -54,20 +160,26 @@ repos:
54160
>
55161
> For the `rev:` always try to use the latest version. You can check the latest release under [GitHub Releases](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/releases/latest)
56162

57-
#### Local repo reference
163+
### Local repo reference
58164

59165
1. Install or add [PyPI](https://pypi.org/project/chatgpt-pre-commit-hooks) package to your project.
60166

61167
- if you are using [pip](https://pip.pypa.io):
62168

63169
```shell
64-
pip install chatgpt-pre-commit-hooks
170+
pip install --upgrade chatgpt-pre-commit-hooks
65171
```
66172

67173
- or include it in a `requirements.txt` file in your project:
68174

69175
```text
70-
chatgpt-pre-commit-hooks
176+
chatgpt-pre-commit-hooks~=0.1.0
177+
```
178+
179+
and run:
180+
181+
```shell
182+
pip install -r requirements.txt
71183
```
72184

73185
- or, even better, in the dev section of your `pyproject.toml` file:
@@ -77,13 +189,19 @@ repos:
77189
dev = ["chatgpt-pre-commit-hooks"]
78190
```
79191

192+
and run:
193+
194+
```shell
195+
pip install .[dev]
196+
```
197+
80198
- or, if you are using [poetry](https://python-poetry.org) as a package manager:
81199

82200
```shell
83201
poetry add chatgpt-pre-commit-hooks --group dev
84202
```
85203

86-
2. Add to your `.pre-commit-config.yaml`
204+
1. Add to your `.pre-commit-config.yaml`
87205

88206
```yml
89207
repos:
@@ -107,22 +225,66 @@ repos:
107225
language: system
108226
```
109227

110-
### OpenAI API Key
228+
## 🛠️ Advanced configuration
111229

112-
Hooks require OpenAI access.
230+
### Extra environment variables
113231

114-
1. Create your [OpenAI API Key](https://platform.openai.com/account/api-keys)
115-
1. Store API key as an environment variable (see: [Best Practices for API Key Safety](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety))
232+
In addition to the environment variables listed in the [📥 Prerequisites setup](#-prerequisites-setup) section, you can set several configurations using extra environment variables.
116233

117-
## 🎣 Hooks
234+
| Name | Type | Default | Description |
235+
|:--------------------|:------:|:---------------:|:---------------------------------------------------------------------------------------------------------------------------------------------|
236+
| `OPENAI_MAX_TOKENS` | int | 1024 | [What are tokens and how to count them?](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them) |
237+
| `OPENAI_MODEL` | string | `gpt-3.5-turbo` | [Model endpoint compatibility](https://platform.openai.com/docs/models/model-endpoint-compatibility) - check `/v1/chat/completions` endpoint |
238+
| `OPENAI_PROXY` | string | _not set_ | http/https client proxy |
118239

119-
### `chatgpt-commit-message`
240+
### Arguments
120241

121-
Hook that uses OpenAI's ChatGPT API to generate a summary of changes made to a codebase and use it to populate the commit message automatically.
242+
Any environment variable can be overridden by hard-coded args in `pre-commit-config.yaml`, except `OPENAI_API_KEY`, `OPENAI_ORGANIZATION`.
243+
244+
| Name | Type | Default | Description |
245+
|:----------------------|:------:|:---------:|:------------------------------------------------------------------------------------------------------------------|
246+
| `--env-prefix` | string | _not set_ | Set prefix for environment variables allowing multiple configurations. Read more: [`--env-prefix`](#--env-prefix) |
247+
| `--openai-max-tokens` | int | _not set_ | Overrides `OPENAI_MAX_TOKENS` |
248+
| `--openai-proxy` | string | _not set_ | Overrides `OPENAI_PROXY` |
249+
| `--openai-model` | string | _not set_ | Overrides `OPENAI_MODEL` |
250+
| `--openai-api-base` | string | _not set_ | Overrides `OPENAI_API_BASE` |
251+
| `--openai-api-type` | string | _not set_ | Overrides `OPENAI_API_TYPE` |
252+
253+
Example:
254+
255+
```yaml
256+
repos:
257+
- repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
258+
rev: vX.Y.Z
259+
hooks:
260+
- id: ... # follow 🎣 Hooks section to see available hooks IDs
261+
args:
262+
- "--env-prefix"
263+
- "personal"
264+
- "--openai-max-tokens"
265+
- "512"
266+
- ...
267+
```
268+
269+
### `--env-prefix`
270+
271+
It's a special arg where you can mark prefixes for your environment variables. This allows you to set many configurations depending on the project, account, profile, etc., for example, `personal`, `work`. Fallback is a global environment variable if prefixed is not found.
272+
273+
For instance, if your prefix is `personal`, then the environment variable must be set `PERSONAL__OPENAI_MAX_TOKENS`, meaning the structure is `<prefix>__<base_env_name>` - two underscores `__` between `prefix` and `base_env_name`.
274+
275+
Example:
276+
277+
### Variables precedence
278+
279+
1. hard-coded args, e.g. `--openai-max-tokens`
280+
1. prefixed environment variable, e.g. `PERSONAL__OPENAI_MAX_TOKENS`
281+
1. global environment variable, e.g. `OPENAI_MAX_TOKENS`
282+
283+
## 👥 Contributing
122284
123-
- Read [setup and usage](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/blob/main/docs/chatgpt_commit_message.md) for this hook.
285+
Contributions to the project are very welcome! Please follow [Contributing Guide](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/blob/main/CONTRIBUTING.md).
124286
125-
## License
287+
## 📄 License
126288
127289
This project is distributed under the terms of the [MIT](https://opensource.org/licenses/MIT) license.
128290
78.5 KB
Loading
154 KB
Loading
158 KB
Loading
110 KB
Loading

0 commit comments

Comments
 (0)