From 78849d2e43c4e5c7318518497067f0447623f0c2 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Mon, 18 Sep 2023 14:49:21 -0300 Subject: [PATCH] Fix returned shift PK in current user events endpoint (#3036) --- CHANGELOG.md | 1 + engine/apps/api/tests/test_schedules.py | 5 ++- engine/apps/schedules/constants.py | 1 + engine/apps/schedules/ical_utils.py | 31 +++++++++++-------- .../apps/schedules/tests/test_ical_utils.py | 9 ++++++ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b67f826dd..261a8bd02f 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 ### Fixed - Fix Slack access token length issue by @toolchainX ([#3016](https://github.com/grafana/oncall/pull/3016)) +- Fix shifts for current user internal endpoint to return the right shift PK ([#3036](https://github.com/grafana/oncall/pull/3036)) ## v1.3.37 (2023-09-12) diff --git a/engine/apps/api/tests/test_schedules.py b/engine/apps/api/tests/test_schedules.py index f5ffb8cede..50e6d508ac 100644 --- a/engine/apps/api/tests/test_schedules.py +++ b/engine/apps/api/tests/test_schedules.py @@ -2115,8 +2115,8 @@ def test_current_user_events( shifts = ( # schedule, user, priority, start time (h), duration (seconds) - (schedule_with_current_user, current_user, 1, 0, (24 * 60 * 60) - 1), # r1-1: 0-23:59:59 (other_schedule, other_user, 1, 0, (24 * 60 * 60) - 1), # r1-1: 0-23:59:59 + (schedule_with_current_user, current_user, 1, 0, (24 * 60 * 60) - 1), # r1-1: 0-23:59:59 ) now = timezone.now() today = now.replace(hour=0, minute=0, second=0, microsecond=0) @@ -2146,6 +2146,9 @@ def test_current_user_events( assert result["schedules"][0]["id"] == schedule_with_current_user.public_primary_key assert result["schedules"][0]["name"] == schedule_with_current_user.name assert len(result["schedules"][0]["events"]) > 0 + for event in result["schedules"][0]["events"]: + # check the current user shift pk is set in the event + assert event["shift"]["pk"] == on_call_shift.public_primary_key @pytest.mark.django_db diff --git a/engine/apps/schedules/constants.py b/engine/apps/schedules/constants.py index 317a609f7f..818ddeb1c7 100644 --- a/engine/apps/schedules/constants.py +++ b/engine/apps/schedules/constants.py @@ -18,6 +18,7 @@ ICAL_STATUS_CANCELLED = "CANCELLED" ICAL_COMPONENT_VEVENT = "VEVENT" RE_PRIORITY = re.compile(r"^\[L(\d+)\]") +RE_EVENT_UID_EXPORT = re.compile(r"([\w\d]+)-(\d+)-([\w\d]+)") RE_EVENT_UID_V1 = re.compile(r"amixr-([\w\d-]+)-U(\d+)-E(\d+)-S(\d+)") RE_EVENT_UID_V2 = re.compile(r"oncall-([\w\d-]+)-PK([\w\d]+)-U(\d+)-E(\d+)-S(\d+)") diff --git a/engine/apps/schedules/ical_utils.py b/engine/apps/schedules/ical_utils.py index 5f3c82fa2f..5a48767762 100644 --- a/engine/apps/schedules/ical_utils.py +++ b/engine/apps/schedules/ical_utils.py @@ -29,6 +29,7 @@ ICAL_STATUS_CANCELLED, ICAL_SUMMARY, ICAL_UID, + RE_EVENT_UID_EXPORT, RE_EVENT_UID_V1, RE_EVENT_UID_V2, RE_PRIORITY, @@ -421,21 +422,25 @@ def parse_event_uid(string: str, sequence: str = None, recurrence_id: str = None if match: _, pk, _, _, source = match.groups() else: - # eventually this path would be automatically deprecated - # once all ical representations are refreshed - match = RE_EVENT_UID_V1.match(string) + match = RE_EVENT_UID_EXPORT.match(string) if match: - _, _, _, source = match.groups() + pk, _, _ = match.groups() else: - # fallback to use the UID string as the rotation ID - pk = string - # in ical imported calendars, sequence and/or recurrence_id - # distinguish main recurring event vs instance modification - # (see https://icalendar.org/iCalendar-RFC-5545/3-8-4-4-recurrence-id.html) - if sequence: - pk = f"{pk}_{sequence}" - if recurrence_id: - pk = f"{pk}_{recurrence_id}" + # eventually this path would be automatically deprecated + # once all ical representations are refreshed + match = RE_EVENT_UID_V1.match(string) + if match: + _, _, _, source = match.groups() + else: + # fallback to use the UID string as the rotation ID + pk = string + # in ical imported calendars, sequence and/or recurrence_id + # distinguish main recurring event vs instance modification + # (see https://icalendar.org/iCalendar-RFC-5545/3-8-4-4-recurrence-id.html) + if sequence: + pk = f"{pk}_{sequence}" + if recurrence_id: + pk = f"{pk}_{recurrence_id}" if source is not None: source = int(source) diff --git a/engine/apps/schedules/tests/test_ical_utils.py b/engine/apps/schedules/tests/test_ical_utils.py index c6520c9625..77368fe359 100644 --- a/engine/apps/schedules/tests/test_ical_utils.py +++ b/engine/apps/schedules/tests/test_ical_utils.py @@ -317,6 +317,15 @@ def test_shifts_dict_from_cached_final( assert shifts == expected_events +def test_parse_event_uid_from_export(): + shift_pk = "OUCE6WAHL35PP" + user_pk = "UHZ38D6AQXXBY" + event_uid = f"{shift_pk}-202309200300-{user_pk}" + pk, source = parse_event_uid(event_uid) + assert pk == shift_pk + assert source is None + + def test_parse_event_uid_v1(): uuid = uuid4() event_uid = f"amixr-{uuid}-U1-E2-S1"