-
Notifications
You must be signed in to change notification settings - Fork 561
feat(github-poc): Submit feature flag code references #5928
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
14 commits
Select commit
Hold shift + click to select a range
5e61fb3
Introduce VCS code references data model
emyller f833098
Prefer auto_now_add
emyller a108c13
Submit code references
emyller 394914c
Oops
emyller 3082123
Improve URL
emyller 85e8495
Experiment uploading code references
emyller 15986c3
Wrap up 🎁
emyller 6ddc4a8
_Improve_ coverage
emyller 1a51c3e
💅
emyller 0b4e5a1
✨
emyller c4c7245
Let's just requests
emyller 862e87d
For each thing its own turn
emyller c5d2593
Simplify CI
emyller 794f3ef
Improve designating relation to the VCS
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
Empty file.
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,6 @@ | ||
| from core.apps import BaseAppConfig | ||
|
|
||
|
|
||
| class CodeReferencesConfig(BaseAppConfig): | ||
| name = "projects.code_references" | ||
| default = True |
51 changes: 51 additions & 0 deletions
51
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Generated by Django 4.2.22 on 2025-08-14 15:12 | ||
|
|
||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("projects", "0027_add_create_project_level_change_requests_permission"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="FeatureFlagCodeReferencesScan", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("repository_url", models.URLField()), | ||
| ( | ||
| "vcs_provider", | ||
| models.CharField( | ||
| choices=[("github", "GitHub")], default="github", max_length=50 | ||
| ), | ||
| ), | ||
| ("revision", models.CharField(max_length=100)), | ||
| ("code_references", models.JSONField(default=list)), | ||
| ("created_at", models.DateTimeField(auto_now_add=True, db_index=True)), | ||
| ( | ||
| "project", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="code_references", | ||
| to="projects.project", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "ordering": ["-created_at"], | ||
| }, | ||
| ), | ||
| ] |
Empty file.
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,32 @@ | ||
| from django.db import models | ||
|
|
||
|
|
||
| class FeatureFlagCodeReferencesScan(models.Model): | ||
| """ | ||
| A scan of feature flag code references in a repository | ||
| """ | ||
|
|
||
| class Providers(models.TextChoices): | ||
| GITHUB = "github", "GitHub" | ||
|
|
||
| project = models.ForeignKey( | ||
| "projects.Project", | ||
| on_delete=models.CASCADE, | ||
| related_name="code_references", | ||
| ) | ||
|
|
||
| # Provider-agnostic URL to the web UI of the repository, e.g. https://github.flagsmith.com/backend/ | ||
| repository_url = models.URLField() | ||
|
|
||
| vcs_provider = models.CharField( | ||
| max_length=50, | ||
| choices=Providers.choices, | ||
| default=Providers.GITHUB, # TODO: Remove when adding other providers | ||
| ) | ||
| revision = models.CharField(max_length=100) | ||
| code_references = models.JSONField(default=list) | ||
|
khvn26 marked this conversation as resolved.
|
||
|
|
||
| created_at = models.DateTimeField(auto_now_add=True, db_index=True) | ||
|
|
||
| class Meta: | ||
| ordering = ["-created_at"] | ||
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,19 @@ | ||
| from common.projects.permissions import VIEW_PROJECT | ||
| from rest_framework.permissions import IsAuthenticated | ||
| from rest_framework.request import Request | ||
| from rest_framework.views import APIView | ||
|
|
||
| from projects.models import Project | ||
| from users.models import FFAdminUser | ||
|
|
||
|
|
||
| class SubmitFeatureFlagCodeReferences(IsAuthenticated): | ||
| def has_permission(self, request: Request, view: APIView) -> bool: | ||
| if not super().has_permission(request, view): | ||
| return False | ||
|
|
||
| if not isinstance(request.user, FFAdminUser): # pragma: no cover | ||
| return False | ||
|
|
||
| project = Project.objects.get(id=view.kwargs["project_pk"]) | ||
| return request.user.has_project_permission(VIEW_PROJECT, project) |
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,39 @@ | ||
| from typing import TypedDict | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
| from projects.code_references.models import FeatureFlagCodeReferencesScan | ||
|
|
||
|
|
||
| class _CodeReference(TypedDict): | ||
| feature_name: str | ||
| file_path: str | ||
| line_number: int | ||
|
khvn26 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class _CodeReferenceSerializer(serializers.Serializer[_CodeReference]): | ||
| feature_name = serializers.CharField(max_length=100) | ||
| file_path = serializers.CharField(max_length=200) | ||
| line_number = serializers.IntegerField(min_value=1) | ||
|
|
||
|
|
||
| class FeatureFlagCodeReferencesScanSerializer( | ||
| serializers.ModelSerializer[FeatureFlagCodeReferencesScan], | ||
| ): | ||
| code_references = _CodeReferenceSerializer( | ||
| many=True, required=True, allow_empty=False | ||
| ) | ||
|
|
||
| class Meta: | ||
| model = FeatureFlagCodeReferencesScan | ||
| fields = [ | ||
| "created_at", | ||
| "repository_url", | ||
| "project", | ||
| "revision", | ||
| "code_references", | ||
| ] | ||
| read_only_fields = [ | ||
| "created_at", | ||
| "project", | ||
| ] | ||
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,13 @@ | ||
| from django.urls import path | ||
|
|
||
| from projects.code_references import views | ||
|
|
||
| app_name = "code_references" | ||
|
|
||
| urlpatterns = [ | ||
| path( | ||
| "projects/<int:project_pk>/code-references/", | ||
| views.FeatureFlagCodeReferencesScanCreateAPIView.as_view(), | ||
| name="code_reference_create", | ||
| ), | ||
| ] |
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,21 @@ | ||
| from rest_framework import generics | ||
|
|
||
| from projects.code_references.models import FeatureFlagCodeReferencesScan | ||
| from projects.code_references.permissions import SubmitFeatureFlagCodeReferences | ||
| from projects.code_references.serializers import FeatureFlagCodeReferencesScanSerializer | ||
|
|
||
|
|
||
| class FeatureFlagCodeReferencesScanCreateAPIView( | ||
| generics.CreateAPIView[FeatureFlagCodeReferencesScan] | ||
| ): | ||
| """ | ||
| API view to create code references for a project | ||
| """ | ||
|
|
||
| serializer_class = FeatureFlagCodeReferencesScanSerializer | ||
| permission_classes = [SubmitFeatureFlagCodeReferences] | ||
|
|
||
| def perform_create( # type: ignore[override] | ||
| self, serializer: FeatureFlagCodeReferencesScanSerializer | ||
| ) -> None: | ||
| serializer.save(project_id=self.kwargs["project_pk"]) |
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.