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

augment API response pagination attributes #2471

Merged
merged 13 commits into from
Jul 14, 2023
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Add `page_size`, `current_page_number`, and `total_pages` attributes to paginated API responses by @joeyorlando ([#2471](https://github.com/grafana/oncall/pull/2471))

## v1.3.11 (2023-07-13)

### Added
Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/alertgroups.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ The above command returns JSON structured in the following way:
"telegram": "https://t.me/c/5354/1234?thread=1234"
}
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ The above command returns JSON structured in the following way:
]
}
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/escalation_chains.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ The above command returns JSON structured in the following way:
"name": "default",
"team_id": null
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/escalation_policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ The above command returns JSON structured in the following way:
"type": "notify_person_next_each_time",
"persons_to_notify_next_each_time": ["U4DNY931HHJS5"]
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
7 changes: 5 additions & 2 deletions docs/sources/oncall-api-reference/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The above command returns JSON structured in the following way:
"channel_id": "CH23212D"
}
},
"templates": {
"templates": {
"grouping_key": null,
"resolve_signal": null,
"acknowledge_signal": null,
Expand Down Expand Up @@ -219,7 +219,10 @@ The above command returns JSON structured in the following way:
}
}
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/on_call_shifts.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ The above command returns JSON structured in the following way:
"by_monthday": null,
"users": ["U4DNY931HHJS5"]
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/outgoing_webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ The above command returns JSON structured in the following way:
"id": "KGEFG74LU1D8L",
"name": "Publish alert group notification to JIRA"
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ The above command returns JSON structured in the following ways:
"important": true,
"type": "notify_by_phone_call"
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
13 changes: 8 additions & 5 deletions docs/sources/oncall-api-reference/resolution_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ The above command returns JSON structured in the following way:
}
```

| Parameter | Required | Description |
| --------------- | :------: | :--------------------- |
| `alert_group_id`| Yes | Alert group ID | |
| `text` | Yes | Resolution note text |
| Parameter | Required | Description |
| ---------------- | :------: | :------------------- | --- |
| `alert_group_id` | Yes | Alert group ID | |
| `text` | Yes | Resolution note text |

**HTTP request**

Expand Down Expand Up @@ -90,7 +90,10 @@ The above command returns JSON structured in the following way:
"created_at": "2020-06-19T12:40:01.429805Z",
"text": "Demo resolution note"
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ The above command returns JSON structured in the following way:
"channel_id": "CH23212D"
}
}
]
],
"current_page_number": 1,
"page_size": 25,
"total_pages": 1
}
```

Expand Down
10 changes: 8 additions & 2 deletions docs/sources/oncall-api-reference/schedules.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ The above command returns JSON structured in the following way:
"user_group_id": "MEOW_SLACK_ID"
}
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down Expand Up @@ -294,7 +297,10 @@ The above command returns JSON structured in the following way:
"shift_start": "2023-01-27T09:00:00Z",
"shift_end": "2023-01-27T17:00:00Z"
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/slack_channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ The above command returns JSON structured in the following way:
"name": "meow_channel",
"slack_id": "MEOW_SLACK_ID"
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/user_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ The above command returns JSON structured in the following way:
"handle": "meow_group"
}
}
]
],
"current_page_number": 1,
"page_size": 50,
"total_pages": 1
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/oncall-api-reference/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ The above command returns JSON structured in the following way:
"username": "alex",
"role": "admin"
}
]
],
"current_page_number": 1,
"page_size": 100,
"total_pages": 1
}
```

Expand Down
9 changes: 3 additions & 6 deletions engine/apps/alerts/models/alert_receive_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ def heartbeat_module(self):
return getattr(heartbeat, self.INTEGRATIONS_TO_REVERSE_URL_MAP[self.integration], None)

# Demo alerts
def send_demo_alert(self, force_route_id=None, payload=None):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

force_route_id seemed to only be passed in when this method was called from ChannelFilter. send_demo_alert (which was already deprecated and is now removed in this PR)

logger.info(f"send_demo_alert integration={self.pk} force_route_id={force_route_id}")
def send_demo_alert(self, payload=None):
logger.info(f"send_demo_alert integration={self.pk}")

if not self.is_demo_alert_enabled:
raise UnableToSendDemoAlert("Unable to send demo alert for this integration.")
Expand All @@ -543,9 +543,7 @@ def send_demo_alert(self, force_route_id=None, payload=None):
"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
)
create_alertmanager_alerts.delay(alert_receive_channel_pk=self.pk, alert=alert, is_demo=True)
else:
create_alert.delay(
title="Demo alert",
Expand All @@ -556,7 +554,6 @@ def send_demo_alert(self, force_route_id=None, payload=None):
integration_unique_data=None,
raw_request_data=payload,
is_demo=True,
force_route_id=force_route_id,
)

@property
Expand Down
5 changes: 0 additions & 5 deletions engine/apps/alerts/models/channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ def str_for_clients(self):
return str(self.filtering_term).replace("`", "")
raise Exception("Unknown filtering term")

def send_demo_alert(self):
"""Deprecated. May be used in the older versions of the plugin"""
integration = self.alert_receive_channel
integration.send_demo_alert(force_route_id=self.pk)

# Insight logs
@property
def insight_logs_type_verbal(self):
Expand Down
2 changes: 0 additions & 2 deletions engine/apps/alerts/tests/test_alert_receiver_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def test_send_demo_alert(mocked_create_alert, make_organization, make_alert_rece
mocked_create_alert.call_args.args[1]["raw_request_data"] == payload
or alert_receive_channel.config.example_payload
)
assert mocked_create_alert.call_args.args[1]["force_route_id"] is None


@mock.patch("apps.integrations.tasks.create_alertmanager_alerts.apply_async", return_value=None)
Expand Down Expand Up @@ -143,7 +142,6 @@ def test_send_demo_alert_alertmanager_payload_shape(
if payload
else alert_receive_channel.config.example_payload["alerts"][0]
)
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)
Expand Down
49 changes: 1 addition & 48 deletions engine/apps/alerts/tests/test_channel_filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from unittest import mock

import pytest

from apps.alerts.models import AlertReceiveChannel, ChannelFilter
from apps.alerts.models import ChannelFilter


@pytest.mark.django_db
Expand Down Expand Up @@ -93,48 +91,3 @@ def test_channel_filter_select_filter_jinja2(make_organization, make_alert_recei
alert_receive_channel, raw_request_data, force_route_id=channel_filter.pk
)
assert satisfied_filter == channel_filter


@mock.patch("apps.integrations.tasks.create_alert.apply_async", return_value=None)
@pytest.mark.django_db
def test_send_demo_alert(
mocked_create_alert,
make_organization,
make_alert_receive_channel,
make_channel_filter,
):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(
organization, integration=AlertReceiveChannel.INTEGRATION_WEBHOOK
)
filtering_term = "test alert"
channel_filter = make_channel_filter(alert_receive_channel, filtering_term=filtering_term, is_default=False)

channel_filter.send_demo_alert()
assert mocked_create_alert.called
assert mocked_create_alert.call_args.args[1]["is_demo"]
assert mocked_create_alert.call_args.args[1]["force_route_id"] == channel_filter.id


@mock.patch("apps.integrations.tasks.create_alertmanager_alerts.apply_async", return_value=None)
@pytest.mark.django_db
@pytest.mark.parametrize(
"integration",
[
AlertReceiveChannel.INTEGRATION_ALERTMANAGER,
AlertReceiveChannel.INTEGRATION_GRAFANA,
AlertReceiveChannel.INTEGRATION_GRAFANA_ALERTING,
],
)
def test_send_demo_alert_alertmanager_payload_shape(
mocked_create_alert, make_organization, make_alert_receive_channel, make_channel_filter, integration
):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(organization)
filtering_term = "test alert"
channel_filter = make_channel_filter(alert_receive_channel, filtering_term=filtering_term, is_default=False)

channel_filter.send_demo_alert()
assert mocked_create_alert.called
assert mocked_create_alert.call_args.args[1]["is_demo"]
assert mocked_create_alert.call_args.args[1]["force_route_id"] == channel_filter.pk
35 changes: 0 additions & 35 deletions engine/apps/api/tests/test_channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,41 +219,6 @@ def test_channel_filter_move_to_position_permissions(
assert response.status_code == expected_status


@pytest.mark.django_db
@pytest.mark.parametrize(
"role,expected_status",
[
(LegacyAccessControlRole.ADMIN, status.HTTP_200_OK),
(LegacyAccessControlRole.EDITOR, status.HTTP_200_OK),
(LegacyAccessControlRole.VIEWER, status.HTTP_403_FORBIDDEN),
],
)
def test_alert_receive_channel_send_demo_alert_permissions(
make_organization_and_user_with_plugin_token,
make_user_auth_headers,
make_alert_receive_channel,
make_channel_filter,
role,
expected_status,
):
organization, user, token = make_organization_and_user_with_plugin_token(role)
alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(alert_receive_channel, is_default=True)
client = APIClient()

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

with patch(
"apps.api.views.channel_filter.ChannelFilterView.send_demo_alert",
return_value=Response(
status=status.HTTP_200_OK,
),
):
response = client.post(url, format="json", **make_user_auth_headers(user, token))

assert response.status_code == expected_status


@pytest.mark.django_db
def test_channel_filter_create_with_order(
make_organization_and_user_with_plugin_token,
Expand Down
Loading