Alternative Architecture DOJO

オルターブースのクラウドネイティブ特化型ブログです。

Visual StudioのGitHub Copilot Chatを使ってコミットメッセージを生成する

こんにちは、MLBお兄さんこと松村です。
この記事は Microsoft Top Partner Engineer'sAdvent Calendar 2023 の14日目の記事です。

japan.zdnet.com


今月(2023年12月現在)に一般提供が開始となった GitHub Copilot Chat を、皆さんはもう使い始めているでしょうか?

github.blog

GitHub Copilot Chat とは、一部の IDE にてチャットウィンドウやエディター内で GitHub Copilot を利用することができる機能です。
現在サポートしている IDE は以下となります。

  • Visual Studio
  • Visual Studio Code
  • JetBrains IDE

docs.github.com

今回の記事は Visual Studio の GitHub Copilot Chat にて、Git のコミットメッセージを作成する際に GitHub Copilot Chat の支援を受けることができる内容を紹介します。

devblogs.microsoft.com

前提

Visual Studio で GitHub Copilot Chat を使用するには、以下の準備を行います。
なお、以下の手順は GitHub Copilot Business の場合となります。
GitHub Copilot Individual はライセンスを持っていないため検証していません。

  1. Visual Studio 2022 17.9.0 Preview をインストールする
    • https://aka.ms/vs2022preview
    • GitHub Copilot Chat を使うだけであればプレビュー版である必要はありません。コミットメッセージ作成に Copilot の支援を受ける場合に、プレビュー版が必要となります。
  2. GitHub のエンタープライズアカウントまたは組織のポリシーで GitHub Copilot Chat を有効にする
  3. Visual Studio に GitHub Copilot Chat 関連の拡張機能をインストールする
  4. Visual Studio を起動する
  5. Git の変更ウィンドウを表示する
    • [表示]>[Git 変更]
  6. プレビュー機能を有効にする
    • [ツール]>[オプション]>[GitHub]>[Copilot チャット]>[コミットメッセージの提案を有効にする]

コミットメッセージを AI で生成する

それでは実際に Git のコミットメッセージを GitHub Copilot Chat で生成してみましょう。
といってもコマンドで行うのではなく、Visual Studio の「Git 変更ウィンドウ」で操作をします。

今回は例として、以下のサンプルに変更を加えてみて試します。

github.com

変更箇所

コードは変更すること無く、数ヶ所にコメントを追加します。差分は以下の通りです。

$ git diff Program.cs
warning: in the working copy of 'Program.cs', CRLF will be replaced by LF the next time Git touches it
diff --git a/Program.cs b/Program.cs
index c643b8d..aadfef7 100644
--- a/Program.cs
+++ b/Program.cs
@@ -11,11 +11,20 @@ namespace AzureSamples.AzureSQL
 {
     public class Program
     {
+        /// <summary>
+        /// Entry point for the application
+        /// </summary>
+        /// <param name="args"></param>
         public static void Main(string[] args)
         {
             CreateHostBuilder(args).Build().Run();
         }

+        /// <summary>
+        /// Create IHostBuilder
+        /// </summary>
+        /// <param name="args">Arguments of application</param>
+        /// <returns><see cref="IHostBuilder"/></returns>
         public static IHostBuilder CreateHostBuilder(string[] args) =>
             Host.CreateDefaultBuilder(args)
                 .ConfigureWebHostDefaults(webBuilder =>
$ git diff Startup.cs
warning: in the working copy of 'Startup.cs', CRLF will be replaced by LF the next time Git touches it
diff --git a/Startup.cs b/Startup.cs
index bf54aa5..b4d7157 100644
--- a/Startup.cs
+++ b/Startup.cs
@@ -15,11 +15,18 @@ namespace AzureSamples.AzureSQL
 {
     public class Startup
     {
+        /// <summary>
+        /// コンストラクタ
+        /// </summary>
+        /// <param name="configuration"></param>
         public Startup(IConfiguration configuration)
         {
             Configuration = configuration;
         }

+        /// <summary>
+        /// Configuration
+        /// </summary>
         public IConfiguration Configuration { get; }

         // This method gets called by the runtime. Use this method to add services to the container.

コミットメッセージを生成する

Visual Studio の Git 変更ウィンドウにて、上記のファイルをステージにセットします。
そのうえでコミットメッセージ欄の下にあるペンのアイコンを押します。
このボタンを押すことで GitHub Copilot Chat でのコミットメッセージ生成が始まります。

すると、以下のコミットメッセージが生成されました。

The most significant changes involve the addition of summary comments to the methods and properties in the `Program.cs` and `Startup.cs` files. These comments enhance the readability and maintainability of the code by providing a brief explanation of the purpose of the methods and properties.

List of changes:

1. In `Program.cs`, summary comments were added to the `Main` and `CreateHostBuilder` methods. These comments provide a brief explanation of the purpose of these methods, making the code easier to understand and maintain.

2. In `Startup.cs`, summary comments were added to the `Startup` constructor and the `Configuration` property. These comments provide a brief explanation of the purpose of the constructor and property, improving the readability of the code.

References to the code changes:

- `Program.cs`: Addition of summary comments to the `Main` and `CreateHostBuilder` methods.
- `Startup.cs`: Addition of summary comments to the `Startup` constructor and the `Configuration` property.

この時点ではまだ確定ではなく、「AI 候補を挿入する」を押すことでコミットメッセージに入力されます。
まだ日本語のコミットメッセージは生成されないですね。

GitHub Copilot Chat のログを読む

コミットメッセージを生成したときのログは、Visual Studio の出力ウィンドウに出力されます。
上記のコミットメッセージを生成したときのログは以下の内容です。

[CopilotSessionProvider Information] Begin sending message (ConversationId:6a0f7d82-96f1-4826-85db-8a27dbd06fdc, CorrelationId:bca88be7-01d4-4945-b5cb-1b84adb44ac1, MessageId: 4b7a06a5-50e0-4545-89d0-0a3695d13b3f)
[CopilotSessionProvider Information] Request content: "The following code changes from one or more source files are in diff format.
Lines starting with '+' are code that was added, and lines starting with '-' are code that was removed.
All other lines are provided for additional context.

Changes using Microsoft.Extensions.Logging; in Program.cs
  namespace AzureSamples.AzureSQL {     public class Program     {+        /// <summary>+        /// Entry point for the application+        /// </summary>+        /// <param name="args"></param>         public static void Main(string[] args)         {             CreateHostBuilder(args).Build().Run();         } +        /// <summary>+        /// Create IHostBuilder+        /// </summary>+        /// <param name="args">Arguments of application</param>+        /// <returns><see cref="IHostBuilder"/></returns>         public static IHostBuilder CreateHostBuilder(string[] args) =>             Host.CreateDefaultBuilder(args)                 .ConfigureWebHostDefaults(webBuilder =>                 {                     webBuilder.UseStartup<Startup>();
Changes using Microsoft.Extensions.Logging; in Startup.cs
  namespace AzureSamples.AzureSQL {     public class Startup     {+        /// <summary>+        /// コンストラクタ+        /// </summary>+        /// <param name="configuration"></param>         public Startup(IConfiguration configuration)         {             Configuration = configuration;         } +        /// <summary>+        /// Configuration+        /// </summary>         public IConfiguration Configuration { get; }          // This method gets called by the runtime. Use this method to add services to the container.         public void ConfigureServices(IServiceCollection services)         {


Describe the above changes, using Markdown to respond in this format:
"""
* [change-explanation]
"""

Explanation of changes:

""""
[CopilotSessionProvider Information] Begin sending message (ConversationId:1b0afd0b-f864-453a-ab62-9f2cc14683c1, CorrelationId:1fd40037-bf99-4388-9c40-130ec23caeaa, MessageId: 8a917d9e-fd40-4acb-8ea4-9d1beb5a8f8f)
[CopilotSessionProvider Information] Request content: "The following list contains descriptions of code changes to files.

* * The changes in the code are primarily the addition of summary comments to the methods and properties in the `Program.cs` and `Startup.cs` files. These comments provide a brief explanation of the purpose of the methods and properties, improving the readability and maintainability of the code.
  * In `Program.cs`, summary comments were added to the `Main` and `CreateHostBuilder` methods.
  * In `Startup.cs`, summary comments were added to the `Startup` constructor and the `Configuration` property.


Summarize the most important changes, followed by a list of the changes.  Include references to the code changes at the end of each description."

ログを読むと、ソースコードの差分をプロンプトにしていることがわかります。
+ から始まるコードは追加されたコード、- から始まるコードは削除されたコードである、という説明もあります。

Lines starting with '+' are code that was added, and lines starting with '-' are code that was removed. All other lines are provided for additional context.

Azure OpenAI Service に同じことを頼んでみる

GitHub Copilot は OpenAI の GPT-4 モデルが利用されています。
https://github.blog/2023-11-08-universe-2023-copilot-transforms-github-into-the-ai-powered-developer-platform/

GPT-4 powers Copilot Chat: we’ve upgraded your Copilot Chat experience, bringing more accurate code suggestions and explanations with OpenAI’s GPT-4 model.

そこで、Azure OpenAI Service の GPT-4 モデルの ChatGPT に同様の情報を送って、結果を見てみます。

GitHub Copilot Chat が生成した内容と似ていますね。
上記ではシステムメッセージは特に工夫していないので、物足りなさがあるのはそのせいかもしれません。


Visual Studio での GitHub Copilot Chat によるコミットメッセージの生成はまだ日本語生成に対応していませんが、どのような変更が行われているかをうまく説明してくれているように思います。
コミットメッセージに悩んだときは、この機能を使ってみても良さそうですね。

また、マイクロソフトではこの機能についてのフィードバックを募っていますので、試した際に気になったことはフィードバックしてみましょう。

aka.ms


サービス一覧 www.alterbooth.com cloudpointer.tech www.alterbooth.com