Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4edb4d6
Update Src/AiCommitMessage/Services/Cache/ICacheProvider.cs
gitauto-ai[bot] Apr 27, 2025
3373438
Replace content of Src/AiCommitMessage/Services/Cache/FileCacheProvid…
gitauto-ai[bot] Apr 27, 2025
ea1bf7a
Replace content of Src/AiCommitMessage/Services/Cache/CommitMessageCa…
gitauto-ai[bot] Apr 27, 2025
3acf216
Merge branch 'main' into gitauto/issue-222-20250427-035619
guibranco May 14, 2025
db0270b
CSharpier format
gstraccini[bot] May 14, 2025
7fc7ef8
Merge branch 'main' into gitauto/issue-222-20250427-035619
guibranco May 27, 2025
fd6329a
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jun 20, 2025
af95291
Merge branch 'main' into gitauto/issue-222-20250427-035619
guibranco Jul 11, 2025
564084f
Merge branch 'main' into gitauto/issue-222-20250427-035619
guibranco Jul 15, 2025
32817b7
Update FileCacheProvider.cs
guibranco Jul 15, 2025
4a61af1
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 15, 2025
9e3650c
CSharpier format + Add workflow template
gstraccini[bot] Jul 15, 2025
9384e15
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 17, 2025
135d4d4
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 17, 2025
c7e0beb
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 17, 2025
1d1aac7
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 18, 2025
8cb342e
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 18, 2025
8c5db86
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 18, 2025
327f779
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 18, 2025
5bb3444
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 21, 2025
8de59a7
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 22, 2025
3a97c83
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 28, 2025
5d8ddf1
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Jul 28, 2025
e8e5768
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Aug 7, 2025
83c3382
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Sep 3, 2025
84ff088
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Sep 8, 2025
46d4972
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Sep 15, 2025
d6942d5
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Oct 2, 2025
31a594c
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Oct 6, 2025
00b45e2
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Oct 15, 2025
e7babdd
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Oct 22, 2025
25c1b6f
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Oct 22, 2025
9d240dc
Merge branch 'main' into gitauto/issue-222-20250427-035619
gstraccini[bot] Nov 3, 2025
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
36 changes: 36 additions & 0 deletions Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading.Tasks;

namespace AiCommitMessage.Services.Cache
{
public class CommitMessageCacheService
{
private readonly ICacheProvider _cacheProvider;

public CommitMessageCacheService(ICacheProvider cacheProvider)
{
_cacheProvider = cacheProvider;
}

public async Task<string> GetOrGenerateAsync(
string model,
string branch,
string authorMessage,
string diff,
Func<Task<string>> generateFunc)

Check failure on line 20 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / linter-check

File is not formatted correctly. Run 'dotnet csharpier .' to fix.
{
string hash = _cacheProvider.GenerateHash(model, branch, authorMessage, diff);
string? cached = await _cacheProvider.LoadAsync(model, hash);

Check warning on line 23 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 23 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 23 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 23 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
if (cached != null)
{
Console.WriteLine("\u2705 Loaded from cache.");

Check warning on line 26 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Build

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)

Check warning on line 26 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)

Check warning on line 26 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)

Check warning on line 26 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)
return cached;
}

string result = await generateFunc();
await _cacheProvider.SaveAsync(model, hash, result);
Console.WriteLine("\ud83d\udcac Cached new result.");

Check warning on line 32 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Build

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)

Check warning on line 32 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)

Check warning on line 32 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)

Check warning on line 32 in Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Use AnsiConsole instead of System.Console (https://spectreconsole.net/analyzer/rules/spectre1000)
return result;
}
}
}
82 changes: 82 additions & 0 deletions Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;

namespace AiCommitMessage.Services.Cache
{
public class FileCacheProvider : ICacheProvider
{
private record CachedResponse(string Model, string Response, string Checksum, DateTime Timestamp);

Check warning on line 9 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Private record classes which are not derived in the current assembly should be marked as 'sealed'. (https://rules.sonarsource.com/csharp/RSPEC-3260)

Check failure on line 9 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / linter-check

File is not formatted correctly. Run 'dotnet csharpier .' to fix.

private readonly string _cacheDir;

public FileCacheProvider(string appName = "CommitMessageTool")

Check failure on line 13 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L13

Use the overloading mechanism instead of the optional parameters.
{
string? envPath = Environment.GetEnvironmentVariable("COMMIT_CACHE_PATH");

Check warning on line 15 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
if (!string.IsNullOrWhiteSpace(envPath))
{
_cacheDir = Path.Combine(envPath, appName, "commit-cache");
}
else
{
string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (string.IsNullOrWhiteSpace(baseDir))
{
string? home = Environment.GetEnvironmentVariable("HOME") ?? Environment.GetEnvironmentVariable("USERPROFILE");

Check warning on line 25 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 25 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 25 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 25 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
if (string.IsNullOrEmpty(home))

Check failure on line 26 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L26

Add curly braces around the nested statement(s) in this 'if' block.
throw new InvalidOperationException("Unable to determine a valid user data directory");
baseDir = Path.Combine(home, ".config");
}
_cacheDir = Path.Combine(baseDir, appName, "commit-cache");
}
Directory.CreateDirectory(_cacheDir);
}

public string GenerateHash(string model, string branch, string authorMessage, string diff)
{
string combined = $"{model}|{branch}|{authorMessage}|{diff}";
using var sha = SHA256.Create();
byte[] hashBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(combined));

Check notice on line 39 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

codefactor.io / CodeFactor

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L39

Prefer static 'System.Security.Cryptography.SHA256.HashData' method over 'ComputeHash'. (CA1850)
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();

Check notice on line 40 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

codefactor.io / CodeFactor

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L40

Prefer 'System.Convert.ToHexString(byte[])' over call chains based on 'System.BitConverter.ToString(byte[])'. (CA1872)
}

public async Task<string?> LoadAsync(string model, string hash, int maxAgeDays = 30)

Check failure on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L43

Use the overloading mechanism instead of the optional parameters.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
string path = Path.Combine(_cacheDir, $"{hash}.json");
if (!File.Exists(path))

Check failure on line 46 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L46

Add curly braces around the nested statement(s) in this 'if' block.
return null;

var json = await File.ReadAllTextAsync(path);
var cached = JsonSerializer.Deserialize<CachedResponse>(json);
if (cached is null || cached.Model != model)

Check failure on line 51 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L51

Add curly braces around the nested statement(s) in this 'if' block.
return null;

bool expired = DateTime.UtcNow - cached.Timestamp > TimeSpan.FromDays(maxAgeDays);
bool validChecksum = cached.Checksum == ComputeChecksum(model, cached.Response);

if (expired || !validChecksum)
{
File.Delete(path);
return null;
}
return cached.Response;
}

public async Task SaveAsync(string model, string hash, string response)
{
string path = Path.Combine(_cacheDir, $"{hash}.json");
var checksum = ComputeChecksum(model, response);
var cached = new CachedResponse(model, response, checksum, DateTime.UtcNow);
var json = JsonSerializer.Serialize(cached);
await File.WriteAllTextAsync(path, json);
}

private string ComputeChecksum(string model, string content)

Check warning on line 74 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

Make 'ComputeChecksum' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
using var sha = SHA256.Create();
var combined = $"{model}|{content}";
byte[] hashBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(combined));

Check notice on line 78 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

codefactor.io / CodeFactor

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L78

Prefer static 'System.Security.Cryptography.SHA256.HashData' method over 'ComputeHash'. (CA1850)
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();

Check notice on line 79 in Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs

View check run for this annotation

codefactor.io / CodeFactor

Src/AiCommitMessage/Services/Cache/FileCacheProvider.cs#L79

Prefer 'System.Convert.ToHexString(byte[])' over call chains based on 'System.BitConverter.ToString(byte[])'. (CA1872)
}
}
}
11 changes: 11 additions & 0 deletions Src/AiCommitMessage/Services/Cache/ICacheProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace AiCommitMessage.Services.Cache
{
public interface ICacheProvider
{
string GenerateHash(string model, string branch, string authorMessage, string diff);
Task<string?> LoadAsync(string model, string hash, int maxAgeDays = 30);

Check failure on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/AiCommitMessage/Services/Cache/ICacheProvider.cs#L8

Use the overloading mechanism instead of the optional parameters.

Check warning on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View workflow job for this annotation

GitHub Actions / SonarCloud Analysis

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in Src/AiCommitMessage/Services/Cache/ICacheProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
Task SaveAsync(string model, string hash, string response);
}
}
Loading