|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "dev.gaijin.team/go/golib/e" |
| 8 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 9 | + |
| 10 | + "dev.gaijin.team/go/go-gerrit-mcp/internal/gerritclient" |
| 11 | + "dev.gaijin.team/go/go-gerrit-mcp/internal/llmxml" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + errUnknownAction = e.New("unknown action, expected submit, abandon, restore, wip, or ready") |
| 16 | + errSubmitMessage = e.New("submit does not accept a message; vote or comment separately") |
| 17 | +) |
| 18 | + |
| 19 | +type transitionChangeInput struct { |
| 20 | + Change string `json:"change" jsonschema:"Change identifier: numeric ID, project~number, or Change-Id"` |
| 21 | + Action string `json:"action" jsonschema:"One of: submit, abandon, restore, wip, ready"` |
| 22 | + Message string `json:"message,omitempty" jsonschema:"Optional message; not accepted for submit"` |
| 23 | +} |
| 24 | + |
| 25 | +func transitionChange(c *gerritclient.Client) Tool { |
| 26 | + return Tool{ |
| 27 | + Name: NameTransitionChange, |
| 28 | + Register: func(s *mcp.Server) { |
| 29 | + mcp.AddTool(s, &mcp.Tool{ |
| 30 | + Name: NameTransitionChange, |
| 31 | + Description: "Transition a Gerrit change's state. Actions and the states they move " + |
| 32 | + "between: submit (NEW -> MERGED, requires satisfied submit requirements), " + |
| 33 | + "abandon (NEW -> ABANDONED), restore (ABANDONED -> NEW), wip (marks NEW change " + |
| 34 | + "work-in-progress), ready (work-in-progress -> ready for review). An optional " + |
| 35 | + "message accompanies every action except submit. Gerrit's refusal (blocked " + |
| 36 | + "submit, restore of a merged change) is reported verbatim. Refused on changes " + |
| 37 | + "not owned by the authenticated account unless the operator disabled the " + |
| 38 | + "own-changes restriction.", |
| 39 | + }, func(ctx context.Context, _ *mcp.CallToolRequest, in transitionChangeInput, |
| 40 | + ) (*mcp.CallToolResult, any, error) { |
| 41 | + action := strings.ToLower(strings.TrimSpace(in.Action)) |
| 42 | + |
| 43 | + status, err := runTransition(ctx, c, action, in) |
| 44 | + if err != nil { |
| 45 | + return nil, nil, err |
| 46 | + } |
| 47 | + |
| 48 | + attrs := []llmxml.Attribute{ |
| 49 | + llmxml.Attr("change", in.Change), |
| 50 | + llmxml.Attr("action", action), |
| 51 | + } |
| 52 | + if status != "" { |
| 53 | + attrs = append(attrs, llmxml.Attr("status", status)) |
| 54 | + } |
| 55 | + |
| 56 | + return textResult(llmxml.NewElement("change_transitioned", attrs...).String()), nil, nil |
| 57 | + }) |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// runTransition maps the action enum onto the client operation and reports |
| 63 | +// the change status when the endpoint returns one. |
| 64 | +func runTransition(ctx context.Context, c *gerritclient.Client, action string, in transitionChangeInput, |
| 65 | +) (string, error) { |
| 66 | + switch action { |
| 67 | + case "submit": |
| 68 | + if strings.TrimSpace(in.Message) != "" { |
| 69 | + return "", errSubmitMessage |
| 70 | + } |
| 71 | + |
| 72 | + info, err := c.SubmitChange(ctx, in.Change) |
| 73 | + if err != nil { |
| 74 | + return "", err |
| 75 | + } |
| 76 | + |
| 77 | + return info.Status, nil |
| 78 | + |
| 79 | + case "abandon": |
| 80 | + info, err := c.AbandonChange(ctx, in.Change, in.Message) |
| 81 | + if err != nil { |
| 82 | + return "", err |
| 83 | + } |
| 84 | + |
| 85 | + return info.Status, nil |
| 86 | + |
| 87 | + case "restore": |
| 88 | + info, err := c.RestoreChange(ctx, in.Change, in.Message) |
| 89 | + if err != nil { |
| 90 | + return "", err |
| 91 | + } |
| 92 | + |
| 93 | + return info.Status, nil |
| 94 | + |
| 95 | + case "wip": |
| 96 | + return "", c.SetWorkInProgress(ctx, in.Change, in.Message) |
| 97 | + case "ready": |
| 98 | + return "", c.SetReadyForReview(ctx, in.Change, in.Message) |
| 99 | + default: |
| 100 | + return "", errUnknownAction.WithField("action", in.Action) |
| 101 | + } |
| 102 | +} |
0 commit comments