Compare commits

...

2 Commits

Author SHA1 Message Date
aecf3d33b4 Merge pull request '* Changed agent to check commits from last updated branch' (#88) from feature/PipelineAgent into master
Reviewed-on: #88
2026-01-23 20:17:46 +00:00
1e95f8e1a5 * Changed agent to check commits from last updated branch
Some checks failed
ci/woodpecker/push/push_to_repo Pipeline failed
ci/woodpecker/pr/merge_to_master Pipeline was successful
* Added method to get last updated branch to the service
2026-01-23 21:16:11 +01:00
3 changed files with 61 additions and 22 deletions

View File

@@ -9,16 +9,19 @@ using Services.Vault;
namespace PipelineAgent.ChangesChecker;
public class ChangesCheckerAgent(IOpenAiService openAiService, IVaultService vaultService, IGiteaService giteaService) : IChangesCheckerAgent
public class ChangesCheckerAgent(IOpenAiService openAiService, IVaultService vaultService, IGiteaService giteaService)
: IChangesCheckerAgent
{
public async Task<int> CheckChangesAsync(string repositoryPath)
{
try
{
var giteaApiToken = await vaultService.GetSecretAsync("api_keys/gitea", "gitea_api_token_write", "secret").ConfigureAwait(false);
var giteaApiToken = await vaultService.GetSecretAsync("api_keys/gitea", "gitea_api_token_write", "secret")
.ConfigureAwait(false);
var giteaConfiguration = GetGiteaConfiguration(repositoryPath, giteaApiToken);
var giteaClient = giteaService.CreateGiteaClient(giteaConfiguration);
var lastChangesFromGitea = await GetLastChangesFromGiteaAsync(giteaClient, giteaConfiguration).ConfigureAwait(false);
var lastChangesFromGitea =
await GetLastChangesFromGiteaAsync(giteaClient, giteaConfiguration).ConfigureAwait(false);
if (lastChangesFromGitea.Count == 0)
{
@@ -37,21 +40,22 @@ public class ChangesCheckerAgent(IOpenAiService openAiService, IVaultService vau
return 0;
}
if (!decisionDoc.RootElement.TryGetProperty("decision", out var decisionElement) || decisionElement.ValueKind == JsonValueKind.Null)
if (!decisionDoc.RootElement.TryGetProperty("decision", out var decisionElement) ||
decisionElement.ValueKind == JsonValueKind.Null)
{
Console.WriteLine("AIGate: decision property missing or null in LLM response, continuing by default.");
return 0;
}
var decision = decisionElement.GetString();
if (!decisionDoc.RootElement.TryGetProperty("changes", out var changesElement) || changesElement.ValueKind == JsonValueKind.Null)
if (!decisionDoc.RootElement.TryGetProperty("changes", out var changesElement) ||
changesElement.ValueKind == JsonValueKind.Null)
{
return 0;
}
var content = GetFormattedJsonRequest(changesElement);
var (status, response) = await giteaService.SendRequestAsync(giteaConfiguration, "contents", content).ConfigureAwait(false);
var (status, response) = await giteaService.SendRequestAsync(giteaConfiguration, "contents", content)
.ConfigureAwait(false);
if (status)
{
@@ -78,19 +82,34 @@ public class ChangesCheckerAgent(IOpenAiService openAiService, IVaultService vau
}
}
private async Task<Dictionary<string, string>> GetLastChangesFromGiteaAsync(RepositoryApi giteaClient, GiteaConfiguration giteaConfiguration)
private async Task<Dictionary<string, string>> GetLastChangesFromGiteaAsync(RepositoryApi giteaClient,
GiteaConfiguration giteaConfiguration)
{
var lastChanges = new Dictionary<string, string>(StringComparer.Ordinal);
var lastCommit = await giteaService.GetLastCommitAsync(giteaClient, giteaConfiguration).ConfigureAwait(false);
var lastUpdatedBranch =
(await giteaService.GetAllBranchesAsync(giteaClient, giteaConfiguration).ConfigureAwait(false))
.Where(x => x.Name != "master" && x.Name.StartsWith("feature/")).OrderByDescending(x => x.Commit.Timestamp)
.FirstOrDefault();
if (lastCommit == null || (lastCommit.VarCommit?.Message is not null && lastCommit.VarCommit.Message.Contains("LLM: Code review suggestions", StringComparison.Ordinal)))
if (lastUpdatedBranch == null ||
lastUpdatedBranch.Commit.Message.Contains("LLM: Code review suggestions", StringComparison.Ordinal))
{
return lastChanges;
}
var lastCommit = await giteaService
.GetCommitByIdAsync(giteaClient, giteaConfiguration, lastUpdatedBranch.Commit.Id).ConfigureAwait(false);
if (lastCommit == null)
{
return lastChanges;
}
foreach (CommitAffectedFiles commitAffectedFile in lastCommit.Files)
{
var fileContent = await giteaService.GetFileContentAsync(giteaClient, giteaConfiguration, commitAffectedFile.Filename).ConfigureAwait(false);
var fileContent = await giteaService
.GetFileContentAsync(giteaClient, giteaConfiguration, commitAffectedFile.Filename)
.ConfigureAwait(false);
if (!string.IsNullOrEmpty(fileContent.Content) && !lastChanges.ContainsKey(commitAffectedFile.Filename))
{
@@ -107,7 +126,9 @@ public class ChangesCheckerAgent(IOpenAiService openAiService, IVaultService vau
int lastSlash = repoSpan.LastIndexOf('/');
int secondLastSlash = lastSlash > 0 ? repoSpan.Slice(0, lastSlash).LastIndexOf('/') : -1;
string owner = secondLastSlash >= 0 ? repoSpan.Slice(secondLastSlash + 1, lastSlash - secondLastSlash - 1).ToString() : string.Empty;
string owner = secondLastSlash >= 0
? repoSpan.Slice(secondLastSlash + 1, lastSlash - secondLastSlash - 1).ToString()
: string.Empty;
string repository = lastSlash >= 0 ? repoSpan.Slice(lastSlash + 1).ToString() : repositoryPath;
const string branch = "master";
const string host = "https://git.modwad.pl";

View File

@@ -30,8 +30,8 @@ public class GiteaService : IGiteaService
public async Task<List<Commit>> GetLastCommitsAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration,
int limit)
{
if (repositoryApi == null) throw new ArgumentNullException(nameof(repositoryApi));
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
ArgumentNullException.ThrowIfNull(repositoryApi);
ArgumentNullException.ThrowIfNull(configuration);
if (limit <= 0) throw new ArgumentOutOfRangeException(nameof(limit), "Limit must be greater than zero.");
var lastCommits = await repositoryApi.RepoGetAllCommitsAsync(configuration.Owner, configuration.Repository,
@@ -43,11 +43,24 @@ public class GiteaService : IGiteaService
{
ArgumentNullException.ThrowIfNull(repositoryApi);
ArgumentNullException.ThrowIfNull(configuration);
var lastCommit = await GetLastCommitsAsync(repositoryApi, configuration, 1);
return lastCommit.FirstOrDefault();
}
public async Task<Commit?> GetCommitByIdAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration,
string commitId)
{
ArgumentNullException.ThrowIfNull(repositoryApi);
ArgumentNullException.ThrowIfNull(configuration);
if (string.IsNullOrEmpty(commitId))
throw new ArgumentException("CommitId cannot be null or empty.", nameof(commitId));
var lastCommit =
await repositoryApi.RepoGetSingleCommitAsync(configuration.Owner, configuration.Repository, commitId);
return lastCommit;
}
public async Task<List<Branch>> GetAllBranchesAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(repositoryApi);
@@ -63,7 +76,8 @@ public class GiteaService : IGiteaService
{
ArgumentNullException.ThrowIfNull(repositoryApi);
ArgumentNullException.ThrowIfNull(configuration);
if (string.IsNullOrEmpty(filename)) throw new ArgumentException("Filename cannot be null or empty.", nameof(filename));
if (string.IsNullOrEmpty(filename))
throw new ArgumentException("Filename cannot be null or empty.", nameof(filename));
var repoGetContentsAsync =
await repositoryApi.RepoGetContentsAsync(configuration.Owner, configuration.Repository, filename);
@@ -75,15 +89,17 @@ public class GiteaService : IGiteaService
{
ArgumentNullException.ThrowIfNull(configuration);
ArgumentNullException.ThrowIfNull(content);
if (string.IsNullOrEmpty(endpoint)) throw new ArgumentException("Endpoint cannot be null or empty.", nameof(endpoint));
if (string.IsNullOrEmpty(endpoint))
throw new ArgumentException("Endpoint cannot be null or empty.", nameof(endpoint));
var requestUrl = $"{configuration.Url}/{endpoint}";
using var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
request.Content = content;
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("token", configuration.ApiToken);
request.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("token", configuration.ApiToken);
try
{
var giteaResponse = await HttpClient.SendAsync(request);

View File

@@ -11,6 +11,8 @@ public interface IGiteaService
Task<ContentsResponse> GetFileContentAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration,
string filename);
Task<Commit?> GetCommitByIdAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration, string commitId);
Task<Commit?> GetLastCommitAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration);
Task<List<Branch>> GetAllBranchesAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration);