Skip to content

Commit 14dcf32

Browse files
committed
refactored logic, simplified switch statement
1 parent 25d39be commit 14dcf32

File tree

1 file changed

+68
-71
lines changed

1 file changed

+68
-71
lines changed

pkg/github/discussions.go

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/shurcooL/githubv4"
1515
)
1616

17-
// Define reusable fragments for discussions
1817
type DiscussionFragment struct {
1918
Number githubv4.Int
2019
Title githubv4.String
@@ -29,40 +28,43 @@ type DiscussionFragment struct {
2928
URL githubv4.String `graphql:"url"`
3029
}
3130

32-
type discussionQueries struct {
33-
BasicNoOrder struct {
34-
Repository struct {
35-
Discussions struct {
36-
Nodes []DiscussionFragment
37-
} `graphql:"discussions(first: 100)"`
38-
} `graphql:"repository(owner: $owner, name: $repo)"`
39-
}
31+
type BasicNoOrder struct {
32+
Repository struct {
33+
Discussions struct {
34+
Nodes []DiscussionFragment
35+
} `graphql:"discussions(first: 100)"`
36+
} `graphql:"repository(owner: $owner, name: $repo)"`
37+
}
4038

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-
}
39+
type BasicWithOrder struct {
40+
Repository struct {
41+
Discussions struct {
42+
Nodes []DiscussionFragment
43+
} `graphql:"discussions(first: 100, orderBy: { field: $orderByField, direction: $orderByDirection })"`
44+
} `graphql:"repository(owner: $owner, name: $repo)"`
45+
}
4846

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-
}
5647

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-
}
48+
49+
type WithCategoryAndOrder struct {
50+
Repository struct {
51+
Discussions struct {
52+
Nodes []DiscussionFragment
53+
} `graphql:"discussions(first: 100, categoryId: $categoryId, orderBy: { field: $orderByField, direction: $orderByDirection })"`
54+
} `graphql:"repository(owner: $owner, name: $repo)"`
6455
}
6556

57+
58+
type WithCategoryNoOrder struct {
59+
Repository struct {
60+
Discussions struct {
61+
Nodes []DiscussionFragment
62+
} `graphql:"discussions(first: 100, categoryId: $categoryId)"`
63+
} `graphql:"repository(owner: $owner, name: $repo)"`
64+
}
65+
66+
67+
6668
func fragmentToDiscussion(fragment DiscussionFragment) *github.Discussion {
6769
return &github.Discussion{
6870
Number: github.Ptr(int(fragment.Number)),
@@ -79,6 +81,19 @@ func fragmentToDiscussion(fragment DiscussionFragment) *github.Discussion {
7981
}
8082
}
8183

84+
func getQueryType (useOrdering bool, categoryID *githubv4.ID) any {
85+
if categoryID != nil && useOrdering {
86+
return &WithCategoryAndOrder{}
87+
}
88+
if categoryID != nil && !useOrdering {
89+
return &WithCategoryNoOrder{}
90+
}
91+
if categoryID == nil && useOrdering {
92+
return &BasicWithOrder{}
93+
}
94+
return &BasicNoOrder{}
95+
}
96+
8297
func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
8398
return mcp.NewTool("list_discussions",
8499
mcp.WithDescription(t("TOOL_LIST_DISCUSSIONS_DESCRIPTION", "List discussions for a repository")),
@@ -151,72 +166,54 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
151166
// we shouldn't use ordering unless both a 'field' and 'direction' are provided
152167
useOrdering := orderBy != "" && direction != ""
153168
if useOrdering {
154-
orderObject := githubv4.DiscussionOrder{
155-
Field: githubv4.DiscussionOrderField(orderBy),
156-
Direction: githubv4.OrderDirection(direction),
157-
}
158-
vars["orderBy"] = orderObject
169+
vars["orderByField"] = githubv4.DiscussionOrderField(orderBy)
170+
vars["orderByDirection"] = githubv4.OrderDirection(direction)
159171
}
160172

161173
if categoryID != nil {
162174
vars["categoryId"] = *categoryID
163175
}
164176

165177
var discussions []*github.Discussion
166-
queries := &discussionQueries{}
178+
discussionQuery := getQueryType(useOrdering, categoryID)
179+
180+
if err := client.Query(ctx, discussionQuery, vars); err != nil {
181+
return mcp.NewToolResultError(err.Error()), nil
182+
}
167183

168184
// we need to check what user inputs we received at runtime, and use the
169-
// most appropriate query
170-
switch {
171-
// use query WithCategoryAndOrder
172-
case categoryID != nil && useOrdering:
173-
log.Printf("GraphQL Query with category and order: %+v", queries.WithCategoryAndOrder)
185+
// most appropriate query based on that
186+
switch queryType := discussionQuery.(type) {
187+
case *WithCategoryAndOrder:
188+
log.Printf("GraphQL Query with category and order: %+v", queryType)
174189
log.Printf("GraphQL Variables: %+v", vars)
175-
176-
if err := client.Query(ctx, &queries.WithCategoryAndOrder, vars); err != nil {
177-
return mcp.NewToolResultError(err.Error()), nil
178-
}
179-
180-
for _, node := range queries.WithCategoryAndOrder.Repository.Discussions.Nodes {
190+
191+
for _, node := range queryType.Repository.Discussions.Nodes {
181192
discussions = append(discussions, fragmentToDiscussion(node))
182193
}
183194

184-
// use query WithCategoryNoOrder
185-
case categoryID != nil && !useOrdering:
186-
log.Printf("GraphQL Query with category no order: %+v", queries.WithCategoryNoOrder)
195+
case *WithCategoryNoOrder:
196+
log.Printf("GraphQL Query with category no order: %+v", queryType)
187197
log.Printf("GraphQL Variables: %+v", vars)
188198

189-
if err := client.Query(ctx, &queries.WithCategoryNoOrder, vars); err != nil {
190-
return mcp.NewToolResultError(err.Error()), nil
191-
}
192-
193-
for _, node := range queries.WithCategoryNoOrder.Repository.Discussions.Nodes {
199+
for _, node := range queryType.Repository.Discussions.Nodes {
194200
discussions = append(discussions, fragmentToDiscussion(node))
195201
}
196202

197-
// use query BasicWithOrder
198-
case categoryID == nil && useOrdering:
199-
log.Printf("GraphQL Query basic with order: %+v", queries.BasicWithOrder)
203+
case *BasicWithOrder:
204+
log.Printf("GraphQL Query basic with order: %+v", queryType)
200205
log.Printf("GraphQL Variables: %+v", vars)
201206

202-
if err := client.Query(ctx, &queries.BasicWithOrder, vars); err != nil {
203-
return mcp.NewToolResultError(err.Error()), nil
204-
}
205-
206-
for _, node := range queries.BasicWithOrder.Repository.Discussions.Nodes {
207+
for _, node := range queryType.Repository.Discussions.Nodes {
207208
discussions = append(discussions, fragmentToDiscussion(node))
208209
}
209210

210-
// use query BasicNoOrder
211-
case categoryID == nil && !useOrdering:
212-
log.Printf("GraphQL Query basic no order: %+v", queries.BasicNoOrder)
213-
log.Printf("GraphQL Variables: %+v", vars)
214211

215-
if err := client.Query(ctx, &queries.BasicNoOrder, vars); err != nil {
216-
return mcp.NewToolResultError(err.Error()), nil
217-
}
212+
case *BasicNoOrder:
213+
log.Printf("GraphQL Query basic no order: %+v", queryType)
214+
log.Printf("GraphQL Variables: %+v", vars)
218215

219-
for _, node := range queries.BasicNoOrder.Repository.Discussions.Nodes {
216+
for _, node := range queryType.Repository.Discussions.Nodes {
220217
discussions = append(discussions, fragmentToDiscussion(node))
221218
}
222219
}

0 commit comments

Comments
 (0)