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

100 lines
4.4 KiB
C#
Executable File

using MarketAlly.AIPlugin;
using MarketAlly.AIPlugin.Refactoring.Plugins;
using System.Text.Json;
namespace MarketAlly.AIPlugin.Refactoring.Plugins;
[AIPlugin("github-clone", "Clone and validate GitHub repositories for project analysis")]
public class GitHubClonePlugin : IAIPlugin
{
[AIParameter("GitHub repository URL to clone", required: true)]
public string RepositoryUrl { get; set; } = string.Empty;
[AIParameter("Target directory path where repository should be cloned", required: true)]
public string TargetPath { get; set; } = string.Empty;
[AIParameter("Specific branch to clone (optional, defaults to main/master)", required: false)]
public string? Branch { get; set; }
[AIParameter("Whether to perform shallow clone (depth=1) for faster cloning", required: false)]
public bool ShallowClone { get; set; } = true;
[AIParameter("Whether to overwrite existing directory if it exists", required: false)]
public bool OverwriteExisting { get; set; } = false;
[AIParameter("GitHub personal access token for private repositories (optional)", required: false)]
public string? AccessToken { get; set; }
public IReadOnlyDictionary<string, Type> SupportedParameters => new Dictionary<string, Type>
{
["repositoryUrl"] = typeof(string),
["repositoryurl"] = typeof(string),
["targetPath"] = typeof(string),
["targetpath"] = typeof(string),
["branch"] = typeof(string),
["shallowClone"] = typeof(bool),
["shallowclone"] = typeof(bool),
["overwriteExisting"] = typeof(bool),
["overwriteexisting"] = typeof(bool),
["accessToken"] = typeof(string),
["accesstoken"] = typeof(string)
};
public async Task<AIPluginResult> ExecuteAsync(IReadOnlyDictionary<string, object> parameters)
{
try
{
// Extract parameters
var repositoryUrl = (parameters.ContainsKey("repositoryUrl") ? parameters["repositoryUrl"] :
parameters.ContainsKey("repositoryurl") ? parameters["repositoryurl"] : null)?.ToString();
var targetPath = (parameters.ContainsKey("targetPath") ? parameters["targetPath"] :
parameters.ContainsKey("targetpath") ? parameters["targetpath"] : null)?.ToString();
var branch = parameters.ContainsKey("branch") ? parameters["branch"]?.ToString() : null;
var shallowClone = parameters.ContainsKey("shallowClone") ? Convert.ToBoolean(parameters["shallowClone"]) :
parameters.ContainsKey("shallowclone") ? Convert.ToBoolean(parameters["shallowclone"]) : true;
var overwriteExisting = parameters.ContainsKey("overwriteExisting") ? Convert.ToBoolean(parameters["overwriteExisting"]) :
parameters.ContainsKey("overwriteexisting") ? Convert.ToBoolean(parameters["overwriteexisting"]) : false;
var accessToken = parameters.ContainsKey("accessToken") ? parameters["accessToken"]?.ToString() :
parameters.ContainsKey("accesstoken") ? parameters["accesstoken"]?.ToString() : null;
var cloneManager = new GitHubCloneManager();
// Validate repository URL first
var validation = await cloneManager.ValidateRepositoryAsync(repositoryUrl);
if (!validation.IsValid)
{
return new AIPluginResult(new Exception($"Invalid repository: {validation.Error}"), "Repository validation failed");
}
// Prepare clone options
var cloneOptions = new GitCloneOptions
{
RepositoryUrl = repositoryUrl,
TargetPath = targetPath,
Branch = branch ?? validation.DefaultBranch ?? "main",
ShallowClone = shallowClone,
OverwriteExisting = overwriteExisting
};
// Execute clone
var result = await cloneManager.CloneRepositoryAsync(cloneOptions);
if (result.Success)
{
return new AIPluginResult(result, "Repository cloned successfully");
}
else
{
return new AIPluginResult(new Exception(result.Error), "Clone operation failed");
}
}
catch (Exception ex)
{
return new AIPluginResult(ex, "Plugin execution failed");
}
}
}