| 1 | namespace AgentForge.Abstractions; |
| 2 | |
| 3 | public sealed record ForgeOrgImportRequest( |
| 4 | string ForgeOrg, |
| 5 | string? ExternalOrg = null, |
| 6 | bool DryRun = false, |
| 7 | string DefaultRole = "member"); |
| 8 | |
| 9 | public sealed record ForgeOrgImportMemberItem( |
| 10 | string Login, |
| 11 | string Action, |
| 12 | string? Detail = null); |
| 13 | |
| 14 | public sealed record ForgeOrgImportResponse( |
| 15 | string ForgeOrg, |
| 16 | string ExternalOrg, |
| 17 | int Total, |
| 18 | int Imported, |
| 19 | int Skipped, |
| 20 | int Failed, |
| 21 | IReadOnlyList<ForgeOrgImportMemberItem> Items); |
| 22 | |
| 23 | public interface IForgeOrgImporter |
| 24 | { |
| 25 | string SourceId { get; } |
| 26 | |
| 27 | string PluginId { get; } |
| 28 | |
| 29 | Task<ForgeOrgImportResponse> ImportMembersAsync( |
| 30 | ForgeOrgImportRequest request, |
| 31 | CancellationToken cancellationToken = default); |
| 32 | } |
| 33 | |
| 34 | public sealed class ForgeOrgImporterRegistry |
| 35 | { |
| 36 | private readonly IReadOnlyDictionary<string, IForgeOrgImporter> _importers; |
| 37 | |
| 38 | public ForgeOrgImporterRegistry(IEnumerable<IForgeOrgImporter> importers) |
| 39 | { |
| 40 | var map = new Dictionary<string, IForgeOrgImporter>(StringComparer.OrdinalIgnoreCase); |
| 41 | foreach (var importer in importers) |
| 42 | { |
| 43 | if (map.ContainsKey(importer.SourceId)) |
| 44 | throw new InvalidOperationException( |
| 45 | $"Duplicate org importer for source '{importer.SourceId}' ({importer.PluginId})."); |
| 46 | |
| 47 | map[importer.SourceId] = importer; |
| 48 | } |
| 49 | |
| 50 | _importers = map; |
| 51 | } |
| 52 | |
| 53 | public bool TryGet(string sourceId, out IForgeOrgImporter? importer) => |
| 54 | _importers.TryGetValue(sourceId, out importer); |
| 55 | |
| 56 | public IForgeOrgImporter GetRequired(string sourceId) => |
| 57 | TryGet(sourceId, out var importer) && importer is not null |
| 58 | ? importer |
| 59 | : throw new ForgeImportNotLoadedException(sourceId, "members"); |
| 60 | } |
| 61 | |