| 1 | using AgentForge.Abstractions; |
| 2 | using AgentForge.Contracts; |
| 3 | using AgentForge.Data; |
| 4 | using AgentForge.Models; |
| 5 | using AgentForge.Plugin.Sdk; |
| 6 | using AgentForge.Services; |
| 7 | using Microsoft.AspNetCore.Builder; |
| 8 | using Microsoft.AspNetCore.Http; |
| 9 | using Microsoft.AspNetCore.Routing; |
| 10 | |
| 11 | namespace AgentForge.Plugin.Lens.Endpoints; |
| 12 | |
| 13 | internal static class ForgeLensEndpoints |
| 14 | { |
| 15 | internal static RouteGroupBuilder MapLensEndpoints(this RouteGroupBuilder api) |
| 16 | { |
| 17 | api.MapGet($"/repos/{ForgeRepoRouting.Pattern}/lens", GetForgeLens); |
| 18 | return api; |
| 19 | } |
| 20 | |
| 21 | private static IResult GetForgeLens( |
| 22 | string name, |
| 23 | string? file, |
| 24 | HttpContext http, |
| 25 | ForgeRepository repository, |
| 26 | ForgeUrls urls) |
| 27 | { |
| 28 | var repo = ForgePluginEndpoints.RequireRepo(repository, name, http); |
| 29 | var normalizedFile = string.IsNullOrWhiteSpace(file) ? null : file.Trim(); |
| 30 | |
| 31 | var lensIssues = repository.ListIssues(repo.Id) |
| 32 | .Where(issue => issue.Anchors.Count > 0) |
| 33 | .Where(issue => normalizedFile is null || issue.Anchors.Any(a => CodeAnchorJson.MatchesFile(a, normalizedFile))) |
| 34 | .Select(issue => new ForgeLensIssueItem( |
| 35 | issue.Number, |
| 36 | issue.Title, |
| 37 | issue.Status.ToSlug(), |
| 38 | urls.Issue(repo.Name, issue.Number), |
| 39 | issue.Anchors)) |
| 40 | .ToList(); |
| 41 | |
| 42 | var lensMrs = repository.ListMergeRequests(repo.Id) |
| 43 | .Where(mr => mr.Anchors.Count > 0) |
| 44 | .Where(mr => normalizedFile is null || mr.Anchors.Any(a => CodeAnchorJson.MatchesFile(a, normalizedFile))) |
| 45 | .Select(mr => new ForgeLensMergeRequestItem( |
| 46 | mr.Number, |
| 47 | mr.Title, |
| 48 | mr.Status.ToSlug(), |
| 49 | urls.MergeRequest(repo.Name, mr.Number), |
| 50 | mr.Anchors)) |
| 51 | .ToList(); |
| 52 | |
| 53 | return Results.Ok(new ForgeLensResponse(repo.Name, normalizedFile, lensIssues, lensMrs)); |
| 54 | } |
| 55 | } |
| 56 | |