-
Notifications
You must be signed in to change notification settings - Fork 429
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
fix: Delete feature external resources when GitHub integration was deleted #3836
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||
|
||||||
from core.models import SoftDeleteExportableModel | ||||||
from django.db import models | ||||||
from django_lifecycle import BEFORE_DELETE, LifecycleModelMixin, hook | ||||||
|
||||||
from organisations.models import Organisation | ||||||
|
||||||
|
@@ -21,7 +22,7 @@ def has_github_configuration(organisation_id: int) -> bool: | |||||
).exists() | ||||||
|
||||||
|
||||||
class GithubRepository(SoftDeleteExportableModel): | ||||||
class GithubRepository(LifecycleModelMixin, SoftDeleteExportableModel): | ||||||
github_configuration = models.ForeignKey( | ||||||
GithubConfiguration, related_name="repository_config", on_delete=models.CASCADE | ||||||
) | ||||||
|
@@ -47,3 +48,19 @@ class Meta: | |||||
name="unique_repository_data", | ||||||
) | ||||||
] | ||||||
|
||||||
@hook(BEFORE_DELETE) | ||||||
def delete_feature_external_respurces( | ||||||
self, | ||||||
) -> None: | ||||||
from features.feature_external_resources.models import ( | ||||||
FeatureExternalResource, | ||||||
) | ||||||
|
||||||
FeatureExternalResource.objects.filter( | ||||||
feature__in=self.project.features.all(), | ||||||
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. I'd avoid a
Suggested change
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. Done. |
||||||
type__in=[ | ||||||
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. Do you think we need to add an index on 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. Good idea, I added an index |
||||||
FeatureExternalResource.ResourceType.GITHUB_ISSUE, | ||||||
FeatureExternalResource.ResourceType.GITHUB_PR, | ||||||
], | ||||||
).delete() |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||
from rest_framework import status | ||||||
from rest_framework.test import APIClient | ||||||
|
||||||
from features.feature_external_resources.models import FeatureExternalResource | ||||||
from integrations.github.models import GithubConfiguration, GithubRepository | ||||||
from organisations.models import Organisation | ||||||
from projects.models import Project | ||||||
|
@@ -191,18 +192,28 @@ def test_cannot_create_github_repository_due_to_unique_constraint( | |||||
def test_github_delete_repository( | ||||||
client: APIClient, | ||||||
organisation: Organisation, | ||||||
feature_external_resource: FeatureExternalResource, | ||||||
github_configuration: GithubConfiguration, | ||||||
github_repository: GithubRepository, | ||||||
mocker, | ||||||
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.
Suggested change
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. Added |
||||||
) -> None: | ||||||
# Given | ||||||
mock_generate_token = mocker.patch( | ||||||
"integrations.github.github.generate_token", | ||||||
) | ||||||
mock_generate_token.return_value = "mocked_token" | ||||||
url = reverse( | ||||||
"api-v1:organisations:repositories-detail", | ||||||
args=[organisation.id, github_configuration.id, github_repository.id], | ||||||
) | ||||||
for feature in github_repository.project.features.all(): | ||||||
assert FeatureExternalResource.objects.filter(feature=feature).exists() | ||||||
# When | ||||||
response = client.delete(url) | ||||||
# Then | ||||||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||||||
for feature in github_repository.project.features.all(): | ||||||
assert not FeatureExternalResource.objects.filter(feature=feature).exists() | ||||||
|
||||||
|
||||||
def mocked_requests_get(*args, **kwargs): | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, was corrected