Skip to content

Commit

Permalink
Fix returned shift PK in current user events endpoint (#3036)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasb authored Sep 18, 2023
1 parent 69fcb58 commit 78849d2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion engine/apps/api/tests/test_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions engine/apps/schedules/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+)")

Expand Down
31 changes: 18 additions & 13 deletions engine/apps/schedules/ical_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions engine/apps/schedules/tests/test_ical_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 78849d2

Please sign in to comment.