Skip to content
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: Move call to GitHub integration tasks out from trigger_feature_state_change_webhooks #3905

34 changes: 34 additions & 0 deletions api/features/signals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import logging
from dataclasses import asdict

from django.db.models.signals import post_save
from django.dispatch import receiver

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

# noinspection PyUnresolvedReferences
from .models import FeatureState
from .tasks import trigger_feature_state_change_webhooks
Expand All @@ -12,6 +18,34 @@

@receiver(post_save, sender=FeatureState)
def trigger_feature_state_change_webhooks_signal(instance, **kwargs):
khvn26 marked this conversation as resolved.
Show resolved Hide resolved
# Enqueue Github integration tasks if feature has GitHub external resources
if (
not instance.identity_id
and not instance.feature_segment
and instance.feature.external_resources.exists()
and instance.environment.project.github_project.exists()
and hasattr(instance.environment.project.organisation, "github_config")
khvn26 marked this conversation as resolved.
Show resolved Hide resolved
):
github_configuration = (

Check warning on line 29 in api/features/signals.py

View check run for this annotation

Codecov / codecov/patch

api/features/signals.py#L29

Added line #L29 was not covered by tests
Organisation.objects.prefetch_related("github_config")
.get(id=instance.environment.project.organisation_id)
.github_config.first()
)
feature_states = []
feature_states.append(instance)

Check warning on line 35 in api/features/signals.py

View check run for this annotation

Codecov / codecov/patch

api/features/signals.py#L34-L35

Added lines #L34 - L35 were not covered by tests

feature_data: GithubData = generate_data(

Check warning on line 37 in api/features/signals.py

View check run for this annotation

Codecov / codecov/patch

api/features/signals.py#L37

Added line #L37 was not covered by tests
github_configuration=github_configuration,
feature_id=instance.feature.id,
feature_name=instance.feature.name,
type=WebhookEventType.FLAG_UPDATED.value,
feature_states=feature_states,
)

call_github_app_webhook_for_feature_state.delay(

Check warning on line 45 in api/features/signals.py

View check run for this annotation

Codecov / codecov/patch

api/features/signals.py#L45

Added line #L45 was not covered by tests
args=(asdict(feature_data),),
)

if instance.environment_feature_version_id:
return
trigger_feature_state_change_webhooks(instance)
37 changes: 0 additions & 37 deletions api/features/tasks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import logging
from dataclasses import asdict

from environments.models import Webhook
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 @@ -59,39 +55,6 @@ def trigger_feature_state_change_webhooks(
)
)

if (
not instance.identity_id
and not instance.feature_segment
and instance.feature.external_resources.exists()
and instance.environment.project.github_project.exists()
and hasattr(instance.environment.project.organisation, "github_config")
):
github_configuration = (
Organisation.objects.prefetch_related("github_config")
.get(id=instance.environment.project.organisationn_id)
.github_config.first()
)
feature_state = {
"environment_name": new_state["environment"]["name"],
"feature_value": new_state["enabled"],
}
feature_states = []
feature_states.append(instance)

feature_data: GithubData = generate_data(
github_configuration=github_configuration,
feature_id=history_instance.feature.id,
feature_name=history_instance.feature.name,
type=WebhookEventType.FLAG_UPDATED.value,
feature_states=feature_states,
)

feature_data.feature_states.append(feature_state)

call_github_app_webhook_for_feature_state.delay(
args=(asdict(feature_data),),
)


def _get_previous_state(
instance: FeatureState,
Expand Down