-
Notifications
You must be signed in to change notification settings - Fork 562
feat(github-poc): Retrieve feature code references #5931
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c4f9b29
Retrieve code references for a feature
emyller f5015b9
Improve JSON typing
emyller dc72ef4
Add GIN index to help filtering code references
emyller 9823242
Add frontend-required info to code references spec
emyller 0b2a17f
Fix typing
emyller 2ca2632
Add code references counts to feature list
emyller 60a1112
Rely on data, not guesses
emyller 7bc6166
Revert "Add GIN index to help filtering code references"
emyller 6244ebf
Refactor GitHub PoC retrieval endpoints
emyller 38466ff
Improve max file path setting
emyller 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # TODO: Implement history cleanup? | ||
| FEATURE_FLAG_CODE_REFERENCES_RETENTION_DAYS = 30 | ||
|
|
||
| # Linux maximum file path length, as per limits.h/PATH_MAX | ||
| MAX_FILE_PATH_LENGTH = 4096 |
2 changes: 1 addition & 1 deletion
2
api/projects/code_references/migrations/0001_code_references.py
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| from datetime import timedelta | ||
| from urllib.parse import urljoin | ||
|
|
||
| from django.contrib.postgres.expressions import ArraySubquery | ||
| from django.db.models import BooleanField, F, Func, OuterRef, QuerySet, Subquery, Value | ||
| from django.db.models.functions import JSONObject | ||
| from django.utils import timezone | ||
|
|
||
| from features.models import Feature | ||
| from projects.code_references.constants import ( | ||
| FEATURE_FLAG_CODE_REFERENCES_RETENTION_DAYS, | ||
| ) | ||
| from projects.code_references.models import FeatureFlagCodeReferencesScan | ||
| from projects.code_references.types import ( | ||
| CodeReference, | ||
| FeatureFlagCodeReferencesRepositorySummary, | ||
| VCSProvider, | ||
| ) | ||
|
|
||
|
|
||
| def annotate_feature_queryset_with_code_references_summary( | ||
| queryset: QuerySet[Feature], | ||
| ) -> QuerySet[Feature]: | ||
| """Extend feature objects with a `code_references_counts` | ||
|
|
||
| NOTE: This adds compatibility with `CodeReferenceRepositoryCountSerializer` | ||
| while preventing N+1 queries from the serializer. | ||
| """ | ||
| history_delta = timedelta(days=FEATURE_FLAG_CODE_REFERENCES_RETENTION_DAYS) | ||
| last_feature_found_at = ( | ||
| FeatureFlagCodeReferencesScan.objects.annotate( | ||
| feature_name=OuterRef("feature_name"), | ||
| contains_feature_name=Func( | ||
| F("code_references"), | ||
| Value("$[*] ? (@.feature_name == $feature_name)"), | ||
| JSONObject(feature_name=F("feature_name")), | ||
| function="jsonb_path_exists", | ||
| output_field=BooleanField(), | ||
| ), | ||
| ) | ||
| .filter( | ||
| project=OuterRef("project_id"), | ||
| created_at__gte=timezone.now() - history_delta, | ||
| repository_url=OuterRef("repository_url"), | ||
| contains_feature_name=True, | ||
| ) | ||
| .values("created_at") | ||
| .order_by("-created_at")[:1] | ||
| ) | ||
| counts_by_repository = ( | ||
| FeatureFlagCodeReferencesScan.objects | ||
| # Count code references from JSON matching the feature name | ||
| .annotate( | ||
| feature_name=OuterRef("name"), | ||
| last_feature_found_at=Subquery(last_feature_found_at), | ||
| count=Func( | ||
| Func( | ||
| F("code_references"), | ||
| Value("$[*] ? (@.feature_name == $feature_name)"), | ||
| JSONObject(feature_name=F("feature_name")), | ||
| function="jsonb_path_query_array", | ||
| ), | ||
| function="jsonb_array_length", | ||
| ), | ||
| ) | ||
| # Only from the latest scans of each repository | ||
| .filter( | ||
| created_at__gte=timezone.now() - history_delta, | ||
| project_id=OuterRef("project_id"), | ||
|
emyller marked this conversation as resolved.
|
||
| ) | ||
| .order_by("repository_url", "-created_at") | ||
| .distinct("repository_url") | ||
| .values( | ||
| json=JSONObject( | ||
| repository_url=F("repository_url"), | ||
| count=F("count"), | ||
| last_successful_repository_scanned_at=F("created_at"), | ||
| last_feature_found_at=F("last_feature_found_at"), | ||
| ), | ||
| ) | ||
| ) | ||
|
|
||
| return queryset.annotate( | ||
| code_references_counts=ArraySubquery(counts_by_repository), | ||
| ) | ||
|
|
||
|
|
||
| def get_code_references_for_feature_flag( | ||
| feature: Feature, | ||
| ) -> list[FeatureFlagCodeReferencesRepositorySummary]: | ||
| """Obtain a summary of latest code references for a feature | ||
|
|
||
| Only query from the latest scans of each repository_url. This is used to | ||
| populate `FeatureFlagCodeReferencesSerializer`. | ||
| """ | ||
| history_delta = timedelta(days=FEATURE_FLAG_CODE_REFERENCES_RETENTION_DAYS) | ||
| last_feature_found_at = ( | ||
| FeatureFlagCodeReferencesScan.objects.filter( | ||
| project=feature.project, | ||
| created_at__gte=timezone.now() - history_delta, | ||
| repository_url=OuterRef("repository_url"), | ||
| code_references__contains=[{"feature_name": feature.name}], | ||
| ) | ||
| .values("created_at") | ||
| .order_by("-created_at")[:1] | ||
| ) | ||
|
|
||
| last_scans_of_each_repository = ( | ||
| FeatureFlagCodeReferencesScan.objects.filter(project=feature.project) | ||
| .annotate(last_feature_found_at=Subquery(last_feature_found_at)) | ||
| .order_by("repository_url", "-created_at") | ||
| .distinct("repository_url") | ||
| ) | ||
|
|
||
| return [ | ||
| FeatureFlagCodeReferencesRepositorySummary( | ||
| repository_url=scan.repository_url, | ||
| vcs_provider=VCSProvider(scan.vcs_provider), | ||
| revision=scan.revision, | ||
| last_successful_repository_scanned_at=scan.created_at, | ||
| last_feature_found_at=scan.last_feature_found_at, | ||
| code_references=[ | ||
| CodeReference( | ||
| feature_name=feature.name, | ||
| file_path=reference["file_path"], | ||
| line_number=reference["line_number"], | ||
| permalink=_get_permalink( | ||
| provider=VCSProvider(scan.vcs_provider), | ||
| repository_url=scan.repository_url, | ||
| revision=scan.revision, | ||
| file_path=reference["file_path"], | ||
| line_number=reference["line_number"], | ||
| ), | ||
| ) | ||
| for reference in scan.code_references | ||
| if reference["feature_name"] == feature.name | ||
| ], | ||
| ) | ||
| for scan in last_scans_of_each_repository | ||
| ] | ||
|
|
||
|
|
||
| def _get_permalink( | ||
| provider: VCSProvider, | ||
| repository_url: str, | ||
| revision: str, | ||
| file_path: str, | ||
| line_number: int, | ||
| ) -> str: | ||
| """Generate a permalink for the code reference.""" | ||
| match provider: | ||
| case VCSProvider.GITHUB: | ||
| return urljoin( | ||
| repository_url, f"blob/{revision}/{file_path}#L{line_number}" | ||
| ) | ||
| raise NotImplementedError( # pragma: no cover | ||
| f"Permalink generation for {provider} is not implemented." | ||
| ) | ||
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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import datetime | ||
| from typing import TypedDict | ||
|
|
||
| from django.db.models import TextChoices | ||
|
|
||
|
|
||
| class VCSProvider(TextChoices): | ||
| GITHUB = "github", "GitHub" | ||
|
|
||
|
|
||
| class JSONCodeReference(TypedDict): | ||
| feature_name: str | ||
| file_path: str | ||
| line_number: int | ||
|
|
||
|
|
||
| @dataclass | ||
| class CodeReference: | ||
| feature_name: str | ||
| file_path: str | ||
| line_number: int | ||
| permalink: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class FeatureFlagCodeReferencesRepositorySummary: | ||
| repository_url: str | ||
| vcs_provider: VCSProvider | ||
| revision: str | ||
| last_successful_repository_scanned_at: datetime | ||
| last_feature_found_at: datetime | None | ||
| code_references: list[CodeReference] | ||
|
|
||
|
|
||
| @dataclass | ||
| class CodeReferencesRepositoryCount: | ||
|
emyller marked this conversation as resolved.
|
||
| repository_url: str | ||
| count: int | ||
| last_successful_repository_scanned_at: datetime | ||
| last_feature_found_at: datetime | None | ||
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.