From a1cdf32e7f5495d80688bf17529ba0309a5112ab Mon Sep 17 00:00:00 2001 From: Brian Phillips <28457+brianphillips@users.noreply.github.com> Date: Fri, 23 Feb 2024 12:38:31 -0600 Subject: [PATCH] fix(oncall-api): return all users from lowest escalation level Instead of return all users that match the escalation level of the first user returned in the oncall list, return users that match the lowest escalation level. Previously, if a higher escalation level user was the first item in the array, only users at that same level would be returned. Fixes PagerDuty/backstage-plugin#77 --- src/apis/pagerduty.test.ts | 65 ++++++++++++++++++++++++++++++++++++++ src/apis/pagerduty.ts | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/apis/pagerduty.test.ts b/src/apis/pagerduty.test.ts index c62aaa0..3d0d33f 100644 --- a/src/apis/pagerduty.test.ts +++ b/src/apis/pagerduty.test.ts @@ -754,6 +754,71 @@ describe("PagerDuty API", () => { expect(fetch).toHaveBeenCalledTimes(1); }); + it.each(testInputs)("should return relevant users from escalation policy level 1 even if another level is returned first", async () => { + const escalationPolicyId = "12345"; + const expectedResponse = [ + { + id: "userId1", + name: "John Doe", + email: "john.doe@email.com", + avatar_url: "https://example.pagerduty.com/avatars/123", + html_url: "https://example.pagerduty.com/users/123", + summary: "John Doe", + } + ]; + + const mockAPIResponse = { + "oncalls": [ + { + "user": { + "id": "userId3", + "summary": "Jane Doe", + "name": "Jane Doe", + "email": "jane.does@email.com", + "avatar_url": "https://example.pagerduty.com/avatars/123", + "html_url": "https://example.pagerduty.com/users/123", + }, + "escalation_level": 3 + }, + { + "user": { + "id": expectedResponse[0].id, + "summary": expectedResponse[0].summary, + "name": expectedResponse[0].name, + "email": expectedResponse[0].email, + "avatar_url": expectedResponse[0].avatar_url, + "html_url": expectedResponse[0].html_url, + }, + "escalation_level": 1 + }, + { + "user": { + "id": "userId2", + "summary": "James Doe", + "name": "James Doe", + "email": "james.does@email.com", + "avatar_url": "https://example.pagerduty.com/avatars/123", + "html_url": "https://example.pagerduty.com/users/123", + }, + "escalation_level": 2 + } + ] + }; + + global.fetch = jest.fn(() => + Promise.resolve({ + status: 200, + json: () => Promise.resolve(mockAPIResponse) + }) + ) as jest.Mock; + + const result = await getOncallUsers(escalationPolicyId); + + expect(result).toEqual(expectedResponse); + expect(result.length).toEqual(1); + expect(fetch).toHaveBeenCalledTimes(1); + }); + it.each(testInputs)("should return list of users ordered by name ASC from other escalation levels when level 1 is empty", async () => { const escalationPolicyId = "12345"; const expectedResponse = [ diff --git a/src/apis/pagerduty.ts b/src/apis/pagerduty.ts index 492e04f..1ddcc90 100644 --- a/src/apis/pagerduty.ts +++ b/src/apis/pagerduty.ts @@ -390,7 +390,7 @@ export async function getOncallUsers(escalationPolicy: string): Promise { - return oncall.escalation_level === result.oncalls[0].escalation_level; + return oncall.escalation_level === oncallsSorted[0].escalation_level; }); usersItem = [...oncallsFiltered]