Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa committed May 2, 2024
1 parent 608c881 commit d70c4a7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
23 changes: 16 additions & 7 deletions api/features/feature_external_resources/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from features.models import Feature, FeatureState
from integrations.github.github import GithubData, generate_data
from integrations.github.tasks import call_github_app_webhook_for_feature_state
from organisations.models import Organisation
from webhooks.webhooks import WebhookEventType

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -47,11 +48,15 @@ class Meta:
]

@hook(AFTER_SAVE)
def exectute_after_save_actions(self):
def execute_after_save_actions(self):
# Add a comment to GitHub Issue/PR when feature is linked to the GH external resource
if github_configuration := self.feature.project.organisation.github_config.filter(
deleted_at__isnull=True
).first():
if (
github_configuration := Organisation.objects.prefetch_related(
"github_config"
)
.get(id=self.feature.project.organisation_id)
.github_config.first()
):
feature_states = FeatureState.objects.filter(
feature_id=self.feature_id, identity_id__isnull=True
)
Expand All @@ -70,9 +75,13 @@ def exectute_after_save_actions(self):
@hook(BEFORE_DELETE)
def execute_before_save_actions(self) -> None:
# Add a comment to GitHub Issue/PR when feature is unlinked to the GH external resource
if github_configuration := self.feature.project.organisation.github_config.filter(
deleted_at__isnull=True
).first():
if (
github_configuration := Organisation.objects.prefetch_related(
"github_config"
)
.get(id=self.feature.project.organisation_id)
.github_config.first()
):
feature_data: GithubData = generate_data(
github_configuration=github_configuration,
feature_id=self.feature_id,
Expand Down
13 changes: 10 additions & 3 deletions api/features/feature_external_resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from features.models import Feature
from features.permissions import FeatureExternalResourcePermissions
from organisations.models import Organisation

from .models import FeatureExternalResource
from .serializers import FeatureExternalResourceSerializer
Expand All @@ -29,9 +30,15 @@ def create(self, request, *args, **kwargs):
),
)

if not feature.project.organisation.github_config.filter(
deleted_at__isnull=True
).exists() or not hasattr(feature.project, "github_project"):
if not (
(
Organisation.objects.prefetch_related("github_config")
.get(id=feature.project.organisation_id)
.github_config.first()
)
or not hasattr(feature.project, "github_project")
):

return Response(
data={
"detail": "This Project doesn't have a valid GitHub integration configuration"
Expand Down
7 changes: 4 additions & 3 deletions api/features/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from features.models import Feature, FeatureState
from integrations.github.github import GithubData, generate_data
from integrations.github.tasks import call_github_app_webhook_for_feature_state
from organisations.models import Organisation
from task_processor.decorators import register_task_handler
from webhooks.constants import WEBHOOK_DATETIME_FORMAT
from webhooks.webhooks import (
Expand Down Expand Up @@ -66,9 +67,9 @@ def trigger_feature_state_change_webhooks(
and hasattr(instance.environment.project.organisation, "github_config")
):
github_configuration = (
instance.environment.project.organisation.github_config.get(
deleted_at__isnull=True
)
Organisation.objects.prefetch_related("github_config")
.get(id=instance.environment.project.organisationn_id)
.github_config.first()
)
feature_state = {
"environment_name": new_state["environment"]["name"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.25 on 2024-05-01 21:37
# Generated by Django 3.2.25 on 2024-05-02 19:49

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -19,6 +19,6 @@ class Migration(migrations.Migration):
),
migrations.AddConstraint(
model_name='githubconfiguration',
constraint=models.UniqueConstraint(fields=('organisation', 'installation_id'), name='unique_github_config_data'),
constraint=models.UniqueConstraint(condition=models.Q(('deleted_at__isnull', True)), fields=('organisation',), name='githubconf_organisation_id_idx'),
),
]
4 changes: 2 additions & 2 deletions api/integrations/github/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Meta:
models.UniqueConstraint(
fields=[
"organisation",
"installation_id",
],
name="unique_github_config_data",
name="githubconf_organisation_id_idx",
condition=models.Q(deleted_at__isnull=True),
)
]

Expand Down
5 changes: 0 additions & 5 deletions api/integrations/github/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ def get_queryset(self):
)

def create(self, request, *args, **kwargs):

organisation_id = self.kwargs["organisation_pk"]
if GithubConfiguration.has_github_configuration(organisation_id):
raise DuplicateGitHubIntegration

try:
return super().create(request, *args, **kwargs)
except IntegrityError:
Expand Down

0 comments on commit d70c4a7

Please sign in to comment.