Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitAuto: [FEATURE] Preserve original message for merge conflict resolutions +semver: minor #114

Merged
merged 35 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a1011cd
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
ed918f8
Update Src/AiCommitMessage/Options/GenerateCommitMessageOptions.cs
gitauto-ai[bot] Nov 7, 2024
5251d1c
Update Src/AiCommitMessage/Utility/Constants.cs
gitauto-ai[bot] Nov 7, 2024
7d228e1
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
5c3544f
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
6abb939
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
a541beb
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
22986fd
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
3f4f1b2
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
0fc4fc8
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
ba2b973
Update Src/AiCommitMessage/Program.cs
gitauto-ai[bot] Nov 7, 2024
20607c9
Update Src/AiCommitMessage/AiCommitMessage.csproj
gitauto-ai[bot] Nov 7, 2024
134dd72
Merge branch 'main' into gitauto/issue-113-347f3d44-b7bf-48ef-97fb-29…
gstraccini[bot] Nov 11, 2024
6224f3c
Merge branch 'main' into gitauto/issue-113-347f3d44-b7bf-48ef-97fb-29…
gstraccini[bot] Nov 11, 2024
eb3c060
Merge branch 'main' into gitauto/issue-113-347f3d44-b7bf-48ef-97fb-29…
gstraccini[bot] Nov 12, 2024
f29ddab
Update AiCommitMessage.csproj
guibranco Dec 12, 2024
d6652ac
Merge branch 'main' into gitauto/issue-113-347f3d44-b7bf-48ef-97fb-29…
gstraccini[bot] Dec 12, 2024
e5b199c
Update GenerateCommitMessageOptions.cs
guibranco Dec 12, 2024
93fbf2d
Update Constants.cs
guibranco Dec 12, 2024
64833be
Update Program.cs
guibranco Dec 12, 2024
647afd4
Update GenerateCommitMessageService.cs
guibranco Dec 12, 2024
7b0c88f
Update Program.cs
guibranco Dec 12, 2024
123b971
Update GenerateCommitMessageService.cs
guibranco Dec 12, 2024
a584fcc
Update GenerateCommitMessageService.cs +semver: minor
guibranco Dec 12, 2024
667a335
Update GenerateCommitMessageService.cs
guibranco Dec 12, 2024
fc0d041
CSharpier format
gstraccini[bot] Dec 12, 2024
1f407ff
Update GenerateCommitMessageService.cs
guibranco Dec 12, 2024
a06632e
Create GenerateCommitMessageServiceTests.cs
guibranco Dec 12, 2024
a0404fd
Update GenerateCommitMessageServiceTests.cs
guibranco Dec 12, 2024
ebbe1c8
Update GenerateCommitMessageServiceTests.cs
guibranco Dec 12, 2024
afbcb5b
Update GenerateCommitMessageServiceTests.cs
guibranco Dec 12, 2024
f0935a1
Update GenerateCommitMessageServiceTests.cs
guibranco Dec 12, 2024
8242aa4
#113 fix - correcting test cases for better functionality
guibranco Dec 12, 2024
83c3a16
#113 fix - update environment variable setting in test file
guibranco Dec 12, 2024
8ad567c
#113 refactor - enhance regex options and adjust code structure
guibranco Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions Src/AiCommitMessage/Options/GenerateCommitMessageOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,50 @@
namespace AiCommitMessage.Options;

/// <summary>
/// Class GenerateMessageOptions.
/// Represents the options for generating a commit message based on staged changes.
/// </summary>
[Verb("generate-message", HelpText = "Generate a commit message based on staged changes.")]
public class GenerateCommitMessageOptions
{
/// <summary>
/// Gets or sets the message.
/// Gets or sets the initial commit message provided by the user.
/// </summary>
/// <value>The message.</value>
/// <value>The initial commit message used as input for generation.</value>
/// <remarks>
/// This message serves as the base for generating a refined or enhanced commit message.
/// </remarks>
[Option('m', "message", Required = true, HelpText = "The current commit message.")]
public string Message { get; set; }

/// <summary>
/// Gets or sets the branch.
/// Gets or sets the name of the current branch.
/// </summary>
/// <value>The branch.</value>
/// <value>The name of the branch where changes are staged.</value>
/// <remarks>
/// Providing the branch name can help contextualize the commit message for workflows
/// or tools that rely on branch-specific details.
/// </remarks>
[Option('b', "branch", Required = false, HelpText = "The current branch name.")]
public string Branch { get; set; }

/// <summary>
/// Gets or sets the difference.
/// Gets or sets the staged changes for the commit.
/// </summary>
/// <value>The difference.</value>
/// <value>A string representation of the staged changes (e.g., diff output).</value>
/// <remarks>
/// Including the diff can provide more detailed context for generating a commit message
/// tailored to the exact changes being staged.
/// </remarks>
[Option('d', "diff", Required = false, HelpText = "The staged changes.")]
public string Diff { get; set; }

/// <summary>
/// Gets or sets a value indicating whether debug is enabled or not.
/// Gets or sets a value indicating whether debug mode is enabled.
/// </summary>
/// <value><c>true</c> if debug is enabled; otherwise, <c>false</c>.</value>
/// <value><c>true</c> if debug mode is enabled; otherwise, <c>false</c>.</value>
/// <remarks>
/// Debug mode enables additional logging and diagnostic output to assist in troubleshooting.
/// </remarks>
[Option('D', "debug", Required = false, HelpText = "Debug mode.")]
public bool Debug { get; set; }
}
2 changes: 1 addition & 1 deletion Src/AiCommitMessage/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using AiCommitMessage.Options;
using AiCommitMessage.Services;
using AiCommitMessage.Utility;
Expand Down
49 changes: 33 additions & 16 deletions Src/AiCommitMessage/Services/GenerateCommitMessageService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ClientModel;
using System.Diagnostics;
using System.Text.Json;
using System.Text.RegularExpressions;
using AiCommitMessage.Options;
using AiCommitMessage.Utility;
using OpenAI;
Expand All @@ -9,23 +10,41 @@
namespace AiCommitMessage.Services;

/// <summary>
/// Class GenerateCommitMessageService.
/// Service for generating commit messages based on staged changes and contextual information.
/// </summary>
public class GenerateCommitMessageService
{
/// <summary>
/// Regular expression to detect merge conflict resolution messages.
/// </summary>
private static readonly Regex MergeConflictPattern = new (

Check warning on line 20 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Pass a timeout to limit the execution time. (https://rules.sonarsource.com/csharp/RSPEC-6444)

Check warning on line 20 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Pass a timeout to limit the execution time. (https://rules.sonarsource.com/csharp/RSPEC-6444)

Check warning on line 20 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Pass a timeout to limit the execution time. (https://rules.sonarsource.com/csharp/RSPEC-6444)

Check warning on line 20 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Pass a timeout to limit the execution time. (https://rules.sonarsource.com/csharp/RSPEC-6444)
@"^Merge branch '.*' into .*$",
RegexOptions.Compiled
);

/// <summary>
/// Checks whether the given commit message is a merge conflict resolution message.
/// </summary>
/// <param name="message">The commit message to evaluate.</param>
/// <returns><c>true</c> if the message indicates a merge conflict resolution; otherwise, <c>false</c>.</returns>
/// <remarks>
/// This helper method uses a predefined regular expression to match patterns commonly seen in merge conflict resolutions.
/// </remarks>
private static bool IsMergeConflictResolution(string message) =>
MergeConflictPattern.IsMatch(message);

/// <summary>
/// Generates a commit message based on the provided options and the OpenAI API.
/// </summary>
/// <param name="options">An instance of <see cref="GenerateCommitMessageOptions"/> containing the branch name, original message, and git diff.</param>
/// <returns>A string containing the generated commit message from the OpenAI API.</returns>
/// <remarks>
/// This method retrieves the OpenAI API URL and API key from the environment variables. If the URL is not set, it defaults to "https://api.openai.com/v1".
/// If the API key is not provided, it returns a message prompting the user to set the <c>OPENAI_API_KEY</c> environment variable.
/// It constructs a message that includes the branch name, original commit message, and git diff, which is then sent to the OpenAI API using a chat client.
/// If debugging is enabled, it serializes the chat completion response to JSON and writes it to a file named "debug.json".
/// Finally, it returns the generated commit message from the chat completion response.
/// This method retrieves API details (URL and key) from environment variables, constructs a message including the branch name,
/// original commit message, and git diff, and sends it to the OpenAI API for processing. It also handles debugging, saving
/// API responses to a JSON file if debugging is enabled. If the commit message is a merge conflict resolution, it is returned as-is.
/// </remarks>
/// <exception cref="InvalidOperationException">Thrown if both the branch and diff are empty, as meaningful commit generation is not possible.</exception>
public string GenerateCommitMessage(GenerateCommitMessageOptions options)

Check warning on line 47 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 47 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 47 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
var model = EnvironmentLoader.LoadOpenAiModel();
var url = EnvironmentLoader.LoadOpenAiApiUrl();
Expand All @@ -44,7 +63,12 @@
var diff = string.IsNullOrEmpty(options.Diff) ? GitHelper.GetGitDiff() : options.Diff;

// Use the provided message (this will come from the prepare-commit-msg hook)
var message = options.Message; // No fallback to GIT, as commit message is passed in the hook
var message = options.Message; // No fallback to GIT, as the commit message is passed in the hook

if (IsMergeConflictResolution(message))
{
return message;
}

if (string.IsNullOrEmpty(branch) && string.IsNullOrEmpty(diff))
{
Expand Down Expand Up @@ -115,21 +139,14 @@
/// </summary>
/// <returns>A <see cref="GitProvider"/> enumeration value representing the detected Git provider.</returns>
/// <remarks>
/// This method executes a Git command to fetch the remote origin URL configured for the repository.
/// It uses the <c>git config --get remote.origin.url</c> command to obtain the URL, which is then analyzed to determine the Git provider.
/// The method checks for specific substrings in the URL to identify the provider:
/// - If the URL contains "dev.azure.com", it returns <see cref="GitProvider.AzureDevOps"/>.
/// - If the URL contains "bitbucket.org", it returns <see cref="GitProvider.Bitbucket"/>.
/// - If the URL contains "github.com", it returns <see cref="GitProvider.GitHub"/>.
/// - If the URL contains "gitlab.com", it returns <see cref="GitProvider.GitLab"/>.
/// If none of these providers are identified, it returns <see cref="GitProvider.Unidentified"/>.
/// This method is useful for determining the source control environment in which the code is hosted.
/// This method uses the <c>git config --get remote.origin.url</c> command to determine the Git provider based on substrings in the URL.
/// If no known provider is identified, it defaults to <see cref="GitProvider.Unidentified"/>.
/// </remarks>
private static GitProvider GetGitProvider()
{
var processStartInfo = new ProcessStartInfo
{
FileName = "git",

Check warning on line 149 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Make sure the "PATH" used to find this command includes only what you intend. (https://rules.sonarsource.com/csharp/RSPEC-4036)

Check warning on line 149 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Make sure the "PATH" used to find this command includes only what you intend. (https://rules.sonarsource.com/csharp/RSPEC-4036)

Check warning on line 149 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Make sure the "PATH" used to find this command includes only what you intend. (https://rules.sonarsource.com/csharp/RSPEC-4036)
Arguments = "config --get remote.origin.url",
RedirectStandardOutput = true,
UseShellExecute = false,
Expand Down
Loading