Skip to content
10 changes: 7 additions & 3 deletions pkg/github/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,13 @@ func listLabelsLegacy(t translations.TranslationHelperFunc) inventory.ServerTool
return utils.NewToolResultError(err.Error()), nil, nil
}

perPage, err := OptionalIntParamWithDefault(args, "perPage", listLabelsDefaultPerPage)
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
// Distinguish omitted perPage (default 100) from an explicit 0, which is out of range.
perPage := listLabelsDefaultPerPage
if _, present := args["perPage"]; present {
perPage, err = OptionalIntParam(args, "perPage")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
}
after, err := OptionalParam[string](args, "after")
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/github/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ func TestListLabels(t *testing.T) {
expectToolError: true,
expectedToolErrMsg: "perPage value 101 exceeds maximum of 100",
},
{
// Explicit 0 must not be treated as "omitted" and replaced by the default of 100.
// The mock matches first=100 so a silent default would succeed and fail this case.
name: "rejects a page size of 0",
requestArgs: map[string]any{
"owner": "owner",
"repo": "repo",
"perPage": float64(0),
},
mockedClient: githubv4mock.NewMockedHTTPClient(listLabelsMatcher(100, (*githubv4.String)(nil), map[string]any{
"hasNextPage": githubv4.Boolean(false),
"hasPreviousPage": githubv4.Boolean(false),
"startCursor": githubv4.String("c1"),
"endCursor": githubv4.String("c2"),
})),
expectToolError: true,
expectedToolErrMsg: "perPage value 0 is below minimum of 1",
},
Comment thread
vshalpnjabi marked this conversation as resolved.
Resolved
}

for _, tc := range tests {
Expand Down
4 changes: 2 additions & 2 deletions pkg/github/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ func (p CursorPaginationParams) ToGraphQLParams() (*GraphQLPaginationParams, err
if p.PerPage > 100 {
return nil, fmt.Errorf("perPage value %d exceeds maximum of 100", p.PerPage)
}
if p.PerPage < 0 {
return nil, fmt.Errorf("perPage value %d cannot be negative", p.PerPage)
if p.PerPage < 1 {
return nil, fmt.Errorf("perPage value %d is below minimum of 1", p.PerPage)
Comment thread
vshalpnjabi marked this conversation as resolved.
Resolved
}
first := int32(p.PerPage)

Expand Down
60 changes: 60 additions & 0 deletions pkg/github/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,63 @@ func TestOptionalPaginationParams(t *testing.T) {
})
}
}

func TestCursorPaginationParams_ToGraphQLParams(t *testing.T) {
ptr := func(v int32) *int32 { return &v }
strPtr := func(v string) *string { return &v }

tests := []struct {
name string
params CursorPaginationParams
expected *GraphQLPaginationParams
expectError bool
errMsg string
}{
{
name: "perPage 1",
params: CursorPaginationParams{PerPage: 1},
expected: &GraphQLPaginationParams{First: ptr(1)},
},
{
name: "perPage 100",
params: CursorPaginationParams{PerPage: 100},
expected: &GraphQLPaginationParams{First: ptr(100)},
},
{
name: "perPage 50 with after",
params: CursorPaginationParams{PerPage: 50, After: "cursor123"},
expected: &GraphQLPaginationParams{First: ptr(50), After: strPtr("cursor123")},
},
{
name: "perPage 0",
params: CursorPaginationParams{PerPage: 0},
expectError: true,
errMsg: "perPage value 0 is below minimum of 1",
},
{
name: "perPage -1",
params: CursorPaginationParams{PerPage: -1},
expectError: true,
errMsg: "perPage value -1 is below minimum of 1",
},
{
name: "perPage 101",
params: CursorPaginationParams{PerPage: 101},
expectError: true,
errMsg: "perPage value 101 exceeds maximum of 100",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := tc.params.ToGraphQLParams()
if tc.expectError {
require.Error(t, err)
assert.Equal(t, tc.errMsg, err.Error())
} else {
require.NoError(t, err)
assert.Equal(t, tc.expected, result)
}
})
}
}