| 1 | using AgentForge.Contracts; |
| 2 | using AgentForge.Data; |
| 3 | using AgentForge.Data.Entities; |
| 4 | using AgentForge.Models; |
| 5 | using AgentForge.Services; |
| 6 | |
| 7 | namespace AgentForge.Plugin.Sdk; |
| 8 | |
| 9 | public static class ForgePluginResponses |
| 10 | { |
| 11 | public static RepoResponse ToRepoResponse(ForgeRepoEntity repo, GitRepoService git, ForgeRepository? repository = null) => |
| 12 | new( |
| 13 | repo.Id, |
| 14 | repo.Name, |
| 15 | repo.Description, |
| 16 | repo.DriveMode.ToSlug(), |
| 17 | git.BuildCloneUrl(repo.Name), |
| 18 | repo.CreatedAt, |
| 19 | ResolveGroupSlug(repo, repository), |
| 20 | ResolveOrgSlug(repo, repository), |
| 21 | repo.Visibility.ToSlug()); |
| 22 | |
| 23 | private static string? ResolveOrgSlug(ForgeRepoEntity repo, ForgeRepository? repository) |
| 24 | { |
| 25 | if (repo.OrgId is null || repository is null) |
| 26 | return null; |
| 27 | return repository.GetOrgById(repo.OrgId)?.Slug; |
| 28 | } |
| 29 | |
| 30 | private static string? ResolveGroupSlug(ForgeRepoEntity repo, ForgeRepository? repository) |
| 31 | { |
| 32 | if (repo.GroupId is null || repository is null) |
| 33 | return null; |
| 34 | return repository.GetRepoGroupCatalogPath(repo.GroupId); |
| 35 | } |
| 36 | |
| 37 | public static IssueResponse ToIssueResponse(ForgeIssueEntity issue, string repoName, ForgeUrls urls) => |
| 38 | new( |
| 39 | issue.Number, |
| 40 | issue.Title, |
| 41 | issue.Body, |
| 42 | issue.Status.ToSlug(), |
| 43 | issue.CreatedAt, |
| 44 | urls.Issue(repoName, issue.Number), |
| 45 | issue.Anchors, |
| 46 | FormatAnchorBrackets(issue.Anchors), |
| 47 | ForgeArtifactBracket.FormatIssue(repoName, issue.Number), |
| 48 | string.IsNullOrWhiteSpace(issue.CreatedBy) ? null : issue.CreatedBy, |
| 49 | BuildForgeLensUrl(urls, repoName, issue.Number, issue.Anchors)); |
| 50 | |
| 51 | public static IssueDetailResponse ToIssueDetailResponse( |
| 52 | ForgeIssueEntity issue, |
| 53 | string repoName, |
| 54 | ForgeUrls urls, |
| 55 | IReadOnlyList<IssueCommentResponse> comments) => |
| 56 | new( |
| 57 | issue.Number, |
| 58 | issue.Title, |
| 59 | issue.Body, |
| 60 | issue.Status.ToSlug(), |
| 61 | issue.CreatedAt, |
| 62 | urls.Issue(repoName, issue.Number), |
| 63 | issue.Anchors, |
| 64 | comments, |
| 65 | FormatAnchorBrackets(issue.Anchors), |
| 66 | ForgeArtifactBracket.FormatIssue(repoName, issue.Number), |
| 67 | string.IsNullOrWhiteSpace(issue.CreatedBy) ? null : issue.CreatedBy, |
| 68 | BuildForgeLensUrl(urls, repoName, issue.Number, issue.Anchors)); |
| 69 | |
| 70 | public static IssueCommentResponse ToCommentResponse(ForgeIssueCommentEntity comment) => |
| 71 | new(comment.Id, comment.Body, comment.Author, comment.CreatedAt); |
| 72 | |
| 73 | public static IssueCommentResponse ToCommentResponse(ForgeMergeRequestCommentEntity comment) => |
| 74 | new(comment.Id, comment.Body, comment.Author, comment.CreatedAt); |
| 75 | |
| 76 | public static MergeRequestResponse ToMergeRequestResponse( |
| 77 | ForgeMergeRequestEntity mergeRequest, |
| 78 | string repoName, |
| 79 | ForgeUrls urls) => |
| 80 | new( |
| 81 | mergeRequest.Number, |
| 82 | mergeRequest.Title, |
| 83 | mergeRequest.SourceBranch, |
| 84 | mergeRequest.TargetBranch, |
| 85 | mergeRequest.Status.ToSlug(), |
| 86 | mergeRequest.CreatedAt, |
| 87 | urls.MergeRequest(repoName, mergeRequest.Number), |
| 88 | mergeRequest.Anchors, |
| 89 | FormatAnchorBrackets(mergeRequest.Anchors), |
| 90 | ForgeArtifactBracket.FormatMergeRequest(repoName, mergeRequest.Number), |
| 91 | string.IsNullOrWhiteSpace(mergeRequest.CreatedBy) ? null : mergeRequest.CreatedBy); |
| 92 | |
| 93 | public static MergeRequestDetailResponse ToMergeRequestDetailResponse( |
| 94 | ForgeMergeRequestEntity mergeRequest, |
| 95 | string repoName, |
| 96 | ForgeUrls urls, |
| 97 | IReadOnlyList<IssueCommentResponse> comments) => |
| 98 | new( |
| 99 | mergeRequest.Number, |
| 100 | mergeRequest.Title, |
| 101 | mergeRequest.SourceBranch, |
| 102 | mergeRequest.TargetBranch, |
| 103 | mergeRequest.Status.ToSlug(), |
| 104 | mergeRequest.CreatedAt, |
| 105 | urls.MergeRequest(repoName, mergeRequest.Number), |
| 106 | mergeRequest.Anchors, |
| 107 | FormatAnchorBrackets(mergeRequest.Anchors), |
| 108 | ForgeArtifactBracket.FormatMergeRequest(repoName, mergeRequest.Number), |
| 109 | string.IsNullOrWhiteSpace(mergeRequest.CreatedBy) ? null : mergeRequest.CreatedBy, |
| 110 | comments); |
| 111 | |
| 112 | public static CiStatusResponse ToCiStatusResponse(ForgeCiRunEntity? run) => |
| 113 | run is null |
| 114 | ? new CiStatusResponse(CiRunStatus.Unknown.ToSlug(), null, null, null) |
| 115 | : new CiStatusResponse(run.Status.ToSlug(), run.Commit, run.Url, run.UpdatedAt); |
| 116 | |
| 117 | private static IReadOnlyList<string> FormatAnchorBrackets(IReadOnlyList<CodeAnchor> anchors) => |
| 118 | anchors.Select(BracketCodeReferenceFormatter.Format).ToList(); |
| 119 | |
| 120 | private static string? BuildForgeLensUrl(ForgeUrls urls, string repoName, int number, IReadOnlyList<CodeAnchor> anchors) |
| 121 | { |
| 122 | if (anchors.Count == 0) |
| 123 | return null; |
| 124 | |
| 125 | return ForgeLinkBuilder.LensMagicLink(urls.PublicBaseUrl, repoName, number, anchors[0]); |
| 126 | } |
| 127 | } |
| 128 | |