MarketAlly.AIPlugin.Extensions/MarketAlly.AIPlugin.Refacto.../Plugins/GitBranchStatusPlugin.cs

117 lines
4.3 KiB
C#
Executable File

using MarketAlly.AIPlugin;
using LibGit2Sharp;
using System.Text.Json;
namespace MarketAlly.AIPlugin.Refactoring.Plugins;
[AIPlugin("git-branch-status", "Get detailed status and commit information for a specific branch")]
public class GitBranchStatusPlugin : IAIPlugin
{
[AIParameter("Local repository path", required: true)]
public string RepositoryPath { get; set; } = string.Empty;
[AIParameter("Branch name to analyze", required: true)]
public string BranchName { get; set; } = string.Empty;
public IReadOnlyDictionary<string, Type> SupportedParameters => new Dictionary<string, Type>
{
["repositoryPath"] = typeof(string),
["repositorypath"] = typeof(string),
["branchName"] = typeof(string),
["branchname"] = typeof(string)
};
public async Task<AIPluginResult> ExecuteAsync(IReadOnlyDictionary<string, object> parameters)
{
try
{
var repositoryPath = (parameters.ContainsKey("repositoryPath") ? parameters["repositoryPath"] :
parameters.ContainsKey("repositorypath") ? parameters["repositorypath"] : null)?.ToString();
var branchName = (parameters.ContainsKey("branchName") ? parameters["branchName"] :
parameters.ContainsKey("branchname") ? parameters["branchname"] : null)?.ToString();
if (string.IsNullOrEmpty(repositoryPath) || !Directory.Exists(repositoryPath))
{
return new AIPluginResult(new ArgumentException("Invalid repository path"), "Repository path does not exist");
}
var result = await GetBranchStatusAsync(repositoryPath, branchName);
return new AIPluginResult(result, "Branch status retrieved successfully");
}
catch (Exception ex)
{
return new AIPluginResult(ex, "Failed to retrieve branch status");
}
}
private async Task<GitBranchStatus> GetBranchStatusAsync(string repositoryPath, string branchName)
{
return await Task.Run(() =>
{
using var repo = new Repository(repositoryPath);
var branch = repo.Branches[branchName];
if (branch == null)
{
throw new ArgumentException($"Branch '{branchName}' not found");
}
var status = new GitBranchStatus
{
BranchName = branchName,
Exists = true,
IsActive = !branch.IsRemote && branch.Tip != null,
IsCurrent = branch.IsCurrentRepositoryHead,
IsRemote = branch.IsRemote
};
if (branch.Tip != null)
{
status.LastCommitHash = branch.Tip.Sha;
status.LastCommitMessage = branch.Tip.MessageShort;
status.LastCommitAuthor = branch.Tip.Author.Name;
status.LastCommitDate = branch.Tip.Author.When.DateTime;
// Get commit count
status.CommitCount = branch.Commits.Count();
// Get recent commits (last 10)
status.RecentCommits = branch.Commits
.Take(10)
.Select(c => new GitCommitInfo
{
Hash = c.Sha,
Message = c.MessageShort,
Author = c.Author.Name,
Date = c.Author.When.DateTime
})
.ToList();
}
return status;
});
}
}
public class GitBranchStatus
{
public string BranchName { get; set; } = string.Empty;
public bool Exists { get; set; }
public bool IsActive { get; set; }
public bool IsCurrent { get; set; }
public bool IsRemote { get; set; }
public string? LastCommitHash { get; set; }
public string? LastCommitMessage { get; set; }
public string? LastCommitAuthor { get; set; }
public DateTime? LastCommitDate { get; set; }
public int CommitCount { get; set; }
public List<GitCommitInfo> RecentCommits { get; set; } = new();
}
public class GitCommitInfo
{
public string Hash { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public DateTime Date { get; set; }
}