Skip to content

Commit

Permalink
fix(oncall-api): return all users from lowest escalation level
Browse files Browse the repository at this point in the history
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
  • Loading branch information
brianphillips committed Feb 23, 2024
1 parent 44e5600 commit a1cdf32
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/apis/pagerduty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
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": "[email protected]",
"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": "[email protected]",
"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 = [
Expand Down
2 changes: 1 addition & 1 deletion src/apis/pagerduty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export async function getOncallUsers(escalationPolicy: string): Promise<PagerDut
});

const oncallsFiltered = oncallsSorted.filter((oncall) => {
return oncall.escalation_level === result.oncalls[0].escalation_level;
return oncall.escalation_level === oncallsSorted[0].escalation_level;
});

usersItem = [...oncallsFiltered]
Expand Down

0 comments on commit a1cdf32

Please sign in to comment.