| 1 | using AgentForge.Abstractions; |
| 2 | using AgentForge.Contracts; |
| 3 | using AgentForge.Options; |
| 4 | using Microsoft.Extensions.Options; |
| 5 | |
| 6 | namespace AgentForge.Plugin.RepositoryImport.GitHub.Core; |
| 7 | |
| 8 | public static class ForgeRepositoryImportRequestMapping |
| 9 | { |
| 10 | public static ForgeRepositoryImportRequest FromOrgRequest(ImportGitHubOrgRequest request) => |
| 11 | new( |
| 12 | ForgeRepositoryImportScope.Organization, |
| 13 | ForgeOrg: request.ForgeOrg, |
| 14 | ExternalOrg: request.ExternalOrg, |
| 15 | CatalogConfig: request.CatalogConfig, |
| 16 | VisibilityPolicy: request.VisibilityPolicy, |
| 17 | DryRun: request.DryRun, |
| 18 | SkipMirror: request.SkipMirror, |
| 19 | UpdateExisting: request.UpdateExisting, |
| 20 | IncludeArchived: request.IncludeArchived); |
| 21 | |
| 22 | public static ForgeRepositoryImportRequest FromUserRequest(ImportGitHubUserRequest request) => |
| 23 | new( |
| 24 | ForgeRepositoryImportScope.User, |
| 25 | ExternalUser: request.GitHubLogin, |
| 26 | CatalogConfig: request.CatalogConfig, |
| 27 | VisibilityPolicy: request.VisibilityPolicy, |
| 28 | DryRun: request.DryRun, |
| 29 | SkipMirror: request.SkipMirror, |
| 30 | UpdateExisting: request.UpdateExisting, |
| 31 | IncludeArchived: request.IncludeArchived); |
| 32 | |
| 33 | public static ForgeRepositoryImportRequest FromSingleRequest(ImportGitHubSingleRequest request) => |
| 34 | new( |
| 35 | ForgeRepositoryImportScope.Single, |
| 36 | ForgeOrg: request.ForgeOrg, |
| 37 | SingleCloneUrl: request.CloneUrl, |
| 38 | SingleRepoName: request.RepoName, |
| 39 | CatalogPath: request.CatalogPath, |
| 40 | VisibilityPolicy: request.VisibilityPolicy, |
| 41 | DryRun: request.DryRun, |
| 42 | SkipMirror: request.SkipMirror, |
| 43 | UpdateExisting: request.UpdateExisting); |
| 44 | |
| 45 | public static ImportGitHubOrgResponse ToLegacyOrgResponse(ForgeRepositoryImportResponse response) => |
| 46 | new( |
| 47 | response.ForgeOrg ?? "", |
| 48 | response.ExternalSource ?? "", |
| 49 | response.Total, |
| 50 | response.Imported, |
| 51 | response.Skipped, |
| 52 | response.Failed, |
| 53 | response.Items.Select(item => new ImportGitHubOrgItemResult( |
| 54 | item.ExternalName, |
| 55 | item.ForgeName, |
| 56 | item.CatalogPath, |
| 57 | item.Visibility, |
| 58 | item.Action, |
| 59 | item.Detail)).ToList()); |
| 60 | |
| 61 | public static string ResolveCatalogPath( |
| 62 | ForgeRepositoryImportRequest request, |
| 63 | string contentRoot, |
| 64 | ForgeImportCatalogLoader catalogLoader, |
| 65 | ForgeOptions options) |
| 66 | { |
| 67 | if (!string.IsNullOrWhiteSpace(request.CatalogConfig)) |
| 68 | return Path.IsPathRooted(request.CatalogConfig) |
| 69 | ? request.CatalogConfig |
| 70 | : Path.Combine(contentRoot, request.CatalogConfig); |
| 71 | |
| 72 | if (!string.IsNullOrWhiteSpace(options.ImportCatalogPath)) |
| 73 | return Path.IsPathRooted(options.ImportCatalogPath) |
| 74 | ? options.ImportCatalogPath |
| 75 | : Path.Combine(contentRoot, options.ImportCatalogPath); |
| 76 | |
| 77 | return catalogLoader.ResolveDefaultPath(contentRoot, request.ForgeOrg); |
| 78 | } |
| 79 | |
| 80 | public static string? FirstNonEmpty(params string?[] values) |
| 81 | { |
| 82 | foreach (var value in values) |
| 83 | { |
| 84 | if (!string.IsNullOrWhiteSpace(value)) |
| 85 | return value.Trim(); |
| 86 | } |
| 87 | |
| 88 | return null; |
| 89 | } |
| 90 | } |
| 91 | |