|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "dev.gaijin.team/go/golib/e" |
| 10 | + gerrit "github.com/andygrunwald/go-gerrit" |
| 11 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 12 | + |
| 13 | + "dev.gaijin.team/go/go-gerrit-mcp/internal/gerritclient" |
| 14 | + "dev.gaijin.team/go/go-gerrit-mcp/internal/llmxml" |
| 15 | +) |
| 16 | + |
| 17 | +// Thread status filter values. |
| 18 | +const ( |
| 19 | + statusAll = "all" |
| 20 | + statusResolved = "resolved" |
| 21 | + statusUnresolved = "unresolved" |
| 22 | +) |
| 23 | + |
| 24 | +var errInvalidStatus = e.New("invalid status filter, expected all, resolved, or unresolved") |
| 25 | + |
| 26 | +type getChangeCommentsInput struct { |
| 27 | + Change string `json:"change" jsonschema:"Change identifier: numeric ID, project~number, or Change-Id"` |
| 28 | + Status string `json:"status,omitempty" jsonschema:"Thread filter: all, resolved, or unresolved; default all"` |
| 29 | +} |
| 30 | + |
| 31 | +func getChangeComments(c *gerritclient.Client) Tool { |
| 32 | + return Tool{ |
| 33 | + Name: NameGetChangeComments, |
| 34 | + Register: func(s *mcp.Server) { |
| 35 | + mcp.AddTool(s, &mcp.Tool{ |
| 36 | + Name: NameGetChangeComments, |
| 37 | + Description: "List the published review comments of a Gerrit change, grouped by file " + |
| 38 | + "and reconstructed into threads with their resolved state. Comment ids are the " + |
| 39 | + "reply anchors for posting follow-ups.", |
| 40 | + }, func(ctx context.Context, _ *mcp.CallToolRequest, in getChangeCommentsInput, |
| 41 | + ) (*mcp.CallToolResult, any, error) { |
| 42 | + if in.Status == "" { |
| 43 | + in.Status = statusAll |
| 44 | + } |
| 45 | + |
| 46 | + if in.Status != statusAll && in.Status != statusResolved && in.Status != statusUnresolved { |
| 47 | + return nil, nil, errInvalidStatus.WithField("status", in.Status) |
| 48 | + } |
| 49 | + |
| 50 | + comments, err := c.ListChangeComments(ctx, in.Change) |
| 51 | + if err != nil { |
| 52 | + return nil, nil, err |
| 53 | + } |
| 54 | + |
| 55 | + return textResult(renderComments(in, comments)), nil, nil |
| 56 | + }) |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// thread is one comment thread: the root comment and its replies in |
| 62 | +// chronological order. Its resolved state is the state of the last comment. |
| 63 | +type thread struct { |
| 64 | + comments []gerrit.CommentInfo |
| 65 | + unresolved bool |
| 66 | +} |
| 67 | + |
| 68 | +// buildThreads reconstructs threads from a flat comment list. Comments are |
| 69 | +// ordered chronologically; each reply chain is walked to its root, and a |
| 70 | +// reply whose parent is missing starts its own thread. |
| 71 | +func buildThreads(comments []gerrit.CommentInfo) []thread { |
| 72 | + ordered := make([]gerrit.CommentInfo, len(comments)) |
| 73 | + copy(ordered, comments) |
| 74 | + |
| 75 | + slices.SortStableFunc(ordered, compareUpdated) |
| 76 | + |
| 77 | + parent := make(map[string]string, len(ordered)) |
| 78 | + for _, comment := range ordered { |
| 79 | + parent[comment.ID] = comment.InReplyTo |
| 80 | + } |
| 81 | + |
| 82 | + var ( |
| 83 | + roots []string |
| 84 | + grouped = map[string][]gerrit.CommentInfo{} |
| 85 | + ) |
| 86 | + |
| 87 | + for _, comment := range ordered { |
| 88 | + root := rootOf(parent, comment.ID) |
| 89 | + if _, ok := grouped[root]; !ok { |
| 90 | + roots = append(roots, root) |
| 91 | + } |
| 92 | + |
| 93 | + grouped[root] = append(grouped[root], comment) |
| 94 | + } |
| 95 | + |
| 96 | + threads := make([]thread, 0, len(roots)) |
| 97 | + |
| 98 | + for _, root := range roots { |
| 99 | + group := grouped[root] |
| 100 | + last := group[len(group)-1] |
| 101 | + |
| 102 | + threads = append(threads, thread{ |
| 103 | + comments: group, |
| 104 | + unresolved: last.Unresolved != nil && *last.Unresolved, |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + return threads |
| 109 | +} |
| 110 | + |
| 111 | +func compareUpdated(a, b gerrit.CommentInfo) int { |
| 112 | + switch { |
| 113 | + case a.Updated == nil || b.Updated == nil: |
| 114 | + return 0 |
| 115 | + case a.Updated.Before(b.Updated.Time): |
| 116 | + return -1 |
| 117 | + case b.Updated.Before(a.Updated.Time): |
| 118 | + return 1 |
| 119 | + default: |
| 120 | + return 0 |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// rootOf walks a reply chain to its root; the iteration cap guards against |
| 125 | +// reference cycles in malformed data. |
| 126 | +func rootOf(parent map[string]string, id string) string { |
| 127 | + for range len(parent) { |
| 128 | + up, ok := parent[id] |
| 129 | + if !ok || up == "" { |
| 130 | + return id |
| 131 | + } |
| 132 | + |
| 133 | + id = up |
| 134 | + } |
| 135 | + |
| 136 | + return id |
| 137 | +} |
| 138 | + |
| 139 | +func matchesStatus(t thread, status string) bool { |
| 140 | + switch status { |
| 141 | + case statusResolved: |
| 142 | + return !t.unresolved |
| 143 | + case statusUnresolved: |
| 144 | + return t.unresolved |
| 145 | + default: |
| 146 | + return true |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func renderComments(in getChangeCommentsInput, byFile map[string][]gerrit.CommentInfo) string { |
| 151 | + paths := make([]string, 0, len(byFile)) |
| 152 | + for path := range byFile { |
| 153 | + paths = append(paths, path) |
| 154 | + } |
| 155 | + |
| 156 | + slices.Sort(paths) |
| 157 | + |
| 158 | + var ( |
| 159 | + rendered []string |
| 160 | + threadCount int |
| 161 | + ) |
| 162 | + |
| 163 | + for _, path := range paths { |
| 164 | + threads := buildThreads(byFile[path]) |
| 165 | + |
| 166 | + var fileThreads []string |
| 167 | + |
| 168 | + for _, t := range threads { |
| 169 | + if !matchesStatus(t, in.Status) { |
| 170 | + continue |
| 171 | + } |
| 172 | + |
| 173 | + threadCount++ |
| 174 | + |
| 175 | + fileThreads = append(fileThreads, renderThread(t)) |
| 176 | + } |
| 177 | + |
| 178 | + if len(fileThreads) == 0 { |
| 179 | + continue |
| 180 | + } |
| 181 | + |
| 182 | + rendered = append(rendered, llmxml.NewElement("file", llmxml.Attr("path", path)). |
| 183 | + WrapText(strings.Join(fileThreads, "\n")).String()) |
| 184 | + } |
| 185 | + |
| 186 | + root := llmxml.NewElement("comments", |
| 187 | + llmxml.Attr("change", in.Change), |
| 188 | + llmxml.Attr("filter", in.Status), |
| 189 | + llmxml.Attr("threads", threadCount), |
| 190 | + ) |
| 191 | + |
| 192 | + if len(rendered) == 0 { |
| 193 | + return root.String() |
| 194 | + } |
| 195 | + |
| 196 | + return root.WrapText(strings.Join(rendered, "\n")).String() |
| 197 | +} |
| 198 | + |
| 199 | +func renderThread(t thread) string { |
| 200 | + rendered := make([]string, 0, len(t.comments)) |
| 201 | + |
| 202 | + for _, comment := range t.comments { |
| 203 | + el := llmxml.NewElement("comment", |
| 204 | + llmxml.Attr("id", comment.ID), |
| 205 | + llmxml.Attr("author", accountLabel(comment.Author)), |
| 206 | + ) |
| 207 | + |
| 208 | + if comment.Updated != nil { |
| 209 | + el.Attr(llmxml.Attr("date", timestamp(*comment.Updated))) |
| 210 | + } |
| 211 | + |
| 212 | + if comment.PatchSet > 0 { |
| 213 | + el.Attr(llmxml.Attr("patch_set", comment.PatchSet)) |
| 214 | + } |
| 215 | + |
| 216 | + if comment.Range != nil { |
| 217 | + el.Attr(llmxml.Attr("lines", rangeLabel(comment.Range))) |
| 218 | + } else if comment.Line > 0 { |
| 219 | + el.Attr(llmxml.Attr("line", comment.Line)) |
| 220 | + } |
| 221 | + |
| 222 | + if comment.InReplyTo != "" { |
| 223 | + el.Attr(llmxml.Attr("in_reply_to", comment.InReplyTo)) |
| 224 | + } |
| 225 | + |
| 226 | + rendered = append(rendered, el.WrapText(comment.Message).String()) |
| 227 | + } |
| 228 | + |
| 229 | + return llmxml.NewElement("thread", llmxml.Attr("resolved", !t.unresolved)). |
| 230 | + WrapText(strings.Join(rendered, "\n")).String() |
| 231 | +} |
| 232 | + |
| 233 | +func rangeLabel(r *gerrit.CommentRange) string { |
| 234 | + return fmt.Sprintf("%d-%d", r.StartLine, r.EndLine) |
| 235 | +} |
0 commit comments