|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 7 | + "github.com/go-viper/mapstructure/v2" |
| 8 | +) |
| 9 | + |
| 10 | +type ListNotificationsInput struct { |
| 11 | + Filter *string `json:"filter,omitempty"` |
| 12 | + Since *string `json:"since,omitempty"` |
| 13 | + Before *string `json:"before,omitempty"` |
| 14 | + Owner *string `json:"owner,omitempty"` |
| 15 | + Repo *string `json:"repo,omitempty"` |
| 16 | + Page *int `json:"page,omitempty"` |
| 17 | + PerPage *int `json:"perPage,omitempty"` |
| 18 | + After *string `json:"after,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +type DismissNotificationInput struct { |
| 22 | + ThreadID string `json:"threadID"` |
| 23 | + State string `json:"state"` |
| 24 | +} |
| 25 | + |
| 26 | +type MarkAllNotificationsReadInput struct { |
| 27 | + LastReadAt *string `json:"lastReadAt,omitempty"` |
| 28 | + Owner *string `json:"owner,omitempty"` |
| 29 | + Repo *string `json:"repo,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +type GetNotificationDetailsInput struct { |
| 33 | + NotificationID string `json:"notificationID"` |
| 34 | +} |
| 35 | + |
| 36 | +type ManageNotificationSubscriptionInput struct { |
| 37 | + NotificationID string `json:"notificationID"` |
| 38 | + Action string `json:"action"` |
| 39 | +} |
| 40 | + |
| 41 | +type ManageRepositoryNotificationSubscriptionInput struct { |
| 42 | + Owner string `json:"owner"` |
| 43 | + Repo string `json:"repo"` |
| 44 | + Action string `json:"action"` |
| 45 | +} |
| 46 | + |
| 47 | +type ListDiscussionsInput struct { |
| 48 | + Owner string `json:"owner"` |
| 49 | + Repo *string `json:"repo,omitempty"` |
| 50 | + Category *string `json:"category,omitempty"` |
| 51 | + OrderBy *string `json:"orderBy,omitempty"` |
| 52 | + Direction *string `json:"direction,omitempty"` |
| 53 | + PerPage *int `json:"perPage,omitempty"` |
| 54 | + After *string `json:"after,omitempty"` |
| 55 | +} |
| 56 | + |
| 57 | +type GetDiscussionInput struct { |
| 58 | + Owner string `json:"owner"` |
| 59 | + Repo string `json:"repo"` |
| 60 | + DiscussionNumber int32 `json:"discussionNumber"` |
| 61 | +} |
| 62 | + |
| 63 | +type GetDiscussionCommentsInput struct { |
| 64 | + Owner string `json:"owner"` |
| 65 | + Repo string `json:"repo"` |
| 66 | + DiscussionNumber int32 `json:"discussionNumber"` |
| 67 | + IncludeReplies *bool `json:"includeReplies,omitempty"` |
| 68 | + PerPage *int `json:"perPage,omitempty"` |
| 69 | + After *string `json:"after,omitempty"` |
| 70 | +} |
| 71 | + |
| 72 | +type DiscussionCommentWriteInput struct { |
| 73 | + Method string `json:"method"` |
| 74 | + Owner *string `json:"owner,omitempty"` |
| 75 | + Repo *string `json:"repo,omitempty"` |
| 76 | + DiscussionNumber *int `json:"discussionNumber,omitempty"` |
| 77 | + Body *string `json:"body,omitempty"` |
| 78 | + CommentNodeID *string `json:"commentNodeID,omitempty"` |
| 79 | +} |
| 80 | + |
| 81 | +type ListDiscussionCategoriesInput struct { |
| 82 | + Owner string `json:"owner"` |
| 83 | + Repo *string `json:"repo,omitempty"` |
| 84 | +} |
| 85 | + |
| 86 | +// Preserve presence and the existing parameter validators at the compatibility |
| 87 | +// boundary. In particular, an explicit perPage: 0 differs from an omitted value |
| 88 | +// for discussion comments, and mutation parameters are required per method. |
| 89 | +func discussionNotificationArguments(input any) (map[string]any, error) { |
| 90 | + raw, err := json.Marshal(input) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + var args map[string]any |
| 95 | + err = json.Unmarshal(raw, &args) |
| 96 | + return args, err |
| 97 | +} |
| 98 | + |
| 99 | +func normalizeDiscussionReadArguments(raw json.RawMessage) (json.RawMessage, error) { |
| 100 | + var args map[string]any |
| 101 | + if err := json.Unmarshal(raw, &args); err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + // These two tools historically used WeakDecode: retain its case-insensitive |
| 105 | + // field matching, numeric-string conversion and fractional truncation. |
| 106 | + var input GetDiscussionInput |
| 107 | + if err := mapstructure.WeakDecode(args, &input); err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + args["owner"] = input.Owner |
| 111 | + args["repo"] = input.Repo |
| 112 | + args["discussionNumber"] = input.DiscussionNumber |
| 113 | + return json.Marshal(args) |
| 114 | +} |
| 115 | + |
| 116 | +func normalizeDiscussionNotificationIntegers(fields ...string) inventory.InputNormalizer { |
| 117 | + return func(raw json.RawMessage) (json.RawMessage, error) { |
| 118 | + var args map[string]any |
| 119 | + if err := json.Unmarshal(raw, &args); err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + for _, field := range fields { |
| 124 | + if _, ok := args[field]; !ok { |
| 125 | + continue |
| 126 | + } |
| 127 | + value, err := OptionalIntParam(args, field) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + if value == 0 { |
| 132 | + switch field { |
| 133 | + case "perPage": |
| 134 | + value = 30 |
| 135 | + case "page": |
| 136 | + value = 1 |
| 137 | + } |
| 138 | + } |
| 139 | + args[field] = value |
| 140 | + } |
| 141 | + return json.Marshal(args) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func normalizeDiscussionCommentWriteArguments(raw json.RawMessage) (json.RawMessage, error) { |
| 146 | + var args map[string]any |
| 147 | + if err := json.Unmarshal(raw, &args); err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + method, _ := args["method"].(string) |
| 151 | + used := map[string]bool{"method": true} |
| 152 | + switch method { |
| 153 | + case "add", "reply": |
| 154 | + used["owner"], used["repo"], used["discussionNumber"], used["body"] = true, true, true, true |
| 155 | + if method == "reply" { |
| 156 | + used["commentNodeID"] = true |
| 157 | + } |
| 158 | + case "update": |
| 159 | + used["commentNodeID"], used["body"] = true, true |
| 160 | + case "delete", "mark_answer", "unmark_answer": |
| 161 | + used["commentNodeID"] = true |
| 162 | + } |
| 163 | + // Unused method-specific parameters were never decoded by the legacy |
| 164 | + // handler, even when their values did not match the advertised schema. |
| 165 | + for _, field := range []string{"owner", "repo", "discussionNumber", "body", "commentNodeID"} { |
| 166 | + if !used[field] { |
| 167 | + delete(args, field) |
| 168 | + } |
| 169 | + } |
| 170 | + normalized, err := json.Marshal(args) |
| 171 | + if err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + return normalizeDiscussionNotificationIntegers("discussionNumber")(normalized) |
| 175 | +} |
0 commit comments