oncall-engine/engine/apps/public_api/serializers/schedules_ical.py
Joey Orlando 4a5c4263e0
feat: convert schedule.channel (char field) to schedule.slack_channel (foreign key) (#5199)
# What this PR does

`OnCallSchedule` equivalent of
https://github.com/grafana/oncall/pull/5191.

**NOTE**: merge after https://github.com/grafana/oncall/pull/5224 (so
that I can use some of the new serializer fields defined in there)

### Migration
```bash
Running migrations:                                                                                                                                                                                                │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Starting migration to populate slack_channel field.                                                                │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Total schedules to process: 1                                                                                      │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Schedule 26 updated with SlackChannel 2 (slack_id: C043LL6RTS7).                                                   │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Bulk updated 1 OnCallSchedules with their Slack channel.                                                           │
│ source=engine:app google_trace_id=none logger=apps.schedules.migrations.0019_auto_20241021_1735 Finished migration. Total schedules processed: 1. Schedules updated: 1. Missing SlackChannels: 0.                  │
│   Applying schedules.0019_auto_20241021_1735... OK
```

### Tested Public API
```txt
POST {{oncall_host}}/api/v1/schedules/
Authorization: {{oncall_api_key}}
Content-Type: application/json

{
    "name": "Demo testy testy2",
    "type": "web",
    "time_zone": "America/Los_Angeles",
    "slack": {
        "channel_id": "C05PPLYN1U1"
    }
}

HTTP/1.1 201 Created
Content-Type: application/json
Vary: Accept, Origin
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: DENY
Content-Length: 198
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin

{
  "id": "SBBN73UTUTVCE",
  "team_id": null,
  "name": "Demo testy testy2",
  "time_zone": "America/Los_Angeles",
  "on_call_now": [],
  "shifts": [],
  "slack": {
    "channel_id": "C05PPLYN1U1",
    "user_group_id": null
  },
  "type": "web"
}
```

### Tested via UI (eg; internal API)

https://www.loom.com/share/e66bf3468b144dd782da5eb6e0bfd0af

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-11-04 14:27:21 -05:00

83 lines
3 KiB
Python

from apps.public_api.serializers.schedules_base import ScheduleBaseSerializer
from apps.schedules.models import OnCallScheduleICal
from apps.schedules.tasks import (
drop_cached_ical_task,
refresh_ical_final_schedule,
schedule_notify_about_empty_shifts_in_schedule,
schedule_notify_about_gaps_in_schedule,
)
from common.api_helpers.custom_fields import TeamPrimaryKeyRelatedField
from common.api_helpers.utils import validate_ical_url
# TODO: update the following once we bump mypy to 1.11 (which supports generics)
# class ScheduleICalSerializer(ScheduleBaseSerializer[OnCallScheduleICal]):
class ScheduleICalSerializer(ScheduleBaseSerializer):
class Meta:
model = OnCallScheduleICal
fields = [
"id",
"team_id",
"name",
"ical_url_primary",
"ical_url_overrides",
"slack",
"on_call_now",
]
extra_kwargs = {
"ical_url_primary": {"required": True, "allow_null": False},
"ical_url_overrides": {"required": False, "allow_null": True},
}
def validate_ical_url_primary(self, url):
return validate_ical_url(url)
def validate_ical_url_overrides(self, url):
return validate_ical_url(url)
def create(self, validated_data):
created_schedule = super().create(validated_data)
# for iCal-based schedules we need to refresh final schedule information
refresh_ical_final_schedule.apply_async((created_schedule.pk,))
return created_schedule
class ScheduleICalUpdateSerializer(ScheduleICalSerializer):
team_id = TeamPrimaryKeyRelatedField(required=False, allow_null=True, source="team")
class Meta:
model = OnCallScheduleICal
fields = [
"id",
"team_id",
"name",
"ical_url_primary",
"ical_url_overrides",
"slack",
"on_call_now",
]
extra_kwargs = {
"name": {"required": False},
"ical_url_primary": {"required": False, "allow_null": False},
"ical_url_overrides": {"required": False, "allow_null": True},
}
def update(self, instance, validated_data):
ical_changed = False
validated_data = self._correct_validated_data(validated_data)
if "ical_url_primary" in validated_data and validated_data["ical_url_primary"] != instance.ical_url_primary:
ical_changed = True
if (
"ical_url_overrides" in validated_data
and validated_data["ical_url_overrides"] != instance.ical_url_overrides
):
ical_changed = True
if ical_changed:
drop_cached_ical_task.apply_async(
(instance.pk,),
)
schedule_notify_about_empty_shifts_in_schedule.apply_async((instance.pk,))
schedule_notify_about_gaps_in_schedule.apply_async((instance.pk,))
refresh_ical_final_schedule.apply_async((instance.pk,))
return super().update(instance, validated_data)