Skip to content

Commit 44f8f35

Browse files
committed
restore original categoryID code, simplify vars management
1 parent 7eeb87c commit 44f8f35

File tree

1 file changed

+106
-103
lines changed

1 file changed

+106
-103
lines changed

pkg/github/discussions.go

Lines changed: 106 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,36 @@ type DiscussionFragment struct {
3131

3232
type discussionQueries struct {
3333
BasicNoOrder struct {
34-
Repository struct {
35-
Discussions struct {
36-
Nodes []DiscussionFragment
37-
} `graphql:"discussions(first: 100)"`
38-
} `graphql:"repository(owner: $owner, name: $repo)"`
39-
}
40-
41-
BasicWithOrder struct {
42-
Repository struct {
43-
Discussions struct {
44-
Nodes []DiscussionFragment
45-
} `graphql:"discussions(first: 100, orderBy: $orderBy)"`
46-
} `graphql:"repository(owner: $owner, name: $repo)"`
47-
}
48-
49-
WithCategoryAndOrder struct {
50-
Repository struct {
51-
Discussions struct {
52-
Nodes []DiscussionFragment
53-
} `graphql:"discussions(first: 100, categoryId: $categoryId, orderBy: $orderBy)"`
54-
} `graphql:"repository(owner: $owner, name: $repo)"`
55-
}
56-
57-
WithCategoryNoOrder struct {
58-
Repository struct {
59-
Discussions struct {
60-
Nodes []DiscussionFragment
61-
} `graphql:"discussions(first: 100, categoryId: $categoryId)"`
62-
} `graphql:"repository(owner: $owner, name: $repo)"`
63-
}
34+
Repository struct {
35+
Discussions struct {
36+
Nodes []DiscussionFragment
37+
} `graphql:"discussions(first: 100)"`
38+
} `graphql:"repository(owner: $owner, name: $repo)"`
39+
}
40+
41+
BasicWithOrder struct {
42+
Repository struct {
43+
Discussions struct {
44+
Nodes []DiscussionFragment
45+
} `graphql:"discussions(first: 100, orderBy: $orderBy)"`
46+
} `graphql:"repository(owner: $owner, name: $repo)"`
47+
}
48+
49+
WithCategoryAndOrder struct {
50+
Repository struct {
51+
Discussions struct {
52+
Nodes []DiscussionFragment
53+
} `graphql:"discussions(first: 100, categoryId: $categoryId, orderBy: $orderBy)"`
54+
} `graphql:"repository(owner: $owner, name: $repo)"`
55+
}
56+
57+
WithCategoryNoOrder struct {
58+
Repository struct {
59+
Discussions struct {
60+
Nodes []DiscussionFragment
61+
} `graphql:"discussions(first: 100, categoryId: $categoryId)"`
62+
} `graphql:"repository(owner: $owner, name: $repo)"`
63+
}
6464
}
6565

6666
func fragmentToDiscussion(fragment DiscussionFragment) *github.Discussion {
@@ -101,9 +101,9 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
101101
mcp.Description("Order discussions by field. If provided, the 'direction' also needs to be provided."),
102102
mcp.Enum("CREATED_AT", "UPDATED_AT"),
103103
),
104-
mcp.WithString("direction",
105-
mcp.Description("Order direction."),
106-
mcp.Enum("ASC", "DESC"),
104+
mcp.WithString("direction",
105+
mcp.Description("Order direction."),
106+
mcp.Enum("ASC", "DESC"),
107107
),
108108
),
109109
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
@@ -125,8 +125,8 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
125125
if err != nil {
126126
return mcp.NewToolResultError(err.Error()), nil
127127
}
128-
129-
direction, err := OptionalParam[string](request, "direction")
128+
129+
direction, err := OptionalParam[string](request, "direction")
130130
if err != nil {
131131
return mcp.NewToolResultError(err.Error()), nil
132132
}
@@ -136,82 +136,85 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
136136
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub GQL client: %v", err)), nil
137137
}
138138

139-
baseVars := map[string]interface{}{
140-
"owner": githubv4.String(owner),
141-
"repo": githubv4.String(repo),
142-
}
139+
var categoryID *githubv4.ID
140+
if category != "" {
141+
id := githubv4.ID(category)
142+
categoryID = &id
143+
}
144+
145+
vars := map[string]interface{}{
146+
"owner": githubv4.String(owner),
147+
"repo": githubv4.String(repo),
148+
}
143149

144150
// this is an extra check in case the tool description is misinterpreted, because
145151
// we shouldn't use ordering unless both a 'field' and 'direction' are provided
146-
useOrdering := orderBy != "" && direction != ""
152+
useOrdering := orderBy != "" && direction != ""
147153
if useOrdering {
148-
orderObject := githubv4.DiscussionOrder{
149-
Field: githubv4.DiscussionOrderField(orderBy),
150-
Direction: githubv4.OrderDirection(direction),
151-
}
152-
baseVars["orderBy"] = orderObject
153-
}
154+
orderObject := githubv4.DiscussionOrder{
155+
Field: githubv4.DiscussionOrderField(orderBy),
156+
Direction: githubv4.OrderDirection(direction),
157+
}
158+
vars["orderBy"] = orderObject
159+
}
160+
161+
if categoryID != nil {
162+
vars["categoryId"] = githubv4.ID(categoryID)
163+
}
154164

155165
var discussions []*github.Discussion
156166
queries := &discussionQueries{}
157167

158-
if category != "" {
159-
vars := make(map[string]interface{})
160-
for k, v := range baseVars {
161-
vars[k] = v
162-
}
163-
vars["categoryId"] = githubv4.ID(category)
164-
165-
if useOrdering {
166-
log.Printf("GraphQL Query with category and order: %+v", queries.WithCategoryAndOrder)
167-
log.Printf("GraphQL Variables: %+v", vars)
168-
169-
if err := client.Query(ctx, &queries.WithCategoryAndOrder, vars); err != nil {
170-
return mcp.NewToolResultError(err.Error()), nil
171-
}
172-
173-
for _, node := range queries.WithCategoryAndOrder.Repository.Discussions.Nodes {
174-
discussions = append(discussions, fragmentToDiscussion(node))
175-
}
176-
} else {
177-
log.Printf("GraphQL Query with category no order: %+v", queries.WithCategoryNoOrder)
178-
log.Printf("GraphQL Variables: %+v", vars)
179-
180-
if err := client.Query(ctx, &queries.WithCategoryNoOrder, vars); err != nil {
181-
return mcp.NewToolResultError(err.Error()), nil
182-
}
183-
184-
for _, node := range queries.WithCategoryNoOrder.Repository.Discussions.Nodes {
185-
discussions = append(discussions, fragmentToDiscussion(node))
186-
}
187-
}
188-
} else {
189-
if useOrdering {
190-
log.Printf("GraphQL Query basic with order: %+v", queries.BasicWithOrder)
191-
log.Printf("GraphQL Variables: %+v", baseVars)
192-
193-
if err := client.Query(ctx, &queries.BasicWithOrder, baseVars); err != nil {
194-
return mcp.NewToolResultError(err.Error()), nil
195-
}
196-
197-
for _, node := range queries.BasicWithOrder.Repository.Discussions.Nodes {
198-
discussions = append(discussions, fragmentToDiscussion(node))
199-
}
200-
} else {
201-
log.Printf("GraphQL Query basic no order: %+v", queries.BasicNoOrder)
202-
log.Printf("GraphQL Variables: %+v", baseVars)
203-
204-
if err := client.Query(ctx, &queries.BasicNoOrder, baseVars); err != nil {
205-
return mcp.NewToolResultError(err.Error()), nil
206-
}
207-
208-
for _, node := range queries.BasicNoOrder.Repository.Discussions.Nodes {
209-
discussions = append(discussions, fragmentToDiscussion(node))
210-
}
211-
}
212-
}
213-
214-
// Marshal and return
168+
if categoryID != nil {
169+
if useOrdering {
170+
log.Printf("GraphQL Query with category and order: %+v", queries.WithCategoryAndOrder)
171+
log.Printf("GraphQL Variables: %+v", vars)
172+
173+
if err := client.Query(ctx, &queries.WithCategoryAndOrder, vars); err != nil {
174+
return mcp.NewToolResultError(err.Error()), nil
175+
}
176+
177+
for _, node := range queries.WithCategoryAndOrder.Repository.Discussions.Nodes {
178+
discussions = append(discussions, fragmentToDiscussion(node))
179+
}
180+
} else {
181+
log.Printf("GraphQL Query with category no order: %+v", queries.WithCategoryNoOrder)
182+
log.Printf("GraphQL Variables: %+v", vars)
183+
184+
if err := client.Query(ctx, &queries.WithCategoryNoOrder, vars); err != nil {
185+
return mcp.NewToolResultError(err.Error()), nil
186+
}
187+
188+
for _, node := range queries.WithCategoryNoOrder.Repository.Discussions.Nodes {
189+
discussions = append(discussions, fragmentToDiscussion(node))
190+
}
191+
}
192+
} else {
193+
if useOrdering {
194+
log.Printf("GraphQL Query basic with order: %+v", queries.BasicWithOrder)
195+
log.Printf("GraphQL Variables: %+v", vars)
196+
197+
if err := client.Query(ctx, &queries.BasicWithOrder, vars); err != nil {
198+
return mcp.NewToolResultError(err.Error()), nil
199+
}
200+
201+
for _, node := range queries.BasicWithOrder.Repository.Discussions.Nodes {
202+
discussions = append(discussions, fragmentToDiscussion(node))
203+
}
204+
} else {
205+
log.Printf("GraphQL Query basic no order: %+v", queries.BasicNoOrder)
206+
log.Printf("GraphQL Variables: %+v", vars)
207+
208+
if err := client.Query(ctx, &queries.BasicNoOrder, vars); err != nil {
209+
return mcp.NewToolResultError(err.Error()), nil
210+
}
211+
212+
for _, node := range queries.BasicNoOrder.Repository.Discussions.Nodes {
213+
discussions = append(discussions, fragmentToDiscussion(node))
214+
}
215+
}
216+
}
217+
215218
out, err := json.Marshal(discussions)
216219
if err != nil {
217220
return nil, fmt.Errorf("failed to marshal discussions: %w", err)

0 commit comments

Comments
 (0)