From 9ead70a4ed575e89aa3cd55192a05bc9354da5c3 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Tue, 26 Sep 2023 14:04:53 -0300 Subject: [PATCH] Add schedules enable_web_overrides option to public API (#3062) Related updates (to be merged afterwards): - https://github.com/grafana/amixr-api-go-client/pull/14 - https://github.com/grafana/terraform-provider-grafana/pull/1056 --- CHANGELOG.md | 1 + .../sources/oncall-api-reference/schedules.md | 1 + .../serializers/schedules_calendar.py | 4 ++ .../apps/public_api/tests/test_schedules.py | 46 +++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d186aee0b..5515119410 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Unify breadcrumbs behaviour with other Grafana Apps and main core ([#1906](https://github.com/grafana/oncall/issues/1906)) +- Add `enable_web_overrides` option to schedules public API ([#3062](https://github.com/grafana/oncall/pull/3062)) ### Fixed diff --git a/docs/sources/oncall-api-reference/schedules.md b/docs/sources/oncall-api-reference/schedules.md index f9335ff5cf..9e89f42219 100644 --- a/docs/sources/oncall-api-reference/schedules.md +++ b/docs/sources/oncall-api-reference/schedules.md @@ -47,6 +47,7 @@ The above command returns JSON structured in the following way: | `time_zone` | No | Optional | Schedule time zone. Is used for manually added on-call shifts in Schedules with type `calendar`. Default time zone is `UTC`. For more information about time zones, see [time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). | | `ical_url_primary` | No | If type = `ical` | URL of external iCal calendar for schedule with type `ical`. | | `ical_url_overrides` | No | Optional | URL of external iCal calendar for schedule with any type. Events from this calendar override events from primary calendar or from on-call shifts. | +| `enable_web_overrides` | No | Optional | Whether to enable web overrides or not. Setting specific for API/Terraform based schedules (`calendar` type). | | `slack` | No | Optional | Dictionary with Slack-specific settings for a schedule. Includes `channel_id` and `user_group_id` fields, that take a channel ID and a user group ID from Slack. | | `shifts` | No | Optional | List of shifts. Used for manually added on-call shifts in Schedules with type `calendar`. | diff --git a/engine/apps/public_api/serializers/schedules_calendar.py b/engine/apps/public_api/serializers/schedules_calendar.py index a71b82bc1c..8f85208cbc 100644 --- a/engine/apps/public_api/serializers/schedules_calendar.py +++ b/engine/apps/public_api/serializers/schedules_calendar.py @@ -28,9 +28,11 @@ class Meta: "on_call_now", "shifts", "ical_url_overrides", + "enable_web_overrides", ] extra_kwargs = { "ical_url_overrides": {"required": False, "allow_null": True}, + "enable_web_overrides": {"required": False, "allow_null": True}, } def validate_shifts(self, shifts): @@ -61,10 +63,12 @@ class Meta: "on_call_now", "shifts", "ical_url_overrides", + "enable_web_overrides", ] extra_kwargs = { "name": {"required": False}, "ical_url_overrides": {"required": False, "allow_null": True}, + "enable_web_overrides": {"required": False, "allow_null": True}, } def update(self, instance, validated_data): diff --git a/engine/apps/public_api/tests/test_schedules.py b/engine/apps/public_api/tests/test_schedules.py index 3834c8df5d..2e1ed13e49 100644 --- a/engine/apps/public_api/tests/test_schedules.py +++ b/engine/apps/public_api/tests/test_schedules.py @@ -94,6 +94,7 @@ def test_get_calendar_schedule( "user_group_id": None, }, "ical_url_overrides": None, + "enable_web_overrides": False, } assert response.status_code == status.HTTP_200_OK @@ -130,6 +131,7 @@ def test_create_calendar_schedule(make_organization_and_user_with_token): "user_group_id": None, }, "ical_url_overrides": None, + "enable_web_overrides": False, } assert response.status_code == status.HTTP_201_CREATED @@ -180,6 +182,7 @@ def test_create_calendar_schedule_with_shifts(make_organization_and_user_with_to "user_group_id": None, }, "ical_url_overrides": None, + "enable_web_overrides": False, } assert response.status_code == status.HTTP_201_CREATED @@ -227,6 +230,7 @@ def test_update_calendar_schedule( "user_group_id": None, }, "ical_url_overrides": None, + "enable_web_overrides": False, } assert response.status_code == status.HTTP_200_OK @@ -236,6 +240,45 @@ def test_update_calendar_schedule( assert response.json() == result +@pytest.mark.django_db +def test_update_calendar_schedule_enable_web_overrides( + make_organization_and_user_with_token, + make_schedule, +): + organization, user, token = make_organization_and_user_with_token() + client = APIClient() + + schedule = make_schedule( + organization, + schedule_class=OnCallScheduleCalendar, + ) + + url = reverse("api-public:schedules-detail", kwargs={"pk": schedule.public_primary_key}) + + data = { + "enable_web_overrides": True, + } + response = client.put(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}") + + result = { + "id": schedule.public_primary_key, + "team_id": None, + "name": schedule.name, + "type": "calendar", + "time_zone": "UTC", + "on_call_now": [], + "shifts": [], + "slack": {"channel_id": None, "user_group_id": None}, + "ical_url_overrides": None, + "enable_web_overrides": True, + } + + assert response.status_code == status.HTTP_200_OK + schedule.refresh_from_db() + assert schedule.enable_web_overrides + assert response.json() == result + + @pytest.mark.django_db def test_get_web_schedule( make_organization_and_user_with_token, @@ -363,6 +406,7 @@ def test_update_ical_url_overrides_calendar_schedule( "user_group_id": None, }, "ical_url_overrides": ICAL_URL, + "enable_web_overrides": False, } assert response.status_code == status.HTTP_200_OK @@ -418,6 +462,7 @@ def test_update_calendar_schedule_with_custom_event( "user_group_id": None, }, "ical_url_overrides": None, + "enable_web_overrides": False, } assert response.status_code == status.HTTP_200_OK @@ -732,6 +777,7 @@ def test_get_schedule_list( "shifts": [], "slack": {"channel_id": slack_channel_id, "user_group_id": user_group_id}, "ical_url_overrides": None, + "enable_web_overrides": False, }, { "id": schedule_ical.public_primary_key,