-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathissue_fields.go
More file actions
239 lines (224 loc) · 8.71 KB
/
issue_fields.go
File metadata and controls
239 lines (224 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package github
import (
"context"
"encoding/json"
"fmt"
ghcontext "github.com/github/github-mcp-server/pkg/context"
ghErrors "github.com/github/github-mcp-server/pkg/errors"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/scopes"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/shurcooL/githubv4"
)
// IssueField represents a repository issue field definition.
type IssueField struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
DataType string `json:"data_type"`
Visibility string `json:"visibility"`
Options []IssueSingleSelectFieldOption `json:"options,omitempty"`
}
// IssueSingleSelectFieldOption represents an option for a single_select issue field.
type IssueSingleSelectFieldOption struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Color string `json:"color"`
Priority *int `json:"priority,omitempty"`
}
// issueFieldNode is the GraphQL fragment for a single issue field in the IssueFields union.
// Only the fragment matching __typename is populated; read from the matching fragment.
type issueFieldNode struct {
TypeName githubv4.String `graphql:"__typename"`
IssueFieldText struct {
ID githubv4.ID
Name githubv4.String
Description githubv4.String
DataType githubv4.String
Visibility githubv4.String
} `graphql:"... on IssueFieldText"`
IssueFieldNumber struct {
ID githubv4.ID
Name githubv4.String
Description githubv4.String
DataType githubv4.String
Visibility githubv4.String
} `graphql:"... on IssueFieldNumber"`
IssueFieldDate struct {
ID githubv4.ID
Name githubv4.String
Description githubv4.String
DataType githubv4.String
Visibility githubv4.String
} `graphql:"... on IssueFieldDate"`
IssueFieldSingleSelect struct {
ID githubv4.ID
Name githubv4.String
Description githubv4.String
DataType githubv4.String
Visibility githubv4.String
Options []struct {
ID githubv4.ID
Name githubv4.String
Description githubv4.String
Color githubv4.String
Priority *int
}
} `graphql:"... on IssueFieldSingleSelect"`
}
// issueFieldsRepoQuery is the GraphQL query for listing issue fields on a repository.
type issueFieldsRepoQuery struct {
Repository struct {
IssueFields struct {
Nodes []issueFieldNode
} `graphql:"issueFields(first: 100)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
// issueFieldsOrgQuery is the GraphQL query for listing issue fields on an organization.
type issueFieldsOrgQuery struct {
Organization struct {
IssueFields struct {
Nodes []issueFieldNode
} `graphql:"issueFields(first: 100)"`
} `graphql:"organization(login: $login)"`
}
// ListIssueFields creates a tool to list issue field definitions for a repository or organization.
// Gated by FeatureFlagIssueFields: the tool is only registered when the flag is on.
func ListIssueFields(t translations.TranslationHelperFunc) inventory.ServerTool {
st := NewTool(
ToolsetMetadataIssues,
mcp.Tool{
Name: "list_issue_fields",
Description: t("TOOL_LIST_ISSUE_FIELDS_DESCRIPTION", "List issue fields for a repository or organization. Returns field definitions including name, type (text, number, date, single_select), and for single_select fields the list of valid option names. When repo is omitted, returns org-level fields directly."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_LIST_ISSUE_FIELDS_USER_TITLE", "List issue fields"),
ReadOnlyHint: true,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {
Type: "string",
Description: "The account owner of the repository or organization. The name is not case sensitive.",
},
"repo": {
Type: "string",
Description: "The name of the repository. When provided, returns fields for this specific repository (inherited from its organization). When omitted, returns org-level fields directly.",
},
},
Required: []string{"owner"},
},
},
[]scopes.Scope{scopes.Repo, scopes.ReadOrg},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := OptionalParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
gqlClient, err := deps.GetGQLClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub GraphQL client", err), nil, nil
}
fields, err := fetchIssueFields(ctx, gqlClient, owner, repo)
if err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "failed to list issue fields", err), nil, nil
}
r, err := json.Marshal(fields)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal issue fields", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
})
st.FeatureFlagEnable = FeatureFlagIssueFields
return st
}
// fetchIssueFields returns the issue field definitions for the given owner.
// If repo is provided, fields are scoped to that repository (inherited from its
// organization); otherwise fields are returned directly from the organization.
func fetchIssueFields(ctx context.Context, gqlClient *githubv4.Client, owner, repo string) ([]IssueField, error) {
ctxWithFeatures := ghcontext.WithGraphQLFeatures(ctx, "issue_fields", "repo_issue_fields")
if repo != "" {
var query issueFieldsRepoQuery
vars := map[string]any{
"owner": githubv4.String(owner),
"name": githubv4.String(repo),
}
if err := gqlClient.Query(ctxWithFeatures, &query, vars); err != nil {
return nil, err
}
return issueFieldsFromNodes(query.Repository.IssueFields.Nodes), nil
}
var query issueFieldsOrgQuery
vars := map[string]any{
"login": githubv4.String(owner),
}
if err := gqlClient.Query(ctxWithFeatures, &query, vars); err != nil {
return nil, err
}
return issueFieldsFromNodes(query.Organization.IssueFields.Nodes), nil
}
// issueFieldsFromNodes converts GraphQL issue field union nodes into IssueField values.
// Read from the fragment matching __typename; the other fragments are zero-valued.
func issueFieldsFromNodes(nodes []issueFieldNode) []IssueField {
fields := make([]IssueField, 0, len(nodes))
for _, node := range nodes {
var f IssueField
switch string(node.TypeName) {
case "IssueFieldSingleSelect":
opts := make([]IssueSingleSelectFieldOption, 0, len(node.IssueFieldSingleSelect.Options))
for _, o := range node.IssueFieldSingleSelect.Options {
opts = append(opts, IssueSingleSelectFieldOption{
ID: fmt.Sprintf("%v", o.ID),
Name: string(o.Name),
Description: string(o.Description),
Color: string(o.Color),
Priority: o.Priority,
})
}
f = IssueField{
ID: fmt.Sprintf("%v", node.IssueFieldSingleSelect.ID),
Name: string(node.IssueFieldSingleSelect.Name),
Description: string(node.IssueFieldSingleSelect.Description),
DataType: string(node.IssueFieldSingleSelect.DataType),
Visibility: string(node.IssueFieldSingleSelect.Visibility),
Options: opts,
}
case "IssueFieldText":
f = IssueField{
ID: fmt.Sprintf("%v", node.IssueFieldText.ID),
Name: string(node.IssueFieldText.Name),
Description: string(node.IssueFieldText.Description),
DataType: string(node.IssueFieldText.DataType),
Visibility: string(node.IssueFieldText.Visibility),
}
case "IssueFieldNumber":
f = IssueField{
ID: fmt.Sprintf("%v", node.IssueFieldNumber.ID),
Name: string(node.IssueFieldNumber.Name),
Description: string(node.IssueFieldNumber.Description),
DataType: string(node.IssueFieldNumber.DataType),
Visibility: string(node.IssueFieldNumber.Visibility),
}
case "IssueFieldDate":
f = IssueField{
ID: fmt.Sprintf("%v", node.IssueFieldDate.ID),
Name: string(node.IssueFieldDate.Name),
Description: string(node.IssueFieldDate.Description),
DataType: string(node.IssueFieldDate.DataType),
Visibility: string(node.IssueFieldDate.Visibility),
}
default:
continue
}
fields = append(fields, f)
}
return fields
}