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 demo alert for inbound email #2081

Merged
merged 9 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix a bug with permissions for telegram user settings by @alexintech ([#2075](https://github.com/grafana/oncall/pull/2075))
- Fix orphaned messages in Slack by @vadimkerr ([#2023](https://github.com/grafana/oncall/pull/2023))
- Fix demo alert for inbound email integration by @vadimkerr ([#2081](https://github.com/grafana/oncall/pull/2081))

## v1.2.34 (2023-05-31)

Expand Down
57 changes: 25 additions & 32 deletions engine/apps/alerts/models/alert_receive_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,42 +519,35 @@ def heartbeat_module(self):
# Demo alerts
def send_demo_alert(self, force_route_id=None, payload=None):
logger.info(f"send_demo_alert integration={self.pk} force_route_id={force_route_id}")

if not self.is_demo_alert_enabled:
raise UnableToSendDemoAlert("Unable to send demo alert for this integration.")

if payload is None:
payload = self.config.example_payload
if self.is_demo_alert_enabled:
Copy link
Member Author

@vstpme vstpme Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to simplify things a bit and reduce the number of indentation levels.
The only logical change is checking for if not self.is_demo_alert_enabled before anything else.

if self.has_alertmanager_payload_structure:
if (alerts := payload.get("alerts", None)) and type(alerts) == list and len(alerts):
for alert in alerts:
create_alertmanager_alerts.apply_async(
[],
{
"alert_receive_channel_pk": self.pk,
"alert": alert,
"is_demo": True,
"force_route_id": force_route_id,
},
)
else:
raise UnableToSendDemoAlert(
"Unable to send demo alert as payload has no 'alerts' key, it is not array, or it is empty."
)
else:
create_alert.apply_async(
[],
{
"title": "Demo alert",
"message": "Demo alert",
"image_url": None,
"link_to_upstream_details": None,
"alert_receive_channel_pk": self.pk,
"integration_unique_data": None,
"raw_request_data": payload,
"is_demo": True,
"force_route_id": force_route_id,
},

if self.has_alertmanager_payload_structure:
alerts = payload.get("alerts", None)
if not isinstance(alerts, list) or not len(alerts):
raise UnableToSendDemoAlert(
"Unable to send demo alert as payload has no 'alerts' key, it is not array, or it is empty."
)
for alert in alerts:
create_alertmanager_alerts.delay(
alert_receive_channel_pk=self.pk, alert=alert, is_demo=True, force_route_id=force_route_id
)
else:
raise UnableToSendDemoAlert("Unable to send demo alert for this integration")
create_alert.delay(
title="Demo alert",
message="Demo alert",
image_url=None,
link_to_upstream_details=None,
alert_receive_channel_pk=self.pk,
integration_unique_data=None,
raw_request_data=payload,
is_demo=True,
force_route_id=force_route_id,
)

@property
def has_alertmanager_payload_structure(self):
Expand Down
16 changes: 16 additions & 0 deletions engine/apps/alerts/tests/test_alert_receiver_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from apps.alerts.models import AlertReceiveChannel
from common.api_helpers.utils import create_engine_url
from common.exceptions import UnableToSendDemoAlert


@pytest.mark.django_db
Expand Down Expand Up @@ -145,6 +146,21 @@ def test_send_demo_alert_alertmanager_payload_shape(
assert mocked_create_alert.call_args.args[1]["force_route_id"] is None


@mock.patch("apps.integrations.tasks.create_alert.apply_async", return_value=None)
@pytest.mark.parametrize(
"integration", [config.slug for config in AlertReceiveChannel._config if not config.is_demo_alert_enabled]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

)
@pytest.mark.django_db
def test_send_demo_alert_not_enabled(mocked_create_alert, make_organization, make_alert_receive_channel, integration):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(organization, integration=integration)

with pytest.raises(UnableToSendDemoAlert):
alert_receive_channel.send_demo_alert()

assert not mocked_create_alert.called


@pytest.mark.django_db
def test_notify_maintenance_no_general_channel(make_organization, make_alert_receive_channel):
organization = make_organization(general_log_channel_id=None)
Expand Down
23 changes: 23 additions & 0 deletions engine/apps/alerts/tests/test_default_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,26 @@ def test_default_templates_are_valid():
jinja_template_env.from_string(template)
except TemplateSyntaxError as e:
pytest.fail(e.message)


@pytest.mark.parametrize("config", AlertReceiveChannel._config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice ✌️

def test_is_demo_alert_enabled(config):
# is_demo_alert_enabled must be defined
try:
assert isinstance(config.is_demo_alert_enabled, bool), "is_demo_alert_enabled must be bool"
except AttributeError:
pytest.fail("is_demo_alert_enabled must be defined")

# example_payload must be defined
try:
assert config.example_payload is None or isinstance(
config.example_payload, dict
), "example_payload must be dict or None"
except AttributeError:
pytest.fail("example_payload must be defined")

# example_payload must be provided when is_demo_alert_enabled is True
if config.is_demo_alert_enabled:
assert config.example_payload, "example_payload must be defined and non-empty"
else:
assert config.example_payload is None, "example_payload must be None if is_demo_alert_enabled is False"
10 changes: 1 addition & 9 deletions engine/apps/api/serializers/alert_receive_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AlertReceiveChannelSerializer(EagerLoadingMixin, serializers.ModelSerializ
heartbeat = serializers.SerializerMethodField()
allow_delete = serializers.SerializerMethodField()
description_short = serializers.CharField(max_length=250, required=False, allow_null=True)
demo_alert_payload = serializers.SerializerMethodField()
demo_alert_payload = serializers.CharField(source="config.example_payload", read_only=True)
routes_count = serializers.SerializerMethodField()
connected_escalations_chains_count = serializers.SerializerMethodField()

Expand Down Expand Up @@ -162,14 +162,6 @@ def get_alert_count(self, obj):
def get_alert_groups_count(self, obj):
return 0

def get_demo_alert_payload(self, obj):
if obj.is_demo_alert_enabled:
try:
return obj.config.example_payload
except AttributeError:
return "{}"
return None

def get_routes_count(self, obj) -> int:
return obj.channel_filters.count()

Expand Down
42 changes: 42 additions & 0 deletions engine/apps/api/tests/test_alert_receive_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,45 @@ def test_stop_maintenance_integration(
assert alert_receive_channel.maintenance_uuid is None
assert alert_receive_channel.maintenance_started_at is None
assert alert_receive_channel.maintenance_author is None


@pytest.mark.django_db
def test_alert_receive_channel_send_demo_alert(
make_organization_and_user_with_plugin_token,
make_user_auth_headers,
make_alert_receive_channel,
):
organization, user, token = make_organization_and_user_with_plugin_token()
alert_receive_channel = make_alert_receive_channel(
organization, integration=AlertReceiveChannel.INTEGRATION_GRAFANA
)
client = APIClient()

url = reverse(
"api-internal:alert_receive_channel-send-demo-alert",
kwargs={"pk": alert_receive_channel.public_primary_key},
)

response = client.post(url, format="json", **make_user_auth_headers(user, token))
assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
def test_alert_receive_channel_send_demo_alert_not_enabled(
make_organization_and_user_with_plugin_token,
make_user_auth_headers,
make_alert_receive_channel,
):
organization, user, token = make_organization_and_user_with_plugin_token()
alert_receive_channel = make_alert_receive_channel(
organization, integration=AlertReceiveChannel.INTEGRATION_DIRECT_PAGING
)
client = APIClient()

url = reverse(
"api-internal:alert_receive_channel-send-demo-alert",
kwargs={"pk": alert_receive_channel.public_primary_key},
)

response = client.post(url, format="json", **make_user_auth_headers(user, token))
assert response.status_code == status.HTTP_400_BAD_REQUEST
12 changes: 4 additions & 8 deletions engine/apps/api/views/alert_receive_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,16 @@ def get_queryset(self, eager=True, ignore_filtering_by_available_teams=False):
@action(detail=True, methods=["post"], throttle_classes=[DemoAlertThrottler])
def send_demo_alert(self, request, pk):
alert_receive_channel = AlertReceiveChannel.objects.get(public_primary_key=pk)
demo_alert_payload = request.data.get("demo_alert_payload", None)
payload = request.data.get("demo_alert_payload", None)

if not demo_alert_payload:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AlertReceiveChannel.send_demo_alert handles payload=None cases

# If no payload provided, use the demo payload for backword compatibility
payload = alert_receive_channel.config.example_payload
else:
if type(demo_alert_payload) != dict:
raise BadRequest(detail="Payload for demo alert must be a valid json object")
payload = demo_alert_payload
if payload is not None and not isinstance(payload, dict):
raise BadRequest(detail="Payload for demo alert must be a valid json object")

try:
alert_receive_channel.send_demo_alert(payload=payload)
except UnableToSendDemoAlert as e:
raise BadRequest(detail=str(e))

return Response(status=status.HTTP_200_OK)

@action(detail=False, methods=["get"])
Expand Down
2 changes: 2 additions & 0 deletions engine/config_integrations/direct_paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@
resolve_condition = None

acknowledge_condition = None

example_payload = None
2 changes: 1 addition & 1 deletion engine/config_integrations/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@

acknowledge_condition = None

example_payload = {"foo": "bar"}
example_payload = None
4 changes: 3 additions & 1 deletion engine/config_integrations/inbound_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
is_displayed_on_web = settings.FEATURE_INBOUND_EMAIL_ENABLED
is_featured = False
is_able_to_autoresolve = True
is_demo_alert_enabled = False
is_demo_alert_enabled = True


# Default templates
Expand Down Expand Up @@ -46,3 +46,5 @@
resolve_condition = '{{ payload.get("message", "").upper() == "OK" }}'

acknowledge_condition = None

example_payload = {"subject": "Test email subject", "message": "Test email message", "sender": "sender@example.com"}
4 changes: 3 additions & 1 deletion engine/config_integrations/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
is_displayed_on_web = False
is_featured = False
is_able_to_autoresolve = False
is_demo_alert_enabled = True
is_demo_alert_enabled = False

description = None

Expand Down Expand Up @@ -45,3 +45,5 @@
resolve_condition = None

acknowledge_condition = None

example_payload = None
2 changes: 2 additions & 0 deletions engine/config_integrations/manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@
resolve_condition = None

acknowledge_condition = None

example_payload = None
2 changes: 2 additions & 0 deletions engine/config_integrations/slack_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@
acknowledge_condition = None

source_link = '{{ payload.get("amixr_mixin", {}).get("permalink", "")}}'

example_payload = None