🤖

JetBrains AI Assistantのコミットメッセージ生成時にConventional Commitsを生成するプロンプト

に公開

JetBrains AI AssistantにはGitのコミット対象のDiffからコミットメッセージを生成する機能が実装されています。

標準のまま生成するとただメッセージが生成されますが、コミットメッセージ専用カスタムプロンプト(Commit Message Generation)を使ってConventional Commitsに準拠したメッセージを生成させることが可能です。

Conventional Commits

Conventional Commitsとは、コミットメッセージに構造化された形式を持たせる規約です。これにより変更履歴が読みやすくなります。詳細は公式サイトを参照してください。
https://www.conventionalcommits.org/ja/v1.0.0/

カスタムプロンプトの設定方法

IntelliJ 2024.1からはこの生成するコミットメッセージに対して、設定からプロンプト設定が可能になりました。

https://blog.jetbrains.com/blog/2024/04/04/jetbrains-ai-assistant-2024-1-updates/#commit-message-generation-custom-prompts

IntelliJ上の設定画面(Settings | Tools | AI Assistant | Prompt Library)
IntelliJ settings Commit Message Generation

プロンプト例

下記のようなプロンプトにすると、内容に合わせたプレフィックスを付けてくれます。

# Conventional Commits
Avoid overly verbose descriptions or unnecessary details.
Start with a short sentence in imperative form, no more than 50 characters long.
Please write your commitments in Japanese.

Add a prefix based on the following Type to the short sentence and separate them with a colon.
ex.) feat: add a function

Type
Must be one of the following:

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs: Documentation only changes
feat: A new feature
fix: A bug fix
perf: A code change that improves performance
refactor: A code change that neither fixes a bug nor adds a feature
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
test: Adding missing tests or correcting existing tests
  • 上段3行:基本的なコミットメッセージ生成への指示
  • 日本語指定:Please write your commitments in Japanese.を追加することで日本語で生成したいときに追加する
  • 4行目以下がコミットメッセージ規約に関する追加したプロンプト
  • TypeについてはAngularの規約をそのまま採用

https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#type

featやstyle、docsあたりはタイプに合わせたコミット単位であれば、けっこうな精度でこちらが考えているタイプを付けてくれました。

実際の使用例

ちなみにこの記事自体のコミットメッセージを生成すると

docs: JetBrains AI Assistantのコミットメッセージ生成についての新たな記事を追加

と出ました。

Markdownファイルに文章を書いているのでdocsというプレフィックスも適切です。

以上、JetBrains AI Assistantを使ってのコミットメッセージ生成についてのご紹介でした。

Discussion