-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(github): Add cursor-based pagination to integration repos endpoint #112591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jaydgoss
wants to merge
13
commits into
master
Choose a base branch
from
jaygoss/vdy-46-scm-repo-selector-pre-populate-dropdown-with-initial-repos-BE
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a384179
feat(github): Add paginated repos endpoint for SCM onboarding
jaydgoss c62b01b
Revert "feat(github): Add paginated repos endpoint for SCM onboarding"
jaydgoss 9d0d151
feat(github): Add cursor-based pagination to integration repos endpoint
jaydgoss 4b400c7
ref(github): Extract _to_repo_info method for reuse
jaydgoss 78d378b
fix(github): Clamp per_page lower bound and add docstring
jaydgoss 642a481
docs(github): Note installableOnly filtering after pagination slice
jaydgoss b8e1e80
fix(github): Harden paginated path input validation
jaydgoss 4760bef
fix(github): Add type annotation for cursor_result
jaydgoss a856a1e
test(github): Add edge case tests and docstring improvements
jaydgoss 89c140a
ref(github): Address review nits on paginated repos
jaydgoss 8103616
ref(github): Update paginated path for get_repos_cached rename
jaydgoss 64cce91
ref(github): Use real GitHub API pagination instead of cached list sl…
jaydgoss 000570b
ref(github): Add comments for pagination edge cases and fallback test
jaydgoss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| from sentry.models.organization import Organization | ||
| from sentry.models.repository import Repository | ||
| from sentry.shared_integrations.exceptions import IntegrationError | ||
| from sentry.utils.cursors import Cursor, CursorResult | ||
|
|
||
|
|
||
| class IntegrationRepository(TypedDict): | ||
|
|
@@ -54,6 +55,13 @@ def get( | |
| installation has access to, filtering locally instead of | ||
| using the provider's search API which may return results | ||
| beyond the installation's scope. | ||
| :qparam int per_page: When present (without ``search``), enables cursor-based | ||
| pagination. Providers that support paginated browsing return | ||
| one page of results with ``Link`` headers. Providers that | ||
| don't support it fall back to returning the full list. | ||
| The paginated path always returns installation-accessible | ||
| repos (``accessibleOnly`` is ignored). | ||
| :qparam string cursor: Pagination cursor (only used when ``per_page`` is set). | ||
| """ | ||
| integration = self.get_integration(organization.id, integration_id) | ||
|
|
||
|
|
@@ -71,19 +79,41 @@ def get( | |
| search = request.GET.get("search") | ||
| accessible_only = request.GET.get("accessibleOnly", "false").lower() == "true" | ||
|
|
||
| try: | ||
| repositories = install.get_repositories( | ||
| search, | ||
| accessible_only=accessible_only, | ||
| use_cache=accessible_only and bool(search), | ||
| ) | ||
| except (IntegrationError, IdentityNotValid) as e: | ||
| return self.respond({"detail": str(e)}, status=400) | ||
| # When per_page is present and there's no search query, | ||
| # try the paginated path. This lets pagination-aware callers | ||
| # (e.g. the SCM onboarding repo selector) get fast page-at-a-time | ||
| # results, while existing callers that don't send per_page | ||
| # continue to receive the full list. | ||
| paginate = "per_page" in request.GET and not search | ||
| if paginate: | ||
| per_page = self.get_per_page(request) | ||
| cursor = self.get_cursor_from_request(request) | ||
| offset = max(0, cursor.offset) if cursor is not None else 0 | ||
| try: | ||
| repositories, has_next = install.get_repositories_paginated( | ||
| offset=offset, per_page=per_page | ||
| ) | ||
| except NotImplementedError: | ||
| paginate = False | ||
| except (IntegrationError, IdentityNotValid) as e: | ||
| return self.respond({"detail": str(e)}, status=400) | ||
|
|
||
| if not paginate: | ||
| try: | ||
| repositories = install.get_repositories( | ||
| search, | ||
| accessible_only=accessible_only, | ||
| use_cache=accessible_only and bool(search), | ||
| ) | ||
| except (IntegrationError, IdentityNotValid) as e: | ||
| return self.respond({"detail": str(e)}, status=400) | ||
| has_next = False | ||
|
|
||
| installable_only = request.GET.get("installableOnly", "false").lower() == "true" | ||
|
|
||
| # Include a repository if the request is for all repositories, or if we want | ||
| # installable-only repositories and the repository isn't already installed | ||
| # installableOnly filtering happens after pagination, so pages | ||
| # may contain fewer items than per_page when installed repos are | ||
| # excluded. Acceptable for infinite-scroll consumers. | ||
| serialized_repositories = [ | ||
| IntegrationRepository( | ||
| name=repo["name"], | ||
|
|
@@ -95,8 +125,19 @@ def get( | |
| for repo in repositories | ||
| if not installable_only or repo["identifier"] not in installed_repo_names | ||
| ] | ||
| return self.respond( | ||
|
|
||
| response = self.respond( | ||
| {"repos": serialized_repositories, "searchable": install.repo_search} | ||
| ) | ||
|
|
||
| if paginate and (has_next or offset > 0): | ||
| cursor_result: CursorResult[IntegrationRepository] = CursorResult( | ||
| results=[], | ||
| prev=Cursor(0, max(0, offset - per_page), True, offset > 0), | ||
| next=Cursor(0, offset + per_page, False, has_next), | ||
| ) | ||
| self.add_cursor_headers(request, response, cursor_result) | ||
|
|
||
| return response | ||
|
|
||
| return self.respond({"detail": "Repositories not supported"}, status=400) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth seeing if there's a way to work this into using our generic |
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.