Skip to content

Commit 6c1833a

Browse files
Update actions, gists, and notifications tools to use cursor-based pagination
Co-authored-by: SamMorrowDrums <[email protected]>
1 parent 4528745 commit 6c1833a

File tree

3 files changed

+76
-53
lines changed

3 files changed

+76
-53
lines changed

pkg/github/actions.go

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func ListWorkflows(getClient GetClientFn, t translations.TranslationHelperFunc)
5050
return mcp.NewToolResultError(err.Error()), nil
5151
}
5252

53-
// Get optional pagination parameters
54-
pagination, err := OptionalPaginationParams(request)
53+
// Get cursor-based pagination parameters
54+
cursorParams, err := GetCursorBasedParams(request)
5555
if err != nil {
5656
return mcp.NewToolResultError(err.Error()), nil
5757
}
@@ -61,10 +61,10 @@ func ListWorkflows(getClient GetClientFn, t translations.TranslationHelperFunc)
6161
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
6262
}
6363

64-
// Set up list options
64+
// Set up list options - request one extra to check for more results
6565
opts := &github.ListOptions{
66-
PerPage: pagination.PerPage,
67-
Page: pagination.Page,
66+
PerPage: cursorParams.PerPage + 1,
67+
Page: cursorParams.Page,
6868
}
6969

7070
workflows, resp, err := client.Actions.ListWorkflows(ctx, owner, repo, opts)
@@ -73,12 +73,16 @@ func ListWorkflows(getClient GetClientFn, t translations.TranslationHelperFunc)
7373
}
7474
defer func() { _ = resp.Body.Close() }()
7575

76-
r, err := json.Marshal(workflows)
77-
if err != nil {
78-
return nil, fmt.Errorf("failed to marshal response: %w", err)
76+
// Check if there are more results
77+
hasMore := len(workflows.Workflows) > cursorParams.PerPage
78+
if hasMore {
79+
// Remove the extra item
80+
workflows.Workflows = workflows.Workflows[:cursorParams.PerPage]
7981
}
8082

81-
return mcp.NewToolResultText(string(r)), nil
83+
// Create paginated response
84+
paginatedResp := NewPaginatedRESTResponse(workflows, cursorParams.Page, cursorParams.PerPage, hasMore)
85+
return MarshalPaginatedResponse(paginatedResp), nil
8286
}
8387
}
8488

@@ -183,8 +187,8 @@ func ListWorkflowRuns(getClient GetClientFn, t translations.TranslationHelperFun
183187
return mcp.NewToolResultError(err.Error()), nil
184188
}
185189

186-
// Get optional pagination parameters
187-
pagination, err := OptionalPaginationParams(request)
190+
// Get cursor-based pagination parameters
191+
cursorParams, err := GetCursorBasedParams(request)
188192
if err != nil {
189193
return mcp.NewToolResultError(err.Error()), nil
190194
}
@@ -194,15 +198,15 @@ func ListWorkflowRuns(getClient GetClientFn, t translations.TranslationHelperFun
194198
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
195199
}
196200

197-
// Set up list options
201+
// Set up list options - request one extra to check for more results
198202
opts := &github.ListWorkflowRunsOptions{
199203
Actor: actor,
200204
Branch: branch,
201205
Event: event,
202206
Status: status,
203207
ListOptions: github.ListOptions{
204-
PerPage: pagination.PerPage,
205-
Page: pagination.Page,
208+
PerPage: cursorParams.PerPage + 1,
209+
Page: cursorParams.Page,
206210
},
207211
}
208212

@@ -212,12 +216,16 @@ func ListWorkflowRuns(getClient GetClientFn, t translations.TranslationHelperFun
212216
}
213217
defer func() { _ = resp.Body.Close() }()
214218

215-
r, err := json.Marshal(workflowRuns)
216-
if err != nil {
217-
return nil, fmt.Errorf("failed to marshal response: %w", err)
219+
// Check if there are more results
220+
hasMore := len(workflowRuns.WorkflowRuns) > cursorParams.PerPage
221+
if hasMore {
222+
// Remove the extra item
223+
workflowRuns.WorkflowRuns = workflowRuns.WorkflowRuns[:cursorParams.PerPage]
218224
}
219225

220-
return mcp.NewToolResultText(string(r)), nil
226+
// Create paginated response
227+
paginatedResp := NewPaginatedRESTResponse(workflowRuns, cursorParams.Page, cursorParams.PerPage, hasMore)
228+
return MarshalPaginatedResponse(paginatedResp), nil
221229
}
222230
}
223231

@@ -489,8 +497,8 @@ func ListWorkflowJobs(getClient GetClientFn, t translations.TranslationHelperFun
489497
return mcp.NewToolResultError(err.Error()), nil
490498
}
491499

492-
// Get optional pagination parameters
493-
pagination, err := OptionalPaginationParams(request)
500+
// Get cursor-based pagination parameters
501+
cursorParams, err := GetCursorBasedParams(request)
494502
if err != nil {
495503
return mcp.NewToolResultError(err.Error()), nil
496504
}
@@ -500,12 +508,12 @@ func ListWorkflowJobs(getClient GetClientFn, t translations.TranslationHelperFun
500508
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
501509
}
502510

503-
// Set up list options
511+
// Set up list options - request one extra to check for more results
504512
opts := &github.ListWorkflowJobsOptions{
505513
Filter: filter,
506514
ListOptions: github.ListOptions{
507-
PerPage: pagination.PerPage,
508-
Page: pagination.Page,
515+
PerPage: cursorParams.PerPage + 1,
516+
Page: cursorParams.Page,
509517
},
510518
}
511519

@@ -515,18 +523,22 @@ func ListWorkflowJobs(getClient GetClientFn, t translations.TranslationHelperFun
515523
}
516524
defer func() { _ = resp.Body.Close() }()
517525

526+
// Check if there are more results
527+
hasMore := len(jobs.Jobs) > cursorParams.PerPage
528+
if hasMore {
529+
// Remove the extra item
530+
jobs.Jobs = jobs.Jobs[:cursorParams.PerPage]
531+
}
532+
518533
// Add optimization tip for failed job debugging
519534
response := map[string]any{
520535
"jobs": jobs,
521536
"optimization_tip": "For debugging failed jobs, consider using get_job_logs with failed_only=true and run_id=" + fmt.Sprintf("%d", runID) + " to get logs directly without needing to list jobs first",
522537
}
523538

524-
r, err := json.Marshal(response)
525-
if err != nil {
526-
return nil, fmt.Errorf("failed to marshal response: %w", err)
527-
}
528-
529-
return mcp.NewToolResultText(string(r)), nil
539+
// Create paginated response
540+
paginatedResp := NewPaginatedRESTResponse(response, cursorParams.Page, cursorParams.PerPage, hasMore)
541+
return MarshalPaginatedResponse(paginatedResp), nil
530542
}
531543
}
532544

@@ -1006,8 +1018,8 @@ func ListWorkflowRunArtifacts(getClient GetClientFn, t translations.TranslationH
10061018
}
10071019
runID := int64(runIDInt)
10081020

1009-
// Get optional pagination parameters
1010-
pagination, err := OptionalPaginationParams(request)
1021+
// Get cursor-based pagination parameters
1022+
cursorParams, err := GetCursorBasedParams(request)
10111023
if err != nil {
10121024
return mcp.NewToolResultError(err.Error()), nil
10131025
}
@@ -1017,10 +1029,10 @@ func ListWorkflowRunArtifacts(getClient GetClientFn, t translations.TranslationH
10171029
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
10181030
}
10191031

1020-
// Set up list options
1032+
// Set up list options - request one extra to check for more results
10211033
opts := &github.ListOptions{
1022-
PerPage: pagination.PerPage,
1023-
Page: pagination.Page,
1034+
PerPage: cursorParams.PerPage + 1,
1035+
Page: cursorParams.Page,
10241036
}
10251037

10261038
artifacts, resp, err := client.Actions.ListWorkflowRunArtifacts(ctx, owner, repo, runID, opts)
@@ -1029,12 +1041,16 @@ func ListWorkflowRunArtifacts(getClient GetClientFn, t translations.TranslationH
10291041
}
10301042
defer func() { _ = resp.Body.Close() }()
10311043

1032-
r, err := json.Marshal(artifacts)
1033-
if err != nil {
1034-
return nil, fmt.Errorf("failed to marshal response: %w", err)
1044+
// Check if there are more results
1045+
hasMore := len(artifacts.Artifacts) > cursorParams.PerPage
1046+
if hasMore {
1047+
// Remove the extra item
1048+
artifacts.Artifacts = artifacts.Artifacts[:cursorParams.PerPage]
10351049
}
10361050

1037-
return mcp.NewToolResultText(string(r)), nil
1051+
// Create paginated response
1052+
paginatedResp := NewPaginatedRESTResponse(artifacts, cursorParams.Page, cursorParams.PerPage, hasMore)
1053+
return MarshalPaginatedResponse(paginatedResp), nil
10381054
}
10391055
}
10401056

pkg/github/gists.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ func ListGists(getClient GetClientFn, t translations.TranslationHelperFunc) (too
4040
return mcp.NewToolResultError(err.Error()), nil
4141
}
4242

43-
pagination, err := OptionalPaginationParams(request)
43+
cursorParams, err := GetCursorBasedParams(request)
4444
if err != nil {
4545
return mcp.NewToolResultError(err.Error()), nil
4646
}
4747

4848
opts := &github.GistListOptions{
4949
ListOptions: github.ListOptions{
50-
Page: pagination.Page,
51-
PerPage: pagination.PerPage,
50+
Page: cursorParams.Page,
51+
PerPage: cursorParams.PerPage + 1, // Request one extra
5252
},
5353
}
5454

@@ -80,12 +80,16 @@ func ListGists(getClient GetClientFn, t translations.TranslationHelperFunc) (too
8080
return mcp.NewToolResultError(fmt.Sprintf("failed to list gists: %s", string(body))), nil
8181
}
8282

83-
r, err := json.Marshal(gists)
84-
if err != nil {
85-
return nil, fmt.Errorf("failed to marshal response: %w", err)
83+
// Check if there are more results
84+
hasMore := len(gists) > cursorParams.PerPage
85+
if hasMore {
86+
// Remove the extra item
87+
gists = gists[:cursorParams.PerPage]
8688
}
8789

88-
return mcp.NewToolResultText(string(r)), nil
90+
// Create paginated response
91+
paginatedResp := NewPaginatedRESTResponse(gists, cursorParams.Page, cursorParams.PerPage, hasMore)
92+
return MarshalPaginatedResponse(paginatedResp), nil
8993
}
9094
}
9195

pkg/github/notifications.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ListNotifications(getClient GetClientFn, t translations.TranslationHelperFu
7878
return mcp.NewToolResultError(err.Error()), nil
7979
}
8080

81-
paginationParams, err := OptionalPaginationParams(request)
81+
cursorParams, err := GetCursorBasedParams(request)
8282
if err != nil {
8383
return mcp.NewToolResultError(err.Error()), nil
8484
}
@@ -88,8 +88,8 @@ func ListNotifications(getClient GetClientFn, t translations.TranslationHelperFu
8888
All: filter == FilterIncludeRead,
8989
Participating: filter == FilterOnlyParticipating,
9090
ListOptions: github.ListOptions{
91-
Page: paginationParams.Page,
92-
PerPage: paginationParams.PerPage,
91+
Page: cursorParams.Page,
92+
PerPage: cursorParams.PerPage + 1, // Request one extra
9393
},
9494
}
9595

@@ -135,13 +135,16 @@ func ListNotifications(getClient GetClientFn, t translations.TranslationHelperFu
135135
return mcp.NewToolResultError(fmt.Sprintf("failed to get notifications: %s", string(body))), nil
136136
}
137137

138-
// Marshal response to JSON
139-
r, err := json.Marshal(notifications)
140-
if err != nil {
141-
return nil, fmt.Errorf("failed to marshal response: %w", err)
138+
// Check if there are more results
139+
hasMore := len(notifications) > cursorParams.PerPage
140+
if hasMore {
141+
// Remove the extra item
142+
notifications = notifications[:cursorParams.PerPage]
142143
}
143144

144-
return mcp.NewToolResultText(string(r)), nil
145+
// Create paginated response
146+
paginatedResp := NewPaginatedRESTResponse(notifications, cursorParams.Page, cursorParams.PerPage, hasMore)
147+
return MarshalPaginatedResponse(paginatedResp), nil
145148
}
146149
}
147150

0 commit comments

Comments
 (0)