From ab0cceaa08777febd7cd1ae54c614b7e050ba40c Mon Sep 17 00:00:00 2001 From: Samira Mokaram Date: Fri, 13 Nov 2020 11:41:06 +0100 Subject: [PATCH] refactor types --- plugins/pagerduty/src/api/pagerDutyClient.ts | 19 +++--- .../src/components/PagerDutyServiceCard.tsx | 8 +-- plugins/pagerduty/src/components/Pd.tsx | 16 +++++ plugins/pagerduty/src/components/types.tsx | 62 ++++++++----------- 4 files changed, 58 insertions(+), 47 deletions(-) diff --git a/plugins/pagerduty/src/api/pagerDutyClient.ts b/plugins/pagerduty/src/api/pagerDutyClient.ts index d0cb3719d6..7b1aa442c3 100644 --- a/plugins/pagerduty/src/api/pagerDutyClient.ts +++ b/plugins/pagerduty/src/api/pagerDutyClient.ts @@ -18,10 +18,10 @@ import { createApiRef } from '@backstage/core'; import { Service, Incident, - Options, - PagerDutyClientConfig, + RequestOptions, + ClientApiConfig, ServicesResponse, - IncidentResponse, + IncidentsResponse, OnCallsResponse, OnCall, } from '../components/types'; @@ -41,7 +41,7 @@ export class PagerDutyClientApi implements PagerDutyClient { private API_URL = 'https://api.pagerduty.com'; private EVENTS_API_URL = 'https://events.pagerduty.com/v2'; - constructor(private readonly config?: PagerDutyClientConfig) {} + constructor(private readonly config?: ClientApiConfig) {} async getServiceByIntegrationKey(integrationKey: string): Promise { if (!this.config?.token) { @@ -62,7 +62,7 @@ export class PagerDutyClientApi implements PagerDutyClient { const params = `service_ids[]=${serviceId}`; const url = `${this.API_URL}/incidents?${params}`; - const { incidents } = await this.getByUrl(url); + const { incidents } = await this.getByUrl(url); return incidents; } @@ -74,9 +74,9 @@ export class PagerDutyClientApi implements PagerDutyClient { const params = `include[]=users&escalation_policy_ids[]=${policyId}`; const url = `${this.API_URL}/oncalls?${params}`; - const { onCalls } = await this.getByUrl(url); + const { oncalls } = await this.getByUrl(url); - return onCalls; + return oncalls; } triggerPagerDutyAlarm( @@ -127,7 +127,10 @@ export class PagerDutyClientApi implements PagerDutyClient { return response.json(); } - private async request(url: string, options: Options): Promise { + private async request( + url: string, + options: RequestOptions, + ): Promise { try { const response = await fetch(url, options); if (!response.ok) { diff --git a/plugins/pagerduty/src/components/PagerDutyServiceCard.tsx b/plugins/pagerduty/src/components/PagerDutyServiceCard.tsx index 5bb7c9b5a9..d343a9fea1 100644 --- a/plugins/pagerduty/src/components/PagerDutyServiceCard.tsx +++ b/plugins/pagerduty/src/components/PagerDutyServiceCard.tsx @@ -45,13 +45,13 @@ export const PagerDutyServiceCard = ({ entity }: Props) => { const services = await api.getServiceByIntegrationKey(integrationKey); // TODO check services length const service = services[0]; - const incidents = await api.getIncidentsByServiceId(service.id); const oncalls = await api.getOnCallByPolicyId(service.escalation_policy.id); + const users = oncalls.map(item => item.user); return { incidents, - oncalls, + users, id: service.id, name: service.name, homepageUrl: service.html_url, @@ -89,9 +89,9 @@ export const PagerDutyServiceCard = ({ entity }: Props) => { content={ <> - + } - > + /> ); }; diff --git a/plugins/pagerduty/src/components/Pd.tsx b/plugins/pagerduty/src/components/Pd.tsx index eeaea87a7f..36f6e1989d 100644 --- a/plugins/pagerduty/src/components/Pd.tsx +++ b/plugins/pagerduty/src/components/Pd.tsx @@ -1,3 +1,19 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import React from 'react'; import SvgIcon, { SvgIconProps } from '@material-ui/core/SvgIcon'; diff --git a/plugins/pagerduty/src/components/types.tsx b/plugins/pagerduty/src/components/types.tsx index dec8f18739..b229949cb4 100644 --- a/plugins/pagerduty/src/components/types.tsx +++ b/plugins/pagerduty/src/components/types.tsx @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + export type Incident = { id: string; title: string; @@ -32,41 +33,14 @@ export type Service = { name: string; html_url: string; integrationKey: string; - escalation_policy: OnCall; + escalation_policy: { + id: string; + user: User; + }; }; -export type ResponseService = { - services: Service[]; - more: boolean; -}; - -export type ResponseOnCall = { - id: string; - name: string; - email: string; - homepageUrl: string; -}; - -export type Options = { - method: string; - headers: HeadersInit; - body?: BodyInit; -}; - -export type PagerDutyClientConfig = { - token?: string; -}; - -export type ServicesResponse = { - services: Service[]; -}; - -export type IncidentResponse = { - incidents: Incident[]; -}; - -export type OnCallsResponse = { - onCalls: OnCall[]; +export type OnCall = { + user: User; }; export type User = { @@ -77,6 +51,24 @@ export type User = { name: string; }; -export type OnCall = { - user: User; +export type ClientApiConfig = { + token?: string; +}; + +export type ServicesResponse = { + services: Service[]; +}; + +export type IncidentsResponse = { + incidents: Incident[]; +}; + +export type OnCallsResponse = { + oncalls: OnCall[]; +}; + +export type RequestOptions = { + method: string; + headers: HeadersInit; + body?: BodyInit; };