-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Cursor-Based Pagination in ListRepositories Endpoint (#2097)
Signed-off-by: Vyom-Yadav <[email protected]>
- Loading branch information
1 parent
e370b08
commit e88e4b2
Showing
12 changed files
with
1,276 additions
and
985 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
database/migrations/000012_repositories_pagination_idx.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
|
||
DROP INDEX IF EXISTS repositories_cursor_pagination_idx; |
16 changes: 16 additions & 0 deletions
16
database/migrations/000012_repositories_pagination_idx.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
|
||
-- This adds a unique index to the repositories table to support cursor pagination. | ||
CREATE UNIQUE INDEX repositories_cursor_pagination_idx ON repositories(project_id, provider, repo_id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2024 Stacklok, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package controlplane | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// cursorDelimiter is the delimiter used to encode/decode cursors | ||
const cursorDelimiter = "," | ||
|
||
// RepoCursor is a cursor for listing repositories | ||
type RepoCursor struct { | ||
ProjectId string | ||
Provider string | ||
RepoId int32 | ||
} | ||
|
||
func (c *RepoCursor) String() string { | ||
if c == nil || c.ProjectId == "" || c.Provider == "" || c.RepoId <= 0 { | ||
return "" | ||
} | ||
key := strings.Join([]string{c.ProjectId, c.Provider, strconv.Itoa(int(c.RepoId))}, cursorDelimiter) | ||
return EncodeValue(key) | ||
} | ||
|
||
// NewRepoCursor creates a new RepoCursor from an encoded cursor | ||
func NewRepoCursor(encodedCursor string) (*RepoCursor, error) { | ||
if encodedCursor == "" { | ||
return &RepoCursor{}, nil | ||
} | ||
|
||
cursor, err := DecodeValue(encodedCursor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parts := strings.Split(cursor, cursorDelimiter) | ||
if len(parts) != 3 { | ||
return nil, fmt.Errorf("invalid cursor: %s", encodedCursor) | ||
} | ||
parsedRepoId, err := strconv.ParseInt(parts[2], 10, 32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &RepoCursor{ | ||
ProjectId: parts[0], | ||
Provider: parts[1], | ||
RepoId: int32(parsedRepoId), | ||
}, nil | ||
} | ||
|
||
// EncodeValue encodes a string into a base64 encoded string | ||
func EncodeValue(value string) string { | ||
return base64.StdEncoding.EncodeToString([]byte(value)) | ||
} | ||
|
||
// DecodeValue decodes a base64 encoded string into a string | ||
func DecodeValue(value string) (string, error) { | ||
decoded, err := base64.StdEncoding.DecodeString(value) | ||
if err != nil { | ||
return "", fmt.Errorf("error decoding cursor: %w", err) | ||
} | ||
return string(decoded), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright 2024 Stacklok, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package controlplane | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEncodeDecodeValue(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
input string | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "non-empty string", | ||
input: "testCursor", | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "empty string", | ||
input: "", | ||
expectErr: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
encoded := EncodeValue(tc.input) | ||
decoded, err := DecodeValue(encoded) | ||
|
||
if tc.expectErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tc.input, decoded) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestEncodeDecodeCursor(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
repoCursor RepoCursor | ||
expectEmptyCursor bool | ||
}{ | ||
{ | ||
name: "all inputs valid", | ||
repoCursor: RepoCursor{ProjectId: "3b241101-e2bb-4255-8caf-4136c566a964", Provider: "testProvider", RepoId: 123}, | ||
expectEmptyCursor: false, | ||
}, | ||
{ | ||
name: "empty projectId", | ||
repoCursor: RepoCursor{ProjectId: "", Provider: "testProvider", RepoId: 123}, | ||
expectEmptyCursor: true, | ||
}, | ||
{ | ||
name: "empty provider", | ||
repoCursor: RepoCursor{ProjectId: "3b241101-e2bb-4255-8caf-4136c566a964", Provider: "", RepoId: 123}, | ||
expectEmptyCursor: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
encodedCursor := tc.repoCursor.String() | ||
|
||
if tc.expectEmptyCursor { | ||
require.Equal(t, "", encodedCursor) | ||
} else { | ||
decodedRepoCursor, err := NewRepoCursor(encodedCursor) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, tc.repoCursor.ProjectId, decodedRepoCursor.ProjectId) | ||
require.Equal(t, tc.repoCursor.Provider, decodedRepoCursor.Provider) | ||
require.Equal(t, tc.repoCursor.RepoId, decodedRepoCursor.RepoId) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDecodeListRepositoriesByProjectIDCursor(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
cursor string | ||
expectedRepoCursor *RepoCursor | ||
expectErrorDecoding bool | ||
}{ | ||
{ | ||
name: "compliant input", | ||
cursor: EncodeValue("3b241101-e2bb-4255-8caf-4136c566a964,testProvider,123"), | ||
expectedRepoCursor: &RepoCursor{ProjectId: "3b241101-e2bb-4255-8caf-4136c566a964", Provider: "testProvider", RepoId: 123}, | ||
expectErrorDecoding: false, | ||
}, | ||
{ | ||
name: "non-compliant input", | ||
cursor: EncodeValue("nonCompliantInput"), | ||
expectedRepoCursor: nil, | ||
expectErrorDecoding: true, | ||
}, | ||
{ | ||
name: "non-compliant 64 bit input", | ||
cursor: EncodeValue("3b241101-e2bb-4255-8caf-4136c566a964,testProvider,12345678901234567890"), | ||
expectedRepoCursor: nil, | ||
expectErrorDecoding: true, | ||
}, | ||
{ | ||
name: "empty input", | ||
cursor: "", | ||
expectedRepoCursor: &RepoCursor{}, | ||
expectErrorDecoding: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
decodedRepoCursor, err := NewRepoCursor(tc.cursor) | ||
|
||
if tc.expectErrorDecoding { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expectedRepoCursor.ProjectId, decodedRepoCursor.ProjectId) | ||
require.Equal(t, tc.expectedRepoCursor.Provider, decodedRepoCursor.Provider) | ||
require.Equal(t, tc.expectedRepoCursor.RepoId, decodedRepoCursor.RepoId) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.