| 1 | namespace AgentForge.Abstractions; |
| 2 | |
| 3 | public enum ForgeRepositoryImportScope |
| 4 | { |
| 5 | Organization, |
| 6 | User, |
| 7 | Single, |
| 8 | } |
| 9 | |
| 10 | public sealed record ForgeRepositoryImportRequest( |
| 11 | ForgeRepositoryImportScope Scope, |
| 12 | string? ForgeOrg = null, |
| 13 | string? ExternalOrg = null, |
| 14 | string? ExternalUser = null, |
| 15 | string? SingleCloneUrl = null, |
| 16 | string? SingleRepoName = null, |
| 17 | string? CatalogConfig = null, |
| 18 | string? CatalogPath = null, |
| 19 | string? VisibilityPolicy = null, |
| 20 | bool DryRun = false, |
| 21 | bool SkipMirror = false, |
| 22 | bool UpdateExisting = false, |
| 23 | bool IncludeArchived = false); |
| 24 | |
| 25 | public sealed record ForgeRepositoryImportItemResult( |
| 26 | string ExternalName, |
| 27 | string ForgeName, |
| 28 | string CatalogPath, |
| 29 | string Visibility, |
| 30 | string Action, |
| 31 | string? Detail = null); |
| 32 | |
| 33 | public sealed record ForgeRepositoryImportResponse( |
| 34 | string? ForgeOrg, |
| 35 | string? ExternalSource, |
| 36 | int Total, |
| 37 | int Imported, |
| 38 | int Skipped, |
| 39 | int Failed, |
| 40 | IReadOnlyList<ForgeRepositoryImportItemResult> Items); |
| 41 | |
| 42 | public interface IForgeRepositoryImporter |
| 43 | { |
| 44 | string SourceId { get; } |
| 45 | |
| 46 | ForgeRepositoryImportScope Scope { get; } |
| 47 | |
| 48 | string PluginId { get; } |
| 49 | |
| 50 | Task<ForgeRepositoryImportResponse> ImportAsync( |
| 51 | ForgeRepositoryImportRequest request, |
| 52 | CancellationToken cancellationToken = default); |
| 53 | } |
| 54 | |
| 55 | public sealed class ForgeRepositoryImporterRegistry |
| 56 | { |
| 57 | private readonly IReadOnlyDictionary<(string SourceId, ForgeRepositoryImportScope Scope), IForgeRepositoryImporter> _importers; |
| 58 | |
| 59 | public ForgeRepositoryImporterRegistry(IEnumerable<IForgeRepositoryImporter> importers) |
| 60 | { |
| 61 | var map = new Dictionary<(string, ForgeRepositoryImportScope), IForgeRepositoryImporter>(); |
| 62 | foreach (var importer in importers) |
| 63 | { |
| 64 | var key = (importer.SourceId, importer.Scope); |
| 65 | if (map.ContainsKey(key)) |
| 66 | throw new InvalidOperationException( |
| 67 | $"Duplicate repository importer for source '{importer.SourceId}' scope '{importer.Scope}' " + |
| 68 | $"({importer.PluginId})."); |
| 69 | |
| 70 | map[key] = importer; |
| 71 | } |
| 72 | |
| 73 | _importers = map; |
| 74 | } |
| 75 | |
| 76 | public bool TryGet(string sourceId, ForgeRepositoryImportScope scope, out IForgeRepositoryImporter? importer) => |
| 77 | _importers.TryGetValue((sourceId, scope), out importer); |
| 78 | |
| 79 | public IForgeRepositoryImporter GetRequired(string sourceId, ForgeRepositoryImportScope scope) => |
| 80 | TryGet(sourceId, scope, out var importer) && importer is not null |
| 81 | ? importer |
| 82 | : throw new ForgeImportNotLoadedException(sourceId, scope.ToString()); |
| 83 | } |
| 84 | |
| 85 | public sealed class ForgeImportNotLoadedException(string sourceId, string scope) |
| 86 | : Exception($"Importer not loaded for source '{sourceId}' scope '{scope}'."); |
| 87 | |