Skip to content

Commit 5d795ec

Browse files
committed
update pagination for reverse and total
1 parent 985f8b2 commit 5d795ec

File tree

2 files changed

+79
-34
lines changed

2 files changed

+79
-34
lines changed

pkg/github/discussions.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
9292
URL githubv4.String `graphql:"url"`
9393
}
9494
PageInfo struct {
95-
HasNextPage bool
96-
EndCursor string
95+
HasNextPage bool
96+
HasPreviousPage bool
97+
StartCursor string
98+
EndCursor string
9799
}
100+
TotalCount int
98101
} `graphql:"discussions(first: $first, after: $after, categoryId: $categoryId)"`
99102
} `graphql:"repository(owner: $owner, name: $repo)"`
100103
}
@@ -131,9 +134,12 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
131134
response := map[string]interface{}{
132135
"discussions": discussions,
133136
"pageInfo": map[string]interface{}{
134-
"hasNextPage": query.Repository.Discussions.PageInfo.HasNextPage,
135-
"endCursor": query.Repository.Discussions.PageInfo.EndCursor,
137+
"hasNextPage": query.Repository.Discussions.PageInfo.HasNextPage,
138+
"hasPreviousPage": query.Repository.Discussions.PageInfo.HasPreviousPage,
139+
"startCursor": query.Repository.Discussions.PageInfo.StartCursor,
140+
"endCursor": query.Repository.Discussions.PageInfo.EndCursor,
136141
},
142+
"totalCount": query.Repository.Discussions.TotalCount,
137143
}
138144

139145
out, err = json.Marshal(response)
@@ -155,9 +161,12 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
155161
URL githubv4.String `graphql:"url"`
156162
}
157163
PageInfo struct {
158-
HasNextPage bool
159-
EndCursor string
164+
HasNextPage bool
165+
HasPreviousPage bool
166+
StartCursor string
167+
EndCursor string
160168
}
169+
TotalCount int
161170
} `graphql:"discussions(first: $first, after: $after)"`
162171
} `graphql:"repository(owner: $owner, name: $repo)"`
163172
}
@@ -193,9 +202,12 @@ func ListDiscussions(getGQLClient GetGQLClientFn, t translations.TranslationHelp
193202
response := map[string]interface{}{
194203
"discussions": discussions,
195204
"pageInfo": map[string]interface{}{
196-
"hasNextPage": query.Repository.Discussions.PageInfo.HasNextPage,
197-
"endCursor": query.Repository.Discussions.PageInfo.EndCursor,
205+
"hasNextPage": query.Repository.Discussions.PageInfo.HasNextPage,
206+
"hasPreviousPage": query.Repository.Discussions.PageInfo.HasPreviousPage,
207+
"startCursor": query.Repository.Discussions.PageInfo.StartCursor,
208+
"endCursor": query.Repository.Discussions.PageInfo.EndCursor,
198209
},
210+
"totalCount": query.Repository.Discussions.TotalCount,
199211
}
200212

201213
out, err = json.Marshal(response)
@@ -341,9 +353,12 @@ func GetDiscussionComments(getGQLClient GetGQLClientFn, t translations.Translati
341353
Body githubv4.String
342354
}
343355
PageInfo struct {
344-
HasNextPage githubv4.Boolean
345-
EndCursor githubv4.String
356+
HasNextPage githubv4.Boolean
357+
HasPreviousPage githubv4.Boolean
358+
StartCursor githubv4.String
359+
EndCursor githubv4.String
346360
}
361+
TotalCount int
347362
} `graphql:"comments(first: $first, after: $after)"`
348363
} `graphql:"discussion(number: $discussionNumber)"`
349364
} `graphql:"repository(owner: $owner, name: $repo)"`
@@ -372,9 +387,12 @@ func GetDiscussionComments(getGQLClient GetGQLClientFn, t translations.Translati
372387
response := map[string]interface{}{
373388
"comments": comments,
374389
"pageInfo": map[string]interface{}{
375-
"hasNextPage": q.Repository.Discussion.Comments.PageInfo.HasNextPage,
376-
"endCursor": string(q.Repository.Discussion.Comments.PageInfo.EndCursor),
390+
"hasNextPage": q.Repository.Discussion.Comments.PageInfo.HasNextPage,
391+
"hasPreviousPage": q.Repository.Discussion.Comments.PageInfo.HasPreviousPage,
392+
"startCursor": string(q.Repository.Discussion.Comments.PageInfo.StartCursor),
393+
"endCursor": string(q.Repository.Discussion.Comments.PageInfo.EndCursor),
377394
},
395+
"totalCount": q.Repository.Discussion.Comments.TotalCount,
378396
}
379397

380398
out, err := json.Marshal(response)
@@ -448,9 +466,12 @@ func ListDiscussionCategories(getGQLClient GetGQLClientFn, t translations.Transl
448466
Name githubv4.String
449467
}
450468
PageInfo struct {
451-
HasNextPage githubv4.Boolean
452-
EndCursor githubv4.String
469+
HasNextPage githubv4.Boolean
470+
HasPreviousPage githubv4.Boolean
471+
StartCursor githubv4.String
472+
EndCursor githubv4.String
453473
}
474+
TotalCount int
454475
} `graphql:"discussionCategories(first: $first, after: $after)"`
455476
} `graphql:"repository(owner: $owner, name: $repo)"`
456477
}
@@ -480,9 +501,12 @@ func ListDiscussionCategories(getGQLClient GetGQLClientFn, t translations.Transl
480501
response := map[string]interface{}{
481502
"categories": categories,
482503
"pageInfo": map[string]interface{}{
483-
"hasNextPage": q.Repository.DiscussionCategories.PageInfo.HasNextPage,
484-
"endCursor": string(q.Repository.DiscussionCategories.PageInfo.EndCursor),
504+
"hasNextPage": q.Repository.DiscussionCategories.PageInfo.HasNextPage,
505+
"hasPreviousPage": q.Repository.DiscussionCategories.PageInfo.HasPreviousPage,
506+
"startCursor": string(q.Repository.DiscussionCategories.PageInfo.StartCursor),
507+
"endCursor": string(q.Repository.DiscussionCategories.PageInfo.EndCursor),
485508
},
509+
"totalCount": q.Repository.DiscussionCategories.TotalCount,
486510
}
487511

488512
out, err := json.Marshal(response)

pkg/github/discussions_test.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ var (
3030
"discussions": map[string]any{
3131
"nodes": discussionsAll,
3232
"pageInfo": map[string]any{
33-
"hasNextPage": false,
34-
"endCursor": "",
33+
"hasNextPage": false,
34+
"hasPreviousPage": false,
35+
"startCursor": "",
36+
"endCursor": "",
3537
},
38+
"totalCount": 3,
3639
},
3740
},
3841
})
@@ -41,9 +44,12 @@ var (
4144
"discussions": map[string]any{
4245
"nodes": discussionsGeneral,
4346
"pageInfo": map[string]any{
44-
"hasNextPage": false,
45-
"endCursor": "",
47+
"hasNextPage": false,
48+
"hasPreviousPage": false,
49+
"startCursor": "",
50+
"endCursor": "",
4651
},
52+
"totalCount": 2,
4753
},
4854
},
4955
})
@@ -61,9 +67,9 @@ func Test_ListDiscussions(t *testing.T) {
6167
assert.ElementsMatch(t, toolDef.InputSchema.Required, []string{"owner", "repo"})
6268

6369
// Use exact string queries that match implementation output (from error messages)
64-
qDiscussions := "query($after:String$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after){nodes{number,title,createdAt,category{name},url},pageInfo{hasNextPage,endCursor}}}}"
70+
qDiscussions := "query($after:String$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after){nodes{number,title,createdAt,category{name},url},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
6571

66-
qDiscussionsFiltered := "query($after:String$categoryId:ID!$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after, categoryId: $categoryId){nodes{number,title,createdAt,category{name},url},pageInfo{hasNextPage,endCursor}}}}"
72+
qDiscussionsFiltered := "query($after:String$categoryId:ID!$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussions(first: $first, after: $after, categoryId: $categoryId){nodes{number,title,createdAt,category{name},url},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
6773

6874
// Variables matching what GraphQL receives after JSON marshaling/unmarshaling
6975
varsListAll := map[string]interface{}{
@@ -161,9 +167,12 @@ func Test_ListDiscussions(t *testing.T) {
161167
var response struct {
162168
Discussions []*github.Discussion `json:"discussions"`
163169
PageInfo struct {
164-
HasNextPage bool `json:"hasNextPage"`
165-
EndCursor string `json:"endCursor"`
170+
HasNextPage bool `json:"hasNextPage"`
171+
HasPreviousPage bool `json:"hasPreviousPage"`
172+
StartCursor string `json:"startCursor"`
173+
EndCursor string `json:"endCursor"`
166174
} `json:"pageInfo"`
175+
TotalCount int `json:"totalCount"`
167176
}
168177
err = json.Unmarshal([]byte(text), &response)
169178
require.NoError(t, err)
@@ -275,7 +284,7 @@ func Test_GetDiscussionComments(t *testing.T) {
275284
assert.ElementsMatch(t, toolDef.InputSchema.Required, []string{"owner", "repo", "discussionNumber"})
276285

277286
// Use exact string query that matches implementation output
278-
qGetComments := "query($after:String$discussionNumber:Int!$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussion(number: $discussionNumber){comments(first: $first, after: $after){nodes{body},pageInfo{hasNextPage,endCursor}}}}}"
287+
qGetComments := "query($after:String$discussionNumber:Int!$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussion(number: $discussionNumber){comments(first: $first, after: $after){nodes{body},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}}"
279288

280289
// Variables matching what GraphQL receives after JSON marshaling/unmarshaling
281290
vars := map[string]interface{}{
@@ -295,9 +304,12 @@ func Test_GetDiscussionComments(t *testing.T) {
295304
{"body": "This is the second comment"},
296305
},
297306
"pageInfo": map[string]any{
298-
"hasNextPage": false,
299-
"endCursor": "",
307+
"hasNextPage": false,
308+
"hasPreviousPage": false,
309+
"startCursor": "",
310+
"endCursor": "",
300311
},
312+
"totalCount": 2,
301313
},
302314
},
303315
},
@@ -323,9 +335,12 @@ func Test_GetDiscussionComments(t *testing.T) {
323335
var response struct {
324336
Comments []*github.IssueComment `json:"comments"`
325337
PageInfo struct {
326-
HasNextPage bool `json:"hasNextPage"`
327-
EndCursor string `json:"endCursor"`
338+
HasNextPage bool `json:"hasNextPage"`
339+
HasPreviousPage bool `json:"hasPreviousPage"`
340+
StartCursor string `json:"startCursor"`
341+
EndCursor string `json:"endCursor"`
328342
} `json:"pageInfo"`
343+
TotalCount int `json:"totalCount"`
329344
}
330345
err = json.Unmarshal([]byte(textContent.Text), &response)
331346
require.NoError(t, err)
@@ -338,7 +353,7 @@ func Test_GetDiscussionComments(t *testing.T) {
338353

339354
func Test_ListDiscussionCategories(t *testing.T) {
340355
// Use exact string query that matches implementation output
341-
qListCategories := "query($after:String$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussionCategories(first: $first, after: $after){nodes{id,name},pageInfo{hasNextPage,endCursor}}}}"
356+
qListCategories := "query($after:String$first:Int!$owner:String!$repo:String!){repository(owner: $owner, name: $repo){discussionCategories(first: $first, after: $after){nodes{id,name},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount}}}"
342357

343358
// Variables matching what GraphQL receives after JSON marshaling/unmarshaling
344359
vars := map[string]interface{}{
@@ -356,9 +371,12 @@ func Test_ListDiscussionCategories(t *testing.T) {
356371
{"id": "456", "name": "CategoryTwo"},
357372
},
358373
"pageInfo": map[string]any{
359-
"hasNextPage": false,
360-
"endCursor": "",
374+
"hasNextPage": false,
375+
"hasPreviousPage": false,
376+
"startCursor": "",
377+
"endCursor": "",
361378
},
379+
"totalCount": 2,
362380
},
363381
},
364382
})
@@ -385,9 +403,12 @@ func Test_ListDiscussionCategories(t *testing.T) {
385403
var response struct {
386404
Categories []map[string]string `json:"categories"`
387405
PageInfo struct {
388-
HasNextPage bool `json:"hasNextPage"`
389-
EndCursor string `json:"endCursor"`
406+
HasNextPage bool `json:"hasNextPage"`
407+
HasPreviousPage bool `json:"hasPreviousPage"`
408+
StartCursor string `json:"startCursor"`
409+
EndCursor string `json:"endCursor"`
390410
} `json:"pageInfo"`
411+
TotalCount int `json:"totalCount"`
391412
}
392413
require.NoError(t, json.Unmarshal([]byte(text), &response))
393414
assert.Len(t, response.Categories, 2)

0 commit comments

Comments
 (0)