| 1 | using AgentForge.Abstractions; |
| 2 | using AgentForge.Contracts; |
| 3 | using AgentForge.Plugin.RepositoryImport.GitHub.Core; |
| 4 | using AgentForge.Plugin.Sdk; |
| 5 | using AgentForge.Services; |
| 6 | using Microsoft.AspNetCore.Builder; |
| 7 | using Microsoft.AspNetCore.Http; |
| 8 | |
| 9 | namespace AgentForge.Plugin.RepositoryImport.GitHub; |
| 10 | |
| 11 | internal static class ForgeRepositoryImportEndpoints |
| 12 | { |
| 13 | internal static void Map(WebApplication app) |
| 14 | { |
| 15 | app.MapPost("/api/v1/import/repository/github/org", ImportGitHubOrg); |
| 16 | app.MapPost("/api/v1/import/repository/github", ImportGitHubOrgLegacy); |
| 17 | app.MapPost("/api/v1/orgs/{orgSlug}/import/github/repos", ImportGitHubOrgForOrg); |
| 18 | app.MapPost("/api/v1/import/repository/github/single", ImportGitHubSingle); |
| 19 | } |
| 20 | |
| 21 | private static async Task<IResult> ImportGitHubOrg( |
| 22 | ImportGitHubOrgRequest request, |
| 23 | ForgeRepositoryImporterRegistry registry, |
| 24 | HttpContext context, |
| 25 | ForgeRepoAccessService access, |
| 26 | CancellationToken cancellationToken) => |
| 27 | await DispatchOrgAsync(request, registry, context, access, cancellationToken).ConfigureAwait(false); |
| 28 | |
| 29 | private static Task<IResult> ImportGitHubOrgLegacy( |
| 30 | ImportGitHubOrgRequest request, |
| 31 | ForgeRepositoryImporterRegistry registry, |
| 32 | HttpContext context, |
| 33 | ForgeRepoAccessService access, |
| 34 | CancellationToken cancellationToken) => |
| 35 | DispatchOrgAsync(request, registry, context, access, cancellationToken); |
| 36 | |
| 37 | private static Task<IResult> ImportGitHubOrgForOrg( |
| 38 | string orgSlug, |
| 39 | ImportGitHubOrgRequest request, |
| 40 | ForgeRepositoryImporterRegistry registry, |
| 41 | HttpContext context, |
| 42 | ForgeRepoAccessService access, |
| 43 | CancellationToken cancellationToken) |
| 44 | { |
| 45 | access.EnsureHostAdmin(access.Resolve(context)); |
| 46 | var merged = request with { ForgeOrg = string.IsNullOrWhiteSpace(request.ForgeOrg) ? orgSlug : request.ForgeOrg }; |
| 47 | return DispatchOrgCoreAsync(merged, registry, cancellationToken); |
| 48 | } |
| 49 | |
| 50 | private static async Task<IResult> DispatchOrgAsync( |
| 51 | ImportGitHubOrgRequest request, |
| 52 | ForgeRepositoryImporterRegistry registry, |
| 53 | HttpContext context, |
| 54 | ForgeRepoAccessService access, |
| 55 | CancellationToken cancellationToken) |
| 56 | { |
| 57 | access.EnsureHostAdmin(access.Resolve(context)); |
| 58 | return await DispatchOrgCoreAsync(request, registry, cancellationToken).ConfigureAwait(false); |
| 59 | } |
| 60 | |
| 61 | private static async Task<IResult> DispatchOrgCoreAsync( |
| 62 | ImportGitHubOrgRequest request, |
| 63 | ForgeRepositoryImporterRegistry registry, |
| 64 | CancellationToken cancellationToken) |
| 65 | { |
| 66 | if (!registry.TryGet("github", ForgeRepositoryImportScope.Organization, out var importer) || importer is null) |
| 67 | return Results.Json(new { detail = "Repository importer not loaded for source 'github' scope 'Organization'." }, statusCode: 501); |
| 68 | |
| 69 | var mapped = ForgeRepositoryImportRequestMapping.FromOrgRequest(request); |
| 70 | var response = await importer.ImportAsync(mapped, cancellationToken).ConfigureAwait(false); |
| 71 | return Results.Ok(ForgeRepositoryImportRequestMapping.ToLegacyOrgResponse(response)); |
| 72 | } |
| 73 | |
| 74 | private static async Task<IResult> ImportGitHubSingle( |
| 75 | ImportGitHubSingleRequest request, |
| 76 | ForgeRepositoryImporterRegistry registry, |
| 77 | HttpContext context, |
| 78 | ForgeRepoAccessService access, |
| 79 | CancellationToken cancellationToken) |
| 80 | { |
| 81 | access.EnsureHostAdmin(access.Resolve(context)); |
| 82 | if (!registry.TryGet("github", ForgeRepositoryImportScope.Single, out var importer) || importer is null) |
| 83 | return Results.Json(new { detail = "Repository importer not loaded for source 'github' scope 'Single'." }, statusCode: 501); |
| 84 | |
| 85 | var mapped = ForgeRepositoryImportRequestMapping.FromSingleRequest(request); |
| 86 | var response = await importer.ImportAsync(mapped, cancellationToken).ConfigureAwait(false); |
| 87 | return Results.Ok(response); |
| 88 | } |
| 89 | } |
| 90 | |