| 1 | using AgentForge.Plugin.Sdk; |
| 2 | using AgentForge.Contracts; |
| 3 | using AgentForge.Data; |
| 4 | using AgentForge.Models; |
| 5 | using AgentForge.Services; |
| 6 | |
| 7 | namespace AgentForge.Endpoints; |
| 8 | |
| 9 | internal static class ForgeCiEndpoints |
| 10 | { |
| 11 | internal static RouteGroupBuilder MapCiEndpoints(this RouteGroupBuilder api) |
| 12 | { |
| 13 | api.MapPost("/ci/callback", CiCallback); |
| 14 | return api; |
| 15 | } |
| 16 | |
| 17 | private static IResult CiCallback( |
| 18 | CiCallbackRequest request, |
| 19 | HttpContext context, |
| 20 | ForgeRepository repository, |
| 21 | ForgeAuthorizationService authorization) |
| 22 | { |
| 23 | authorization.EnsureCiCallback(ForgePluginHttpAuth.GetBearerToken(context)); |
| 24 | if (!CiRunStatusExtensions.TryParseSlug(request.Status, out var status)) |
| 25 | throw new ForgeApiException(400, "CI status must be pending, success, failure, or cancelled."); |
| 26 | |
| 27 | var repo = ForgePluginEndpoints.RequireRepo(repository, request.Repo); |
| 28 | var run = repository.UpsertCiRun( |
| 29 | repo.Id, |
| 30 | request.Mr, |
| 31 | request.Commit?.Trim() ?? string.Empty, |
| 32 | status, |
| 33 | request.Url?.Trim() ?? string.Empty); |
| 34 | return Results.Ok(ForgePluginResponses.ToCiStatusResponse(run)); |
| 35 | } |
| 36 | } |
| 37 | |